Skip to content

Commit

Permalink
implemented annotations Inject and Component
Browse files Browse the repository at this point in the history
  • Loading branch information
StoneBlood-bit committed Oct 18, 2024
1 parent 87661e2 commit 955a3d5
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/main/java/mate.academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
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<Product> products = productService.getAllFromFile("products.txt");
products.forEach(System.out::println);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/mate.academy/lib/Component.java
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 {

}
4 changes: 4 additions & 0 deletions src/main/java/mate.academy/lib/Inject.java
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 {

}
80 changes: 79 additions & 1 deletion src/main/java/mate.academy/lib/Injector.java
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> readFile(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 955a3d5

Please sign in to comment.