-
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.
implemented annotations Inject and Component
- Loading branch information
1 parent
87661e2
commit 955a3d5
Showing
7 changed files
with
97 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +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 |
---|---|---|
@@ -1,5 +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 |
---|---|---|
@@ -1,13 +1,91 @@ | ||
package mate.academy.lib; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
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) { | ||
return null; | ||
Class<?> clazz = findImplementation(interfaceClazz); | ||
|
||
if (clazz == null) { | ||
throw new RuntimeException("No implementation found for interface: " | ||
+ interfaceClazz.getName()); | ||
} | ||
|
||
if (!clazz.isAnnotationPresent(Component.class)) { | ||
throw new RuntimeException("Class " + clazz.getName() | ||
+ " is not annotated with @Component"); | ||
} | ||
|
||
Object clazzImplementationInstance = null; | ||
Field[] declaredFields = clazz.getDeclaredFields(); | ||
|
||
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 value. Class: " | ||
+ clazz.getName() + ". Field: " + field.getName()); | ||
} | ||
} | ||
} | ||
|
||
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 (NoSuchMethodException | InvocationTargetException | ||
| InstantiationException | IllegalAccessException e) { | ||
throw new RuntimeException("Can't create a new instance of " + clazz.getName()); | ||
} | ||
} | ||
|
||
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; | ||
} | ||
} |
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