Skip to content

Commit

Permalink
moved Component check to separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis-Balako committed Aug 4, 2023
1 parent 20b74b8 commit f35e38f
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/java/mate.academy/lib/Injector.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static Injector getInjector() {
public Object getInstance(Class<?> interfaceClazz) {
Object clazzImplementationInstance = null;
Class<?> clazz = findImplementation(interfaceClazz);
checkComponentAnnotationPresent(clazz);
Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
if (field.isAnnotationPresent(Inject.class)) {
Expand Down Expand Up @@ -55,12 +56,15 @@ private Class<?> findImplementation(Class<?> interfaceClazz) {
return interfaceClazz;
}

private Object createNewInstance(Class<?> clazz) {
private void checkComponentAnnotationPresent(Class<?> clazz) {
if (!clazz.isAnnotationPresent(Component.class)) {
throw new RuntimeException("Injection failed, "
+ "missing @Component annotation on the class "
+ clazz.getName());
}
}

private Object createNewInstance(Class<?> clazz) {
if (instances.containsKey(clazz)) {
return instances.get(clazz);
}
Expand Down

0 comments on commit f35e38f

Please sign in to comment.