-
Notifications
You must be signed in to change notification settings - Fork 998
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
Solution #1004
base: main
Are you sure you want to change the base?
Solution #1004
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job on your pull request! 🌟 Your annotations are well-defined, and the classes use dependency injection effectively. The parsing logic and service implementations are on point. Just a few suggestions to improve code quality and error handling have been provided, but they don't significantly impact the functionality of your code. Keep up the good work, and consider these tips for future enhancements! 👍
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
throw new RuntimeException("Injection failed" | ||
+ ", missing @Component annotation on the class " + clazz.getName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of catching a general RuntimeException
, it's better to use a more specific exception. Also, consider including the cause of the exception in the thrown exception to provide a stack trace for easier debugging.
if (field.isAnnotationPresent(Inject.class)) { | ||
Object fieldInstance = getInstance(field.getType()); | ||
clazzImplementationInstance = createNewInstance(clazz); | ||
try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The instance creation createNewInstance(clazz)
should be checked before the fields injection loop to avoid unnecessary instance creation if any field injection fails. Move this line before the loop starts.
instances.put(clazz, instance); | ||
return instance; | ||
} catch (ReflectiveOperationException e) { | ||
throw new RuntimeException("Can`t create a new instance of " + clazz.getName(), e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When throwing an exception, it's better to use a more specific exception rather than a general RuntimeException
. Also, include the cause of the exception in the thrown exception to provide a stack trace for easier debugging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job! There are some comments left but they do not interfere with task description or common rules.
} | ||
} | ||
} | ||
if (clazzImplementationInstance == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're creating an instance of 'clazz' before checking if it's null. This can lead to NullPointerException if 'clazz' is null.
public class Injector { | ||
private static final Injector injector = new Injector(); | ||
private static final Map<Class<?>, Class<?>> interfaceImplementations = Map.of( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of 'interfaceImplementations' map does not support multiple implementations of a single interface. Consider using a different data structure or strategy to support this.
throw new IllegalArgumentException("Injection failed" | ||
+ ", missing @Component annotation on the class " + clazz.getName()); | ||
} | ||
Object clazzImplementationInstance = createNewInstance(clazz);; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a redundant semicolon at the end of the line.
} | ||
} | ||
|
||
private Class<?> findImplementation(Class<?> interfaceOrClazz) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'findImplementation' method currently only supports interfaces. It will not return correct implementation if a class is passed as an argument.
No description provided.