-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathRedirectionManagerImpl.java
58 lines (47 loc) · 1.96 KB
/
RedirectionManagerImpl.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
54
55
56
57
58
package me.earth.headlessmc.lwjgl;
import me.earth.headlessmc.lwjgl.api.Redirection;
import me.earth.headlessmc.lwjgl.api.RedirectionManager;
import me.earth.headlessmc.lwjgl.redirections.CastRedirection;
import me.earth.headlessmc.lwjgl.redirections.DefaultRedirections;
import me.earth.headlessmc.lwjgl.redirections.LwjglRedirections;
import me.earth.headlessmc.lwjgl.redirections.ObjectRedirection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
public class RedirectionManagerImpl implements RedirectionManager {
private final Map<String, Redirection> redirects = new HashMap<>();
private final Redirection object = new ObjectRedirection(this);
private final Redirection cast = new CastRedirection(this);
public RedirectionManagerImpl() {
LwjglRedirections.register(this);
}
@Override
public void redirect(String desc, Redirection redirection) {
redirects.put(desc, redirection);
}
@Override
public Object invoke(Object obj, String desc, Class<?> type, Object... args)
throws Throwable {
return invoke(desc, type, obj, () -> getFallback(desc, type), args);
}
@Override
public Object invoke(String desc, Class<?> type, Object obj,
Supplier<Redirection> fb, Object... args)
throws Throwable {
Redirection redirection = redirects.get(desc);
if (redirection == null) {
redirection = fb.get();
}
return redirection.invoke(obj, desc, type, args);
}
private Redirection getFallback(String desc, Class<?> type) {
if (desc.startsWith(Redirection.CAST_PREFIX)) {
// TODO: currently cast redirection looks like this:
// <cast> java/lang/String
// <init> <cast> java/lang/String
// It contains no information about the calling class!
return cast;
}
return DefaultRedirections.fallback(type, object);
}
}