diff --git a/pom.xml b/pom.xml index 5a9247c52..6b87b67bc 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ - 11 + 17 UTF-8 UTF-8 3.1.1 diff --git a/src/main/java/mate.academy/Main.java b/src/main/java/mate.academy/Main.java index 9be740020..62ebf42e6 100644 --- a/src/main/java/mate.academy/Main.java +++ b/src/main/java/mate.academy/Main.java @@ -10,7 +10,7 @@ public class Main { 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 = null; + ProductService productService = (ProductService) injector.getInstance(ProductService.class); 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 c7e830420..8bcd94af1 100644 --- a/src/main/java/mate.academy/lib/Component.java +++ b/src/main/java/mate.academy/lib/Component.java @@ -1,5 +1,11 @@ package mate.academy.lib; -public @interface Component { +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface Component { } diff --git a/src/main/java/mate.academy/lib/Inject.java b/src/main/java/mate.academy/lib/Inject.java index 1741f9df9..295195fee 100644 --- a/src/main/java/mate.academy/lib/Inject.java +++ b/src/main/java/mate.academy/lib/Inject.java @@ -1,5 +1,11 @@ package mate.academy.lib; -public @interface Inject { +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Inject { } diff --git a/src/main/java/mate.academy/lib/Injector.java b/src/main/java/mate.academy/lib/Injector.java index e87b914ad..8e7027fe9 100644 --- a/src/main/java/mate.academy/lib/Injector.java +++ b/src/main/java/mate.academy/lib/Injector.java @@ -1,13 +1,83 @@ package mate.academy.lib; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; +import mate.academy.service.FileReaderService; +import mate.academy.service.ProductParser; +import mate.academy.service.ProductService; +import mate.academy.service.impl.FileReaderServiceImpl; +import mate.academy.service.impl.ProductParserImpl; +import mate.academy.service.impl.ProductServiceImpl; + public class Injector { - private static final Injector injector = new Injector(); + private static final Injector INJECTOR = new Injector(); + private static final Map, Class> INTERFACES_IMPLEMENTATIONS = Map.of( + FileReaderService.class, FileReaderServiceImpl.class, + ProductParser.class, ProductParserImpl.class, + ProductService.class, ProductServiceImpl.class); + private static final Map, Object> INSTANCES = new HashMap<>(); public static Injector getInjector() { - return injector; + return INJECTOR; } public Object getInstance(Class interfaceClazz) { - return null; + Object clazzImplementationInstance = null; + Class clazz = findImplementation(interfaceClazz); + Field[] fields = clazz.getDeclaredFields(); + for (Field field : fields) { + if (field.isAnnotationPresent(Inject.class)) { + Object fieldInstance = getInstance(field.getType()); + clazzImplementationInstance = createNewInstance(clazz); + field.setAccessible(true); + try { + field.set(clazzImplementationInstance, fieldInstance); + } catch (IllegalAccessException e) { + throw new RuntimeException("Can't initialize field value. Class: " + + clazz.getName() + ", field: " + field.getName()); + } + } + } + return clazzImplementationInstance == null ? createNewInstance(clazz) + : clazzImplementationInstance; + } + + private Object createNewInstance(Class clazz) { + if (INSTANCES.containsKey(clazz)) { + return INSTANCES.get(clazz); + } + try { + Constructor constructor = clazz.getConstructor(); + Object instance = constructor.newInstance(); + INSTANCES.put(clazz, instance); + return instance; + } catch (ReflectiveOperationException e) { + throw new RuntimeException("Can't create new instance of class - " + clazz.getName()); + } + } + + private Class findImplementation(Class interfaceClazz) { + if (interfaceClazz.isInterface()) { + interfaceCheck(interfaceClazz); + interfaceClazz = INTERFACES_IMPLEMENTATIONS.get(interfaceClazz); + } + componentCheck(interfaceClazz); + return interfaceClazz; + } + + private void componentCheck(Class clazz) { + if (!clazz.isAnnotationPresent(Component.class)) { + throw new RuntimeException("Unsupported class - " + clazz + + ", should be annotated by @Component"); + } + } + + private void interfaceCheck(Class clazz) { + if (!INTERFACES_IMPLEMENTATIONS.containsKey(clazz)) { + throw new RuntimeException("Unsupported interface - " + clazz.getName()); + } } } + diff --git a/src/main/java/mate.academy/service/impl/FileReaderServiceImpl.java b/src/main/java/mate.academy/service/impl/FileReaderServiceImpl.java index 58540d301..752474e0d 100644 --- a/src/main/java/mate.academy/service/impl/FileReaderServiceImpl.java +++ b/src/main/java/mate.academy/service/impl/FileReaderServiceImpl.java @@ -4,8 +4,10 @@ import java.io.IOException; import java.nio.file.Files; import java.util.List; +import mate.academy.lib.Component; import mate.academy.service.FileReaderService; +@Component public class FileReaderServiceImpl implements FileReaderService { @Override public List readFile(String fileName) { diff --git a/src/main/java/mate.academy/service/impl/ProductParserImpl.java b/src/main/java/mate.academy/service/impl/ProductParserImpl.java index 00837f28d..03ea21a56 100644 --- a/src/main/java/mate.academy/service/impl/ProductParserImpl.java +++ b/src/main/java/mate.academy/service/impl/ProductParserImpl.java @@ -1,9 +1,11 @@ package mate.academy.service.impl; import java.math.BigDecimal; +import mate.academy.lib.Component; import mate.academy.model.Product; import mate.academy.service.ProductParser; +@Component public class ProductParserImpl implements ProductParser { public static final int ID_POSITION = 0; public static final int NAME_POSITION = 1; diff --git a/src/main/java/mate.academy/service/impl/ProductServiceImpl.java b/src/main/java/mate.academy/service/impl/ProductServiceImpl.java index 755304777..425ca5552 100644 --- a/src/main/java/mate.academy/service/impl/ProductServiceImpl.java +++ b/src/main/java/mate.academy/service/impl/ProductServiceImpl.java @@ -2,13 +2,18 @@ import java.util.List; import java.util.stream.Collectors; +import mate.academy.lib.Component; +import mate.academy.lib.Inject; import mate.academy.model.Product; import mate.academy.service.FileReaderService; import mate.academy.service.ProductParser; import mate.academy.service.ProductService; +@Component public class ProductServiceImpl implements ProductService { + @Inject private ProductParser productParser; + @Inject private FileReaderService fileReaderService; @Override