Skip to content

Commit

Permalink
Fixes after code review: added checking for singleton pattern, added @…
Browse files Browse the repository at this point in the history
…target annotation, changed order in getInstatnce() method, gathered exceptions in ReflectiveOperationException
  • Loading branch information
noiseintheattic committed Oct 6, 2024
1 parent 6e79da2 commit 891023c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/main/java/mate.academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ 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 = (ProductService) injector.getInstance(ProductService.class);

// Checking the singleton pattern (on AI mentor request)
Injector injectorSecond = Injector.getInjector();
if (!injector.equals(injectorSecond)) {
throw new RuntimeException("The singleton pattern doesn't work correctly");
}

List<Product> products = productService.getAllFromFile("products.txt");
products.forEach(System.out::println);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/mate.academy/lib/Component.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
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 {
}
10 changes: 4 additions & 6 deletions src/main/java/mate.academy/lib/Injector.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ public Object getInstance(Class<?> interfaceClazz) {
Object clazzImplementationInstance = null;
Class<?> clazz = findImplementation(interfaceClazz);
Field[] declaredFields = clazz.getDeclaredFields();
if (clazzImplementationInstance == null) {
clazzImplementationInstance = createNewInstance(clazz);
}
clazzImplementationInstance = createNewInstance(clazz);
for (Field field : declaredFields) {
if (field.isAnnotationPresent(Inject.class)) {
Object fieldInstance = getInstance(field.getType());
clazzImplementationInstance = createNewInstance(clazz);
field.setAccessible(true);
try {
field.set(clazzImplementationInstance, fieldInstance);
Expand All @@ -32,12 +35,7 @@ public Object getInstance(Class<?> interfaceClazz) {
}
}
}
if (clazzImplementationInstance == null) {
clazzImplementationInstance = createNewInstance(clazz);

}
return clazzImplementationInstance;

} else {
throw new RuntimeException("This instance of "
+ interfaceClazz + " class, cannot be created, because "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
@Component
public class ProductServiceImpl implements ProductService {
@Inject
private ProductParser productParser;
private ProductParser txtProductParser;

@Inject
private FileReaderService fileReaderService;
private FileReaderService txtFileReaderService;

@Override
public List<Product> getAllFromFile(String filePath) {
return fileReaderService.readFile(filePath)
return txtFileReaderService.readFile(filePath)
.stream()
.map(productParser::parse)
.map(txtProductParser::parse)
.collect(Collectors.toList());
}
}

0 comments on commit 891023c

Please sign in to comment.