-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
36 lines (32 loc) · 1.58 KB
/
Main.java
File metadata and controls
36 lines (32 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import ConstructorReflection.DIDemo.ClassA;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
// We will initialise object/dependencies in ConstructorReflection.DIDemo class which are package-private using reflection
// Automatic dependency injection using reflection
ClassA rootClass = (ClassA) initialiseDIGraph(Class.forName("ConstructorReflection.DIDemo.ClassA"));
System.out.println(rootClass);
}
static <T> T initialiseDIGraph(Class<T> clazz) throws IllegalAccessException, InvocationTargetException, InstantiationException {
Constructor<?> constructor = getFirstConstructor(clazz);
List<Object> constructorArgs = new ArrayList<>();
for (Class<?> argType : constructor.getParameterTypes()) {
// For ClassA, we will get ClassB and ClassC classes as constructor args
Object argValue = initialiseDIGraph(argType);
constructorArgs.add(argValue);
}
constructor.setAccessible(true); // to access package private constructor
return (T) constructor.newInstance(constructorArgs.toArray());
}
// logic could be extended to consider other multiple constructor of a class as well
static Constructor<?> getFirstConstructor(Class<?> clazz) {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
if (constructors.length == 0) {
throw new RuntimeException("No constructor found");
}
return constructors[0];
}
}