-
Notifications
You must be signed in to change notification settings - Fork 998
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
jv-dependency-injection #1015
base: main
Are you sure you want to change the base?
jv-dependency-injection #1015
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
package mate.academy.lib; | ||
|
||
public @interface Component { | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Component { | ||
} |
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 { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,68 @@ | ||
package mate.academy.lib; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Injector { | ||
private static final Injector injector = new Injector(); | ||
private final Map<Class<?>, Object> instances = new HashMap<>(); | ||
|
||
public static Injector getInjector() { | ||
return injector; | ||
} | ||
|
||
public Object getInstance(Class<?> interfaceClazz) { | ||
return null; | ||
// Try to resolve implementation for the given interface | ||
Class<?> implClass = findImplementation(interfaceClazz); | ||
|
||
// Check if class is annotated with @Component | ||
if (!implClass.isAnnotationPresent(Component.class)) { | ||
throw new RuntimeException("Class " + implClass.getName() | ||
+ " is not marked as @Component"); | ||
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When throwing an exception add an informative message to it. Also, don't forget to add an exception you're catching in |
||
} | ||
|
||
// Check if an instance already exists | ||
if (instances.containsKey(implClass)) { | ||
return instances.get(implClass); | ||
} | ||
|
||
// Create a new instance and inject dependencies | ||
Object instance = createInstance(implClass); | ||
instances.put(implClass, instance); | ||
return instance; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make Interface Implementations map a class field. You can fill it in using |
||
|
||
private Object createInstance(Class<?> implClass) { | ||
try { | ||
Object instance = implClass.getDeclaredConstructor().newInstance(); | ||
|
||
// Inject dependencies into fields annotated with @Inject | ||
for (Field field : implClass.getDeclaredFields()) { | ||
if (field.isAnnotationPresent(Inject.class)) { | ||
field.setAccessible(true); | ||
Object fieldInstance = getInstance(field.getType()); | ||
field.set(instance, fieldInstance); | ||
} | ||
} | ||
|
||
return instance; | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to create instance of " + implClass.getName(), e); | ||
Comment on lines
+45
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to replace many exceptions that have a common parent with a general parental exception to simplify the catch block. Consider using |
||
} | ||
} | ||
|
||
private Class<?> findImplementation(Class<?> interfaceClazz) { | ||
if (interfaceClazz.isInterface()) { | ||
if (interfaceClazz == mate.academy.service.FileReaderService.class) { | ||
return mate.academy.service.impl.FileReaderServiceImpl.class; | ||
} else if (interfaceClazz == mate.academy.service.ProductParser.class) { | ||
return mate.academy.service.impl.ProductParserImpl.class; | ||
} else if (interfaceClazz == mate.academy.service.ProductService.class) { | ||
return mate.academy.service.impl.ProductServiceImpl.class; | ||
} | ||
throw new RuntimeException("No implementation found for " + interfaceClazz.getName()); | ||
} | ||
return interfaceClazz; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the checklist, don't forget to add
@Target
to custom annotations. It specifies the kinds of program element to which an annotation type is applicable.