-
Notifications
You must be signed in to change notification settings - Fork 998
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c2827f
commit b5f8779
Showing
7 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/main/java/mate.academy/service/impl/ProductParserImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mate.academy.lib; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Component { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mate.academy.lib; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Inject { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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 Map<Class<?>, Object> instances = new HashMap<>(); | ||
|
||
public static Injector getInjector() { | ||
return injector; | ||
} | ||
|
||
public Object getInstance(Class<?> interfaceClazz) { | ||
Object clazzImplementationInstance = null; | ||
Class<?> clazz = findImplementation(interfaceClazz); | ||
Field[] declaredFields = clazz.getDeclaredFields(); | ||
if (!clazz.isAnnotationPresent(Component.class)) { | ||
throw new RuntimeException("Injection failed, missing @Component " | ||
+ "annotation on the class " | ||
+ interfaceClazz.getName()); | ||
} | ||
for (Field field : declaredFields) { | ||
if (field.isAnnotationPresent(Inject.class)) { | ||
Object fieldInstance = getInstance(field.getType()); | ||
clazzImplementationInstance = createNewInstance(clazz); | ||
try { | ||
field.setAccessible(true); | ||
field.set(clazzImplementationInstance, fieldInstance); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException("Can`t initialize field " | ||
+ field.getName() + " in " + clazz.getName(), e); | ||
} | ||
} | ||
} | ||
if (clazzImplementationInstance == null) { | ||
clazzImplementationInstance = createNewInstance(clazz); | ||
} | ||
return 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 " + clazz.getName(), e); | ||
} | ||
} | ||
|
||
private Class<?> findImplementation(Class<?> interfaceClazz) { | ||
Map<Class<?>, Class<?>> interfaceImplementations = new HashMap<>(); | ||
interfaceImplementations.put(ProductParser.class, ProductParserImpl.class); | ||
interfaceImplementations.put(FileReaderService.class, FileReaderServiceImpl.class); | ||
interfaceImplementations.put(ProductService.class, ProductServiceImpl.class); | ||
if (interfaceClazz.isInterface()) { | ||
return interfaceImplementations.get(interfaceClazz); | ||
} | ||
return interfaceClazz; | ||
} | ||
} |