-
Notifications
You must be signed in to change notification settings - Fork 997
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
Initial commit #719
base: main
Are you sure you want to change the base?
Initial commit #719
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.
LGTM!
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 solution, let's improve it
Map<Class<?>, Class<?>> interfaceImplementations = new HashMap<>(); | ||
interfaceImplementations.put(ProductService.class, ProductServiceImpl.class); | ||
interfaceImplementations.put(ProductParser.class, ProductParserImpl.class); | ||
interfaceImplementations.put(FileReaderService.class, FileReaderServiceImpl.class); |
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.
Make this field a class level field and initialize it once
Object clazzImplementationInstance = null; | ||
Class<?> clazz = findImplementation(interfaceClazz); | ||
if (!clazz.isAnnotationPresent(Component.class)) { | ||
throw new RuntimeException("Can't work with classes without annotation @Component"); |
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.
in which class annotation was missed? Add more info
} catch (NoSuchMethodException | InvocationTargetException | ||
| InstantiationException | IllegalAccessException 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.
if (clazzImplementationInstance == null) { | ||
clazzImplementationInstance = createNewInstance(clazz); | ||
} | ||
return clazzImplementationInstance; |
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 can use ternary operator here
if (interfaceClazz.isInterface()) { | ||
return interfaceImplementations.get(interfaceClazz); | ||
} | ||
return interfaceClazz; |
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 can use ternary operation here too
…made interfaceImplementations variable a class level field and initialize it once
No description provided.