From f35e38f0b941b8fbefef3e5415fd166838dfc7a5 Mon Sep 17 00:00:00 2001 From: ZVIIR Date: Fri, 4 Aug 2023 16:09:26 +0300 Subject: [PATCH] moved Component check to separate method --- src/main/java/mate.academy/lib/Injector.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/mate.academy/lib/Injector.java b/src/main/java/mate.academy/lib/Injector.java index fdd7d90b2..71f5cb6ac 100644 --- a/src/main/java/mate.academy/lib/Injector.java +++ b/src/main/java/mate.academy/lib/Injector.java @@ -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)) { @@ -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); }