hotspot/src/share/vm/prims/methodHandles.cpp
changeset 14488 ab48109f7d1b
parent 14391 df0a1573d5bd
child 14825 8bc43f610c87
equal deleted inserted replaced
14486:7d079e0eedef 14488:ab48109f7d1b
   136     return init_field_MemberName(mname_oop, k, accessFlags_from(mods), type, name, offset);
   136     return init_field_MemberName(mname_oop, k, accessFlags_from(mods), type, name, offset);
   137   } else if (target_klass == SystemDictionary::reflect_Method_klass()) {
   137   } else if (target_klass == SystemDictionary::reflect_Method_klass()) {
   138     oop clazz  = java_lang_reflect_Method::clazz(target_oop);
   138     oop clazz  = java_lang_reflect_Method::clazz(target_oop);
   139     int slot   = java_lang_reflect_Method::slot(target_oop);
   139     int slot   = java_lang_reflect_Method::slot(target_oop);
   140     Klass* k = java_lang_Class::as_Klass(clazz);
   140     Klass* k = java_lang_Class::as_Klass(clazz);
   141     if (k != NULL && Klass::cast(k)->oop_is_instance()) {
   141     if (k != NULL && k->oop_is_instance()) {
   142       Method* m = InstanceKlass::cast(k)->method_with_idnum(slot);
   142       Method* m = InstanceKlass::cast(k)->method_with_idnum(slot);
   143       return init_method_MemberName(mname_oop, m, true, k);
   143       return init_method_MemberName(mname_oop, m, true, k);
   144     }
   144     }
   145   } else if (target_klass == SystemDictionary::reflect_Constructor_klass()) {
   145   } else if (target_klass == SystemDictionary::reflect_Constructor_klass()) {
   146     oop clazz  = java_lang_reflect_Constructor::clazz(target_oop);
   146     oop clazz  = java_lang_reflect_Constructor::clazz(target_oop);
   147     int slot   = java_lang_reflect_Constructor::slot(target_oop);
   147     int slot   = java_lang_reflect_Constructor::slot(target_oop);
   148     Klass* k = java_lang_Class::as_Klass(clazz);
   148     Klass* k = java_lang_Class::as_Klass(clazz);
   149     if (k != NULL && Klass::cast(k)->oop_is_instance()) {
   149     if (k != NULL && k->oop_is_instance()) {
   150       Method* m = InstanceKlass::cast(k)->method_with_idnum(slot);
   150       Method* m = InstanceKlass::cast(k)->method_with_idnum(slot);
   151       return init_method_MemberName(mname_oop, m, false, k);
   151       return init_method_MemberName(mname_oop, m, false, k);
   152     }
   152     }
   153   } else if (target_klass == SystemDictionary::MemberName_klass()) {
   153   } else if (target_klass == SystemDictionary::MemberName_klass()) {
   154     // Note: This only works if the MemberName has already been resolved.
   154     // Note: This only works if the MemberName has already been resolved.
   185   if (m->is_initializer()) {
   185   if (m->is_initializer()) {
   186     flags |= IS_CONSTRUCTOR | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT);
   186     flags |= IS_CONSTRUCTOR | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT);
   187   } else if (mods.is_static()) {
   187   } else if (mods.is_static()) {
   188     flags |= IS_METHOD | (JVM_REF_invokeStatic << REFERENCE_KIND_SHIFT);
   188     flags |= IS_METHOD | (JVM_REF_invokeStatic << REFERENCE_KIND_SHIFT);
   189   } else if (receiver_limit != mklass &&
   189   } else if (receiver_limit != mklass &&
   190              !Klass::cast(receiver_limit)->is_subtype_of(mklass)) {
   190              !receiver_limit->is_subtype_of(mklass)) {
   191     return NULL;  // bad receiver limit
   191     return NULL;  // bad receiver limit
   192   } else if (Klass::cast(receiver_limit)->is_interface() &&
   192   } else if (receiver_limit->is_interface() &&
   193              Klass::cast(mklass)->is_interface()) {
   193              mklass->is_interface()) {
   194     flags |= IS_METHOD | (JVM_REF_invokeInterface << REFERENCE_KIND_SHIFT);
   194     flags |= IS_METHOD | (JVM_REF_invokeInterface << REFERENCE_KIND_SHIFT);
   195     receiver_limit = mklass;  // ignore passed-in limit; interfaces are interconvertible
   195     receiver_limit = mklass;  // ignore passed-in limit; interfaces are interconvertible
   196     vmindex = klassItable::compute_itable_index(m);
   196     vmindex = klassItable::compute_itable_index(m);
   197   } else if (mklass != receiver_limit && Klass::cast(mklass)->is_interface()) {
   197   } else if (mklass != receiver_limit && mklass->is_interface()) {
   198     flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT);
   198     flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT);
   199     // it is a miranda method, so m->vtable_index is not what we want
   199     // it is a miranda method, so m->vtable_index is not what we want
   200     ResourceMark rm;
   200     ResourceMark rm;
   201     klassVtable* vt = InstanceKlass::cast(receiver_limit)->vtable();
   201     klassVtable* vt = InstanceKlass::cast(receiver_limit)->vtable();
   202     vmindex = vt->index_of_miranda(m->name(), m->signature());
   202     vmindex = vt->index_of_miranda(m->name(), m->signature());
   208   }
   208   }
   209 
   209 
   210   java_lang_invoke_MemberName::set_flags(mname_oop,    flags);
   210   java_lang_invoke_MemberName::set_flags(mname_oop,    flags);
   211   java_lang_invoke_MemberName::set_vmtarget(mname_oop, m);
   211   java_lang_invoke_MemberName::set_vmtarget(mname_oop, m);
   212   java_lang_invoke_MemberName::set_vmindex(mname_oop,  vmindex);   // vtable/itable index
   212   java_lang_invoke_MemberName::set_vmindex(mname_oop,  vmindex);   // vtable/itable index
   213   java_lang_invoke_MemberName::set_clazz(mname_oop,    Klass::cast(receiver_limit)->java_mirror());
   213   java_lang_invoke_MemberName::set_clazz(mname_oop,    receiver_limit->java_mirror());
   214   // Note:  name and type can be lazily computed by resolve_MemberName,
   214   // Note:  name and type can be lazily computed by resolve_MemberName,
   215   // if Java code needs them as resolved String and MethodType objects.
   215   // if Java code needs them as resolved String and MethodType objects.
   216   // The clazz must be eagerly stored, because it provides a GC
   216   // The clazz must be eagerly stored, because it provides a GC
   217   // root to help keep alive the Method*.
   217   // root to help keep alive the Method*.
   218   // If relevant, the vtable or itable value is stored as vmindex.
   218   // If relevant, the vtable or itable value is stored as vmindex.
   256   Metadata* vmtarget = field_holder;
   256   Metadata* vmtarget = field_holder;
   257   int vmindex  = offset;  // determines the field uniquely when combined with static bit
   257   int vmindex  = offset;  // determines the field uniquely when combined with static bit
   258   java_lang_invoke_MemberName::set_flags(mname_oop,    flags);
   258   java_lang_invoke_MemberName::set_flags(mname_oop,    flags);
   259   java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget);
   259   java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget);
   260   java_lang_invoke_MemberName::set_vmindex(mname_oop,  vmindex);
   260   java_lang_invoke_MemberName::set_vmindex(mname_oop,  vmindex);
   261   java_lang_invoke_MemberName::set_clazz(mname_oop,    Klass::cast(field_holder)->java_mirror());
   261   java_lang_invoke_MemberName::set_clazz(mname_oop,    field_holder->java_mirror());
   262   if (name != NULL)
   262   if (name != NULL)
   263     java_lang_invoke_MemberName::set_name(mname_oop,   name);
   263     java_lang_invoke_MemberName::set_name(mname_oop,   name);
   264   if (type != NULL)
   264   if (type != NULL)
   265     java_lang_invoke_MemberName::set_type(mname_oop,   type);
   265     java_lang_invoke_MemberName::set_type(mname_oop,   type);
   266   // Note:  name and type can be lazily computed by resolve_MemberName,
   266   // Note:  name and type can be lazily computed by resolve_MemberName,
   297   if (klass == NULL)
   297   if (klass == NULL)
   298     return false;
   298     return false;
   299   // The following test will fail spuriously during bootstrap of MethodHandle itself:
   299   // The following test will fail spuriously during bootstrap of MethodHandle itself:
   300   //    if (klass != SystemDictionary::MethodHandle_klass())
   300   //    if (klass != SystemDictionary::MethodHandle_klass())
   301   // Test the name instead:
   301   // Test the name instead:
   302   if (Klass::cast(klass)->name() != vmSymbols::java_lang_invoke_MethodHandle())
   302   if (klass->name() != vmSymbols::java_lang_invoke_MethodHandle())
   303     return false;
   303     return false;
   304   Symbol* poly_sig = vmSymbols::object_array_object_signature();
   304   Symbol* poly_sig = vmSymbols::object_array_object_signature();
   305   Method* m = InstanceKlass::cast(klass)->find_method(name, poly_sig);
   305   Method* m = InstanceKlass::cast(klass)->find_method(name, poly_sig);
   306   if (m == NULL)  return false;
   306   if (m == NULL)  return false;
   307   int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS;
   307   int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS;
   361   return vmIntrinsics::_none;
   361   return vmIntrinsics::_none;
   362 }
   362 }
   363 
   363 
   364 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Klass* klass, Symbol* name) {
   364 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Klass* klass, Symbol* name) {
   365   if (klass != NULL &&
   365   if (klass != NULL &&
   366       Klass::cast(klass)->name() == vmSymbols::java_lang_invoke_MethodHandle()) {
   366       klass->name() == vmSymbols::java_lang_invoke_MethodHandle()) {
   367     vmIntrinsics::ID iid = signature_polymorphic_name_id(name);
   367     vmIntrinsics::ID iid = signature_polymorphic_name_id(name);
   368     if (iid != vmIntrinsics::_none)
   368     if (iid != vmIntrinsics::_none)
   369       return iid;
   369       return iid;
   370     if (is_method_handle_invoke_name(klass, name))
   370     if (is_method_handle_invoke_name(klass, name))
   371       return vmIntrinsics::_invokeGeneric;
   371       return vmIntrinsics::_invokeGeneric;
   537 }
   537 }
   538 
   538 
   539 
   539 
   540 
   540 
   541 static oop object_java_mirror() {
   541 static oop object_java_mirror() {
   542   return Klass::cast(SystemDictionary::Object_klass())->java_mirror();
   542   return SystemDictionary::Object_klass()->java_mirror();
   543 }
   543 }
   544 
   544 
   545 static oop field_name_or_null(Symbol* s) {
   545 static oop field_name_or_null(Symbol* s) {
   546   if (s == NULL)  return NULL;
   546   if (s == NULL)  return NULL;
   547   return StringTable::lookup(s);
   547   return StringTable::lookup(s);
   558   // They are optional, since reference types can be resolved lazily.
   558   // They are optional, since reference types can be resolved lazily.
   559   if (bt == T_OBJECT) {
   559   if (bt == T_OBJECT) {
   560     if (s == vmSymbols::object_signature()) {
   560     if (s == vmSymbols::object_signature()) {
   561       return object_java_mirror();
   561       return object_java_mirror();
   562     } else if (s == vmSymbols::class_signature()) {
   562     } else if (s == vmSymbols::class_signature()) {
   563       return Klass::cast(SystemDictionary::Class_klass())->java_mirror();
   563       return SystemDictionary::Class_klass()->java_mirror();
   564     } else if (s == vmSymbols::string_signature()) {
   564     } else if (s == vmSymbols::string_signature()) {
   565       return Klass::cast(SystemDictionary::String_klass())->java_mirror();
   565       return SystemDictionary::String_klass()->java_mirror();
   566     }
   566     }
   567   }
   567   }
   568   return NULL;
   568   return NULL;
   569 }
   569 }
   570 
   570 
   601 
   601 
   602   instanceKlassHandle defc;
   602   instanceKlassHandle defc;
   603   {
   603   {
   604     Klass* defc_klass = java_lang_Class::as_Klass(defc_oop());
   604     Klass* defc_klass = java_lang_Class::as_Klass(defc_oop());
   605     if (defc_klass == NULL)  return empty;  // a primitive; no resolution possible
   605     if (defc_klass == NULL)  return empty;  // a primitive; no resolution possible
   606     if (!Klass::cast(defc_klass)->oop_is_instance()) {
   606     if (!defc_klass->oop_is_instance()) {
   607       if (!Klass::cast(defc_klass)->oop_is_array())  return empty;
   607       if (!defc_klass->oop_is_array())  return empty;
   608       defc_klass = SystemDictionary::Object_klass();
   608       defc_klass = SystemDictionary::Object_klass();
   609     }
   609     }
   610     defc = instanceKlassHandle(THREAD, defc_klass);
   610     defc = instanceKlassHandle(THREAD, defc_klass);
   611   }
   611   }
   612   if (defc.is_null()) {
   612   if (defc.is_null()) {
   765     }
   765     }
   766   case IS_FIELD:
   766   case IS_FIELD:
   767     {
   767     {
   768       // This is taken from LinkResolver::resolve_field, sans access checks.
   768       // This is taken from LinkResolver::resolve_field, sans access checks.
   769       assert(vmtarget->is_klass(), "field vmtarget is Klass*");
   769       assert(vmtarget->is_klass(), "field vmtarget is Klass*");
   770       if (!Klass::cast((Klass*) vmtarget)->oop_is_instance())  break;
   770       if (!((Klass*) vmtarget)->oop_is_instance())  break;
   771       instanceKlassHandle defc(THREAD, (Klass*) vmtarget);
   771       instanceKlassHandle defc(THREAD, (Klass*) vmtarget);
   772       DEBUG_ONLY(vmtarget = NULL);  // safety
   772       DEBUG_ONLY(vmtarget = NULL);  // safety
   773       bool is_static = ((flags & JVM_ACC_STATIC) != 0);
   773       bool is_static = ((flags & JVM_ACC_STATIC) != 0);
   774       fieldDescriptor fd; // find_field initializes fd if found
   774       fieldDescriptor fd; // find_field initializes fd if found
   775       if (!defc->find_field_from_offset(vmindex, is_static, &fd))
   775       if (!defc->find_field_from_offset(vmindex, is_static, &fd))
   803   DEBUG_ONLY(No_Safepoint_Verifier nsv);
   803   DEBUG_ONLY(No_Safepoint_Verifier nsv);
   804   // this code contains no safepoints!
   804   // this code contains no safepoints!
   805 
   805 
   806   // %%% take caller into account!
   806   // %%% take caller into account!
   807 
   807 
   808   if (k == NULL || !Klass::cast(k)->oop_is_instance())  return -1;
   808   if (k == NULL || !k->oop_is_instance())  return -1;
   809 
   809 
   810   int rfill = 0, rlimit = results->length(), rskip = skip;
   810   int rfill = 0, rlimit = results->length(), rskip = skip;
   811   // overflow measurement:
   811   // overflow measurement:
   812   int overflow = 0, overflow_limit = MAX2(1000, rlimit);
   812   int overflow = 0, overflow_limit = MAX2(1000, rlimit);
   813 
   813 
  1030       // Emulate LinkResolver::check_klass_accessability.
  1030       // Emulate LinkResolver::check_klass_accessability.
  1031       Klass* caller = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh));
  1031       Klass* caller = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh));
  1032       if (!Reflection::verify_class_access(caller,
  1032       if (!Reflection::verify_class_access(caller,
  1033                                            reference_klass,
  1033                                            reference_klass,
  1034                                            true)) {
  1034                                            true)) {
  1035         THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), Klass::cast(reference_klass)->external_name());
  1035         THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), reference_klass->external_name());
  1036       }
  1036       }
  1037     }
  1037     }
  1038   }
  1038   }
  1039 
  1039 
  1040   Handle resolved = MethodHandles::resolve_MemberName(mname, CHECK_NULL);
  1040   Handle resolved = MethodHandles::resolve_MemberName(mname, CHECK_NULL);
  1106   result->obj_at_put(0, x);
  1106   result->obj_at_put(0, x);
  1107   x = NULL;
  1107   x = NULL;
  1108   if (vmtarget == NULL) {
  1108   if (vmtarget == NULL) {
  1109     x = NULL;
  1109     x = NULL;
  1110   } else if (vmtarget->is_klass()) {
  1110   } else if (vmtarget->is_klass()) {
  1111     x = Klass::cast((Klass*) vmtarget)->java_mirror();
  1111     x = ((Klass*) vmtarget)->java_mirror();
  1112   } else if (vmtarget->is_method()) {
  1112   } else if (vmtarget->is_method()) {
  1113     Handle mname2 = MethodHandles::new_MemberName(CHECK_NULL);
  1113     Handle mname2 = MethodHandles::new_MemberName(CHECK_NULL);
  1114     x = MethodHandles::init_method_MemberName(mname2(), (Method*)vmtarget, false, NULL);
  1114     x = MethodHandles::init_method_MemberName(mname2(), (Method*)vmtarget, false, NULL);
  1115   }
  1115   }
  1116   result->obj_at_put(1, x);
  1116   result->obj_at_put(1, x);
  1235 
  1235 
  1236   jclass MH_class = NULL;
  1236   jclass MH_class = NULL;
  1237   if (SystemDictionary::MethodHandle_klass() == NULL) {
  1237   if (SystemDictionary::MethodHandle_klass() == NULL) {
  1238     enable_MH = false;
  1238     enable_MH = false;
  1239   } else {
  1239   } else {
  1240     oop mirror = Klass::cast(SystemDictionary::MethodHandle_klass())->java_mirror();
  1240     oop mirror = SystemDictionary::MethodHandle_klass()->java_mirror();
  1241     MH_class = (jclass) JNIHandles::make_local(env, mirror);
  1241     MH_class = (jclass) JNIHandles::make_local(env, mirror);
  1242   }
  1242   }
  1243 
  1243 
  1244   int status;
  1244   int status;
  1245 
  1245