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

Add component annotation and use it in injector class #1026

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

Conversation

RomanChygryn
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! 😊 There are a couple of critical issues that need to be addressed before we can approve it:

  1. Circular Dependency Handling: The current implementation of the getInstance method can lead to a stack overflow if there are circular dependencies. It's important to add a mechanism to detect and handle these circular dependencies to prevent runtime errors.

  2. Instance Creation Logic: In the Injector class, the logic for creating instances is flawed. The clazzImplementationInstance is being reassigned within a loop for each field annotated with @Inject, which results in multiple instances being created for the same class. This should be corrected by moving the instantiation outside the loop to ensure that each class is instantiated only once.

Once these issues are resolved, your code will be much more robust and ready for approval. Keep up the great work, and don't hesitate to reach out if you have any questions or need further guidance! 🚀

✨ 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.

src/main/java/mate.academy/lib/Injector.java Outdated Show resolved Hide resolved
src/main/java/mate.academy/lib/Injector.java Outdated Show resolved Hide resolved
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.


Review Summary

Great job on implementing the dependency injection framework! 🎉 Your code meets most of the task requirements and constraints, and you've done well with the use of annotations and handling dependencies. However, there are a few critical issues that need to be addressed to ensure the correct functioning of your code:

  1. Annotations Target: Remember to add @Target to your custom annotations. Both Inject and Component annotations are missing this, which is necessary to define where these annotations can be applied.

  2. Interface Implementations Map: The map for interface implementations should be a class-level field in the Injector class, initialized using Map.of(). Currently, it's created within the findImplementation method, which is not optimal.

  3. Instance Caching: Implement an instance caching mechanism in the Injector class. Currently, it creates a new instance every time getInstance is called, which can be inefficient. Check the instances map before creating a new instance to reuse existing ones.

  4. Circular Dependencies: Be cautious of potential stack overflow issues due to recursive calls in getInstance when circular dependencies exist. Implement a mechanism to detect and handle such cases.

  5. Instance Creation Efficiency: The clazzImplementationInstance is being created inside the loop for each field annotated with @Inject. This can lead to inefficiencies if a class has multiple such fields. Move the instance creation outside the loop to avoid unnecessary recreations.

These issues should be addressed to enhance the functionality and efficiency of your code. Keep up the good work, and use this feedback to improve your code further. Remember, addressing these points will help you avoid similar issues in the future and strengthen your understanding of dependency injection. Keep learning and coding! 🚀

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

src/main/java/mate.academy/lib/Injector.java Outdated Show resolved Hide resolved
src/main/java/mate.academy/lib/Injector.java Outdated Show resolved Hide resolved
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