205 tty->print("default");
206 }
207 tty->cr();
208 }
209 break;
210
211 case CallInfo::vtable_call:
212 vmindex = info.vtable_index();
213 flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT);
214 assert(info.resolved_klass()->is_subtype_of(m_klass()), "virtual call must be type-safe");
215 if (m_klass->is_interface()) {
216 // This is a vtable call to an interface method (abstract "miranda method" or default method).
217 // The vtable index is meaningless without a class (not interface) receiver type, so get one.
218 // (LinkResolver should help us figure this out.)
219 KlassHandle m_klass_non_interface = info.resolved_klass();
220 if (m_klass_non_interface->is_interface()) {
221 m_klass_non_interface = SystemDictionary::Object_klass();
222 #ifdef ASSERT
223 { ResourceMark rm;
224 Method* m2 = m_klass_non_interface->vtable()->method_at(vmindex);
225 assert(m->name() == m2->name() && m->signature() == m2->signature(),
226 err_msg("at %d, %s != %s", vmindex,
227 m->name_and_sig_as_C_string(), m2->name_and_sig_as_C_string()));
228 }
229 #endif //ASSERT
230 }
231 if (!m->is_public()) {
232 assert(m->is_public(), "virtual call must be to public interface method");
233 return NULL; // elicit an error later in product build
234 }
235 assert(info.resolved_klass()->is_subtype_of(m_klass_non_interface()), "virtual call must be type-safe");
236 m_klass = m_klass_non_interface;
237 }
238 if (TraceInvokeDynamic) {
239 ResourceMark rm;
240 tty->print_cr("memberName: invokevirtual method_holder::method: %s, receiver: %s, vtableindex: %d, access_flags:",
241 Method::name_and_sig_as_C_string(m->method_holder(), m->name(), m->signature()),
242 m_klass->internal_name(), vmindex);
243 m->access_flags().print_on(tty);
244 if (m->is_default_method()) {
245 tty->print("default");
311 // The base clazz and field offset (vmindex) must be eagerly stored,
312 // because they unambiguously identify the field.
313 // Although the fieldDescriptor::_index would also identify the field,
314 // we do not use it, because it is harder to decode.
315 // TO DO: maybe intern mname_oop
316 return mname();
317 }
318
319 // JVM 2.9 Special Methods:
320 // A method is signature polymorphic if and only if all of the following conditions hold :
321 // * It is declared in the java.lang.invoke.MethodHandle class.
322 // * It has a single formal parameter of type Object[].
323 // * It has a return type of Object.
324 // * It has the ACC_VARARGS and ACC_NATIVE flags set.
325 bool MethodHandles::is_method_handle_invoke_name(Klass* klass, Symbol* name) {
326 if (klass == NULL)
327 return false;
328 // The following test will fail spuriously during bootstrap of MethodHandle itself:
329 // if (klass != SystemDictionary::MethodHandle_klass())
330 // Test the name instead:
331 if (klass->name() != vmSymbols::java_lang_invoke_MethodHandle())
332 return false;
333 Symbol* poly_sig = vmSymbols::object_array_object_signature();
334 Method* m = InstanceKlass::cast(klass)->find_method(name, poly_sig);
335 if (m == NULL) return false;
336 int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS;
337 int flags = m->access_flags().as_int();
338 return (flags & required) == required;
339 }
340
341
342 Symbol* MethodHandles::signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid) {
343 assert(is_signature_polymorphic_intrinsic(iid), err_msg("iid=%d", iid));
344 switch (iid) {
345 case vmIntrinsics::_invokeBasic: return vmSymbols::invokeBasic_name();
346 case vmIntrinsics::_linkToVirtual: return vmSymbols::linkToVirtual_name();
347 case vmIntrinsics::_linkToStatic: return vmSymbols::linkToStatic_name();
348 case vmIntrinsics::_linkToSpecial: return vmSymbols::linkToSpecial_name();
349 case vmIntrinsics::_linkToInterface: return vmSymbols::linkToInterface_name();
350 }
351 assert(false, "");
375 // There is one static signature-polymorphic method for each JVM invocation mode.
376 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToVirtual_name): return vmIntrinsics::_linkToVirtual;
377 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToStatic_name): return vmIntrinsics::_linkToStatic;
378 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToSpecial_name): return vmIntrinsics::_linkToSpecial;
379 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToInterface_name): return vmIntrinsics::_linkToInterface;
380 }
381
382 // Cover the case of invokeExact and any future variants of invokeFoo.
383 Klass* mh_klass = SystemDictionary::well_known_klass(
384 SystemDictionary::WK_KLASS_ENUM_NAME(MethodHandle_klass) );
385 if (mh_klass != NULL && is_method_handle_invoke_name(mh_klass, name))
386 return vmIntrinsics::_invokeGeneric;
387
388 // Note: The pseudo-intrinsic _compiledLambdaForm is never linked against.
389 // Instead it is used to mark lambda forms bound to invokehandle or invokedynamic.
390 return vmIntrinsics::_none;
391 }
392
393 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Klass* klass, Symbol* name) {
394 if (klass != NULL &&
395 klass->name() == vmSymbols::java_lang_invoke_MethodHandle()) {
396 vmIntrinsics::ID iid = signature_polymorphic_name_id(name);
397 if (iid != vmIntrinsics::_none)
398 return iid;
399 if (is_method_handle_invoke_name(klass, name))
400 return vmIntrinsics::_invokeGeneric;
401 }
402 return vmIntrinsics::_none;
403 }
404
405
406 // convert the external string or reflective type to an internal signature
407 Symbol* MethodHandles::lookup_signature(oop type_str, bool intern_if_not_found, TRAPS) {
408 if (java_lang_invoke_MethodType::is_instance(type_str)) {
409 return java_lang_invoke_MethodType::as_signature(type_str, intern_if_not_found, THREAD);
410 } else if (java_lang_Class::is_instance(type_str)) {
411 return java_lang_Class::as_signature(type_str, false, THREAD);
412 } else if (java_lang_String::is_instance_inlined(type_str)) {
413 if (intern_if_not_found) {
414 return java_lang_String::as_symbol(type_str, THREAD);
415 } else {
569
570 static oop object_java_mirror() {
571 return SystemDictionary::Object_klass()->java_mirror();
572 }
573
574 oop MethodHandles::field_name_or_null(Symbol* s) {
575 if (s == NULL) return NULL;
576 return StringTable::lookup(s);
577 }
578
579 oop MethodHandles::field_signature_type_or_null(Symbol* s) {
580 if (s == NULL) return NULL;
581 BasicType bt = FieldType::basic_type(s);
582 if (is_java_primitive(bt)) {
583 assert(s->utf8_length() == 1, "");
584 return java_lang_Class::primitive_mirror(bt);
585 }
586 // Here are some more short cuts for common types.
587 // They are optional, since reference types can be resolved lazily.
588 if (bt == T_OBJECT) {
589 if (s == vmSymbols::object_signature()) {
590 return object_java_mirror();
591 } else if (s == vmSymbols::class_signature()) {
592 return SystemDictionary::Class_klass()->java_mirror();
593 } else if (s == vmSymbols::string_signature()) {
594 return SystemDictionary::String_klass()->java_mirror();
595 }
596 }
597 return NULL;
598 }
599
600
601 // An unresolved member name is a mere symbolic reference.
602 // Resolving it plants a vmtarget/vmindex in it,
603 // which refers directly to JVM internals.
604 Handle MethodHandles::resolve_MemberName(Handle mname, KlassHandle caller, TRAPS) {
605 Handle empty;
606 assert(java_lang_invoke_MemberName::is_instance(mname()), "");
607
608 if (java_lang_invoke_MemberName::vmtarget(mname()) != NULL) {
609 // Already resolved.
610 DEBUG_ONLY(int vmindex = java_lang_invoke_MemberName::vmindex(mname()));
611 assert(vmindex >= Method::nonvirtual_vtable_index, "");
612 return mname;
613 }
847 bool local_only = !(search_superc | search_intfc);
848 bool classes_only = false;
849
850 if (name != NULL) {
851 if (name->utf8_length() == 0) return 0; // a match is not possible
852 }
853 if (sig != NULL) {
854 if (sig->utf8_length() == 0) return 0; // a match is not possible
855 if (sig->byte_at(0) == '(')
856 match_flags &= ~(IS_FIELD | IS_TYPE);
857 else
858 match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD);
859 }
860
861 if ((match_flags & IS_TYPE) != 0) {
862 // NYI, and Core Reflection works quite well for this query
863 }
864
865 if ((match_flags & IS_FIELD) != 0) {
866 for (FieldStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) {
867 if (name != NULL && st.name() != name)
868 continue;
869 if (sig != NULL && st.signature() != sig)
870 continue;
871 // passed the filters
872 if (rskip > 0) {
873 --rskip;
874 } else if (rfill < rlimit) {
875 Handle result(thread, results->obj_at(rfill++));
876 if (!java_lang_invoke_MemberName::is_instance(result()))
877 return -99; // caller bug!
878 oop saved = MethodHandles::init_field_MemberName(result, st.field_descriptor());
879 if (saved != result())
880 results->obj_at_put(rfill-1, saved); // show saved instance to user
881 } else if (++overflow >= overflow_limit) {
882 match_flags = 0; break; // got tired of looking at overflow
883 }
884 }
885 }
886
887 if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) {
888 // watch out for these guys:
889 Symbol* init_name = vmSymbols::object_initializer_name();
890 Symbol* clinit_name = vmSymbols::class_initializer_name();
891 if (name == clinit_name) clinit_name = NULL; // hack for exposing <clinit>
892 bool negate_name_test = false;
893 // fix name so that it captures the intention of IS_CONSTRUCTOR
894 if (!(match_flags & IS_METHOD)) {
895 // constructors only
896 if (name == NULL) {
897 name = init_name;
898 } else if (name != init_name) {
899 return 0; // no constructors of this method name
900 }
901 } else if (!(match_flags & IS_CONSTRUCTOR)) {
902 // methods only
903 if (name == NULL) {
904 name = init_name;
905 negate_name_test = true; // if we see the name, we *omit* the entry
906 } else if (name == init_name) {
907 return 0; // no methods of this constructor name
908 }
909 } else {
910 // caller will accept either sort; no need to adjust name
911 }
912 for (MethodStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) {
913 Method* m = st.method();
914 Symbol* m_name = m->name();
915 if (m_name == clinit_name)
916 continue;
917 if (name != NULL && ((m_name != name) ^ negate_name_test))
918 continue;
919 if (sig != NULL && m->signature() != sig)
920 continue;
921 // passed the filters
922 if (rskip > 0) {
923 --rskip;
924 } else if (rfill < rlimit) {
925 Handle result(thread, results->obj_at(rfill++));
926 if (!java_lang_invoke_MemberName::is_instance(result()))
927 return -99; // caller bug!
928 CallInfo info(m);
929 oop saved = MethodHandles::init_method_MemberName(result, info);
930 if (saved != result())
931 results->obj_at_put(rfill-1, saved); // show saved instance to user
932 } else if (++overflow >= overflow_limit) {
933 match_flags = 0; break; // got tired of looking at overflow
934 }
935 }
936 }
937
938 // return number of elements we at leasted wanted to initialize
939 return rfill + overflow;
|
205 tty->print("default");
206 }
207 tty->cr();
208 }
209 break;
210
211 case CallInfo::vtable_call:
212 vmindex = info.vtable_index();
213 flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT);
214 assert(info.resolved_klass()->is_subtype_of(m_klass()), "virtual call must be type-safe");
215 if (m_klass->is_interface()) {
216 // This is a vtable call to an interface method (abstract "miranda method" or default method).
217 // The vtable index is meaningless without a class (not interface) receiver type, so get one.
218 // (LinkResolver should help us figure this out.)
219 KlassHandle m_klass_non_interface = info.resolved_klass();
220 if (m_klass_non_interface->is_interface()) {
221 m_klass_non_interface = SystemDictionary::Object_klass();
222 #ifdef ASSERT
223 { ResourceMark rm;
224 Method* m2 = m_klass_non_interface->vtable()->method_at(vmindex);
225 assert(m->name()->equals(m2->name()) && m->signature()->equals(m2->signature()),
226 err_msg("at %d, %s != %s", vmindex,
227 m->name_and_sig_as_C_string(), m2->name_and_sig_as_C_string()));
228 }
229 #endif //ASSERT
230 }
231 if (!m->is_public()) {
232 assert(m->is_public(), "virtual call must be to public interface method");
233 return NULL; // elicit an error later in product build
234 }
235 assert(info.resolved_klass()->is_subtype_of(m_klass_non_interface()), "virtual call must be type-safe");
236 m_klass = m_klass_non_interface;
237 }
238 if (TraceInvokeDynamic) {
239 ResourceMark rm;
240 tty->print_cr("memberName: invokevirtual method_holder::method: %s, receiver: %s, vtableindex: %d, access_flags:",
241 Method::name_and_sig_as_C_string(m->method_holder(), m->name(), m->signature()),
242 m_klass->internal_name(), vmindex);
243 m->access_flags().print_on(tty);
244 if (m->is_default_method()) {
245 tty->print("default");
311 // The base clazz and field offset (vmindex) must be eagerly stored,
312 // because they unambiguously identify the field.
313 // Although the fieldDescriptor::_index would also identify the field,
314 // we do not use it, because it is harder to decode.
315 // TO DO: maybe intern mname_oop
316 return mname();
317 }
318
319 // JVM 2.9 Special Methods:
320 // A method is signature polymorphic if and only if all of the following conditions hold :
321 // * It is declared in the java.lang.invoke.MethodHandle class.
322 // * It has a single formal parameter of type Object[].
323 // * It has a return type of Object.
324 // * It has the ACC_VARARGS and ACC_NATIVE flags set.
325 bool MethodHandles::is_method_handle_invoke_name(Klass* klass, Symbol* name) {
326 if (klass == NULL)
327 return false;
328 // The following test will fail spuriously during bootstrap of MethodHandle itself:
329 // if (klass != SystemDictionary::MethodHandle_klass())
330 // Test the name instead:
331 if (klass->name()->not_equals(vmSymbols::java_lang_invoke_MethodHandle()))
332 return false;
333 Symbol* poly_sig = vmSymbols::object_array_object_signature();
334 Method* m = InstanceKlass::cast(klass)->find_method(name, poly_sig);
335 if (m == NULL) return false;
336 int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS;
337 int flags = m->access_flags().as_int();
338 return (flags & required) == required;
339 }
340
341
342 Symbol* MethodHandles::signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid) {
343 assert(is_signature_polymorphic_intrinsic(iid), err_msg("iid=%d", iid));
344 switch (iid) {
345 case vmIntrinsics::_invokeBasic: return vmSymbols::invokeBasic_name();
346 case vmIntrinsics::_linkToVirtual: return vmSymbols::linkToVirtual_name();
347 case vmIntrinsics::_linkToStatic: return vmSymbols::linkToStatic_name();
348 case vmIntrinsics::_linkToSpecial: return vmSymbols::linkToSpecial_name();
349 case vmIntrinsics::_linkToInterface: return vmSymbols::linkToInterface_name();
350 }
351 assert(false, "");
375 // There is one static signature-polymorphic method for each JVM invocation mode.
376 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToVirtual_name): return vmIntrinsics::_linkToVirtual;
377 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToStatic_name): return vmIntrinsics::_linkToStatic;
378 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToSpecial_name): return vmIntrinsics::_linkToSpecial;
379 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToInterface_name): return vmIntrinsics::_linkToInterface;
380 }
381
382 // Cover the case of invokeExact and any future variants of invokeFoo.
383 Klass* mh_klass = SystemDictionary::well_known_klass(
384 SystemDictionary::WK_KLASS_ENUM_NAME(MethodHandle_klass) );
385 if (mh_klass != NULL && is_method_handle_invoke_name(mh_klass, name))
386 return vmIntrinsics::_invokeGeneric;
387
388 // Note: The pseudo-intrinsic _compiledLambdaForm is never linked against.
389 // Instead it is used to mark lambda forms bound to invokehandle or invokedynamic.
390 return vmIntrinsics::_none;
391 }
392
393 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Klass* klass, Symbol* name) {
394 if (klass != NULL &&
395 klass->name()->equals(vmSymbols::java_lang_invoke_MethodHandle())) {
396 vmIntrinsics::ID iid = signature_polymorphic_name_id(name);
397 if (iid != vmIntrinsics::_none)
398 return iid;
399 if (is_method_handle_invoke_name(klass, name))
400 return vmIntrinsics::_invokeGeneric;
401 }
402 return vmIntrinsics::_none;
403 }
404
405
406 // convert the external string or reflective type to an internal signature
407 Symbol* MethodHandles::lookup_signature(oop type_str, bool intern_if_not_found, TRAPS) {
408 if (java_lang_invoke_MethodType::is_instance(type_str)) {
409 return java_lang_invoke_MethodType::as_signature(type_str, intern_if_not_found, THREAD);
410 } else if (java_lang_Class::is_instance(type_str)) {
411 return java_lang_Class::as_signature(type_str, false, THREAD);
412 } else if (java_lang_String::is_instance_inlined(type_str)) {
413 if (intern_if_not_found) {
414 return java_lang_String::as_symbol(type_str, THREAD);
415 } else {
569
570 static oop object_java_mirror() {
571 return SystemDictionary::Object_klass()->java_mirror();
572 }
573
574 oop MethodHandles::field_name_or_null(Symbol* s) {
575 if (s == NULL) return NULL;
576 return StringTable::lookup(s);
577 }
578
579 oop MethodHandles::field_signature_type_or_null(Symbol* s) {
580 if (s == NULL) return NULL;
581 BasicType bt = FieldType::basic_type(s);
582 if (is_java_primitive(bt)) {
583 assert(s->utf8_length() == 1, "");
584 return java_lang_Class::primitive_mirror(bt);
585 }
586 // Here are some more short cuts for common types.
587 // They are optional, since reference types can be resolved lazily.
588 if (bt == T_OBJECT) {
589 if (s->equals(vmSymbols::object_signature())) {
590 return object_java_mirror();
591 } else if (s->equals(vmSymbols::class_signature())) {
592 return SystemDictionary::Class_klass()->java_mirror();
593 } else if (s->equals(vmSymbols::string_signature())) {
594 return SystemDictionary::String_klass()->java_mirror();
595 }
596 }
597 return NULL;
598 }
599
600
601 // An unresolved member name is a mere symbolic reference.
602 // Resolving it plants a vmtarget/vmindex in it,
603 // which refers directly to JVM internals.
604 Handle MethodHandles::resolve_MemberName(Handle mname, KlassHandle caller, TRAPS) {
605 Handle empty;
606 assert(java_lang_invoke_MemberName::is_instance(mname()), "");
607
608 if (java_lang_invoke_MemberName::vmtarget(mname()) != NULL) {
609 // Already resolved.
610 DEBUG_ONLY(int vmindex = java_lang_invoke_MemberName::vmindex(mname()));
611 assert(vmindex >= Method::nonvirtual_vtable_index, "");
612 return mname;
613 }
847 bool local_only = !(search_superc | search_intfc);
848 bool classes_only = false;
849
850 if (name != NULL) {
851 if (name->utf8_length() == 0) return 0; // a match is not possible
852 }
853 if (sig != NULL) {
854 if (sig->utf8_length() == 0) return 0; // a match is not possible
855 if (sig->byte_at(0) == '(')
856 match_flags &= ~(IS_FIELD | IS_TYPE);
857 else
858 match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD);
859 }
860
861 if ((match_flags & IS_TYPE) != 0) {
862 // NYI, and Core Reflection works quite well for this query
863 }
864
865 if ((match_flags & IS_FIELD) != 0) {
866 for (FieldStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) {
867 if (name != NULL && st.name()->not_equals(name))
868 continue;
869 if (sig != NULL && st.signature()->not_equals(sig))
870 continue;
871 // passed the filters
872 if (rskip > 0) {
873 --rskip;
874 } else if (rfill < rlimit) {
875 Handle result(thread, results->obj_at(rfill++));
876 if (!java_lang_invoke_MemberName::is_instance(result()))
877 return -99; // caller bug!
878 oop saved = MethodHandles::init_field_MemberName(result, st.field_descriptor());
879 if (saved != result())
880 results->obj_at_put(rfill-1, saved); // show saved instance to user
881 } else if (++overflow >= overflow_limit) {
882 match_flags = 0; break; // got tired of looking at overflow
883 }
884 }
885 }
886
887 if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) {
888 // watch out for these guys:
889 Symbol* init_name = vmSymbols::object_initializer_name();
890 Symbol* clinit_name = vmSymbols::class_initializer_name();
891 if (name->equals(clinit_name)) clinit_name = NULL; // hack for exposing <clinit>
892 bool negate_name_test = false;
893 // fix name so that it captures the intention of IS_CONSTRUCTOR
894 if (!(match_flags & IS_METHOD)) {
895 // constructors only
896 if (name == NULL) {
897 name = init_name;
898 } else if (name->not_equals(init_name)) {
899 return 0; // no constructors of this method name
900 }
901 } else if (!(match_flags & IS_CONSTRUCTOR)) {
902 // methods only
903 if (name == NULL) {
904 name = init_name;
905 negate_name_test = true; // if we see the name, we *omit* the entry
906 } else if (name->equals(init_name)) {
907 return 0; // no methods of this constructor name
908 }
909 } else {
910 // caller will accept either sort; no need to adjust name
911 }
912 for (MethodStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) {
913 Method* m = st.method();
914 Symbol* m_name = m->name();
915 if (m_name->equals(clinit_name))
916 continue;
917 if (name->not_equals(NULL) && ((m_name->not_equals(name)) ^ negate_name_test))
918 continue;
919 if (sig->not_equals(NULL) && m->signature()->not_equals(sig))
920 continue;
921 // passed the filters
922 if (rskip > 0) {
923 --rskip;
924 } else if (rfill < rlimit) {
925 Handle result(thread, results->obj_at(rfill++));
926 if (!java_lang_invoke_MemberName::is_instance(result()))
927 return -99; // caller bug!
928 CallInfo info(m);
929 oop saved = MethodHandles::init_method_MemberName(result, info);
930 if (saved != result())
931 results->obj_at_put(rfill-1, saved); // show saved instance to user
932 } else if (++overflow >= overflow_limit) {
933 match_flags = 0; break; // got tired of looking at overflow
934 }
935 }
936 }
937
938 // return number of elements we at leasted wanted to initialize
939 return rfill + overflow;
|