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

My hometask dependency injection #1005

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

Conversation

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

Hey there! It looks like there are a few things that need your attention before we can move forward. Here are some pointers to help you out:

  1. Make sure to provide more informative exception messages, which will be super helpful during debugging.
  2. Consider using clazz.getDeclaredFields() to include private fields in your search, as it's more comprehensive than clazz.getFields().
  3. Don't forget to check for required annotations like @Component before creating instances in your Injector class.
  4. Catching a general exception like ReflectiveOperationException can make your code cleaner and easier to manage.
  5. When rethrowing exceptions, it's a good practice to include the original exception to maintain the stack trace.
  6. Lastly, be careful with the initialization of your Interface Implementations map to avoid confusion and redundancy.

Keep up the good work, and with a few tweaks, your code will be in great shape! 👍 Remember, these changes are important to ensure that your code is robust and maintainable. You're doing great, and I'm sure you'll have it polished up in no time! 😊

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

Comment on lines 16 to 22
static {
interfaceImplementations =
Map.of(FileReaderService.class, FileReaderServiceImpl.class,
ProductParser.class, ProductParserImpl.class,
ProductService.class, ProductServiceImpl.class
);
}

Choose a reason for hiding this comment

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

Make Interface Implementations map a class field. You can fill it in using Map.of(). However, it should be initialized in a static block or directly upon declaration, not in both places.

if (!clazzImplementation.isAnnotationPresent(Component.class)) {
throw new RuntimeException("Class implementation: "
+ clazzImplementation.getName()
+ "doesn't have 'Component' annotation ");

Choose a reason for hiding this comment

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

When throwing an exception, add an informative message to it. Also, include the caught exception in the RuntimeException to provide a stack trace for easier debugging. For example: throw new RuntimeException("Injection failed, missing @Component annotation on the class " + clazzImplementation.getName(), e);

field.setAccessible(true);
field.set(clazzImplementationInstance, fieldTypeObject);
} catch (IllegalAccessException e) {
throw new RuntimeException("Can't initialize field: " + field.getName());

Choose a reason for hiding this comment

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

When rethrowing the exception, you should include the original exception as a cause to maintain the stack trace. For example: throw new RuntimeException("Can't initialize field: " + field.getName(), e);

Object newInstance = constructor.newInstance();
instances.put(clazzImplementation, newInstance);
return newInstance;
} catch (ReflectiveOperationException e) {

Choose a reason for hiding this comment

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

It is better to replace many exceptions that have a common parent with a general parental exception. In this case, you can catch ReflectiveOperationException instead of listing all reflective exceptions separately.

}
}

private Class<?> findImplementation(Class<?> clazz) {

Choose a reason for hiding this comment

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

Don't forget about annotations checks in your Injector class. You should check if the class has the @component annotation before attempting to create an instance.

+ clazzImplementation.getName()
+ "doesn't have 'Component' annotation ");
}
Field[] fields = clazzImplementation.getFields();

Choose a reason for hiding this comment

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

You should use clazz.getDeclaredFields() instead of clazz.getFields() to also include private fields in your search, as getFields() only returns public fields including inherited ones.

if (clazz.isInterface()) {
return interfaceImplementations.get(clazz);
}
throw new RuntimeException("Unsupported type class: " + clazz.getName());

Choose a reason for hiding this comment

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

This exception message could be more informative by explaining that the class is not an interface. For example: throw new RuntimeException("Expected an interface but got class: " + clazz.getName());

@skucherenko7
Copy link
Author

I corrected my hometask

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