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

feat: implemented dependency injection mechanism #1024

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

PavloMelnyk10
Copy link

feat: implemented dependency injection mechanism using @component and @Inject annotations

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 dependency injection framework! 🎉 Your code meets the core requirements and is mostly well-structured. There are a few minor improvements you could consider, such as adding @Target to your custom annotations and using ReflectiveOperationException for more specific exception handling. Also, ensure that all classes meant for injection are annotated with @Component to avoid runtime issues. Keep up the great work and happy coding! 😊

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

return instances.get(interfaceClazz);
}
Class<?> clazz = findImplementation(interfaceClazz);
if (clazz == null || !clazz.isAnnotationPresent(Component.class)) {

Choose a reason for hiding this comment

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

Ensure that the Component annotation is correctly applied to all classes that should be managed by the injector. If a class is missing this annotation, it will cause a RuntimeException.

}
Class<?> clazz = findImplementation(interfaceClazz);
if (clazz == null || !clazz.isAnnotationPresent(Component.class)) {
throw new RuntimeException("No implementation found for: " + interfaceClazz.getName());

Choose a reason for hiding this comment

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

Consider providing more detailed error messages or logging to help diagnose issues when no implementation is found.

for (Field field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(Inject.class)) {
field.setAccessible(true);
Object fieldInstance = getInstance(field.getType());

Choose a reason for hiding this comment

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

Ensure that circular dependencies are not present, as this could lead to a StackOverflowError during instance creation.

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.

2 participants