-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathObjectRedirection.java
51 lines (44 loc) · 1.72 KB
/
ObjectRedirection.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
package me.earth.headlessmc.lwjgl.redirections;
import lombok.RequiredArgsConstructor;
import lombok.val;
import me.earth.headlessmc.lwjgl.api.Redirection;
import me.earth.headlessmc.lwjgl.api.RedirectionManager;
import me.earth.headlessmc.lwjgl.util.DescriptionUtil;
import java.lang.reflect.Array;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.Arrays;
@RequiredArgsConstructor
public class ObjectRedirection implements Redirection {
private final RedirectionManager manager;
@Override
public Object invoke(Object obj, String d, Class<?> type, Object... args) {
if (type.isInterface()) {
return Proxy.newProxyInstance(
type.getClassLoader(), new Class<?>[]{type},
new ProxyRedirection(manager, DescriptionUtil.getDesc(type)));
} else if (type.isArray()) {
int dimension = 0;
while (type.isArray()) {
dimension++;
type = type.getComponentType();
}
val dimensions = new int[dimension];
Arrays.fill(dimensions, 0);
return Array.newInstance(type, dimensions);
} else if (Modifier.isAbstract(type.getModifiers())) {
// TODO: logger for headlessmc-lwjgl?
System.err.println("Can't return abstract class: " + d);
return null;
}
try {
val constructor = type.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
} catch (SecurityException | ReflectiveOperationException e) {
//noinspection CallToPrintStackTrace
e.printStackTrace();
return null;
}
}
}