-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy
More file actions
92 lines (84 loc) · 3 KB
/
proxy
File metadata and controls
92 lines (84 loc) · 3 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package jep;
import jep.python.InvocationHandler;
import jep.python.PyObject;
/**
* Uses java.lang.reflect.Proxy to wrap Python objects.
*
* @author sohail
*/
class Proxy {
private static final long serialVersionUID = 1L;
protected static Object newDirectProxyInstance(Jep jep, long ltarget,
Class<?> targetInterface) {
ClassLoader loader = jep.getClassLoader();
InvocationHandler ih = null;
try {
ih = new InvocationHandler(jep, ltarget, true);
} catch (JepException e) {
throw new IllegalArgumentException(e);
}
Class<?> classes[] = { targetInterface };
return java.lang.reflect.Proxy.newProxyInstance(loader, classes, ih);
}
/**
* <pre>
* Returns an instance of a proxy class for the specified
* interfaces that dispatches method invocations to the specified
* invocation handler. This method is equivalent to:
*
* Proxy.getProxyClass(loader, interfaces).
* getConstructor(new Class[] { InvocationHandler.class }).
* newInstance(new Object[] { handler });
*
*
* Proxy.newProxyInstance throws IllegalArgumentException for the
* same reasons that Proxy.getProxyClass does.
* </pre>
*
* @param jep
* a <code>Jep</code> value
* @param ltarget
* a <code>long</code> value
* @param interfaces
* the list of interfaces to implement
* @return an <code>Object</code> value
* @throws IllegalArgumentException
* if an error occurs
*/
protected static Object newProxyInstance(Jep jep, long ltarget,
String[] interfaces) {
ClassLoader loader = jep.getClassLoader();
InvocationHandler ih = null;
try {
ih = new InvocationHandler(jep, ltarget, false);
} catch (JepException e) {
throw new IllegalArgumentException(e);
}
Class<?> classes[] = new Class<?>[interfaces.length];
try {
for (int i = 0; i < interfaces.length; i++)
classes[i] = loader.loadClass(interfaces[i]);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
return java.lang.reflect.Proxy.newProxyInstance(loader, classes, ih);
}
/**
* If the object passed in is a proxy for a PyObject, then return the
* wrapped PyObject otherwise return null.
*
* @param proxy
* the Object that may be a proxy
* @return the wrapped PyObject or null if there isn't one
*/
protected static PyObject getPyObject(Object proxy) {
if (java.lang.reflect.Proxy.isProxyClass(proxy.getClass())) {
java.lang.reflect.InvocationHandler ih = java.lang.reflect.Proxy
.getInvocationHandler(proxy);
if (ih instanceof InvocationHandler) {
return ((InvocationHandler) ih).getPyObject();
}
}
return null;
}
}