1 /* |
|
2 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package java.dyn; |
|
27 |
|
28 import java.dyn.MethodHandles.Lookup; |
|
29 import java.lang.reflect.AccessibleObject; |
|
30 import java.lang.reflect.Field; |
|
31 import static java.dyn.MethodHandleNatives.Constants.*; |
|
32 import static java.dyn.MethodHandles.Lookup.IMPL_LOOKUP; |
|
33 |
|
34 /** |
|
35 * The JVM interface for the method handles package is all here. |
|
36 * This is an interface internal and private to an implemetantion of JSR 292. |
|
37 * <em>This class is not part of the JSR 292 standard.</em> |
|
38 * @author jrose |
|
39 */ |
|
40 class MethodHandleNatives { |
|
41 |
|
42 private MethodHandleNatives() { } // static only |
|
43 |
|
44 /// MethodName support |
|
45 |
|
46 static native void init(MemberName self, Object ref); |
|
47 static native void expand(MemberName self); |
|
48 static native void resolve(MemberName self, Class<?> caller); |
|
49 static native int getMembers(Class<?> defc, String matchName, String matchSig, |
|
50 int matchFlags, Class<?> caller, int skip, MemberName[] results); |
|
51 |
|
52 /// MethodHandle support |
|
53 |
|
54 /** Initialize the method handle to adapt the call. */ |
|
55 static native void init(AdapterMethodHandle self, MethodHandle target, int argnum); |
|
56 /** Initialize the method handle to call the correct method, directly. */ |
|
57 static native void init(BoundMethodHandle self, Object target, int argnum); |
|
58 /** Initialize the method handle to call as if by an invoke* instruction. */ |
|
59 static native void init(DirectMethodHandle self, Object ref, boolean doDispatch, Class<?> caller); |
|
60 |
|
61 /** Initialize a method type, once per form. */ |
|
62 static native void init(MethodType self); |
|
63 |
|
64 /** Tell the JVM about a class's bootstrap method. */ |
|
65 static native void registerBootstrap(Class<?> caller, MethodHandle bootstrapMethod); |
|
66 |
|
67 /** Ask the JVM about a class's bootstrap method. */ |
|
68 static native MethodHandle getBootstrap(Class<?> caller); |
|
69 |
|
70 /** Tell the JVM that we need to change the target of an invokedynamic. */ |
|
71 static native void setCallSiteTarget(CallSite site, MethodHandle target); |
|
72 |
|
73 /** Fetch the vmtarget field. |
|
74 * It will be sanitized as necessary to avoid exposing non-Java references. |
|
75 * This routine is for debugging and reflection. |
|
76 */ |
|
77 static native Object getTarget(MethodHandle self, int format); |
|
78 |
|
79 /** Fetch the name of the handled method, if available. |
|
80 * This routine is for debugging and reflection. |
|
81 */ |
|
82 static MemberName getMethodName(MethodHandle self) { |
|
83 return (MemberName) getTarget(self, ETF_METHOD_NAME); |
|
84 } |
|
85 |
|
86 /** Fetch the reflective version of the handled method, if available. |
|
87 */ |
|
88 static AccessibleObject getTargetMethod(MethodHandle self) { |
|
89 return (AccessibleObject) getTarget(self, ETF_REFLECT_METHOD); |
|
90 } |
|
91 |
|
92 /** Fetch the target of this method handle. |
|
93 * If it directly targets a method, return a MemberName for the method. |
|
94 * If it is chained to another method handle, return that handle. |
|
95 */ |
|
96 static Object getTargetInfo(MethodHandle self) { |
|
97 return getTarget(self, ETF_HANDLE_OR_METHOD_NAME); |
|
98 } |
|
99 |
|
100 static Object[] makeTarget(Class<?> defc, String name, String sig, int mods, Class<?> refc) { |
|
101 return new Object[] { defc, name, sig, mods, refc }; |
|
102 } |
|
103 |
|
104 /** Fetch MH-related JVM parameter. |
|
105 * which=0 retrieves MethodHandlePushLimit |
|
106 * which=1 retrieves stack slot push size (in address units) |
|
107 */ |
|
108 static native int getConstant(int which); |
|
109 |
|
110 /** Java copy of MethodHandlePushLimit in range 2..255. */ |
|
111 static final int JVM_PUSH_LIMIT; |
|
112 /** JVM stack motion (in words) after one slot is pushed, usually -1. |
|
113 */ |
|
114 static final int JVM_STACK_MOVE_UNIT; |
|
115 |
|
116 /** Which conv-ops are implemented by the JVM? */ |
|
117 static final int CONV_OP_IMPLEMENTED_MASK; |
|
118 |
|
119 private static native void registerNatives(); |
|
120 static { |
|
121 int JVM_PUSH_LIMIT_; |
|
122 int JVM_STACK_MOVE_UNIT_; |
|
123 int CONV_OP_IMPLEMENTED_MASK_; |
|
124 try { |
|
125 registerNatives(); |
|
126 JVM_PUSH_LIMIT_ = getConstant(Constants.GC_JVM_PUSH_LIMIT); |
|
127 JVM_STACK_MOVE_UNIT_ = getConstant(Constants.GC_JVM_STACK_MOVE_UNIT); |
|
128 CONV_OP_IMPLEMENTED_MASK_ = getConstant(Constants.GC_CONV_OP_IMPLEMENTED_MASK); |
|
129 //sun.reflect.Reflection.registerMethodsToFilter(MethodHandleImpl.class, "init"); |
|
130 } catch (UnsatisfiedLinkError ee) { |
|
131 // ignore; if we use init() methods later we'll see linkage errors |
|
132 JVM_PUSH_LIMIT_ = 3; // arbitrary |
|
133 JVM_STACK_MOVE_UNIT_ = -1; // arbitrary |
|
134 CONV_OP_IMPLEMENTED_MASK_ = 0; |
|
135 JVM_PUSH_LIMIT = JVM_PUSH_LIMIT_; |
|
136 JVM_STACK_MOVE_UNIT = JVM_STACK_MOVE_UNIT_; |
|
137 throw ee; // just die; hopeless to try to run with an older JVM |
|
138 } |
|
139 JVM_PUSH_LIMIT = JVM_PUSH_LIMIT_; |
|
140 JVM_STACK_MOVE_UNIT = JVM_STACK_MOVE_UNIT_; |
|
141 if (CONV_OP_IMPLEMENTED_MASK_ == 0) |
|
142 CONV_OP_IMPLEMENTED_MASK_ = DEFAULT_CONV_OP_IMPLEMENTED_MASK; |
|
143 CONV_OP_IMPLEMENTED_MASK = CONV_OP_IMPLEMENTED_MASK_; |
|
144 } |
|
145 |
|
146 // All compile-time constants go here. |
|
147 // There is an opportunity to check them against the JVM's idea of them. |
|
148 static class Constants { |
|
149 Constants() { } // static only |
|
150 // MethodHandleImpl |
|
151 static final int // for getConstant |
|
152 GC_JVM_PUSH_LIMIT = 0, |
|
153 GC_JVM_STACK_MOVE_UNIT = 1, |
|
154 GC_CONV_OP_IMPLEMENTED_MASK = 2; |
|
155 static final int |
|
156 ETF_HANDLE_OR_METHOD_NAME = 0, // all available data (immediate MH or method) |
|
157 ETF_DIRECT_HANDLE = 1, // ultimate method handle (will be a DMH, may be self) |
|
158 ETF_METHOD_NAME = 2, // ultimate method as MemberName |
|
159 ETF_REFLECT_METHOD = 3; // ultimate method as java.lang.reflect object (sans refClass) |
|
160 |
|
161 // MemberName |
|
162 // The JVM uses values of -2 and above for vtable indexes. |
|
163 // Field values are simple positive offsets. |
|
164 // Ref: src/share/vm/oops/methodOop.hpp |
|
165 // This value is negative enough to avoid such numbers, |
|
166 // but not too negative. |
|
167 static final int |
|
168 MN_IS_METHOD = 0x00010000, // method (not constructor) |
|
169 MN_IS_CONSTRUCTOR = 0x00020000, // constructor |
|
170 MN_IS_FIELD = 0x00040000, // field |
|
171 MN_IS_TYPE = 0x00080000, // nested type |
|
172 MN_SEARCH_SUPERCLASSES = 0x00100000, // for MHN.getMembers |
|
173 MN_SEARCH_INTERFACES = 0x00200000, // for MHN.getMembers |
|
174 VM_INDEX_UNINITIALIZED = -99; |
|
175 |
|
176 // AdapterMethodHandle |
|
177 /** Conversions recognized by the JVM. |
|
178 * They must align with the constants in java.dyn_AdapterMethodHandle, |
|
179 * in the JVM file hotspot/src/share/vm/classfile/javaClasses.hpp. |
|
180 */ |
|
181 static final int |
|
182 OP_RETYPE_ONLY = 0x0, // no argument changes; straight retype |
|
183 OP_RETYPE_RAW = 0x1, // no argument changes; straight retype |
|
184 OP_CHECK_CAST = 0x2, // ref-to-ref conversion; requires a Class argument |
|
185 OP_PRIM_TO_PRIM = 0x3, // converts from one primitive to another |
|
186 OP_REF_TO_PRIM = 0x4, // unboxes a wrapper to produce a primitive |
|
187 OP_PRIM_TO_REF = 0x5, // boxes a primitive into a wrapper (NYI) |
|
188 OP_SWAP_ARGS = 0x6, // swap arguments (vminfo is 2nd arg) |
|
189 OP_ROT_ARGS = 0x7, // rotate arguments (vminfo is displaced arg) |
|
190 OP_DUP_ARGS = 0x8, // duplicates one or more arguments (at TOS) |
|
191 OP_DROP_ARGS = 0x9, // remove one or more argument slots |
|
192 OP_COLLECT_ARGS = 0xA, // combine one or more arguments into a varargs (NYI) |
|
193 OP_SPREAD_ARGS = 0xB, // expand in place a varargs array (of known size) |
|
194 OP_FLYBY = 0xC, // operate first on reified argument list (NYI) |
|
195 OP_RICOCHET = 0xD, // run an adapter chain on the return value (NYI) |
|
196 CONV_OP_LIMIT = 0xE; // limit of CONV_OP enumeration |
|
197 /** Shift and mask values for decoding the AMH.conversion field. |
|
198 * These numbers are shared with the JVM for creating AMHs. |
|
199 */ |
|
200 static final int |
|
201 CONV_OP_MASK = 0xF00, // this nybble contains the conversion op field |
|
202 CONV_VMINFO_MASK = 0x0FF, // LSB is reserved for JVM use |
|
203 CONV_VMINFO_SHIFT = 0, // position of bits in CONV_VMINFO_MASK |
|
204 CONV_OP_SHIFT = 8, // position of bits in CONV_OP_MASK |
|
205 CONV_DEST_TYPE_SHIFT = 12, // byte 2 has the adapter BasicType (if needed) |
|
206 CONV_SRC_TYPE_SHIFT = 16, // byte 2 has the source BasicType (if needed) |
|
207 CONV_STACK_MOVE_SHIFT = 20, // high 12 bits give signed SP change |
|
208 CONV_STACK_MOVE_MASK = (1 << (32 - CONV_STACK_MOVE_SHIFT)) - 1; |
|
209 |
|
210 /** Which conv-ops are implemented by the JVM? */ |
|
211 static final int DEFAULT_CONV_OP_IMPLEMENTED_MASK = |
|
212 // Value to use if the corresponding JVM query fails. |
|
213 ((1<<OP_RETYPE_ONLY) |
|
214 |(1<<OP_RETYPE_RAW) |
|
215 |(1<<OP_CHECK_CAST) |
|
216 |(1<<OP_PRIM_TO_PRIM) |
|
217 |(1<<OP_REF_TO_PRIM) |
|
218 |(1<<OP_SWAP_ARGS) |
|
219 |(1<<OP_ROT_ARGS) |
|
220 |(1<<OP_DUP_ARGS) |
|
221 |(1<<OP_DROP_ARGS) |
|
222 //|(1<<OP_SPREAD_ARGS) |
|
223 ); |
|
224 |
|
225 /** |
|
226 * Basic types as encoded in the JVM. These code values are not |
|
227 * intended for use outside this class. They are used as part of |
|
228 * a private interface between the JVM and this class. |
|
229 */ |
|
230 static final int |
|
231 T_BOOLEAN = 4, |
|
232 T_CHAR = 5, |
|
233 T_FLOAT = 6, |
|
234 T_DOUBLE = 7, |
|
235 T_BYTE = 8, |
|
236 T_SHORT = 9, |
|
237 T_INT = 10, |
|
238 T_LONG = 11, |
|
239 T_OBJECT = 12, |
|
240 //T_ARRAY = 13 |
|
241 T_VOID = 14; |
|
242 //T_ADDRESS = 15 |
|
243 |
|
244 /** |
|
245 * Constant pool reference-kind codes, as used by CONSTANT_MethodHandle CP entries. |
|
246 */ |
|
247 static final int |
|
248 REF_getField = 1, |
|
249 REF_getStatic = 2, |
|
250 REF_putField = 3, |
|
251 REF_putStatic = 4, |
|
252 REF_invokeVirtual = 5, |
|
253 REF_invokeStatic = 6, |
|
254 REF_invokeSpecial = 7, |
|
255 REF_newInvokeSpecial = 8, |
|
256 REF_invokeInterface = 9; |
|
257 } |
|
258 |
|
259 private static native int getNamedCon(int which, Object[] name); |
|
260 static boolean verifyConstants() { |
|
261 Object[] box = { null }; |
|
262 for (int i = 0; ; i++) { |
|
263 box[0] = null; |
|
264 int vmval = getNamedCon(i, box); |
|
265 if (box[0] == null) break; |
|
266 String name = (String) box[0]; |
|
267 try { |
|
268 Field con = Constants.class.getDeclaredField(name); |
|
269 int jval = con.getInt(null); |
|
270 if (jval != vmval) |
|
271 throw new InternalError(name+": JVM has "+vmval+" while Java has "+jval); |
|
272 } catch (Exception ex) { |
|
273 throw new InternalError(name+": access failed, got "+ex); |
|
274 } |
|
275 } |
|
276 return true; |
|
277 } |
|
278 static { |
|
279 verifyConstants(); |
|
280 } |
|
281 |
|
282 // Up-calls from the JVM. |
|
283 // These must NOT be public. |
|
284 |
|
285 /** |
|
286 * The JVM is linking an invokedynamic instruction. Create a reified call site for it. |
|
287 */ |
|
288 static CallSite makeDynamicCallSite(MethodHandle bootstrapMethod, |
|
289 String name, MethodType type, |
|
290 Object info, |
|
291 MemberName callerMethod, int callerBCI) { |
|
292 return CallSite.makeSite(bootstrapMethod, name, type, info, callerMethod, callerBCI); |
|
293 } |
|
294 |
|
295 /** |
|
296 * Called by the JVM to check the length of a spread array. |
|
297 */ |
|
298 static void checkSpreadArgument(Object av, int n) { |
|
299 MethodHandleStatics.checkSpreadArgument(av, n); |
|
300 } |
|
301 |
|
302 /** |
|
303 * The JVM wants a pointer to a MethodType. Oblige it by finding or creating one. |
|
304 */ |
|
305 static MethodType findMethodHandleType(Class<?> rtype, Class<?>[] ptypes) { |
|
306 return MethodType.makeImpl(rtype, ptypes, true); |
|
307 } |
|
308 |
|
309 /** |
|
310 * The JVM wants to use a MethodType with invokeGeneric. Give the runtime fair warning. |
|
311 */ |
|
312 static void notifyGenericMethodType(MethodType type) { |
|
313 type.form().notifyGenericMethodType(); |
|
314 } |
|
315 |
|
316 /** |
|
317 * The JVM wants to raise an exception. Here's the path. |
|
318 */ |
|
319 static void raiseException(int code, Object actual, Object required) { |
|
320 String message; |
|
321 // disregard the identity of the actual object, if it is not a class: |
|
322 if (!(actual instanceof Class) && !(actual instanceof MethodType)) |
|
323 actual = actual.getClass(); |
|
324 if (actual != null) |
|
325 message = "required "+required+" but encountered "+actual; |
|
326 else |
|
327 message = "required "+required; |
|
328 switch (code) { |
|
329 case 192: // checkcast |
|
330 throw new ClassCastException(message); |
|
331 default: |
|
332 throw new InternalError("unexpected code "+code+": "+message); |
|
333 } |
|
334 } |
|
335 |
|
336 /** |
|
337 * The JVM is resolving a CONSTANT_MethodHandle CP entry. And it wants our help. |
|
338 * It will make an up-call to this method. (Do not change the name or signature.) |
|
339 */ |
|
340 static MethodHandle linkMethodHandleConstant(Class<?> callerClass, int refKind, |
|
341 Class<?> defc, String name, Object type) { |
|
342 try { |
|
343 Lookup lookup = IMPL_LOOKUP.in(callerClass); |
|
344 switch (refKind) { |
|
345 case REF_getField: return lookup.findGetter( defc, name, (Class<?>) type ); |
|
346 case REF_getStatic: return lookup.findStaticGetter( defc, name, (Class<?>) type ); |
|
347 case REF_putField: return lookup.findSetter( defc, name, (Class<?>) type ); |
|
348 case REF_putStatic: return lookup.findStaticSetter( defc, name, (Class<?>) type ); |
|
349 case REF_invokeVirtual: return lookup.findVirtual( defc, name, (MethodType) type ); |
|
350 case REF_invokeStatic: return lookup.findStatic( defc, name, (MethodType) type ); |
|
351 case REF_invokeSpecial: return lookup.findSpecial( defc, name, (MethodType) type, callerClass ); |
|
352 case REF_newInvokeSpecial: return lookup.findConstructor( defc, (MethodType) type ); |
|
353 case REF_invokeInterface: return lookup.findVirtual( defc, name, (MethodType) type ); |
|
354 } |
|
355 throw new IllegalArgumentException("bad MethodHandle constant "+name+" : "+type); |
|
356 } catch (ReflectiveOperationException ex) { |
|
357 Error err = new IncompatibleClassChangeError(); |
|
358 err.initCause(ex); |
|
359 throw err; |
|
360 } |
|
361 } |
|
362 } |
|