From 891023c00ba6f02b4e169bbc099a19b6cb72453c Mon Sep 17 00:00:00 2001 From: Bartlomiej Smreczak Date: Sun, 6 Oct 2024 14:18:54 +0200 Subject: [PATCH] Fixes after code review: added checking for singleton pattern, added @Target annotation, changed order in getInstatnce() method, gathered exceptions in ReflectiveOperationException --- src/main/java/mate.academy/Main.java | 7 +++++++ src/main/java/mate.academy/lib/Component.java | 3 +++ src/main/java/mate.academy/lib/Injector.java | 10 ++++------ .../mate.academy/service/impl/ProductServiceImpl.java | 8 ++++---- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/java/mate.academy/Main.java b/src/main/java/mate.academy/Main.java index 62ebf42e6..8099e8342 100644 --- a/src/main/java/mate.academy/Main.java +++ b/src/main/java/mate.academy/Main.java @@ -11,6 +11,13 @@ public static void main(String[] args) { // Please test your Injector here. Feel free to push this class as a part of your solution Injector injector = Injector.getInjector(); ProductService productService = (ProductService) injector.getInstance(ProductService.class); + + // Checking the singleton pattern (on AI mentor request) + Injector injectorSecond = Injector.getInjector(); + if (!injector.equals(injectorSecond)) { + throw new RuntimeException("The singleton pattern doesn't work correctly"); + } + List products = productService.getAllFromFile("products.txt"); products.forEach(System.out::println); } diff --git a/src/main/java/mate.academy/lib/Component.java b/src/main/java/mate.academy/lib/Component.java index 563153122..46e39cec8 100644 --- a/src/main/java/mate.academy/lib/Component.java +++ b/src/main/java/mate.academy/lib/Component.java @@ -1,8 +1,11 @@ package mate.academy.lib; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) public @interface Component { } diff --git a/src/main/java/mate.academy/lib/Injector.java b/src/main/java/mate.academy/lib/Injector.java index 584db3079..fa10d25f3 100644 --- a/src/main/java/mate.academy/lib/Injector.java +++ b/src/main/java/mate.academy/lib/Injector.java @@ -19,10 +19,13 @@ public Object getInstance(Class interfaceClazz) { Object clazzImplementationInstance = null; Class clazz = findImplementation(interfaceClazz); Field[] declaredFields = clazz.getDeclaredFields(); + if (clazzImplementationInstance == null) { + clazzImplementationInstance = createNewInstance(clazz); + } + clazzImplementationInstance = createNewInstance(clazz); for (Field field : declaredFields) { if (field.isAnnotationPresent(Inject.class)) { Object fieldInstance = getInstance(field.getType()); - clazzImplementationInstance = createNewInstance(clazz); field.setAccessible(true); try { field.set(clazzImplementationInstance, fieldInstance); @@ -32,12 +35,7 @@ public Object getInstance(Class interfaceClazz) { } } } - if (clazzImplementationInstance == null) { - clazzImplementationInstance = createNewInstance(clazz); - - } return clazzImplementationInstance; - } else { throw new RuntimeException("This instance of " + interfaceClazz + " class, cannot be created, because " diff --git a/src/main/java/mate.academy/service/impl/ProductServiceImpl.java b/src/main/java/mate.academy/service/impl/ProductServiceImpl.java index 29e20843b..0b408c3a3 100644 --- a/src/main/java/mate.academy/service/impl/ProductServiceImpl.java +++ b/src/main/java/mate.academy/service/impl/ProductServiceImpl.java @@ -12,16 +12,16 @@ @Component public class ProductServiceImpl implements ProductService { @Inject - private ProductParser productParser; + private ProductParser txtProductParser; @Inject - private FileReaderService fileReaderService; + private FileReaderService txtFileReaderService; @Override public List getAllFromFile(String filePath) { - return fileReaderService.readFile(filePath) + return txtFileReaderService.readFile(filePath) .stream() - .map(productParser::parse) + .map(txtProductParser::parse) .collect(Collectors.toList()); } }