Skip to content

Commit

Permalink
Add Injector with Component
Browse files Browse the repository at this point in the history
  • Loading branch information
fwhoami committed Oct 22, 2024
1 parent 87661e2 commit feb035d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/main/java/mate.academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import mate.academy.service.ProductService;

public class Main {
private static final String FILE_PATH = "src/main/java/resources/products.txt";

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;
List<Product> products = productService.getAllFromFile("products.txt");
ProductService productService = (ProductService) injector.getInstance(ProductService.class);
List<Product> products = productService.getAllFromFile(FILE_PATH);
products.forEach(System.out::println);
}
}
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 {

}
5 changes: 4 additions & 1 deletion src/main/java/mate.academy/lib/Inject.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package mate.academy.lib;

public @interface Inject {
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Inject {
}
63 changes: 62 additions & 1 deletion src/main/java/mate.academy/lib/Injector.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,74 @@
package mate.academy.lib;

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 final Map<Class<?>, Class<?>> componentMap = Map.of(
ProductService.class, ProductServiceImpl.class,
ProductParser.class, ProductParserImpl.class,
FileReaderService.class, FileReaderServiceImpl.class);
private final Map<Class<?>, Object> instances = new HashMap<>();

public static Injector getInjector() {
return injector;
}

public Object getInstance(Class<?> interfaceClazz) {
return null;
if (instances.containsKey(interfaceClazz)) {
return instances.get(interfaceClazz);
}
validateImplementationPresent(interfaceClazz);
Class<?> implementationClass = componentMap.get(interfaceClazz);
validateComponentPresent(implementationClass);
Object instance = newInstance(implementationClass);
Field[] fields = implementationClass.getDeclaredFields();
for (Field field : fields) {
if (!field.isAnnotationPresent(Inject.class)) {
continue;
}
Object fieldValue = getInstance(field.getType());
setFieldValue(instance, field, fieldValue);
}
return instance;
}

private static void setFieldValue(Object instance, Field field, Object fieldValue) {
try {
field.setAccessible(true);
field.set(instance, fieldValue);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to initialize field value", e);
}
}

private static void validateComponentPresent(Class<?> implementationClass) {
if (!implementationClass.isAnnotationPresent(Component.class)) {
throw new RuntimeException("Target class = ["
+ implementationClass.getSimpleName() + "] should have @Component present");
}
}

private void validateImplementationPresent(Class<?> interfaceClazz) {
if (!componentMap.containsKey(interfaceClazz)) {
throw new RuntimeException(String.format("No implementation for interface %s",
interfaceClazz.getName()));
}
}

private Object newInstance(Class<?> implementationClass) {
try {
return implementationClass.getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException e) {
throw new RuntimeException("An error occurred while creating a new instance", e);
}
}
}
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
File renamed without changes.

0 comments on commit feb035d

Please sign in to comment.