Debian Codegraph @

native/jpype_module/src/main/java/org/jpype/proxy/JPypeProxy.java

raw symbols SCIP
1 /* ****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
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 * @author Karl Einar Nelson
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 // See following link for Java 8 default access implementation
48 // https://blog.jooq.org/correct-reflective-access-to-interface-default-methods-in-java-8-9-10/
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 // Proxies must point to the correct class loader. For most cases the
78 // system classloader is find. But if the class is in a custom classloader
79 // we need to use that one instead
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 // We can save a lot of effort on the C++ side by doing all the
105 // type lookup work here.
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 // Check first to see if Python has implementated it
121 Object result = hostInvoke(context.getContext(), method.getName(), instance, returnType, parameterTypes, args, missing);
122
123 // If we get a good result than return it
124 if (result != missing)
125 return result;
126
127 // If it is a default method in the interface then we have to invoke it using special reflection.
128 if (method.isDefault())
129 {
130 try
131 {
132 Class<?> cls = method.getDeclaringClass();
133
134 // Java 8
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 // Else throw... (this should never happen as proxies are checked when created.)
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 }