|
1 /* |
|
2 * Copyright (c) 2008, 2014, 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. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 #include "precompiled.hpp" |
|
26 #include "classfile/symbolTable.hpp" |
|
27 #include "compiler/compileBroker.hpp" |
|
28 #include "interpreter/interpreter.hpp" |
|
29 #include "interpreter/oopMapCache.hpp" |
|
30 #include "memory/allocation.inline.hpp" |
|
31 #include "memory/oopFactory.hpp" |
|
32 #include "prims/jvmtiRedefineClassesTrace.hpp" |
|
33 #include "prims/methodHandles.hpp" |
|
34 #include "runtime/compilationPolicy.hpp" |
|
35 #include "runtime/javaCalls.hpp" |
|
36 #include "runtime/reflection.hpp" |
|
37 #include "runtime/signature.hpp" |
|
38 #include "runtime/stubRoutines.hpp" |
|
39 |
|
40 |
|
41 /* |
|
42 * JSR 292 reference implementation: method handles |
|
43 * The JDK 7 reference implementation represented method handle |
|
44 * combinations as chains. Each link in the chain had a "vmentry" |
|
45 * field which pointed at a bit of assembly code which performed |
|
46 * one transformation before dispatching to the next link in the chain. |
|
47 * |
|
48 * The current reference implementation pushes almost all code generation |
|
49 * responsibility to (trusted) Java code. A method handle contains a |
|
50 * pointer to its "LambdaForm", which embodies all details of the method |
|
51 * handle's behavior. The LambdaForm is a normal Java object, managed |
|
52 * by a runtime coded in Java. |
|
53 */ |
|
54 |
|
55 bool MethodHandles::_enabled = false; // set true after successful native linkage |
|
56 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL; |
|
57 |
|
58 //------------------------------------------------------------------------------ |
|
59 // MethodHandles::generate_adapters |
|
60 // |
|
61 void MethodHandles::generate_adapters() { |
|
62 if (!EnableInvokeDynamic || SystemDictionary::MethodHandle_klass() == NULL) return; |
|
63 |
|
64 assert(_adapter_code == NULL, "generate only once"); |
|
65 |
|
66 ResourceMark rm; |
|
67 TraceTime timer("MethodHandles adapters generation", TraceStartupTime); |
|
68 _adapter_code = MethodHandlesAdapterBlob::create(adapter_code_size); |
|
69 if (_adapter_code == NULL) |
|
70 vm_exit_out_of_memory(adapter_code_size, OOM_MALLOC_ERROR, |
|
71 "CodeCache: no room for MethodHandles adapters"); |
|
72 { |
|
73 CodeBuffer code(_adapter_code); |
|
74 MethodHandlesAdapterGenerator g(&code); |
|
75 g.generate(); |
|
76 code.log_section_sizes("MethodHandlesAdapterBlob"); |
|
77 } |
|
78 } |
|
79 |
|
80 //------------------------------------------------------------------------------ |
|
81 // MethodHandlesAdapterGenerator::generate |
|
82 // |
|
83 void MethodHandlesAdapterGenerator::generate() { |
|
84 // Generate generic method handle adapters. |
|
85 // Generate interpreter entries |
|
86 for (Interpreter::MethodKind mk = Interpreter::method_handle_invoke_FIRST; |
|
87 mk <= Interpreter::method_handle_invoke_LAST; |
|
88 mk = Interpreter::MethodKind(1 + (int)mk)) { |
|
89 vmIntrinsics::ID iid = Interpreter::method_handle_intrinsic(mk); |
|
90 StubCodeMark mark(this, "MethodHandle::interpreter_entry", vmIntrinsics::name_at(iid)); |
|
91 address entry = MethodHandles::generate_method_handle_interpreter_entry(_masm, iid); |
|
92 if (entry != NULL) { |
|
93 Interpreter::set_entry_for_kind(mk, entry); |
|
94 } |
|
95 // If the entry is not set, it will throw AbstractMethodError. |
|
96 } |
|
97 } |
|
98 |
|
99 void MethodHandles::set_enabled(bool z) { |
|
100 if (_enabled != z) { |
|
101 guarantee(z && EnableInvokeDynamic, "can only enable once, and only if -XX:+EnableInvokeDynamic"); |
|
102 _enabled = z; |
|
103 } |
|
104 } |
|
105 |
|
106 // MemberName support |
|
107 |
|
108 // import java_lang_invoke_MemberName.* |
|
109 enum { |
|
110 IS_METHOD = java_lang_invoke_MemberName::MN_IS_METHOD, |
|
111 IS_CONSTRUCTOR = java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR, |
|
112 IS_FIELD = java_lang_invoke_MemberName::MN_IS_FIELD, |
|
113 IS_TYPE = java_lang_invoke_MemberName::MN_IS_TYPE, |
|
114 CALLER_SENSITIVE = java_lang_invoke_MemberName::MN_CALLER_SENSITIVE, |
|
115 REFERENCE_KIND_SHIFT = java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT, |
|
116 REFERENCE_KIND_MASK = java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK, |
|
117 SEARCH_SUPERCLASSES = java_lang_invoke_MemberName::MN_SEARCH_SUPERCLASSES, |
|
118 SEARCH_INTERFACES = java_lang_invoke_MemberName::MN_SEARCH_INTERFACES, |
|
119 ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE |
|
120 }; |
|
121 |
|
122 Handle MethodHandles::new_MemberName(TRAPS) { |
|
123 Handle empty; |
|
124 instanceKlassHandle k(THREAD, SystemDictionary::MemberName_klass()); |
|
125 if (!k->is_initialized()) k->initialize(CHECK_(empty)); |
|
126 return Handle(THREAD, k->allocate_instance(THREAD)); |
|
127 } |
|
128 |
|
129 oop MethodHandles::init_MemberName(Handle mname, Handle target) { |
|
130 // This method is used from java.lang.invoke.MemberName constructors. |
|
131 // It fills in the new MemberName from a java.lang.reflect.Member. |
|
132 Thread* thread = Thread::current(); |
|
133 oop target_oop = target(); |
|
134 Klass* target_klass = target_oop->klass(); |
|
135 if (target_klass == SystemDictionary::reflect_Field_klass()) { |
|
136 oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder() |
|
137 int slot = java_lang_reflect_Field::slot(target_oop); // fd.index() |
|
138 KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); |
|
139 if (!k.is_null() && k->oop_is_instance()) { |
|
140 fieldDescriptor fd(InstanceKlass::cast(k()), slot); |
|
141 oop mname2 = init_field_MemberName(mname, fd); |
|
142 if (mname2 != NULL) { |
|
143 // Since we have the reified name and type handy, add them to the result. |
|
144 if (java_lang_invoke_MemberName::name(mname2) == NULL) |
|
145 java_lang_invoke_MemberName::set_name(mname2, java_lang_reflect_Field::name(target_oop)); |
|
146 if (java_lang_invoke_MemberName::type(mname2) == NULL) |
|
147 java_lang_invoke_MemberName::set_type(mname2, java_lang_reflect_Field::type(target_oop)); |
|
148 } |
|
149 return mname2; |
|
150 } |
|
151 } else if (target_klass == SystemDictionary::reflect_Method_klass()) { |
|
152 oop clazz = java_lang_reflect_Method::clazz(target_oop); |
|
153 int slot = java_lang_reflect_Method::slot(target_oop); |
|
154 KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); |
|
155 if (!k.is_null() && k->oop_is_instance()) { |
|
156 Method* m = InstanceKlass::cast(k())->method_with_idnum(slot); |
|
157 if (m == NULL || is_signature_polymorphic(m->intrinsic_id())) |
|
158 return NULL; // do not resolve unless there is a concrete signature |
|
159 CallInfo info(m, k()); |
|
160 return init_method_MemberName(mname, info); |
|
161 } |
|
162 } else if (target_klass == SystemDictionary::reflect_Constructor_klass()) { |
|
163 oop clazz = java_lang_reflect_Constructor::clazz(target_oop); |
|
164 int slot = java_lang_reflect_Constructor::slot(target_oop); |
|
165 KlassHandle k(thread, java_lang_Class::as_Klass(clazz)); |
|
166 if (!k.is_null() && k->oop_is_instance()) { |
|
167 Method* m = InstanceKlass::cast(k())->method_with_idnum(slot); |
|
168 if (m == NULL) return NULL; |
|
169 CallInfo info(m, k()); |
|
170 return init_method_MemberName(mname, info); |
|
171 } |
|
172 } |
|
173 return NULL; |
|
174 } |
|
175 |
|
176 oop MethodHandles::init_method_MemberName(Handle mname, CallInfo& info) { |
|
177 assert(info.resolved_appendix().is_null(), "only normal methods here"); |
|
178 methodHandle m = info.resolved_method(); |
|
179 KlassHandle m_klass = m->method_holder(); |
|
180 int flags = (jushort)( m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS ); |
|
181 int vmindex = Method::invalid_vtable_index; |
|
182 |
|
183 switch (info.call_kind()) { |
|
184 case CallInfo::itable_call: |
|
185 vmindex = info.itable_index(); |
|
186 // More importantly, the itable index only works with the method holder. |
|
187 assert(m_klass->verify_itable_index(vmindex), ""); |
|
188 flags |= IS_METHOD | (JVM_REF_invokeInterface << REFERENCE_KIND_SHIFT); |
|
189 if (TraceInvokeDynamic) { |
|
190 ResourceMark rm; |
|
191 tty->print_cr("memberName: invokeinterface method_holder::method: %s, itableindex: %d, access_flags:", |
|
192 Method::name_and_sig_as_C_string(m->method_holder(), m->name(), m->signature()), |
|
193 vmindex); |
|
194 m->access_flags().print_on(tty); |
|
195 if (!m->is_abstract()) { |
|
196 tty->print("default"); |
|
197 } |
|
198 tty->cr(); |
|
199 } |
|
200 break; |
|
201 |
|
202 case CallInfo::vtable_call: |
|
203 vmindex = info.vtable_index(); |
|
204 flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT); |
|
205 assert(info.resolved_klass()->is_subtype_of(m_klass()), "virtual call must be type-safe"); |
|
206 if (m_klass->is_interface()) { |
|
207 // This is a vtable call to an interface method (abstract "miranda method" or default method). |
|
208 // The vtable index is meaningless without a class (not interface) receiver type, so get one. |
|
209 // (LinkResolver should help us figure this out.) |
|
210 KlassHandle m_klass_non_interface = info.resolved_klass(); |
|
211 if (m_klass_non_interface->is_interface()) { |
|
212 m_klass_non_interface = SystemDictionary::Object_klass(); |
|
213 #ifdef ASSERT |
|
214 { ResourceMark rm; |
|
215 Method* m2 = m_klass_non_interface->vtable()->method_at(vmindex); |
|
216 assert(m->name() == m2->name() && m->signature() == m2->signature(), |
|
217 err_msg("at %d, %s != %s", vmindex, |
|
218 m->name_and_sig_as_C_string(), m2->name_and_sig_as_C_string())); |
|
219 } |
|
220 #endif //ASSERT |
|
221 } |
|
222 if (!m->is_public()) { |
|
223 assert(m->is_public(), "virtual call must be to public interface method"); |
|
224 return NULL; // elicit an error later in product build |
|
225 } |
|
226 assert(info.resolved_klass()->is_subtype_of(m_klass_non_interface()), "virtual call must be type-safe"); |
|
227 m_klass = m_klass_non_interface; |
|
228 } |
|
229 if (TraceInvokeDynamic) { |
|
230 ResourceMark rm; |
|
231 tty->print_cr("memberName: invokevirtual method_holder::method: %s, receiver: %s, vtableindex: %d, access_flags:", |
|
232 Method::name_and_sig_as_C_string(m->method_holder(), m->name(), m->signature()), |
|
233 m_klass->internal_name(), vmindex); |
|
234 m->access_flags().print_on(tty); |
|
235 if (m->is_default_method()) { |
|
236 tty->print("default"); |
|
237 } |
|
238 tty->cr(); |
|
239 } |
|
240 break; |
|
241 |
|
242 case CallInfo::direct_call: |
|
243 vmindex = Method::nonvirtual_vtable_index; |
|
244 if (m->is_static()) { |
|
245 flags |= IS_METHOD | (JVM_REF_invokeStatic << REFERENCE_KIND_SHIFT); |
|
246 } else if (m->is_initializer()) { |
|
247 flags |= IS_CONSTRUCTOR | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT); |
|
248 } else { |
|
249 flags |= IS_METHOD | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT); |
|
250 } |
|
251 break; |
|
252 |
|
253 default: assert(false, "bad CallInfo"); return NULL; |
|
254 } |
|
255 |
|
256 // @CallerSensitive annotation detected |
|
257 if (m->caller_sensitive()) { |
|
258 flags |= CALLER_SENSITIVE; |
|
259 } |
|
260 |
|
261 oop mname_oop = mname(); |
|
262 java_lang_invoke_MemberName::set_flags( mname_oop, flags); |
|
263 java_lang_invoke_MemberName::set_vmtarget(mname_oop, m()); |
|
264 java_lang_invoke_MemberName::set_vmindex( mname_oop, vmindex); // vtable/itable index |
|
265 java_lang_invoke_MemberName::set_clazz( mname_oop, m_klass->java_mirror()); |
|
266 // Note: name and type can be lazily computed by resolve_MemberName, |
|
267 // if Java code needs them as resolved String and MethodType objects. |
|
268 // The clazz must be eagerly stored, because it provides a GC |
|
269 // root to help keep alive the Method*. |
|
270 // If relevant, the vtable or itable value is stored as vmindex. |
|
271 // This is done eagerly, since it is readily available without |
|
272 // constructing any new objects. |
|
273 // TO DO: maybe intern mname_oop |
|
274 m->method_holder()->add_member_name(m->method_idnum(), mname); |
|
275 |
|
276 return mname(); |
|
277 } |
|
278 |
|
279 oop MethodHandles::init_field_MemberName(Handle mname, fieldDescriptor& fd, bool is_setter) { |
|
280 int flags = (jushort)( fd.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS ); |
|
281 flags |= IS_FIELD | ((fd.is_static() ? JVM_REF_getStatic : JVM_REF_getField) << REFERENCE_KIND_SHIFT); |
|
282 if (is_setter) flags += ((JVM_REF_putField - JVM_REF_getField) << REFERENCE_KIND_SHIFT); |
|
283 Metadata* vmtarget = fd.field_holder(); |
|
284 int vmindex = fd.offset(); // determines the field uniquely when combined with static bit |
|
285 oop mname_oop = mname(); |
|
286 java_lang_invoke_MemberName::set_flags(mname_oop, flags); |
|
287 java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget); |
|
288 java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex); |
|
289 java_lang_invoke_MemberName::set_clazz(mname_oop, fd.field_holder()->java_mirror()); |
|
290 oop type = field_signature_type_or_null(fd.signature()); |
|
291 oop name = field_name_or_null(fd.name()); |
|
292 if (name != NULL) |
|
293 java_lang_invoke_MemberName::set_name(mname_oop, name); |
|
294 if (type != NULL) |
|
295 java_lang_invoke_MemberName::set_type(mname_oop, type); |
|
296 // Note: name and type can be lazily computed by resolve_MemberName, |
|
297 // if Java code needs them as resolved String and Class objects. |
|
298 // Note that the incoming type oop might be pre-resolved (non-null). |
|
299 // The base clazz and field offset (vmindex) must be eagerly stored, |
|
300 // because they unambiguously identify the field. |
|
301 // Although the fieldDescriptor::_index would also identify the field, |
|
302 // we do not use it, because it is harder to decode. |
|
303 // TO DO: maybe intern mname_oop |
|
304 return mname(); |
|
305 } |
|
306 |
|
307 // JVM 2.9 Special Methods: |
|
308 // A method is signature polymorphic if and only if all of the following conditions hold : |
|
309 // * It is declared in the java.lang.invoke.MethodHandle class. |
|
310 // * It has a single formal parameter of type Object[]. |
|
311 // * It has a return type of Object. |
|
312 // * It has the ACC_VARARGS and ACC_NATIVE flags set. |
|
313 bool MethodHandles::is_method_handle_invoke_name(Klass* klass, Symbol* name) { |
|
314 if (klass == NULL) |
|
315 return false; |
|
316 // The following test will fail spuriously during bootstrap of MethodHandle itself: |
|
317 // if (klass != SystemDictionary::MethodHandle_klass()) |
|
318 // Test the name instead: |
|
319 if (klass->name() != vmSymbols::java_lang_invoke_MethodHandle()) |
|
320 return false; |
|
321 Symbol* poly_sig = vmSymbols::object_array_object_signature(); |
|
322 Method* m = InstanceKlass::cast(klass)->find_method(name, poly_sig); |
|
323 if (m == NULL) return false; |
|
324 int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS; |
|
325 int flags = m->access_flags().as_int(); |
|
326 return (flags & required) == required; |
|
327 } |
|
328 |
|
329 |
|
330 Symbol* MethodHandles::signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid) { |
|
331 assert(is_signature_polymorphic_intrinsic(iid), err_msg("iid=%d", iid)); |
|
332 switch (iid) { |
|
333 case vmIntrinsics::_invokeBasic: return vmSymbols::invokeBasic_name(); |
|
334 case vmIntrinsics::_linkToVirtual: return vmSymbols::linkToVirtual_name(); |
|
335 case vmIntrinsics::_linkToStatic: return vmSymbols::linkToStatic_name(); |
|
336 case vmIntrinsics::_linkToSpecial: return vmSymbols::linkToSpecial_name(); |
|
337 case vmIntrinsics::_linkToInterface: return vmSymbols::linkToInterface_name(); |
|
338 } |
|
339 assert(false, ""); |
|
340 return 0; |
|
341 } |
|
342 |
|
343 int MethodHandles::signature_polymorphic_intrinsic_ref_kind(vmIntrinsics::ID iid) { |
|
344 switch (iid) { |
|
345 case vmIntrinsics::_invokeBasic: return 0; |
|
346 case vmIntrinsics::_linkToVirtual: return JVM_REF_invokeVirtual; |
|
347 case vmIntrinsics::_linkToStatic: return JVM_REF_invokeStatic; |
|
348 case vmIntrinsics::_linkToSpecial: return JVM_REF_invokeSpecial; |
|
349 case vmIntrinsics::_linkToInterface: return JVM_REF_invokeInterface; |
|
350 } |
|
351 assert(false, err_msg("iid=%d", iid)); |
|
352 return 0; |
|
353 } |
|
354 |
|
355 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Symbol* name) { |
|
356 vmSymbols::SID name_id = vmSymbols::find_sid(name); |
|
357 switch (name_id) { |
|
358 // The ID _invokeGeneric stands for all non-static signature-polymorphic methods, except built-ins. |
|
359 case vmSymbols::VM_SYMBOL_ENUM_NAME(invoke_name): return vmIntrinsics::_invokeGeneric; |
|
360 // The only built-in non-static signature-polymorphic method is MethodHandle.invokeBasic: |
|
361 case vmSymbols::VM_SYMBOL_ENUM_NAME(invokeBasic_name): return vmIntrinsics::_invokeBasic; |
|
362 |
|
363 // There is one static signature-polymorphic method for each JVM invocation mode. |
|
364 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToVirtual_name): return vmIntrinsics::_linkToVirtual; |
|
365 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToStatic_name): return vmIntrinsics::_linkToStatic; |
|
366 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToSpecial_name): return vmIntrinsics::_linkToSpecial; |
|
367 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToInterface_name): return vmIntrinsics::_linkToInterface; |
|
368 } |
|
369 |
|
370 // Cover the case of invokeExact and any future variants of invokeFoo. |
|
371 Klass* mh_klass = SystemDictionary::well_known_klass( |
|
372 SystemDictionary::WK_KLASS_ENUM_NAME(MethodHandle_klass) ); |
|
373 if (mh_klass != NULL && is_method_handle_invoke_name(mh_klass, name)) |
|
374 return vmIntrinsics::_invokeGeneric; |
|
375 |
|
376 // Note: The pseudo-intrinsic _compiledLambdaForm is never linked against. |
|
377 // Instead it is used to mark lambda forms bound to invokehandle or invokedynamic. |
|
378 return vmIntrinsics::_none; |
|
379 } |
|
380 |
|
381 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Klass* klass, Symbol* name) { |
|
382 if (klass != NULL && |
|
383 klass->name() == vmSymbols::java_lang_invoke_MethodHandle()) { |
|
384 vmIntrinsics::ID iid = signature_polymorphic_name_id(name); |
|
385 if (iid != vmIntrinsics::_none) |
|
386 return iid; |
|
387 if (is_method_handle_invoke_name(klass, name)) |
|
388 return vmIntrinsics::_invokeGeneric; |
|
389 } |
|
390 return vmIntrinsics::_none; |
|
391 } |
|
392 |
|
393 |
|
394 // convert the external string or reflective type to an internal signature |
|
395 Symbol* MethodHandles::lookup_signature(oop type_str, bool intern_if_not_found, TRAPS) { |
|
396 if (java_lang_invoke_MethodType::is_instance(type_str)) { |
|
397 return java_lang_invoke_MethodType::as_signature(type_str, intern_if_not_found, CHECK_NULL); |
|
398 } else if (java_lang_Class::is_instance(type_str)) { |
|
399 return java_lang_Class::as_signature(type_str, false, CHECK_NULL); |
|
400 } else if (java_lang_String::is_instance(type_str)) { |
|
401 if (intern_if_not_found) { |
|
402 return java_lang_String::as_symbol(type_str, CHECK_NULL); |
|
403 } else { |
|
404 return java_lang_String::as_symbol_or_null(type_str); |
|
405 } |
|
406 } else { |
|
407 THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized type", NULL); |
|
408 } |
|
409 } |
|
410 |
|
411 static const char OBJ_SIG[] = "Ljava/lang/Object;"; |
|
412 enum { OBJ_SIG_LEN = 18 }; |
|
413 |
|
414 bool MethodHandles::is_basic_type_signature(Symbol* sig) { |
|
415 assert(vmSymbols::object_signature()->utf8_length() == (int)OBJ_SIG_LEN, ""); |
|
416 assert(vmSymbols::object_signature()->equals(OBJ_SIG), ""); |
|
417 const int len = sig->utf8_length(); |
|
418 for (int i = 0; i < len; i++) { |
|
419 switch (sig->byte_at(i)) { |
|
420 case 'L': |
|
421 // only java/lang/Object is valid here |
|
422 if (sig->index_of_at(i, OBJ_SIG, OBJ_SIG_LEN) != i) |
|
423 return false; |
|
424 i += OBJ_SIG_LEN-1; //-1 because of i++ in loop |
|
425 continue; |
|
426 case '(': case ')': case 'V': |
|
427 case 'I': case 'J': case 'F': case 'D': |
|
428 continue; |
|
429 //case '[': |
|
430 //case 'Z': case 'B': case 'C': case 'S': |
|
431 default: |
|
432 return false; |
|
433 } |
|
434 } |
|
435 return true; |
|
436 } |
|
437 |
|
438 Symbol* MethodHandles::lookup_basic_type_signature(Symbol* sig, bool keep_last_arg, TRAPS) { |
|
439 Symbol* bsig = NULL; |
|
440 if (sig == NULL) { |
|
441 return sig; |
|
442 } else if (is_basic_type_signature(sig)) { |
|
443 sig->increment_refcount(); |
|
444 return sig; // that was easy |
|
445 } else if (sig->byte_at(0) != '(') { |
|
446 BasicType bt = char2type(sig->byte_at(0)); |
|
447 if (is_subword_type(bt)) { |
|
448 bsig = vmSymbols::int_signature(); |
|
449 } else { |
|
450 assert(bt == T_OBJECT || bt == T_ARRAY, "is_basic_type_signature was false"); |
|
451 bsig = vmSymbols::object_signature(); |
|
452 } |
|
453 } else { |
|
454 ResourceMark rm; |
|
455 stringStream buffer(128); |
|
456 buffer.put('('); |
|
457 int arg_pos = 0, keep_arg_pos = -1; |
|
458 if (keep_last_arg) |
|
459 keep_arg_pos = ArgumentCount(sig).size() - 1; |
|
460 for (SignatureStream ss(sig); !ss.is_done(); ss.next()) { |
|
461 BasicType bt = ss.type(); |
|
462 size_t this_arg_pos = buffer.size(); |
|
463 if (ss.at_return_type()) { |
|
464 buffer.put(')'); |
|
465 } |
|
466 if (arg_pos == keep_arg_pos) { |
|
467 buffer.write((char*) ss.raw_bytes(), |
|
468 (int) ss.raw_length()); |
|
469 } else if (bt == T_OBJECT || bt == T_ARRAY) { |
|
470 buffer.write(OBJ_SIG, OBJ_SIG_LEN); |
|
471 } else { |
|
472 if (is_subword_type(bt)) |
|
473 bt = T_INT; |
|
474 buffer.put(type2char(bt)); |
|
475 } |
|
476 arg_pos++; |
|
477 } |
|
478 const char* sigstr = buffer.base(); |
|
479 int siglen = (int) buffer.size(); |
|
480 bsig = SymbolTable::new_symbol(sigstr, siglen, THREAD); |
|
481 } |
|
482 assert(is_basic_type_signature(bsig) || |
|
483 // detune assert in case the injected argument is not a basic type: |
|
484 keep_last_arg, ""); |
|
485 return bsig; |
|
486 } |
|
487 |
|
488 void MethodHandles::print_as_basic_type_signature_on(outputStream* st, |
|
489 Symbol* sig, |
|
490 bool keep_arrays, |
|
491 bool keep_basic_names) { |
|
492 st = st ? st : tty; |
|
493 int len = sig->utf8_length(); |
|
494 int array = 0; |
|
495 bool prev_type = false; |
|
496 for (int i = 0; i < len; i++) { |
|
497 char ch = sig->byte_at(i); |
|
498 switch (ch) { |
|
499 case '(': case ')': |
|
500 prev_type = false; |
|
501 st->put(ch); |
|
502 continue; |
|
503 case '[': |
|
504 if (!keep_basic_names && keep_arrays) |
|
505 st->put(ch); |
|
506 array++; |
|
507 continue; |
|
508 case 'L': |
|
509 { |
|
510 if (prev_type) st->put(','); |
|
511 int start = i+1, slash = start; |
|
512 while (++i < len && (ch = sig->byte_at(i)) != ';') { |
|
513 if (ch == '/' || ch == '.' || ch == '$') slash = i+1; |
|
514 } |
|
515 if (slash < i) start = slash; |
|
516 if (!keep_basic_names) { |
|
517 st->put('L'); |
|
518 } else { |
|
519 for (int j = start; j < i; j++) |
|
520 st->put(sig->byte_at(j)); |
|
521 prev_type = true; |
|
522 } |
|
523 break; |
|
524 } |
|
525 default: |
|
526 { |
|
527 if (array && char2type(ch) != T_ILLEGAL && !keep_arrays) { |
|
528 ch = '['; |
|
529 array = 0; |
|
530 } |
|
531 if (prev_type) st->put(','); |
|
532 const char* n = NULL; |
|
533 if (keep_basic_names) |
|
534 n = type2name(char2type(ch)); |
|
535 if (n == NULL) { |
|
536 // unknown letter, or we don't want to know its name |
|
537 st->put(ch); |
|
538 } else { |
|
539 st->print("%s", n); |
|
540 prev_type = true; |
|
541 } |
|
542 break; |
|
543 } |
|
544 } |
|
545 // Switch break goes here to take care of array suffix: |
|
546 if (prev_type) { |
|
547 while (array > 0) { |
|
548 st->print("[]"); |
|
549 --array; |
|
550 } |
|
551 } |
|
552 array = 0; |
|
553 } |
|
554 } |
|
555 |
|
556 |
|
557 |
|
558 static oop object_java_mirror() { |
|
559 return SystemDictionary::Object_klass()->java_mirror(); |
|
560 } |
|
561 |
|
562 oop MethodHandles::field_name_or_null(Symbol* s) { |
|
563 if (s == NULL) return NULL; |
|
564 return StringTable::lookup(s); |
|
565 } |
|
566 |
|
567 oop MethodHandles::field_signature_type_or_null(Symbol* s) { |
|
568 if (s == NULL) return NULL; |
|
569 BasicType bt = FieldType::basic_type(s); |
|
570 if (is_java_primitive(bt)) { |
|
571 assert(s->utf8_length() == 1, ""); |
|
572 return java_lang_Class::primitive_mirror(bt); |
|
573 } |
|
574 // Here are some more short cuts for common types. |
|
575 // They are optional, since reference types can be resolved lazily. |
|
576 if (bt == T_OBJECT) { |
|
577 if (s == vmSymbols::object_signature()) { |
|
578 return object_java_mirror(); |
|
579 } else if (s == vmSymbols::class_signature()) { |
|
580 return SystemDictionary::Class_klass()->java_mirror(); |
|
581 } else if (s == vmSymbols::string_signature()) { |
|
582 return SystemDictionary::String_klass()->java_mirror(); |
|
583 } |
|
584 } |
|
585 return NULL; |
|
586 } |
|
587 |
|
588 |
|
589 // An unresolved member name is a mere symbolic reference. |
|
590 // Resolving it plants a vmtarget/vmindex in it, |
|
591 // which refers directly to JVM internals. |
|
592 Handle MethodHandles::resolve_MemberName(Handle mname, KlassHandle caller, TRAPS) { |
|
593 Handle empty; |
|
594 assert(java_lang_invoke_MemberName::is_instance(mname()), ""); |
|
595 |
|
596 if (java_lang_invoke_MemberName::vmtarget(mname()) != NULL) { |
|
597 // Already resolved. |
|
598 DEBUG_ONLY(int vmindex = java_lang_invoke_MemberName::vmindex(mname())); |
|
599 assert(vmindex >= Method::nonvirtual_vtable_index, ""); |
|
600 return mname; |
|
601 } |
|
602 |
|
603 Handle defc_oop(THREAD, java_lang_invoke_MemberName::clazz(mname())); |
|
604 Handle name_str(THREAD, java_lang_invoke_MemberName::name( mname())); |
|
605 Handle type_str(THREAD, java_lang_invoke_MemberName::type( mname())); |
|
606 int flags = java_lang_invoke_MemberName::flags(mname()); |
|
607 int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; |
|
608 if (!ref_kind_is_valid(ref_kind)) { |
|
609 THROW_MSG_(vmSymbols::java_lang_InternalError(), "obsolete MemberName format", empty); |
|
610 } |
|
611 |
|
612 DEBUG_ONLY(int old_vmindex); |
|
613 assert((old_vmindex = java_lang_invoke_MemberName::vmindex(mname())) == 0, "clean input"); |
|
614 |
|
615 if (defc_oop.is_null() || name_str.is_null() || type_str.is_null()) { |
|
616 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "nothing to resolve", empty); |
|
617 } |
|
618 |
|
619 instanceKlassHandle defc; |
|
620 { |
|
621 Klass* defc_klass = java_lang_Class::as_Klass(defc_oop()); |
|
622 if (defc_klass == NULL) return empty; // a primitive; no resolution possible |
|
623 if (!defc_klass->oop_is_instance()) { |
|
624 if (!defc_klass->oop_is_array()) return empty; |
|
625 defc_klass = SystemDictionary::Object_klass(); |
|
626 } |
|
627 defc = instanceKlassHandle(THREAD, defc_klass); |
|
628 } |
|
629 if (defc.is_null()) { |
|
630 THROW_MSG_(vmSymbols::java_lang_InternalError(), "primitive class", empty); |
|
631 } |
|
632 defc->link_class(CHECK_(empty)); // possible safepoint |
|
633 |
|
634 // convert the external string name to an internal symbol |
|
635 TempNewSymbol name = java_lang_String::as_symbol_or_null(name_str()); |
|
636 if (name == NULL) return empty; // no such name |
|
637 if (name == vmSymbols::class_initializer_name()) |
|
638 return empty; // illegal name |
|
639 |
|
640 vmIntrinsics::ID mh_invoke_id = vmIntrinsics::_none; |
|
641 if ((flags & ALL_KINDS) == IS_METHOD && |
|
642 (defc() == SystemDictionary::MethodHandle_klass()) && |
|
643 (ref_kind == JVM_REF_invokeVirtual || |
|
644 ref_kind == JVM_REF_invokeSpecial || |
|
645 // static invocation mode is required for _linkToVirtual, etc.: |
|
646 ref_kind == JVM_REF_invokeStatic)) { |
|
647 vmIntrinsics::ID iid = signature_polymorphic_name_id(name); |
|
648 if (iid != vmIntrinsics::_none && |
|
649 ((ref_kind == JVM_REF_invokeStatic) == is_signature_polymorphic_static(iid))) { |
|
650 // Virtual methods invoke and invokeExact, plus internal invokers like _invokeBasic. |
|
651 // For a static reference it could an internal linkage routine like _linkToVirtual, etc. |
|
652 mh_invoke_id = iid; |
|
653 } |
|
654 } |
|
655 |
|
656 // convert the external string or reflective type to an internal signature |
|
657 TempNewSymbol type = lookup_signature(type_str(), (mh_invoke_id != vmIntrinsics::_none), CHECK_(empty)); |
|
658 if (type == NULL) return empty; // no such signature exists in the VM |
|
659 |
|
660 // Time to do the lookup. |
|
661 switch (flags & ALL_KINDS) { |
|
662 case IS_METHOD: |
|
663 { |
|
664 CallInfo result; |
|
665 { |
|
666 assert(!HAS_PENDING_EXCEPTION, ""); |
|
667 if (ref_kind == JVM_REF_invokeStatic) { |
|
668 LinkResolver::resolve_static_call(result, |
|
669 defc, name, type, caller, caller.not_null(), false, THREAD); |
|
670 } else if (ref_kind == JVM_REF_invokeInterface) { |
|
671 LinkResolver::resolve_interface_call(result, Handle(), defc, |
|
672 defc, name, type, caller, caller.not_null(), false, THREAD); |
|
673 } else if (mh_invoke_id != vmIntrinsics::_none) { |
|
674 assert(!is_signature_polymorphic_static(mh_invoke_id), ""); |
|
675 LinkResolver::resolve_handle_call(result, |
|
676 defc, name, type, caller, THREAD); |
|
677 } else if (ref_kind == JVM_REF_invokeSpecial) { |
|
678 LinkResolver::resolve_special_call(result, |
|
679 defc, name, type, caller, caller.not_null(), THREAD); |
|
680 } else if (ref_kind == JVM_REF_invokeVirtual) { |
|
681 LinkResolver::resolve_virtual_call(result, Handle(), defc, |
|
682 defc, name, type, caller, caller.not_null(), false, THREAD); |
|
683 } else { |
|
684 assert(false, err_msg("ref_kind=%d", ref_kind)); |
|
685 } |
|
686 if (HAS_PENDING_EXCEPTION) { |
|
687 return empty; |
|
688 } |
|
689 } |
|
690 if (result.resolved_appendix().not_null()) { |
|
691 // The resolved MemberName must not be accompanied by an appendix argument, |
|
692 // since there is no way to bind this value into the MemberName. |
|
693 // Caller is responsible to prevent this from happening. |
|
694 THROW_MSG_(vmSymbols::java_lang_InternalError(), "appendix", empty); |
|
695 } |
|
696 oop mname2 = init_method_MemberName(mname, result); |
|
697 return Handle(THREAD, mname2); |
|
698 } |
|
699 case IS_CONSTRUCTOR: |
|
700 { |
|
701 CallInfo result; |
|
702 { |
|
703 assert(!HAS_PENDING_EXCEPTION, ""); |
|
704 if (name == vmSymbols::object_initializer_name()) { |
|
705 LinkResolver::resolve_special_call(result, |
|
706 defc, name, type, caller, caller.not_null(), THREAD); |
|
707 } else { |
|
708 break; // will throw after end of switch |
|
709 } |
|
710 if (HAS_PENDING_EXCEPTION) { |
|
711 return empty; |
|
712 } |
|
713 } |
|
714 assert(result.is_statically_bound(), ""); |
|
715 oop mname2 = init_method_MemberName(mname, result); |
|
716 return Handle(THREAD, mname2); |
|
717 } |
|
718 case IS_FIELD: |
|
719 { |
|
720 fieldDescriptor result; // find_field initializes fd if found |
|
721 { |
|
722 assert(!HAS_PENDING_EXCEPTION, ""); |
|
723 LinkResolver::resolve_field(result, defc, name, type, caller, Bytecodes::_nop, false, false, THREAD); |
|
724 if (HAS_PENDING_EXCEPTION) { |
|
725 return empty; |
|
726 } |
|
727 } |
|
728 oop mname2 = init_field_MemberName(mname, result, ref_kind_is_setter(ref_kind)); |
|
729 return Handle(THREAD, mname2); |
|
730 } |
|
731 default: |
|
732 THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format", empty); |
|
733 } |
|
734 |
|
735 return empty; |
|
736 } |
|
737 |
|
738 // Conversely, a member name which is only initialized from JVM internals |
|
739 // may have null defc, name, and type fields. |
|
740 // Resolving it plants a vmtarget/vmindex in it, |
|
741 // which refers directly to JVM internals. |
|
742 void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) { |
|
743 assert(java_lang_invoke_MemberName::is_instance(mname()), ""); |
|
744 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); |
|
745 int vmindex = java_lang_invoke_MemberName::vmindex(mname()); |
|
746 if (vmtarget == NULL) { |
|
747 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand"); |
|
748 } |
|
749 |
|
750 bool have_defc = (java_lang_invoke_MemberName::clazz(mname()) != NULL); |
|
751 bool have_name = (java_lang_invoke_MemberName::name(mname()) != NULL); |
|
752 bool have_type = (java_lang_invoke_MemberName::type(mname()) != NULL); |
|
753 int flags = java_lang_invoke_MemberName::flags(mname()); |
|
754 |
|
755 if (suppress != 0) { |
|
756 if (suppress & _suppress_defc) have_defc = true; |
|
757 if (suppress & _suppress_name) have_name = true; |
|
758 if (suppress & _suppress_type) have_type = true; |
|
759 } |
|
760 |
|
761 if (have_defc && have_name && have_type) return; // nothing needed |
|
762 |
|
763 switch (flags & ALL_KINDS) { |
|
764 case IS_METHOD: |
|
765 case IS_CONSTRUCTOR: |
|
766 { |
|
767 assert(vmtarget->is_method(), "method or constructor vmtarget is Method*"); |
|
768 methodHandle m(THREAD, (Method*)vmtarget); |
|
769 DEBUG_ONLY(vmtarget = NULL); // safety |
|
770 if (m.is_null()) break; |
|
771 if (!have_defc) { |
|
772 InstanceKlass* defc = m->method_holder(); |
|
773 java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror()); |
|
774 } |
|
775 if (!have_name) { |
|
776 //not java_lang_String::create_from_symbol; let's intern member names |
|
777 Handle name = StringTable::intern(m->name(), CHECK); |
|
778 java_lang_invoke_MemberName::set_name(mname(), name()); |
|
779 } |
|
780 if (!have_type) { |
|
781 Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK); |
|
782 java_lang_invoke_MemberName::set_type(mname(), type()); |
|
783 } |
|
784 return; |
|
785 } |
|
786 case IS_FIELD: |
|
787 { |
|
788 assert(vmtarget->is_klass(), "field vmtarget is Klass*"); |
|
789 if (!((Klass*) vmtarget)->oop_is_instance()) break; |
|
790 instanceKlassHandle defc(THREAD, (Klass*) vmtarget); |
|
791 DEBUG_ONLY(vmtarget = NULL); // safety |
|
792 bool is_static = ((flags & JVM_ACC_STATIC) != 0); |
|
793 fieldDescriptor fd; // find_field initializes fd if found |
|
794 if (!defc->find_field_from_offset(vmindex, is_static, &fd)) |
|
795 break; // cannot expand |
|
796 if (!have_defc) { |
|
797 java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror()); |
|
798 } |
|
799 if (!have_name) { |
|
800 //not java_lang_String::create_from_symbol; let's intern member names |
|
801 Handle name = StringTable::intern(fd.name(), CHECK); |
|
802 java_lang_invoke_MemberName::set_name(mname(), name()); |
|
803 } |
|
804 if (!have_type) { |
|
805 // If it is a primitive field type, don't mess with short strings like "I". |
|
806 Handle type = field_signature_type_or_null(fd.signature()); |
|
807 if (type.is_null()) { |
|
808 java_lang_String::create_from_symbol(fd.signature(), CHECK); |
|
809 } |
|
810 java_lang_invoke_MemberName::set_type(mname(), type()); |
|
811 } |
|
812 return; |
|
813 } |
|
814 } |
|
815 THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format"); |
|
816 } |
|
817 |
|
818 int MethodHandles::find_MemberNames(KlassHandle k, |
|
819 Symbol* name, Symbol* sig, |
|
820 int mflags, KlassHandle caller, |
|
821 int skip, objArrayHandle results) { |
|
822 // %%% take caller into account! |
|
823 |
|
824 Thread* thread = Thread::current(); |
|
825 |
|
826 if (k.is_null() || !k->oop_is_instance()) return -1; |
|
827 |
|
828 int rfill = 0, rlimit = results->length(), rskip = skip; |
|
829 // overflow measurement: |
|
830 int overflow = 0, overflow_limit = MAX2(1000, rlimit); |
|
831 |
|
832 int match_flags = mflags; |
|
833 bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0); |
|
834 bool search_intfc = ((match_flags & SEARCH_INTERFACES) != 0); |
|
835 bool local_only = !(search_superc | search_intfc); |
|
836 bool classes_only = false; |
|
837 |
|
838 if (name != NULL) { |
|
839 if (name->utf8_length() == 0) return 0; // a match is not possible |
|
840 } |
|
841 if (sig != NULL) { |
|
842 if (sig->utf8_length() == 0) return 0; // a match is not possible |
|
843 if (sig->byte_at(0) == '(') |
|
844 match_flags &= ~(IS_FIELD | IS_TYPE); |
|
845 else |
|
846 match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD); |
|
847 } |
|
848 |
|
849 if ((match_flags & IS_TYPE) != 0) { |
|
850 // NYI, and Core Reflection works quite well for this query |
|
851 } |
|
852 |
|
853 if ((match_flags & IS_FIELD) != 0) { |
|
854 for (FieldStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) { |
|
855 if (name != NULL && st.name() != name) |
|
856 continue; |
|
857 if (sig != NULL && st.signature() != sig) |
|
858 continue; |
|
859 // passed the filters |
|
860 if (rskip > 0) { |
|
861 --rskip; |
|
862 } else if (rfill < rlimit) { |
|
863 Handle result(thread, results->obj_at(rfill++)); |
|
864 if (!java_lang_invoke_MemberName::is_instance(result())) |
|
865 return -99; // caller bug! |
|
866 oop saved = MethodHandles::init_field_MemberName(result, st.field_descriptor()); |
|
867 if (saved != result()) |
|
868 results->obj_at_put(rfill-1, saved); // show saved instance to user |
|
869 } else if (++overflow >= overflow_limit) { |
|
870 match_flags = 0; break; // got tired of looking at overflow |
|
871 } |
|
872 } |
|
873 } |
|
874 |
|
875 if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) { |
|
876 // watch out for these guys: |
|
877 Symbol* init_name = vmSymbols::object_initializer_name(); |
|
878 Symbol* clinit_name = vmSymbols::class_initializer_name(); |
|
879 if (name == clinit_name) clinit_name = NULL; // hack for exposing <clinit> |
|
880 bool negate_name_test = false; |
|
881 // fix name so that it captures the intention of IS_CONSTRUCTOR |
|
882 if (!(match_flags & IS_METHOD)) { |
|
883 // constructors only |
|
884 if (name == NULL) { |
|
885 name = init_name; |
|
886 } else if (name != init_name) { |
|
887 return 0; // no constructors of this method name |
|
888 } |
|
889 } else if (!(match_flags & IS_CONSTRUCTOR)) { |
|
890 // methods only |
|
891 if (name == NULL) { |
|
892 name = init_name; |
|
893 negate_name_test = true; // if we see the name, we *omit* the entry |
|
894 } else if (name == init_name) { |
|
895 return 0; // no methods of this constructor name |
|
896 } |
|
897 } else { |
|
898 // caller will accept either sort; no need to adjust name |
|
899 } |
|
900 for (MethodStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) { |
|
901 Method* m = st.method(); |
|
902 Symbol* m_name = m->name(); |
|
903 if (m_name == clinit_name) |
|
904 continue; |
|
905 if (name != NULL && ((m_name != name) ^ negate_name_test)) |
|
906 continue; |
|
907 if (sig != NULL && m->signature() != sig) |
|
908 continue; |
|
909 // passed the filters |
|
910 if (rskip > 0) { |
|
911 --rskip; |
|
912 } else if (rfill < rlimit) { |
|
913 Handle result(thread, results->obj_at(rfill++)); |
|
914 if (!java_lang_invoke_MemberName::is_instance(result())) |
|
915 return -99; // caller bug! |
|
916 CallInfo info(m); |
|
917 oop saved = MethodHandles::init_method_MemberName(result, info); |
|
918 if (saved != result()) |
|
919 results->obj_at_put(rfill-1, saved); // show saved instance to user |
|
920 } else if (++overflow >= overflow_limit) { |
|
921 match_flags = 0; break; // got tired of looking at overflow |
|
922 } |
|
923 } |
|
924 } |
|
925 |
|
926 // return number of elements we at leasted wanted to initialize |
|
927 return rfill + overflow; |
|
928 } |
|
929 |
|
930 //------------------------------------------------------------------------------ |
|
931 // MemberNameTable |
|
932 // |
|
933 |
|
934 MemberNameTable::MemberNameTable(int methods_cnt) |
|
935 : GrowableArray<jweak>(methods_cnt, true) { |
|
936 assert_locked_or_safepoint(MemberNameTable_lock); |
|
937 } |
|
938 |
|
939 MemberNameTable::~MemberNameTable() { |
|
940 assert_locked_or_safepoint(MemberNameTable_lock); |
|
941 int len = this->length(); |
|
942 |
|
943 for (int idx = 0; idx < len; idx++) { |
|
944 jweak ref = this->at(idx); |
|
945 JNIHandles::destroy_weak_global(ref); |
|
946 } |
|
947 } |
|
948 |
|
949 void MemberNameTable::add_member_name(int index, jweak mem_name_wref) { |
|
950 assert_locked_or_safepoint(MemberNameTable_lock); |
|
951 this->at_put_grow(index, mem_name_wref); |
|
952 } |
|
953 |
|
954 // Return a member name oop or NULL. |
|
955 oop MemberNameTable::get_member_name(int index) { |
|
956 assert_locked_or_safepoint(MemberNameTable_lock); |
|
957 |
|
958 jweak ref = this->at(index); |
|
959 oop mem_name = JNIHandles::resolve(ref); |
|
960 return mem_name; |
|
961 } |
|
962 |
|
963 #if INCLUDE_JVMTI |
|
964 oop MemberNameTable::find_member_name_by_method(Method* old_method) { |
|
965 assert_locked_or_safepoint(MemberNameTable_lock); |
|
966 oop found = NULL; |
|
967 int len = this->length(); |
|
968 |
|
969 for (int idx = 0; idx < len; idx++) { |
|
970 oop mem_name = JNIHandles::resolve(this->at(idx)); |
|
971 if (mem_name == NULL) { |
|
972 continue; |
|
973 } |
|
974 Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mem_name); |
|
975 if (method == old_method) { |
|
976 found = mem_name; |
|
977 break; |
|
978 } |
|
979 } |
|
980 return found; |
|
981 } |
|
982 |
|
983 // It is called at safepoint only |
|
984 void MemberNameTable::adjust_method_entries(Method** old_methods, Method** new_methods, |
|
985 int methods_length, bool *trace_name_printed) { |
|
986 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint"); |
|
987 // search the MemberNameTable for uses of either obsolete or EMCP methods |
|
988 for (int j = 0; j < methods_length; j++) { |
|
989 Method* old_method = old_methods[j]; |
|
990 Method* new_method = new_methods[j]; |
|
991 oop mem_name = find_member_name_by_method(old_method); |
|
992 if (mem_name != NULL) { |
|
993 java_lang_invoke_MemberName::adjust_vmtarget(mem_name, new_method); |
|
994 |
|
995 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) { |
|
996 if (!(*trace_name_printed)) { |
|
997 // RC_TRACE_MESG macro has an embedded ResourceMark |
|
998 RC_TRACE_MESG(("adjust: name=%s", |
|
999 old_method->method_holder()->external_name())); |
|
1000 *trace_name_printed = true; |
|
1001 } |
|
1002 // RC_TRACE macro has an embedded ResourceMark |
|
1003 RC_TRACE(0x00400000, ("MemberName method update: %s(%s)", |
|
1004 new_method->name()->as_C_string(), |
|
1005 new_method->signature()->as_C_string())); |
|
1006 } |
|
1007 } |
|
1008 } |
|
1009 } |
|
1010 #endif // INCLUDE_JVMTI |
|
1011 |
|
1012 // |
|
1013 // Here are the native methods in java.lang.invoke.MethodHandleNatives |
|
1014 // They are the private interface between this JVM and the HotSpot-specific |
|
1015 // Java code that implements JSR 292 method handles. |
|
1016 // |
|
1017 // Note: We use a JVM_ENTRY macro to define each of these, for this is the way |
|
1018 // that intrinsic (non-JNI) native methods are defined in HotSpot. |
|
1019 // |
|
1020 |
|
1021 JVM_ENTRY(jint, MHN_getConstant(JNIEnv *env, jobject igcls, jint which)) { |
|
1022 switch (which) { |
|
1023 case MethodHandles::GC_COUNT_GWT: |
|
1024 #ifdef COMPILER2 |
|
1025 return true; |
|
1026 #else |
|
1027 return false; |
|
1028 #endif |
|
1029 } |
|
1030 return 0; |
|
1031 } |
|
1032 JVM_END |
|
1033 |
|
1034 #ifndef PRODUCT |
|
1035 #define EACH_NAMED_CON(template, requirement) \ |
|
1036 template(MethodHandles,GC_COUNT_GWT) \ |
|
1037 template(java_lang_invoke_MemberName,MN_IS_METHOD) \ |
|
1038 template(java_lang_invoke_MemberName,MN_IS_CONSTRUCTOR) \ |
|
1039 template(java_lang_invoke_MemberName,MN_IS_FIELD) \ |
|
1040 template(java_lang_invoke_MemberName,MN_IS_TYPE) \ |
|
1041 template(java_lang_invoke_MemberName,MN_CALLER_SENSITIVE) \ |
|
1042 template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \ |
|
1043 template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \ |
|
1044 template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_SHIFT) \ |
|
1045 template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_MASK) \ |
|
1046 template(MethodHandles,GC_LAMBDA_SUPPORT) \ |
|
1047 /*end*/ |
|
1048 |
|
1049 #define IGNORE_REQ(req_expr) /* req_expr */ |
|
1050 #define ONE_PLUS(scope,value) 1+ |
|
1051 static const int con_value_count = EACH_NAMED_CON(ONE_PLUS, IGNORE_REQ) 0; |
|
1052 #define VALUE_COMMA(scope,value) scope::value, |
|
1053 static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA, IGNORE_REQ) 0 }; |
|
1054 #define STRING_NULL(scope,value) #value "\0" |
|
1055 static const char con_names[] = { EACH_NAMED_CON(STRING_NULL, IGNORE_REQ) }; |
|
1056 |
|
1057 static bool advertise_con_value(int which) { |
|
1058 if (which < 0) return false; |
|
1059 bool ok = true; |
|
1060 int count = 0; |
|
1061 #define INC_COUNT(scope,value) \ |
|
1062 ++count; |
|
1063 #define CHECK_REQ(req_expr) \ |
|
1064 if (which < count) return ok; \ |
|
1065 ok = (req_expr); |
|
1066 EACH_NAMED_CON(INC_COUNT, CHECK_REQ); |
|
1067 #undef INC_COUNT |
|
1068 #undef CHECK_REQ |
|
1069 assert(count == con_value_count, ""); |
|
1070 if (which < count) return ok; |
|
1071 return false; |
|
1072 } |
|
1073 |
|
1074 #undef ONE_PLUS |
|
1075 #undef VALUE_COMMA |
|
1076 #undef STRING_NULL |
|
1077 #undef EACH_NAMED_CON |
|
1078 #endif // PRODUCT |
|
1079 |
|
1080 JVM_ENTRY(jint, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) { |
|
1081 #ifndef PRODUCT |
|
1082 if (advertise_con_value(which)) { |
|
1083 assert(which >= 0 && which < con_value_count, ""); |
|
1084 int con = con_values[which]; |
|
1085 objArrayHandle box(THREAD, (objArrayOop) JNIHandles::resolve(box_jh)); |
|
1086 if (box.not_null() && box->klass() == Universe::objectArrayKlassObj() && box->length() > 0) { |
|
1087 const char* str = &con_names[0]; |
|
1088 for (int i = 0; i < which; i++) |
|
1089 str += strlen(str) + 1; // skip name and null |
|
1090 oop name = java_lang_String::create_oop_from_str(str, CHECK_0); // possible safepoint |
|
1091 box->obj_at_put(0, name); |
|
1092 } |
|
1093 return con; |
|
1094 } |
|
1095 #endif |
|
1096 return 0; |
|
1097 } |
|
1098 JVM_END |
|
1099 |
|
1100 // void init(MemberName self, AccessibleObject ref) |
|
1101 JVM_ENTRY(void, MHN_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) { |
|
1102 if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); } |
|
1103 if (target_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); } |
|
1104 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); |
|
1105 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh)); |
|
1106 MethodHandles::init_MemberName(mname, target); |
|
1107 } |
|
1108 JVM_END |
|
1109 |
|
1110 // void expand(MemberName self) |
|
1111 JVM_ENTRY(void, MHN_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) { |
|
1112 if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); } |
|
1113 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); |
|
1114 MethodHandles::expand_MemberName(mname, 0, CHECK); |
|
1115 } |
|
1116 JVM_END |
|
1117 |
|
1118 // void resolve(MemberName self, Class<?> caller) |
|
1119 JVM_ENTRY(jobject, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh)) { |
|
1120 if (mname_jh == NULL) { THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "mname is null"); } |
|
1121 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); |
|
1122 |
|
1123 // The trusted Java code that calls this method should already have performed |
|
1124 // access checks on behalf of the given caller. But, we can verify this. |
|
1125 if (VerifyMethodHandles && caller_jh != NULL && |
|
1126 java_lang_invoke_MemberName::clazz(mname()) != NULL) { |
|
1127 Klass* reference_klass = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname())); |
|
1128 if (reference_klass != NULL && reference_klass->oop_is_objArray()) { |
|
1129 reference_klass = ObjArrayKlass::cast(reference_klass)->bottom_klass(); |
|
1130 } |
|
1131 |
|
1132 // Reflection::verify_class_access can only handle instance classes. |
|
1133 if (reference_klass != NULL && reference_klass->oop_is_instance()) { |
|
1134 // Emulate LinkResolver::check_klass_accessability. |
|
1135 Klass* caller = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh)); |
|
1136 if (!Reflection::verify_class_access(caller, |
|
1137 reference_klass, |
|
1138 true)) { |
|
1139 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), reference_klass->external_name()); |
|
1140 } |
|
1141 } |
|
1142 } |
|
1143 |
|
1144 KlassHandle caller(THREAD, |
|
1145 caller_jh == NULL ? (Klass*) NULL : |
|
1146 java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh))); |
|
1147 Handle resolved = MethodHandles::resolve_MemberName(mname, caller, CHECK_NULL); |
|
1148 |
|
1149 if (resolved.is_null()) { |
|
1150 int flags = java_lang_invoke_MemberName::flags(mname()); |
|
1151 int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK; |
|
1152 if (!MethodHandles::ref_kind_is_valid(ref_kind)) { |
|
1153 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "obsolete MemberName format"); |
|
1154 } |
|
1155 if ((flags & ALL_KINDS) == IS_FIELD) { |
|
1156 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), "field resolution failed"); |
|
1157 } else if ((flags & ALL_KINDS) == IS_METHOD || |
|
1158 (flags & ALL_KINDS) == IS_CONSTRUCTOR) { |
|
1159 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchFieldError(), "method resolution failed"); |
|
1160 } else { |
|
1161 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "resolution failed"); |
|
1162 } |
|
1163 } |
|
1164 |
|
1165 return JNIHandles::make_local(THREAD, resolved()); |
|
1166 } |
|
1167 JVM_END |
|
1168 |
|
1169 static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) { |
|
1170 if (mname == NULL || |
|
1171 java_lang_invoke_MemberName::vmtarget(mname) == NULL) { |
|
1172 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved"); |
|
1173 } else { |
|
1174 int flags = java_lang_invoke_MemberName::flags(mname); |
|
1175 if ((flags & IS_FIELD) != 0 && |
|
1176 (must_be_static |
|
1177 ? (flags & JVM_ACC_STATIC) != 0 |
|
1178 : (flags & JVM_ACC_STATIC) == 0)) { |
|
1179 int vmindex = java_lang_invoke_MemberName::vmindex(mname); |
|
1180 return (jlong) vmindex; |
|
1181 } |
|
1182 } |
|
1183 const char* msg = (must_be_static ? "static field required" : "non-static field required"); |
|
1184 THROW_MSG_0(vmSymbols::java_lang_InternalError(), msg); |
|
1185 return 0; |
|
1186 } |
|
1187 |
|
1188 JVM_ENTRY(jlong, MHN_objectFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) { |
|
1189 return find_member_field_offset(JNIHandles::resolve(mname_jh), false, THREAD); |
|
1190 } |
|
1191 JVM_END |
|
1192 |
|
1193 JVM_ENTRY(jlong, MHN_staticFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) { |
|
1194 return find_member_field_offset(JNIHandles::resolve(mname_jh), true, THREAD); |
|
1195 } |
|
1196 JVM_END |
|
1197 |
|
1198 JVM_ENTRY(jobject, MHN_staticFieldBase(JNIEnv *env, jobject igcls, jobject mname_jh)) { |
|
1199 // use the other function to perform sanity checks: |
|
1200 jlong ignore = find_member_field_offset(JNIHandles::resolve(mname_jh), true, CHECK_NULL); |
|
1201 oop clazz = java_lang_invoke_MemberName::clazz(JNIHandles::resolve_non_null(mname_jh)); |
|
1202 return JNIHandles::make_local(THREAD, clazz); |
|
1203 } |
|
1204 JVM_END |
|
1205 |
|
1206 JVM_ENTRY(jobject, MHN_getMemberVMInfo(JNIEnv *env, jobject igcls, jobject mname_jh)) { |
|
1207 if (mname_jh == NULL) return NULL; |
|
1208 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh)); |
|
1209 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname()); |
|
1210 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(mname()); |
|
1211 objArrayHandle result = oopFactory::new_objArray(SystemDictionary::Object_klass(), 2, CHECK_NULL); |
|
1212 jvalue vmindex_value; vmindex_value.j = (long)vmindex; |
|
1213 oop x = java_lang_boxing_object::create(T_LONG, &vmindex_value, CHECK_NULL); |
|
1214 result->obj_at_put(0, x); |
|
1215 x = NULL; |
|
1216 if (vmtarget == NULL) { |
|
1217 x = NULL; |
|
1218 } else if (vmtarget->is_klass()) { |
|
1219 x = ((Klass*) vmtarget)->java_mirror(); |
|
1220 } else if (vmtarget->is_method()) { |
|
1221 x = mname(); |
|
1222 } |
|
1223 result->obj_at_put(1, x); |
|
1224 return JNIHandles::make_local(env, result()); |
|
1225 } |
|
1226 JVM_END |
|
1227 |
|
1228 |
|
1229 |
|
1230 // static native int getMembers(Class<?> defc, String matchName, String matchSig, |
|
1231 // int matchFlags, Class<?> caller, int skip, MemberName[] results); |
|
1232 JVM_ENTRY(jint, MHN_getMembers(JNIEnv *env, jobject igcls, |
|
1233 jclass clazz_jh, jstring name_jh, jstring sig_jh, |
|
1234 int mflags, jclass caller_jh, jint skip, jobjectArray results_jh)) { |
|
1235 if (clazz_jh == NULL || results_jh == NULL) return -1; |
|
1236 KlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz_jh))); |
|
1237 |
|
1238 objArrayHandle results(THREAD, (objArrayOop) JNIHandles::resolve(results_jh)); |
|
1239 if (results.is_null() || !results->is_objArray()) return -1; |
|
1240 |
|
1241 TempNewSymbol name = NULL; |
|
1242 TempNewSymbol sig = NULL; |
|
1243 if (name_jh != NULL) { |
|
1244 name = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(name_jh)); |
|
1245 if (name == NULL) return 0; // a match is not possible |
|
1246 } |
|
1247 if (sig_jh != NULL) { |
|
1248 sig = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(sig_jh)); |
|
1249 if (sig == NULL) return 0; // a match is not possible |
|
1250 } |
|
1251 |
|
1252 KlassHandle caller; |
|
1253 if (caller_jh != NULL) { |
|
1254 oop caller_oop = JNIHandles::resolve_non_null(caller_jh); |
|
1255 if (!java_lang_Class::is_instance(caller_oop)) return -1; |
|
1256 caller = KlassHandle(THREAD, java_lang_Class::as_Klass(caller_oop)); |
|
1257 } |
|
1258 |
|
1259 if (name != NULL && sig != NULL && results.not_null()) { |
|
1260 // try a direct resolve |
|
1261 // %%% TO DO |
|
1262 } |
|
1263 |
|
1264 int res = MethodHandles::find_MemberNames(k, name, sig, mflags, |
|
1265 caller, skip, results); |
|
1266 // TO DO: expand at least some of the MemberNames, to avoid massive callbacks |
|
1267 return res; |
|
1268 } |
|
1269 JVM_END |
|
1270 |
|
1271 JVM_ENTRY(void, MHN_setCallSiteTargetNormal(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) { |
|
1272 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh)); |
|
1273 Handle target (THREAD, JNIHandles::resolve(target_jh)); |
|
1274 { |
|
1275 // Walk all nmethods depending on this call site. |
|
1276 MutexLocker mu(Compile_lock, thread); |
|
1277 Universe::flush_dependents_on(call_site, target); |
|
1278 java_lang_invoke_CallSite::set_target(call_site(), target()); |
|
1279 } |
|
1280 } |
|
1281 JVM_END |
|
1282 |
|
1283 JVM_ENTRY(void, MHN_setCallSiteTargetVolatile(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) { |
|
1284 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh)); |
|
1285 Handle target (THREAD, JNIHandles::resolve(target_jh)); |
|
1286 { |
|
1287 // Walk all nmethods depending on this call site. |
|
1288 MutexLocker mu(Compile_lock, thread); |
|
1289 Universe::flush_dependents_on(call_site, target); |
|
1290 java_lang_invoke_CallSite::set_target_volatile(call_site(), target()); |
|
1291 } |
|
1292 } |
|
1293 JVM_END |
|
1294 |
|
1295 /** |
|
1296 * Throws a java/lang/UnsupportedOperationException unconditionally. |
|
1297 * This is required by the specification of MethodHandle.invoke if |
|
1298 * invoked directly. |
|
1299 */ |
|
1300 JVM_ENTRY(jobject, MH_invoke_UOE(JNIEnv* env, jobject mh, jobjectArray args)) { |
|
1301 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invoke cannot be invoked reflectively"); |
|
1302 return NULL; |
|
1303 } |
|
1304 JVM_END |
|
1305 |
|
1306 /** |
|
1307 * Throws a java/lang/UnsupportedOperationException unconditionally. |
|
1308 * This is required by the specification of MethodHandle.invokeExact if |
|
1309 * invoked directly. |
|
1310 */ |
|
1311 JVM_ENTRY(jobject, MH_invokeExact_UOE(JNIEnv* env, jobject mh, jobjectArray args)) { |
|
1312 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invokeExact cannot be invoked reflectively"); |
|
1313 return NULL; |
|
1314 } |
|
1315 JVM_END |
|
1316 |
|
1317 /// JVM_RegisterMethodHandleMethods |
|
1318 |
|
1319 #undef CS // Solaris builds complain |
|
1320 |
|
1321 #define LANG "Ljava/lang/" |
|
1322 #define JLINV "Ljava/lang/invoke/" |
|
1323 |
|
1324 #define OBJ LANG"Object;" |
|
1325 #define CLS LANG"Class;" |
|
1326 #define STRG LANG"String;" |
|
1327 #define CS JLINV"CallSite;" |
|
1328 #define MT JLINV"MethodType;" |
|
1329 #define MH JLINV"MethodHandle;" |
|
1330 #define MEM JLINV"MemberName;" |
|
1331 |
|
1332 #define CC (char*) /*cast a literal from (const char*)*/ |
|
1333 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) |
|
1334 |
|
1335 // These are the native methods on java.lang.invoke.MethodHandleNatives. |
|
1336 static JNINativeMethod MHN_methods[] = { |
|
1337 {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHN_init_Mem)}, |
|
1338 {CC"expand", CC"("MEM")V", FN_PTR(MHN_expand_Mem)}, |
|
1339 {CC"resolve", CC"("MEM""CLS")"MEM, FN_PTR(MHN_resolve_Mem)}, |
|
1340 {CC"getConstant", CC"(I)I", FN_PTR(MHN_getConstant)}, |
|
1341 // static native int getNamedCon(int which, Object[] name) |
|
1342 {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHN_getNamedCon)}, |
|
1343 // static native int getMembers(Class<?> defc, String matchName, String matchSig, |
|
1344 // int matchFlags, Class<?> caller, int skip, MemberName[] results); |
|
1345 {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)}, |
|
1346 {CC"objectFieldOffset", CC"("MEM")J", FN_PTR(MHN_objectFieldOffset)}, |
|
1347 {CC"setCallSiteTargetNormal", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetNormal)}, |
|
1348 {CC"setCallSiteTargetVolatile", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetVolatile)}, |
|
1349 {CC"staticFieldOffset", CC"("MEM")J", FN_PTR(MHN_staticFieldOffset)}, |
|
1350 {CC"staticFieldBase", CC"("MEM")"OBJ, FN_PTR(MHN_staticFieldBase)}, |
|
1351 {CC"getMemberVMInfo", CC"("MEM")"OBJ, FN_PTR(MHN_getMemberVMInfo)} |
|
1352 }; |
|
1353 |
|
1354 static JNINativeMethod MH_methods[] = { |
|
1355 // UnsupportedOperationException throwers |
|
1356 {CC"invoke", CC"(["OBJ")"OBJ, FN_PTR(MH_invoke_UOE)}, |
|
1357 {CC"invokeExact", CC"(["OBJ")"OBJ, FN_PTR(MH_invokeExact_UOE)} |
|
1358 }; |
|
1359 |
|
1360 /** |
|
1361 * Helper method to register native methods. |
|
1362 */ |
|
1363 static bool register_natives(JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods) { |
|
1364 int status = env->RegisterNatives(clazz, methods, nMethods); |
|
1365 if (status != JNI_OK || env->ExceptionOccurred()) { |
|
1366 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support."); |
|
1367 env->ExceptionClear(); |
|
1368 return false; |
|
1369 } |
|
1370 return true; |
|
1371 } |
|
1372 |
|
1373 /** |
|
1374 * This one function is exported, used by NativeLookup. |
|
1375 */ |
|
1376 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) { |
|
1377 if (!EnableInvokeDynamic) { |
|
1378 warning("JSR 292 is disabled in this JVM. Use -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic to enable."); |
|
1379 return; // bind nothing |
|
1380 } |
|
1381 |
|
1382 assert(!MethodHandles::enabled(), "must not be enabled"); |
|
1383 bool enable_MH = true; |
|
1384 |
|
1385 jclass MH_class = NULL; |
|
1386 if (SystemDictionary::MethodHandle_klass() == NULL) { |
|
1387 enable_MH = false; |
|
1388 } else { |
|
1389 oop mirror = SystemDictionary::MethodHandle_klass()->java_mirror(); |
|
1390 MH_class = (jclass) JNIHandles::make_local(env, mirror); |
|
1391 } |
|
1392 |
|
1393 if (enable_MH) { |
|
1394 ThreadToNativeFromVM ttnfv(thread); |
|
1395 |
|
1396 if (enable_MH) { |
|
1397 enable_MH = register_natives(env, MHN_class, MHN_methods, sizeof(MHN_methods)/sizeof(JNINativeMethod)); |
|
1398 } |
|
1399 if (enable_MH) { |
|
1400 enable_MH = register_natives(env, MH_class, MH_methods, sizeof(MH_methods)/sizeof(JNINativeMethod)); |
|
1401 } |
|
1402 } |
|
1403 |
|
1404 if (TraceInvokeDynamic) { |
|
1405 tty->print_cr("MethodHandle support loaded (using LambdaForms)"); |
|
1406 } |
|
1407 |
|
1408 if (enable_MH) { |
|
1409 MethodHandles::generate_adapters(); |
|
1410 MethodHandles::set_enabled(true); |
|
1411 } |
|
1412 } |
|
1413 JVM_END |