Skip to content
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
7 changes: 7 additions & 0 deletions src/main/java/mate.academy/lib/Component.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package mate.academy.lib;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Component {

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

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
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.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<>();
private final Map<Class<?>, Class<?>> interfaceImplementations = Map.of(
FileReaderService.class, FileReaderServiceImpl.class,
ProductParser.class, ProductParserImpl.class,
ProductService.class, ProductServiceImpl.class
);

public static Injector getInjector() {

return injector;
}

public Object getInstance(Class<?> interfaceClazz) {
return null;
Class<?> clazz = findImplementation(interfaceClazz);

if (!clazz.isAnnotationPresent(Component.class)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @Component check happens AFTER createNewInstance() is called at line 26. This violates checklist item #4 - the check must happen BEFORE instantiation. Move this validation to createNewInstance() or before calling it.

throw new RuntimeException("Can't create class!");
}
Object clazzImplementationInstance = createNewInstance(clazz);
Field[] declaredFields = clazz.getDeclaredFields();
for (Field fields : declaredFields) {
if (fields.isAnnotationPresent(Inject.class)) {
Object fieldsInstance = getInstance(fields.getType());

try {
fields.setAccessible(true);
fields.set(clazzImplementationInstance, fieldsInstance);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CHECKLIST ITEM #8 VIOLATION: Replace IllegalAccessException with ReflectiveOperationException which is its parent class (consistent with line 62).

} catch (IllegalAccessException e) {
throw new RuntimeException("Can't initialize field value."

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates checklist item #7: The caught exception is not chained to the RuntimeException. Use the constructor's second parameter: throw new RuntimeException(..., e);

+ "Class: " + clazz.getName() + ". Field: "
Comment on lines +42 to +45

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CHECKLIST ITEM #7 VIOLATION: The exception is not chained to the RuntimeException. Use throw new RuntimeException("message", e) to include the caught exception.

+ fields.getName());
}
}
}
return clazzImplementationInstance;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checklist item #4 violated: There's no check for @Component annotation before creating an instance. According to the task requirements, if the Component annotation is missing above the class, we shouldn't be able to create an instance and should throw an exception. The createNewImstance method should verify clazz.isAnnotationPresent(Component.class) before instantiation.

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 a new instance of " + clazz.getName(), e);
}
}

private Class<?> findImplementation(Class<?> interfaceClazz) {
if (interfaceClazz.isInterface()) {
return interfaceImplementations.get(interfaceClazz);
}
return interfaceClazz;
}
}
2 changes: 2 additions & 0 deletions src/main/java/mate.academy/model/Product.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package mate.academy.model;

import java.math.BigDecimal;
import mate.academy.lib.Component;

@Component
public class Product {
private Long id;
private String name;
Expand Down
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checklist item #6 violation: interfaceImplementations should be initialized using Map.of() as a class field, not populated with put() inside findImplementation(). Change line 20 to: Map<Class, Class> interfaceImplementations = Map.of(FileReaderService.class, FileReaderServiceImpl.class, ProductParser.class, ProductParserImpl.class, ProductService.class, ProductServiceImpl.class);

Expand Down
Loading