Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented Injector class #1022

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

GarikSlavsky
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your hard work on this pull request! 😊 While the overall implementation is solid and aligns well with the task requirements, there are a few critical issues that need to be addressed before we can proceed. Specifically, the createNewInstance method is being called twice unnecessarily, which could lead to performance inefficiencies. Additionally, the findImplementation method may return null, which could cause runtime errors if not handled properly. Lastly, the clazzImplementationInstance variable should be initialized more safely to prevent potential null pointer exceptions. Please address these issues to ensure the robustness and efficiency of your code. Keep up the great work, and I look forward to your revisions! 🚀

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review button in the Reviewers tab.


public static Injector getInjector() {
return injector;
}

public Object getInstance(Class<?> interfaceClazz) {
return null;
Object clazzImplementationInstance = null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable clazzImplementationInstance is initialized to null but is not immediately assigned a value. Consider initializing it with createNewInstance(clazz) after checking for the @Inject annotation to avoid potential null pointer exceptions.


public static Injector getInjector() {
return injector;
}

public Object getInstance(Class<?> interfaceClazz) {
return null;
Object clazzImplementationInstance = null;
Class<?> clazz = findImplementation(interfaceClazz);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The findImplementation method may return null if the interfaceClazz is not found in the interfaceImplementations map. Consider adding a check to handle this scenario and throw an appropriate exception if no implementation is found.

Comment on lines 33 to 34
Object fieldInstance = getInstance(field.getType());
clazzImplementationInstance = createNewInstance(clazz);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The createNewInstance method is called twice for the same clazz, once inside the loop and once after the loop. This could lead to unnecessary object creation. Consider restructuring the code to call createNewInstance only once after all fields are processed.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job on implementing the code! 🎉 While the overall implementation is solid, please ensure that all classes intended for injection are annotated with @component to avoid runtime exceptions. Additionally, double-check the Injector class configuration to ensure it returns the correct instances. Keep up the good work and continue learning from these experiences!

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

Comment on lines +12 to +13
ProductService productService = (ProductService) injector.getInstance(
ProductService.class);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the Injector class is correctly implemented and configured to return an instance of ProductService. If the Injector is not properly set up, this line may cause a runtime error.

Comment on lines +51 to +55
if (!clazz.isAnnotationPresent(Component.class)) {
throw new RequiredAnnotationAbsenceException(
"Cannot create an instance of the class without Component annotation "
+ clazz);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that all classes you intend to create instances for are annotated with @component. Otherwise, the RequiredAnnotationAbsenceException will be thrown, as seen in this check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants