hotspot/src/share/vm/prims/methodHandles.cpp
changeset 36819 bca375d368ed
parent 36199 855b44ce93c0
child 36822 cdc493d7bc9a
equal deleted inserted replaced
36818:b40330c06dea 36819:bca375d368ed
   316   return mname();
   316   return mname();
   317 }
   317 }
   318 
   318 
   319 // JVM 2.9 Special Methods:
   319 // JVM 2.9 Special Methods:
   320 // A method is signature polymorphic if and only if all of the following conditions hold :
   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.
   321 // * It is declared in the java.lang.invoke.MethodHandle/VarHandle classes.
   322 // * It has a single formal parameter of type Object[].
   322 // * It has a single formal parameter of type Object[].
   323 // * It has a return type of Object.
   323 // * It has a return type of Object for a polymorphic return type, otherwise a fixed return type.
   324 // * It has the ACC_VARARGS and ACC_NATIVE flags set.
   324 // * It has the ACC_VARARGS and ACC_NATIVE flags set.
   325 bool MethodHandles::is_method_handle_invoke_name(Klass* klass, Symbol* name) {
   325 bool MethodHandles::is_method_handle_invoke_name(Klass* klass, Symbol* name) {
   326   if (klass == NULL)
   326   if (klass == NULL)
   327     return false;
   327     return false;
   328   // The following test will fail spuriously during bootstrap of MethodHandle itself:
   328   // The following test will fail spuriously during bootstrap of MethodHandle itself:
   329   //    if (klass != SystemDictionary::MethodHandle_klass())
   329   //    if (klass != SystemDictionary::MethodHandle_klass())
   330   // Test the name instead:
   330   // Test the name instead:
   331   if (klass->name() != vmSymbols::java_lang_invoke_MethodHandle())
   331   if (klass->name() != vmSymbols::java_lang_invoke_MethodHandle() &&
       
   332       klass->name() != vmSymbols::java_lang_invoke_VarHandle()) {
   332     return false;
   333     return false;
       
   334   }
       
   335 
       
   336   // Look up signature polymorphic method with polymorphic return type
   333   Symbol* poly_sig = vmSymbols::object_array_object_signature();
   337   Symbol* poly_sig = vmSymbols::object_array_object_signature();
   334   Method* m = InstanceKlass::cast(klass)->find_method(name, poly_sig);
   338   InstanceKlass* iklass = InstanceKlass::cast(klass);
   335   if (m == NULL)  return false;
   339   Method* m = iklass->find_method(name, poly_sig);
   336   int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS;
   340   if (m != NULL) {
   337   int flags = m->access_flags().as_int();
   341     int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS;
   338   return (flags & required) == required;
   342     int flags = m->access_flags().as_int();
       
   343     if ((flags & required) == required) {
       
   344       return true;
       
   345     }
       
   346   }
       
   347 
       
   348   // Look up signature polymorphic method with non-polymorphic (non Object) return type
       
   349   int me;
       
   350   int ms = iklass->find_method_by_name(name, &me);
       
   351   if (ms == -1) return false;
       
   352   for (; ms < me; ms++) {
       
   353     Method* m = iklass->methods()->at(ms);
       
   354     int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS;
       
   355     int flags = m->access_flags().as_int();
       
   356     if ((flags & required) == required && ArgumentCount(m->signature()).size() == 1) {
       
   357       return true;
       
   358     }
       
   359   }
       
   360   return false;
   339 }
   361 }
   340 
   362 
   341 
   363 
   342 Symbol* MethodHandles::signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid) {
   364 Symbol* MethodHandles::signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid) {
   343   assert(is_signature_polymorphic_intrinsic(iid), "%d %s", iid, vmIntrinsics::name_at(iid));
   365   assert(is_signature_polymorphic_intrinsic(iid), "%d %s", iid, vmIntrinsics::name_at(iid));
   393   }
   415   }
   394 
   416 
   395   // Cover the case of invokeExact and any future variants of invokeFoo.
   417   // Cover the case of invokeExact and any future variants of invokeFoo.
   396   Klass* mh_klass = SystemDictionary::well_known_klass(
   418   Klass* mh_klass = SystemDictionary::well_known_klass(
   397                               SystemDictionary::WK_KLASS_ENUM_NAME(MethodHandle_klass) );
   419                               SystemDictionary::WK_KLASS_ENUM_NAME(MethodHandle_klass) );
   398   if (mh_klass != NULL && is_method_handle_invoke_name(mh_klass, name))
   420   if (mh_klass != NULL && is_method_handle_invoke_name(mh_klass, name)) {
   399     return vmIntrinsics::_invokeGeneric;
   421     return vmIntrinsics::_invokeGeneric;
       
   422   }
       
   423 
       
   424   // Cover the case of methods on VarHandle.
       
   425   Klass* vh_klass = SystemDictionary::well_known_klass(
       
   426                               SystemDictionary::WK_KLASS_ENUM_NAME(VarHandle_klass) );
       
   427   if (vh_klass != NULL && is_method_handle_invoke_name(vh_klass, name)) {
       
   428     return vmIntrinsics::_invokeGeneric;
       
   429   }
   400 
   430 
   401   // Note: The pseudo-intrinsic _compiledLambdaForm is never linked against.
   431   // Note: The pseudo-intrinsic _compiledLambdaForm is never linked against.
   402   // Instead it is used to mark lambda forms bound to invokehandle or invokedynamic.
   432   // Instead it is used to mark lambda forms bound to invokehandle or invokedynamic.
   403   return vmIntrinsics::_none;
   433   return vmIntrinsics::_none;
   404 }
   434 }
   405 
   435 
   406 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Klass* klass, Symbol* name) {
   436 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Klass* klass, Symbol* name) {
   407   if (klass != NULL &&
   437   if (klass != NULL &&
   408       klass->name() == vmSymbols::java_lang_invoke_MethodHandle()) {
   438       (klass->name() == vmSymbols::java_lang_invoke_MethodHandle() ||
       
   439        klass->name() == vmSymbols::java_lang_invoke_VarHandle())) {
   409     vmIntrinsics::ID iid = signature_polymorphic_name_id(name);
   440     vmIntrinsics::ID iid = signature_polymorphic_name_id(name);
   410     if (iid != vmIntrinsics::_none)
   441     if (iid != vmIntrinsics::_none)
   411       return iid;
   442       return iid;
   412     if (is_method_handle_invoke_name(klass, name))
   443     if (is_method_handle_invoke_name(klass, name))
   413       return vmIntrinsics::_invokeGeneric;
   444       return vmIntrinsics::_invokeGeneric;