/
Injector.java
53 lines (47 loc) · 1.68 KB
/
Injector.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.denizenscript.denizen.utilities.command.manager;
import com.denizenscript.denizen.utilities.debugging.Debug;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Injector {
private final Class<?>[] argClasses;
private final Object[] args;
public Injector(Object... args) {
this.args = args;
argClasses = new Class[args.length];
for (int i = 0; i < args.length; ++i) {
argClasses[i] = args[i].getClass();
}
}
public Object getInstance(Class<?> clazz) {
try {
Constructor<?> ctr = clazz.getConstructor(argClasses);
ctr.setAccessible(true);
return ctr.newInstance(args);
}
catch (NoSuchMethodException e) {
try {
return clazz.newInstance();
}
catch (Exception ex) {
Debug.echoError("Error initializing commands class " + clazz + ": ");
Debug.echoError(ex);
return null;
}
}
catch (InvocationTargetException e) {
Debug.echoError("Error initializing commands class " + clazz + ": ");
Debug.echoError(e);
return null;
}
catch (InstantiationException e) {
Debug.echoError("Error initializing commands class " + clazz + ": ");
Debug.echoError(e);
return null;
}
catch (IllegalAccessException e) {
Debug.echoError("Error initializing commands class " + clazz + ": ");
Debug.echoError(e);
return null;
}
}
}