Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add component annotation and use it in injector class #1026

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {

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

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
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();

Expand All @@ -8,6 +21,74 @@ public static Injector getInjector() {
}

public Object getInstance(Class<?> interfaceClazz) {
return null;
return getInstance(interfaceClazz, new HashSet<>());
}

private Object getInstance(Class<?> interfaceClazz, Set<Class<?>> instantiationStack) {
Class<?> clazz = findImplementation(interfaceClazz);

if (!clazz.isAnnotationPresent(Component.class)) {
throw new RuntimeException(
"Class " + clazz.getName()
+ " is not annotated with @Component or no implementation found"
);
}

if (instantiationStack.contains(clazz)) {
throw new RuntimeException(
"Circular dependency detected for class: " + clazz.getName()
);
}

instantiationStack.add(clazz);

Object clazzImplementationInstance = createNewInstance(clazz);

Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
if (field.isAnnotationPresent(Inject.class)) {
Object fieldInstance = getInstance(field.getType(), instantiationStack);

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()
);
}
}
}

instantiationStack.remove(clazz);

return clazzImplementationInstance;
}

private Object createNewInstance(Class<?> clazz) {
try {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
} catch (Exception e) {
throw new RuntimeException("Failed to create instance of class: " + clazz.getName(), e);
}
}

private Class<?> findImplementation(Class<?> interfaceClazz) {
Map<Class<?>, Class<?>> interfaceToImplementation = new HashMap<>();
interfaceToImplementation.put(FileReaderService.class, FileReaderServiceImpl.class);
interfaceToImplementation.put(ProductParser.class, ProductParserImpl.class);
interfaceToImplementation.put(ProductService.class, ProductServiceImpl.class);

Class<?> implementationClass =
interfaceToImplementation.get(interfaceClazz);
if (implementationClass == null) {
throw new RuntimeException(
"No implementation found for interface: " + interfaceClazz.getName()
);
}
return implementationClass;
}
}
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
Loading