-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicProxyMixin.java
More file actions
58 lines (46 loc) · 1.43 KB
/
DynamicProxyMixin.java
File metadata and controls
58 lines (46 loc) · 1.43 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package btp.oneP;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
public class DynamicProxyMixin {
public static void main(String[] args) {
}
}
class MixinProxy implements InvocationHandler{
Map<String,Object> delegatesByMethod;
public MixinProxy(TwoTuple<Object,Class<?>>...pairs){
this.delegatesByMethod = new HashMap<String,Object>();
for(TwoTuple<Object,Class<?>> pair:pairs){
for(Method m : pair.second.getMethods()){
String methodName = m.getName();
if(!this.delegatesByMethod.containsKey(methodName)){
this.delegatesByMethod.put(methodName, pair.first);
}
}
}
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
Object delegate = this.delegatesByMethod.get(methodName);
return method.invoke(delegate, args);
}
public static Object newInstance(TwoTuple...pairs){
Class[] interfaces = new Class[pairs.length];
for(int i=0;i<pairs.length;i++){
interfaces[i] = (Class) pairs[i].second;
}
ClassLoader cl = pairs[0].first.getClass().getClassLoader();
return Proxy.newProxyInstance(cl, interfaces, new MixinProxy(pairs));
}
}
class TwoTuple<K,V>{
K first;
V second;
public TwoTuple(K k,V v){
this.first = k;
this.second = v;
}
}