-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Injector.java
47 lines (42 loc) · 1.6 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
package net.citizensnpcs.api.command;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import net.citizensnpcs.api.util.Messaging;
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) {
Messaging.severe("Error initializing commands class " + clazz + ": ");
ex.printStackTrace();
return null;
}
} catch (InvocationTargetException e) {
Messaging.severe("Error initializing commands class " + clazz + ": ");
e.printStackTrace();
return null;
} catch (InstantiationException e) {
Messaging.severe("Error initializing commands class " + clazz + ": ");
e.printStackTrace();
return null;
} catch (IllegalAccessException e) {
Messaging.severe("Error initializing commands class " + clazz + ": ");
e.printStackTrace();
return null;
}
}
}