| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package org.jpype.proxy; |
| 17 |
|
| 18 |
import java.lang.invoke.MethodHandles; |
| 19 |
import java.lang.invoke.MethodHandles.Lookup; |
| 20 |
import java.lang.invoke.MethodType; |
| 21 |
import java.lang.reflect.Constructor; |
| 22 |
import java.lang.reflect.InvocationHandler; |
| 23 |
import java.lang.reflect.Method; |
| 24 |
import java.lang.reflect.Proxy; |
| 25 |
import java.util.logging.Level; |
| 26 |
import java.util.logging.Logger; |
| 27 |
import org.jpype.JPypeContext; |
| 28 |
import org.jpype.manager.TypeManager; |
| 29 |
import org.jpype.ref.JPypeReferenceQueue; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class JPypeProxy implements InvocationHandler |
| 36 |
{ |
| 37 |
|
| 38 |
private final static Constructor<Lookup> constructor; |
| 39 |
private final static JPypeReferenceQueue referenceQueue = JPypeReferenceQueue.getInstance(); |
| 40 |
JPypeContext context; |
| 41 |
public long instance; |
| 42 |
public long cleanup; |
| 43 |
Class<?>[] interfaces; |
| 44 |
ClassLoader cl = ClassLoader.getSystemClassLoader(); |
| 45 |
public static Object missing = new Object(); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static |
| 50 |
{ |
| 51 |
Constructor<Lookup> c = null; |
| 52 |
if (System.getProperty("java.version").startsWith("1.")) |
| 53 |
{ |
| 54 |
try |
| 55 |
{ |
| 56 |
c = Lookup.class |
| 57 |
.getDeclaredConstructor(Class.class); |
| 58 |
c.setAccessible(true); |
| 59 |
} catch (NoSuchMethodException | SecurityException ex) |
| 60 |
{ |
| 61 |
Logger.getLogger(JPypeProxy.class.getName()).log(Level.SEVERE, null, ex); |
| 62 |
} |
| 63 |
} |
| 64 |
constructor = c; |
| 65 |
} |
| 66 |
|
| 67 |
public static JPypeProxy newProxy(JPypeContext context, |
| 68 |
long instance, |
| 69 |
long cleanup, |
| 70 |
Class<?>[] interfaces) |
| 71 |
{ |
| 72 |
JPypeProxy proxy = new JPypeProxy(); |
| 73 |
proxy.context = context; |
| 74 |
proxy.instance = instance; |
| 75 |
proxy.interfaces = interfaces; |
| 76 |
proxy.cleanup = cleanup; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
for (Class cls : interfaces) |
| 81 |
{ |
| 82 |
ClassLoader icl = cls.getClassLoader(); |
| 83 |
if (icl != null && icl != proxy.cl) |
| 84 |
proxy.cl = icl; |
| 85 |
} |
| 86 |
return proxy; |
| 87 |
} |
| 88 |
|
| 89 |
public Object newInstance() |
| 90 |
{ |
| 91 |
Object out = Proxy.newProxyInstance(cl, interfaces, this); |
| 92 |
referenceQueue.registerRef(out, instance, cleanup); |
| 93 |
return out; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public Object invoke(Object proxy, Method method, Object[] args) |
| 98 |
throws Throwable |
| 99 |
{ |
| 100 |
|
| 101 |
if (context.isShutdown()) |
| 102 |
throw new RuntimeException("Proxy called during shutdown"); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
TypeManager typeManager = context.getTypeManager(); |
| 107 |
long returnType; |
| 108 |
long[] parameterTypes; |
| 109 |
synchronized (typeManager) |
| 110 |
{ |
| 111 |
returnType = typeManager.findClass(method.getReturnType()); |
| 112 |
Class<?>[] types = method.getParameterTypes(); |
| 113 |
parameterTypes = new long[types.length]; |
| 114 |
for (int i = 0; i < types.length; ++i) |
| 115 |
{ |
| 116 |
parameterTypes[i] = typeManager.findClass(types[i]); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
Object result = hostInvoke(context.getContext(), method.getName(), instance, returnType, parameterTypes, args, missing); |
| 122 |
|
| 123 |
|
| 124 |
if (result != missing) |
| 125 |
return result; |
| 126 |
|
| 127 |
|
| 128 |
if (method.isDefault()) |
| 129 |
{ |
| 130 |
try |
| 131 |
{ |
| 132 |
Class<?> cls = method.getDeclaringClass(); |
| 133 |
|
| 134 |
|
| 135 |
if (constructor != null) |
| 136 |
{ |
| 137 |
return constructor.newInstance(cls) |
| 138 |
.findSpecial(cls, |
| 139 |
method.getName(), |
| 140 |
MethodType.methodType(method.getReturnType()), |
| 141 |
cls) |
| 142 |
.bindTo(proxy) |
| 143 |
.invokeWithArguments(args); |
| 144 |
} |
| 145 |
|
| 146 |
return MethodHandles.lookup() |
| 147 |
.findSpecial(cls, |
| 148 |
method.getName(), |
| 149 |
MethodType.methodType(method.getReturnType()), |
| 150 |
cls) |
| 151 |
.bindTo(proxy) |
| 152 |
.invokeWithArguments(args); |
| 153 |
} catch (java.lang.IllegalAccessException ex) |
| 154 |
{ |
| 155 |
throw new RuntimeException(ex); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
throw new NoSuchMethodError(method.getName()); |
| 161 |
} |
| 162 |
|
| 163 |
private static native Object hostInvoke(long context, String name, long pyObject, |
| 164 |
long returnType, long[] argsTypes, Object[] args, Object bad); |
| 165 |
} |