hotspot/src/share/vm/interpreter/linkResolver.cpp
changeset 31019 d05fcdd70109
parent 30773 3f15e2dc056b
child 32189 5264b560ab1b
equal deleted inserted replaced
30886:d2a0ec86d6ef 31019:d05fcdd70109
    50 
    50 
    51 //------------------------------------------------------------------------------------------------------------------------
    51 //------------------------------------------------------------------------------------------------------------------------
    52 // Implementation of CallInfo
    52 // Implementation of CallInfo
    53 
    53 
    54 
    54 
    55 void CallInfo::set_static(KlassHandle resolved_klass, methodHandle resolved_method, TRAPS) {
    55 void CallInfo::set_static(KlassHandle resolved_klass, const methodHandle& resolved_method, TRAPS) {
    56   int vtable_index = Method::nonvirtual_vtable_index;
    56   int vtable_index = Method::nonvirtual_vtable_index;
    57   set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
    57   set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
    58 }
    58 }
    59 
    59 
    60 
    60 
    61 void CallInfo::set_interface(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int itable_index, TRAPS) {
    61 void CallInfo::set_interface(KlassHandle resolved_klass,
       
    62                              KlassHandle selected_klass,
       
    63                              const methodHandle& resolved_method,
       
    64                              const methodHandle& selected_method,
       
    65                              int itable_index, TRAPS) {
    62   // This is only called for interface methods. If the resolved_method
    66   // This is only called for interface methods. If the resolved_method
    63   // comes from java/lang/Object, it can be the subject of a virtual call, so
    67   // comes from java/lang/Object, it can be the subject of a virtual call, so
    64   // we should pick the vtable index from the resolved method.
    68   // we should pick the vtable index from the resolved method.
    65   // In that case, the caller must call set_virtual instead of set_interface.
    69   // In that case, the caller must call set_virtual instead of set_interface.
    66   assert(resolved_method->method_holder()->is_interface(), "");
    70   assert(resolved_method->method_holder()->is_interface(), "");
    67   assert(itable_index == resolved_method()->itable_index(), "");
    71   assert(itable_index == resolved_method()->itable_index(), "");
    68   set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);
    72   set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);
    69 }
    73 }
    70 
    74 
    71 void CallInfo::set_virtual(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index, TRAPS) {
    75 void CallInfo::set_virtual(KlassHandle resolved_klass,
       
    76                            KlassHandle selected_klass,
       
    77                            const methodHandle& resolved_method,
       
    78                            const methodHandle& selected_method,
       
    79                            int vtable_index, TRAPS) {
    72   assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");
    80   assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");
    73   assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");
    81   assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");
    74   CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);
    82   CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);
    75   set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK);
    83   set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK);
    76   assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");
    84   assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");
    77 }
    85 }
    78 
    86 
    79 void CallInfo::set_handle(methodHandle resolved_method, Handle resolved_appendix, Handle resolved_method_type, TRAPS) {
    87 void CallInfo::set_handle(const methodHandle& resolved_method,
       
    88                           Handle resolved_appendix,
       
    89                           Handle resolved_method_type, TRAPS) {
    80   if (resolved_method.is_null()) {
    90   if (resolved_method.is_null()) {
    81     THROW_MSG(vmSymbols::java_lang_InternalError(), "resolved method is null");
    91     THROW_MSG(vmSymbols::java_lang_InternalError(), "resolved method is null");
    82   }
    92   }
    83   KlassHandle resolved_klass = SystemDictionary::MethodHandle_klass();
    93   KlassHandle resolved_klass = SystemDictionary::MethodHandle_klass();
    84   assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
    94   assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
    91   _resolved_method_type = resolved_method_type;
   101   _resolved_method_type = resolved_method_type;
    92 }
   102 }
    93 
   103 
    94 void CallInfo::set_common(KlassHandle resolved_klass,
   104 void CallInfo::set_common(KlassHandle resolved_klass,
    95                           KlassHandle selected_klass,
   105                           KlassHandle selected_klass,
    96                           methodHandle resolved_method,
   106                           const methodHandle& resolved_method,
    97                           methodHandle selected_method,
   107                           const methodHandle& selected_method,
    98                           CallKind kind,
   108                           CallKind kind,
    99                           int index,
   109                           int index,
   100                           TRAPS) {
   110                           TRAPS) {
   101   assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
   111   assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
   102   _resolved_klass  = resolved_klass;
   112   _resolved_klass  = resolved_klass;
   208     fatal(err_msg_res("Unexpected call kind %d", call_kind()));
   218     fatal(err_msg_res("Unexpected call kind %d", call_kind()));
   209   }
   219   }
   210 }
   220 }
   211 #endif //ASSERT
   221 #endif //ASSERT
   212 
   222 
   213 
   223 #ifndef PRODUCT
   214 
   224 void CallInfo::print() {
       
   225   ResourceMark rm;
       
   226   const char* kindstr = "unknown";
       
   227   switch (_call_kind) {
       
   228   case direct_call: kindstr = "direct"; break;
       
   229   case vtable_call: kindstr = "vtable"; break;
       
   230   case itable_call: kindstr = "itable"; break;
       
   231   }
       
   232   tty->print_cr("Call %s@%d %s", kindstr, _call_index,
       
   233                 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
       
   234 }
       
   235 #endif
       
   236 
       
   237 //------------------------------------------------------------------------------------------------------------------------
       
   238 // Implementation of LinkInfo
       
   239 
       
   240 LinkInfo::LinkInfo(constantPoolHandle pool, int index, TRAPS) {
       
   241    // resolve klass
       
   242   Klass* result = pool->klass_ref_at(index, CHECK);
       
   243   _resolved_klass = KlassHandle(THREAD, result);
       
   244 
       
   245   // Get name, signature, and static klass
       
   246   _name          = pool->name_ref_at(index);
       
   247   _signature     = pool->signature_ref_at(index);
       
   248   _current_klass = KlassHandle(THREAD, pool->pool_holder());
       
   249 
       
   250   // Coming from the constant pool always checks access
       
   251   _check_access  = true;
       
   252 }
       
   253 
       
   254 char* LinkInfo::method_string() const {
       
   255   return Method::name_and_sig_as_C_string(_resolved_klass(), _name, _signature);
       
   256 }
       
   257 
       
   258 #ifndef PRODUCT
       
   259 void LinkInfo::print() {
       
   260   ResourceMark rm;
       
   261   tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s",
       
   262                 _resolved_klass->name()->as_C_string(),
       
   263                 _name->as_C_string(),
       
   264                 _signature->as_C_string(),
       
   265                 _current_klass.is_null() ? "(none)" : _current_klass->name()->as_C_string(),
       
   266                 _check_access ? "true" : "false");
       
   267 }
       
   268 #endif // PRODUCT
   215 //------------------------------------------------------------------------------------------------------------------------
   269 //------------------------------------------------------------------------------------------------------------------------
   216 // Klass resolution
   270 // Klass resolution
   217 
   271 
   218 void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) {
   272 void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) {
   219   if (!Reflection::verify_class_access(ref_klass(),
   273   if (!Reflection::verify_class_access(ref_klass(),
   229     );
   283     );
   230     return;
   284     return;
   231   }
   285   }
   232 }
   286 }
   233 
   287 
   234 void LinkResolver::resolve_klass(KlassHandle& result, constantPoolHandle pool, int index, TRAPS) {
       
   235   Klass* result_oop = pool->klass_ref_at(index, CHECK);
       
   236   result = KlassHandle(THREAD, result_oop);
       
   237 }
       
   238 
       
   239 //------------------------------------------------------------------------------------------------------------------------
   288 //------------------------------------------------------------------------------------------------------------------------
   240 // Method resolution
   289 // Method resolution
   241 //
   290 //
   242 // According to JVM spec. $5.4.3c & $5.4.3d
   291 // According to JVM spec. $5.4.3c & $5.4.3d
   243 
   292 
   244 // Look up method in klasses, including static methods
   293 // Look up method in klasses, including static methods
   245 // Then look up local default methods
   294 // Then look up local default methods
   246 void LinkResolver::lookup_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, bool checkpolymorphism, bool in_imethod_resolve, TRAPS) {
   295 methodHandle LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
       
   296                                                     bool checkpolymorphism,
       
   297                                                     bool in_imethod_resolve, TRAPS) {
       
   298   KlassHandle klass = link_info.resolved_klass();
       
   299   Symbol* name = link_info.name();
       
   300   Symbol* signature = link_info.signature();
       
   301 
   247   // Ignore overpasses so statics can be found during resolution
   302   // Ignore overpasses so statics can be found during resolution
   248   Method* result_oop = klass->uncached_lookup_method(name, signature, Klass::skip_overpass);
   303   Method* result = klass->uncached_lookup_method(name, signature, Klass::skip_overpass);
   249 
   304 
   250   if (klass->oop_is_array()) {
   305   if (klass->oop_is_array()) {
   251     // Only consider klass and super klass for arrays
   306     // Only consider klass and super klass for arrays
   252     result = methodHandle(THREAD, result_oop);
   307     return methodHandle(THREAD, result);
   253     return;
       
   254   }
   308   }
   255 
   309 
   256   // JDK 8, JVMS 5.4.3.4: Interface method resolution should
   310   // JDK 8, JVMS 5.4.3.4: Interface method resolution should
   257   // ignore static and non-public methods of java.lang.Object,
   311   // ignore static and non-public methods of java.lang.Object,
   258   // like clone, finalize, registerNatives.
   312   // like clone, finalize, registerNatives.
   259   if (in_imethod_resolve &&
   313   if (in_imethod_resolve &&
   260       result_oop != NULL &&
   314       result != NULL &&
   261       klass->is_interface() &&
   315       klass->is_interface() &&
   262       (result_oop->is_static() || !result_oop->is_public()) &&
   316       (result->is_static() || !result->is_public()) &&
   263       result_oop->method_holder() == SystemDictionary::Object_klass()) {
   317       result->method_holder() == SystemDictionary::Object_klass()) {
   264     result_oop = NULL;
   318     result = NULL;
   265   }
   319   }
   266 
   320 
   267   // Before considering default methods, check for an overpass in the
   321   // Before considering default methods, check for an overpass in the
   268   // current class if a method has not been found.
   322   // current class if a method has not been found.
   269   if (result_oop == NULL) {
   323   if (result == NULL) {
   270     result_oop = InstanceKlass::cast(klass())->find_method(name, signature);
   324     result = InstanceKlass::cast(klass())->find_method(name, signature);
   271   }
   325   }
   272 
   326 
   273   if (result_oop == NULL) {
   327   if (result == NULL) {
   274     Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();
   328     Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();
   275     if (default_methods != NULL) {
   329     if (default_methods != NULL) {
   276       result_oop = InstanceKlass::find_method(default_methods, name, signature);
   330       result = InstanceKlass::find_method(default_methods, name, signature);
   277     }
   331     }
   278   }
   332   }
   279 
   333 
   280   if (checkpolymorphism && result_oop != NULL) {
   334   if (checkpolymorphism && result != NULL) {
   281     vmIntrinsics::ID iid = result_oop->intrinsic_id();
   335     vmIntrinsics::ID iid = result->intrinsic_id();
   282     if (MethodHandles::is_signature_polymorphic(iid)) {
   336     if (MethodHandles::is_signature_polymorphic(iid)) {
   283       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
   337       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
   284       return;
   338       return NULL;
   285     }
   339     }
   286   }
   340   }
   287   result = methodHandle(THREAD, result_oop);
   341   return methodHandle(THREAD, result);
   288 }
   342 }
   289 
   343 
   290 // returns first instance method
   344 // returns first instance method
   291 // Looks up method in classes, then looks up local default methods
   345 // Looks up method in classes, then looks up local default methods
   292 void LinkResolver::lookup_instance_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) {
   346 methodHandle LinkResolver::lookup_instance_method_in_klasses(KlassHandle klass,
   293   Method* result_oop = klass->uncached_lookup_method(name, signature, Klass::find_overpass);
   347                                                              Symbol* name,
   294   result = methodHandle(THREAD, result_oop);
   348                                                              Symbol* signature, TRAPS) {
   295   while (!result.is_null() && result->is_static() && result->method_holder()->super() != NULL) {
   349   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass);
   296     KlassHandle super_klass = KlassHandle(THREAD, result->method_holder()->super());
   350 
   297     result = methodHandle(THREAD, super_klass->uncached_lookup_method(name, signature, Klass::find_overpass));
   351   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
       
   352     Klass* super_klass = result->method_holder()->super();
       
   353     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass);
   298   }
   354   }
   299 
   355 
   300   if (klass->oop_is_array()) {
   356   if (klass->oop_is_array()) {
   301     // Only consider klass and super klass for arrays
   357     // Only consider klass and super klass for arrays
   302     return;
   358     return methodHandle(THREAD, result);
   303   }
   359   }
   304 
   360 
   305   if (result.is_null()) {
   361   if (result == NULL) {
   306     Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();
   362     Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();
   307     if (default_methods != NULL) {
   363     if (default_methods != NULL) {
   308       result = methodHandle(InstanceKlass::find_method(default_methods, name, signature));
   364       result = InstanceKlass::find_method(default_methods, name, signature);
   309       assert(result.is_null() || !result->is_static(), "static defaults not allowed");
   365       assert(result == NULL || !result->is_static(), "static defaults not allowed");
   310     }
   366     }
   311   }
   367   }
       
   368   return methodHandle(THREAD, result);
   312 }
   369 }
   313 
   370 
   314 int LinkResolver::vtable_index_of_interface_method(KlassHandle klass,
   371 int LinkResolver::vtable_index_of_interface_method(KlassHandle klass,
   315                                           methodHandle resolved_method) {
   372                                                    const methodHandle& resolved_method) {
   316 
   373 
   317   int vtable_index = Method::invalid_vtable_index;
   374   int vtable_index = Method::invalid_vtable_index;
   318   Symbol* name = resolved_method->name();
   375   Symbol* name = resolved_method->name();
   319   Symbol* signature = resolved_method->signature();
   376   Symbol* signature = resolved_method->signature();
   320 
   377 
   334     vtable_index = vt->index_of_miranda(name, signature);
   391     vtable_index = vt->index_of_miranda(name, signature);
   335   }
   392   }
   336   return vtable_index;
   393   return vtable_index;
   337 }
   394 }
   338 
   395 
   339 void LinkResolver::lookup_method_in_interfaces(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) {
   396 methodHandle LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info, TRAPS) {
   340   InstanceKlass *ik = InstanceKlass::cast(klass());
   397   InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass()());
   341 
   398 
   342   // Specify 'true' in order to skip default methods when searching the
   399   // Specify 'true' in order to skip default methods when searching the
   343   // interfaces.  Function lookup_method_in_klasses() already looked for
   400   // interfaces.  Function lookup_method_in_klasses() already looked for
   344   // the method in the default methods table.
   401   // the method in the default methods table.
   345   result = methodHandle(THREAD, ik->lookup_method_in_all_interfaces(name, signature, Klass::skip_defaults));
   402   return methodHandle(THREAD,
   346 }
   403     ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(),
   347 
   404     Klass::skip_defaults));
   348 void LinkResolver::lookup_polymorphic_method(methodHandle& result,
   405 }
   349                                              KlassHandle klass, Symbol* name, Symbol* full_signature,
   406 
   350                                              KlassHandle current_klass,
   407 methodHandle LinkResolver::lookup_polymorphic_method(
       
   408                                              const LinkInfo& link_info,
   351                                              Handle *appendix_result_or_null,
   409                                              Handle *appendix_result_or_null,
   352                                              Handle *method_type_result,
   410                                              Handle *method_type_result,
   353                                              TRAPS) {
   411                                              TRAPS) {
       
   412   KlassHandle klass = link_info.resolved_klass();
       
   413   Symbol* name = link_info.name();
       
   414   Symbol* full_signature = link_info.signature();
       
   415 
   354   vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
   416   vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
   355   if (TraceMethodHandles) {
   417   if (TraceMethodHandles) {
   356     ResourceMark rm(THREAD);
   418     ResourceMark rm(THREAD);
   357     tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
   419     tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
   358                   vmIntrinsics::name_at(iid), klass->external_name(),
   420                   vmIntrinsics::name_at(iid), klass->external_name(),
   363     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
   425     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
   364       // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
   426       // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
   365       // Do not erase last argument type (MemberName) if it is a static linkTo method.
   427       // Do not erase last argument type (MemberName) if it is a static linkTo method.
   366       bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
   428       bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
   367       TempNewSymbol basic_signature =
   429       TempNewSymbol basic_signature =
   368         MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK);
   430         MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK_NULL);
   369       if (TraceMethodHandles) {
   431       if (TraceMethodHandles) {
   370         ResourceMark rm(THREAD);
   432         ResourceMark rm(THREAD);
   371         tty->print_cr("lookup_polymorphic_method %s %s => basic %s",
   433         tty->print_cr("lookup_polymorphic_method %s %s => basic %s",
   372                       name->as_C_string(),
   434                       name->as_C_string(),
   373                       full_signature->as_C_string(),
   435                       full_signature->as_C_string(),
   374                       basic_signature->as_C_string());
   436                       basic_signature->as_C_string());
   375       }
   437       }
   376       result = SystemDictionary::find_method_handle_intrinsic(iid,
   438       methodHandle result = SystemDictionary::find_method_handle_intrinsic(iid,
   377                                                               basic_signature,
   439                                                               basic_signature,
   378                                                               CHECK);
   440                                                               CHECK_NULL);
   379       if (result.not_null()) {
   441       if (result.not_null()) {
   380         assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
   442         assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
   381         assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
   443         assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
   382         assert(basic_signature == result->signature(), "predict the result signature");
   444         assert(basic_signature == result->signature(), "predict the result signature");
   383         if (TraceMethodHandles) {
   445         if (TraceMethodHandles) {
   384           tty->print("lookup_polymorphic_method => intrinsic ");
   446           tty->print("lookup_polymorphic_method => intrinsic ");
   385           result->print_on(tty);
   447           result->print_on(tty);
   386         }
   448         }
   387         return;
       
   388       }
   449       }
       
   450       return result;
   389     } else if (iid == vmIntrinsics::_invokeGeneric
   451     } else if (iid == vmIntrinsics::_invokeGeneric
   390                && !THREAD->is_Compiler_thread()
   452                && !THREAD->is_Compiler_thread()
   391                && appendix_result_or_null != NULL) {
   453                && appendix_result_or_null != NULL) {
   392       // This is a method with type-checking semantics.
   454       // This is a method with type-checking semantics.
   393       // We will ask Java code to spin an adapter method for it.
   455       // We will ask Java code to spin an adapter method for it.
   397         if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {
   459         if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {
   398           SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
   460           SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
   399                                             Handle(),
   461                                             Handle(),
   400                                             Handle(),
   462                                             Handle(),
   401                                             true,
   463                                             true,
   402                                             CHECK);
   464                                             CHECK_NULL);
   403         }
   465         }
   404       }
   466       }
   405 
   467 
   406       Handle appendix;
   468       Handle appendix;
   407       Handle method_type;
   469       Handle method_type;
   408       result = SystemDictionary::find_method_handle_invoker(name,
   470       methodHandle result = SystemDictionary::find_method_handle_invoker(
       
   471                                                             name,
   409                                                             full_signature,
   472                                                             full_signature,
   410                                                             current_klass,
   473                                                             link_info.current_klass(),
   411                                                             &appendix,
   474                                                             &appendix,
   412                                                             &method_type,
   475                                                             &method_type,
   413                                                             CHECK);
   476                                                             CHECK_NULL);
   414       if (TraceMethodHandles) {
   477       if (TraceMethodHandles) {
   415         tty->print("lookup_polymorphic_method => (via Java) ");
   478         tty->print("lookup_polymorphic_method => (via Java) ");
   416         result->print_on(tty);
   479         result->print_on(tty);
   417         tty->print("  lookup_polymorphic_method => appendix = ");
   480         tty->print("  lookup_polymorphic_method => appendix = ");
   418         if (appendix.is_null())  tty->print_cr("(none)");
   481         if (appendix.is_null())  tty->print_cr("(none)");
   421       if (result.not_null()) {
   484       if (result.not_null()) {
   422 #ifdef ASSERT
   485 #ifdef ASSERT
   423         ResourceMark rm(THREAD);
   486         ResourceMark rm(THREAD);
   424 
   487 
   425         TempNewSymbol basic_signature =
   488         TempNewSymbol basic_signature =
   426           MethodHandles::lookup_basic_type_signature(full_signature, CHECK);
   489           MethodHandles::lookup_basic_type_signature(full_signature, CHECK_NULL);
   427         int actual_size_of_params = result->size_of_parameters();
   490         int actual_size_of_params = result->size_of_parameters();
   428         int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
   491         int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
   429         // +1 for MethodHandle.this, +1 for trailing MethodType
   492         // +1 for MethodHandle.this, +1 for trailing MethodType
   430         if (!MethodHandles::is_signature_polymorphic_static(iid))  expected_size_of_params += 1;
   493         if (!MethodHandles::is_signature_polymorphic_static(iid))  expected_size_of_params += 1;
   431         if (appendix.not_null())                                   expected_size_of_params += 1;
   494         if (appendix.not_null())                                   expected_size_of_params += 1;
   439 #endif //ASSERT
   502 #endif //ASSERT
   440 
   503 
   441         assert(appendix_result_or_null != NULL, "");
   504         assert(appendix_result_or_null != NULL, "");
   442         (*appendix_result_or_null) = appendix;
   505         (*appendix_result_or_null) = appendix;
   443         (*method_type_result)      = method_type;
   506         (*method_type_result)      = method_type;
   444         return;
       
   445       }
   507       }
       
   508       return result;
   446     }
   509     }
   447   }
   510   }
       
   511   return NULL;
   448 }
   512 }
   449 
   513 
   450 void LinkResolver::check_method_accessability(KlassHandle ref_klass,
   514 void LinkResolver::check_method_accessability(KlassHandle ref_klass,
   451                                               KlassHandle resolved_klass,
   515                                               KlassHandle resolved_klass,
   452                                               KlassHandle sel_klass,
   516                                               KlassHandle sel_klass,
   453                                               methodHandle sel_method,
   517                                               const methodHandle& sel_method,
   454                                               TRAPS) {
   518                                               TRAPS) {
   455 
   519 
   456   AccessFlags flags = sel_method->access_flags();
   520   AccessFlags flags = sel_method->access_flags();
   457 
   521 
   458   // Special case:  arrays always override "clone". JVMS 2.15.
   522   // Special case:  arrays always override "clone". JVMS 2.15.
   491     );
   555     );
   492     return;
   556     return;
   493   }
   557   }
   494 }
   558 }
   495 
   559 
   496 void LinkResolver::resolve_method_statically(methodHandle& resolved_method, KlassHandle& resolved_klass,
   560 methodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code,
   497                                              Bytecodes::Code code, constantPoolHandle pool, int index, TRAPS) {
   561                                                      constantPoolHandle pool, int index, TRAPS) {
   498   // This method is used only
   562   // This method is used only
   499   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
   563   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
   500   // and
   564   // and
   501   // (2) in Bytecode_invoke::static_target
   565   // (2) in Bytecode_invoke::static_target
   502   // It appears to fail when applied to an invokeinterface call site.
   566   // It appears to fail when applied to an invokeinterface call site.
   503   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
   567   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
   504   // resolve klass
   568   // resolve klass
       
   569   KlassHandle resolved_klass;
   505   if (code == Bytecodes::_invokedynamic) {
   570   if (code == Bytecodes::_invokedynamic) {
   506     resolved_klass = SystemDictionary::MethodHandle_klass();
   571     resolved_klass = SystemDictionary::MethodHandle_klass();
   507     Symbol* method_name = vmSymbols::invoke_name();
   572     Symbol* method_name = vmSymbols::invoke_name();
   508     Symbol* method_signature = pool->signature_ref_at(index);
   573     Symbol* method_signature = pool->signature_ref_at(index);
   509     KlassHandle  current_klass(THREAD, pool->pool_holder());
   574     KlassHandle  current_klass(THREAD, pool->pool_holder());
   510     resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK);
   575     LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
   511     return;
   576     return resolve_method(link_info, /*require_methodref*/false, THREAD);
   512   }
   577   }
   513 
   578 
   514   resolve_klass(resolved_klass, pool, index, CHECK);
   579   LinkInfo link_info(pool, index, CHECK_NULL);
   515 
   580   resolved_klass = link_info.resolved_klass();
   516   Symbol*  method_name       = pool->name_ref_at(index);
       
   517   Symbol*  method_signature  = pool->signature_ref_at(index);
       
   518   KlassHandle  current_klass(THREAD, pool->pool_holder());
       
   519 
   581 
   520   if (pool->has_preresolution()
   582   if (pool->has_preresolution()
   521       || (resolved_klass() == SystemDictionary::MethodHandle_klass() &&
   583       || (resolved_klass() == SystemDictionary::MethodHandle_klass() &&
   522           MethodHandles::is_signature_polymorphic_name(resolved_klass(), method_name))) {
   584           MethodHandles::is_signature_polymorphic_name(resolved_klass(), link_info.name()))) {
   523     Method* result_oop = ConstantPool::method_at_if_loaded(pool, index);
   585     Method* result = ConstantPool::method_at_if_loaded(pool, index);
   524     if (result_oop != NULL) {
   586     if (result != NULL) {
   525       resolved_method = methodHandle(THREAD, result_oop);
   587       return methodHandle(THREAD, result);
   526       return;
       
   527     }
   588     }
   528   }
   589   }
   529 
   590 
   530   if (code == Bytecodes::_invokeinterface) {
   591   if (code == Bytecodes::_invokeinterface) {
   531     resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
   592     return resolve_interface_method(link_info, true, THREAD);
   532   } else if (code == Bytecodes::_invokevirtual) {
   593   } else if (code == Bytecodes::_invokevirtual) {
   533     resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
   594     return resolve_method(link_info, /*require_methodref*/true, THREAD);
   534   } else if (!resolved_klass->is_interface()) {
   595   } else if (!resolved_klass->is_interface()) {
   535     resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK);
   596     return resolve_method(link_info, /*require_methodref*/false, THREAD);
   536   } else {
   597   } else {
   537     bool nostatics = (code == Bytecodes::_invokestatic) ? false : true;
   598     bool nostatics = (code == Bytecodes::_invokestatic) ? false : true;
   538     resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, nostatics, CHECK);
   599     return resolve_interface_method(link_info, nostatics, THREAD);
   539   }
   600   }
   540 }
   601 }
   541 
   602 
   542 void LinkResolver::resolve_method(methodHandle& resolved_method, KlassHandle resolved_klass,
   603 // Check and print a loader constraint violation message for method or interface method
   543                                   Symbol* method_name, Symbol* method_signature,
   604 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
   544                                   KlassHandle current_klass, bool check_access,
   605                                                    const methodHandle& resolved_method,
   545                                   bool require_methodref, TRAPS) {
   606                                                    const char* method_type, TRAPS) {
       
   607   Handle current_loader(THREAD, link_info.current_klass()->class_loader());
       
   608   Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
       
   609 
       
   610   ResourceMark rm(THREAD);
       
   611   Symbol* failed_type_symbol =
       
   612     SystemDictionary::check_signature_loaders(link_info.signature(), current_loader,
       
   613                                               resolved_loader, true, CHECK);
       
   614   if (failed_type_symbol != NULL) {
       
   615     const char* msg = "loader constraint violation: when resolving %s"
       
   616       " \"%s\" the class loader (instance of %s) of the current class, %s,"
       
   617       " and the class loader (instance of %s) for the method's defining class, %s, have"
       
   618       " different Class objects for the type %s used in the signature";
       
   619     char* sig = link_info.method_string();
       
   620     const char* loader1_name = SystemDictionary::loader_name(current_loader());
       
   621     char* current = link_info.current_klass()->name()->as_C_string();
       
   622     const char* loader2_name = SystemDictionary::loader_name(resolved_loader());
       
   623     char* target = resolved_method->method_holder()->name()->as_C_string();
       
   624     char* failed_type_name = failed_type_symbol->as_C_string();
       
   625     size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1_name) +
       
   626       strlen(current) + strlen(loader2_name) + strlen(target) +
       
   627       strlen(failed_type_name) + strlen(method_type) + 1;
       
   628     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
       
   629     jio_snprintf(buf, buflen, msg, method_type, sig, loader1_name, current, loader2_name,
       
   630                  target, failed_type_name);
       
   631     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
       
   632   }
       
   633 }
       
   634 
       
   635 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
       
   636                                                   KlassHandle current_klass,
       
   637                                                   KlassHandle sel_klass, TRAPS) {
       
   638   Handle ref_loader(THREAD, current_klass->class_loader());
       
   639   Handle sel_loader(THREAD, sel_klass->class_loader());
       
   640 
       
   641   ResourceMark rm(THREAD);  // needed for check_signature_loaders
       
   642   Symbol* failed_type_symbol =
       
   643     SystemDictionary::check_signature_loaders(sig,
       
   644                                               ref_loader, sel_loader,
       
   645                                               false,
       
   646                                               CHECK);
       
   647   if (failed_type_symbol != NULL) {
       
   648     const char* msg = "loader constraint violation: when resolving field"
       
   649       " \"%s\" the class loader (instance of %s) of the referring class, "
       
   650       "%s, and the class loader (instance of %s) for the field's resolved "
       
   651       "type, %s, have different Class objects for that type";
       
   652     char* field_name = field->as_C_string();
       
   653     const char* loader1_name = SystemDictionary::loader_name(ref_loader());
       
   654     char* sel = sel_klass->name()->as_C_string();
       
   655     const char* loader2_name = SystemDictionary::loader_name(sel_loader());
       
   656     char* failed_type_name = failed_type_symbol->as_C_string();
       
   657     size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1_name) +
       
   658                     strlen(sel) + strlen(loader2_name) + strlen(failed_type_name) + 1;
       
   659     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
       
   660     jio_snprintf(buf, buflen, msg, field_name, loader1_name, sel, loader2_name,
       
   661                      failed_type_name);
       
   662     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
       
   663   }
       
   664 }
       
   665 
       
   666 methodHandle LinkResolver::resolve_method(const LinkInfo& link_info,
       
   667                                           bool require_methodref, TRAPS) {
   546 
   668 
   547   Handle nested_exception;
   669   Handle nested_exception;
       
   670   KlassHandle resolved_klass = link_info.resolved_klass();
   548 
   671 
   549   // 1. check if methodref required, that resolved_klass is not interfacemethodref
   672   // 1. check if methodref required, that resolved_klass is not interfacemethodref
   550   if (require_methodref && resolved_klass->is_interface()) {
   673   if (require_methodref && resolved_klass->is_interface()) {
   551     ResourceMark rm(THREAD);
   674     ResourceMark rm(THREAD);
   552     char buf[200];
   675     char buf[200];
   553     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
   676     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
   554         resolved_klass()->external_name());
   677         resolved_klass()->external_name());
   555     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   678     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   556   }
   679   }
   557 
   680 
   558   // 2. lookup method in resolved klass and its super klasses
   681   // 2. lookup method in resolved klass and its super klasses
   559   lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, true, false, CHECK);
   682   methodHandle resolved_method = lookup_method_in_klasses(link_info, true, false, CHECK_NULL);
   560 
   683 
   561   if (resolved_method.is_null() && !resolved_klass->oop_is_array()) { // not found in the class hierarchy
   684   if (resolved_method.is_null() && !resolved_klass->oop_is_array()) { // not found in the class hierarchy
   562     // 3. lookup method in all the interfaces implemented by the resolved klass
   685     // 3. lookup method in all the interfaces implemented by the resolved klass
   563     lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);
   686     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
   564 
   687 
   565     if (resolved_method.is_null()) {
   688     if (resolved_method.is_null()) {
   566       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
   689       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
   567       lookup_polymorphic_method(resolved_method, resolved_klass, method_name, method_signature,
   690       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD);
   568                                 current_klass, (Handle*)NULL, (Handle*)NULL, THREAD);
       
   569       if (HAS_PENDING_EXCEPTION) {
   691       if (HAS_PENDING_EXCEPTION) {
   570         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
   692         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
   571         CLEAR_PENDING_EXCEPTION;
   693         CLEAR_PENDING_EXCEPTION;
   572       }
   694       }
   573     }
   695     }
   574   }
   696   }
   575 
   697 
   576   if (resolved_method.is_null()) {
   698   if (resolved_method.is_null()) {
   577     // 4. method lookup failed
   699     // 4. method lookup failed
   578     ResourceMark rm(THREAD);
   700     ResourceMark rm(THREAD);
   579     THROW_MSG_CAUSE(vmSymbols::java_lang_NoSuchMethodError(),
   701     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
   580                     Method::name_and_sig_as_C_string(resolved_klass(),
   702                     Method::name_and_sig_as_C_string(resolved_klass(),
   581                                                             method_name,
   703                                                      link_info.name(),
   582                                                             method_signature),
   704                                                      link_info.signature()),
   583                     nested_exception);
   705                     nested_exception, NULL);
   584   }
   706   }
   585 
   707 
   586   // 5. access checks, access checking may be turned off when calling from within the VM.
   708   // 5. access checks, access checking may be turned off when calling from within the VM.
   587   if (check_access) {
   709   KlassHandle current_klass = link_info.current_klass();
       
   710   if (link_info.check_access()) {
   588     assert(current_klass.not_null() , "current_klass should not be null");
   711     assert(current_klass.not_null() , "current_klass should not be null");
   589 
   712 
   590     // check if method can be accessed by the referring class
   713     // check if method can be accessed by the referring class
   591     check_method_accessability(current_klass,
   714     check_method_accessability(current_klass,
   592                                resolved_klass,
   715                                resolved_klass,
   593                                KlassHandle(THREAD, resolved_method->method_holder()),
   716                                KlassHandle(THREAD, resolved_method->method_holder()),
   594                                resolved_method,
   717                                resolved_method,
   595                                CHECK);
   718                                CHECK_NULL);
   596 
   719 
   597     // check loader constraints
   720     // check loader constraints
   598     Handle loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());
   721     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
   599     Handle class_loader (THREAD, resolved_method->method_holder()->class_loader());
   722   }
   600     {
   723 
   601       ResourceMark rm(THREAD);
   724   return resolved_method;
   602       Symbol* failed_type_symbol =
   725 }
   603         SystemDictionary::check_signature_loaders(method_signature, loader,
   726 
   604                                                   class_loader, true, CHECK);
   727 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info,
   605       if (failed_type_symbol != NULL) {
   728                                                     bool nostatics, TRAPS) {
   606         const char* msg = "loader constraint violation: when resolving method"
   729 
   607           " \"%s\" the class loader (instance of %s) of the current class, %s,"
   730   KlassHandle resolved_klass = link_info.resolved_klass();
   608           " and the class loader (instance of %s) for the method's defining class, %s, have"
       
   609           " different Class objects for the type %s used in the signature";
       
   610         char* sig = Method::name_and_sig_as_C_string(resolved_klass(),method_name,method_signature);
       
   611         const char* loader1 = SystemDictionary::loader_name(loader());
       
   612         char* current = InstanceKlass::cast(current_klass())->name()->as_C_string();
       
   613         const char* loader2 = SystemDictionary::loader_name(class_loader());
       
   614         char* target = InstanceKlass::cast(resolved_method->method_holder())
       
   615                        ->name()->as_C_string();
       
   616         char* failed_type_name = failed_type_symbol->as_C_string();
       
   617         size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
       
   618           strlen(current) + strlen(loader2) + strlen(target) +
       
   619           strlen(failed_type_name) + 1;
       
   620         char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
       
   621         jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
       
   622                      target, failed_type_name);
       
   623         THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
       
   624       }
       
   625     }
       
   626   }
       
   627 }
       
   628 
       
   629 void LinkResolver::resolve_interface_method(methodHandle& resolved_method,
       
   630                                             KlassHandle resolved_klass,
       
   631                                             Symbol* method_name,
       
   632                                             Symbol* method_signature,
       
   633                                             KlassHandle current_klass,
       
   634                                             bool check_access,
       
   635                                             bool nostatics, TRAPS) {
       
   636 
   731 
   637   // check if klass is interface
   732   // check if klass is interface
   638   if (!resolved_klass->is_interface()) {
   733   if (!resolved_klass->is_interface()) {
   639     ResourceMark rm(THREAD);
   734     ResourceMark rm(THREAD);
   640     char buf[200];
   735     char buf[200];
   641     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name());
   736     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name());
   642     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   737     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   643   }
   738   }
   644 
   739 
   645   // lookup method in this interface or its super, java.lang.Object
   740   // lookup method in this interface or its super, java.lang.Object
   646   // JDK8: also look for static methods
   741   // JDK8: also look for static methods
   647   lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, false, true, CHECK);
   742   methodHandle resolved_method = lookup_method_in_klasses(link_info, false, true, CHECK_NULL);
   648 
   743 
   649   if (resolved_method.is_null() && !resolved_klass->oop_is_array()) {
   744   if (resolved_method.is_null() && !resolved_klass->oop_is_array()) {
   650     // lookup method in all the super-interfaces
   745     // lookup method in all the super-interfaces
   651     lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);
   746     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
   652   }
   747   }
   653 
   748 
   654   if (resolved_method.is_null()) {
   749   if (resolved_method.is_null()) {
   655     // no method found
   750     // no method found
   656     ResourceMark rm(THREAD);
   751     ResourceMark rm(THREAD);
   657     THROW_MSG(vmSymbols::java_lang_NoSuchMethodError(),
   752     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(),
   658               Method::name_and_sig_as_C_string(resolved_klass(),
   753                    Method::name_and_sig_as_C_string(resolved_klass(),
   659                                                       method_name,
   754                                                     link_info.name(),
   660                                                       method_signature));
   755                                                     link_info.signature()));
   661   }
   756   }
   662 
   757 
   663   if (check_access) {
   758   if (link_info.check_access()) {
   664     // JDK8 adds non-public interface methods, and accessability check requirement
   759     // JDK8 adds non-public interface methods, and accessability check requirement
       
   760     KlassHandle current_klass = link_info.current_klass();
       
   761 
   665     assert(current_klass.not_null() , "current_klass should not be null");
   762     assert(current_klass.not_null() , "current_klass should not be null");
   666 
   763 
   667     // check if method can be accessed by the referring class
   764     // check if method can be accessed by the referring class
   668     check_method_accessability(current_klass,
   765     check_method_accessability(current_klass,
   669                                resolved_klass,
   766                                resolved_klass,
   670                                KlassHandle(THREAD, resolved_method->method_holder()),
   767                                KlassHandle(THREAD, resolved_method->method_holder()),
   671                                resolved_method,
   768                                resolved_method,
   672                                CHECK);
   769                                CHECK_NULL);
   673 
   770 
   674     HandleMark hm(THREAD);
   771     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
   675     Handle loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());
       
   676     Handle class_loader (THREAD, resolved_method->method_holder()->class_loader());
       
   677     {
       
   678       ResourceMark rm(THREAD);
       
   679       Symbol* failed_type_symbol =
       
   680         SystemDictionary::check_signature_loaders(method_signature, loader,
       
   681                                                   class_loader, true, CHECK);
       
   682       if (failed_type_symbol != NULL) {
       
   683         const char* msg = "loader constraint violation: when resolving "
       
   684           "interface method \"%s\" the class loader (instance of %s) of the "
       
   685           "current class, %s, and the class loader (instance of %s) for "
       
   686           "the method's defining class, %s, have different Class objects for the type %s "
       
   687           "used in the signature";
       
   688         char* sig = Method::name_and_sig_as_C_string(resolved_klass(),method_name,method_signature);
       
   689         const char* loader1 = SystemDictionary::loader_name(loader());
       
   690         char* current = InstanceKlass::cast(current_klass())->name()->as_C_string();
       
   691         const char* loader2 = SystemDictionary::loader_name(class_loader());
       
   692         char* target = InstanceKlass::cast(resolved_method->method_holder())
       
   693                        ->name()->as_C_string();
       
   694         char* failed_type_name = failed_type_symbol->as_C_string();
       
   695         size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
       
   696           strlen(current) + strlen(loader2) + strlen(target) +
       
   697           strlen(failed_type_name) + 1;
       
   698         char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
       
   699         jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
       
   700                      target, failed_type_name);
       
   701         THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
       
   702       }
       
   703     }
       
   704   }
   772   }
   705 
   773 
   706   if (nostatics && resolved_method->is_static()) {
   774   if (nostatics && resolved_method->is_static()) {
   707     ResourceMark rm(THREAD);
   775     ResourceMark rm(THREAD);
   708     char buf[200];
   776     char buf[200];
   709     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
   777     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
   710                  Method::name_and_sig_as_C_string(resolved_klass(),
   778                  Method::name_and_sig_as_C_string(resolved_klass(),
   711                  resolved_method->name(), resolved_method->signature()));
   779                  resolved_method->name(), resolved_method->signature()));
   712     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   780     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   713   }
   781   }
   714 
   782 
   715   if (TraceItables && Verbose) {
   783   if (TraceItables && Verbose) {
   716     ResourceMark rm(THREAD);
   784     trace_method_resolution("invokeinterface resolved method: caller-class",
   717     tty->print("invokeinterface resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
   785                             link_info.current_klass(), resolved_klass, resolved_method);
   718                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
       
   719                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
       
   720                    Method::name_and_sig_as_C_string(resolved_klass(),
       
   721                                                     resolved_method->name(),
       
   722                                                     resolved_method->signature()),
       
   723                    resolved_method->method_holder()->internal_name()
       
   724                   );
       
   725     resolved_method->access_flags().print_on(tty);
       
   726     if (resolved_method->is_default_method()) {
       
   727       tty->print("default ");
       
   728     }
       
   729     if (resolved_method->is_overpass()) {
       
   730       tty->print("overpass");
       
   731     }
       
   732     tty->cr();
   786     tty->cr();
   733   }
   787   }
       
   788 
       
   789   return resolved_method;
   734 }
   790 }
   735 
   791 
   736 //------------------------------------------------------------------------------------------------------------------------
   792 //------------------------------------------------------------------------------------------------------------------------
   737 // Field resolution
   793 // Field resolution
   738 
   794 
   739 void LinkResolver::check_field_accessability(KlassHandle ref_klass,
   795 void LinkResolver::check_field_accessability(KlassHandle ref_klass,
   740                                              KlassHandle resolved_klass,
   796                                              KlassHandle resolved_klass,
   741                                              KlassHandle sel_klass,
   797                                              KlassHandle sel_klass,
   742                                              fieldDescriptor& fd,
   798                                              const fieldDescriptor& fd,
   743                                              TRAPS) {
   799                                              TRAPS) {
   744   if (!Reflection::verify_field_access(ref_klass(),
   800   if (!Reflection::verify_field_access(ref_klass(),
   745                                        resolved_klass(),
   801                                        resolved_klass(),
   746                                        sel_klass(),
   802                                        sel_klass(),
   747                                        fd.access_flags(),
   803                                        fd.access_flags(),
   757     );
   813     );
   758     return;
   814     return;
   759   }
   815   }
   760 }
   816 }
   761 
   817 
   762 void LinkResolver::resolve_field_access(fieldDescriptor& result, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {
   818 void LinkResolver::resolve_field_access(fieldDescriptor& fd, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {
   763   // Load these early in case the resolve of the containing klass fails
   819   LinkInfo link_info(pool, index, CHECK);
   764   Symbol* field = pool->name_ref_at(index);
   820   resolve_field(fd, link_info, byte, true, CHECK);
   765   Symbol* sig   = pool->signature_ref_at(index);
   821 }
   766 
   822 
   767   // resolve specified klass
   823 void LinkResolver::resolve_field(fieldDescriptor& fd,
   768   KlassHandle resolved_klass;
   824                                  const LinkInfo& link_info,
   769   resolve_klass(resolved_klass, pool, index, CHECK);
   825                                  Bytecodes::Code byte, bool initialize_class,
   770 
       
   771   KlassHandle  current_klass(THREAD, pool->pool_holder());
       
   772   resolve_field(result, resolved_klass, field, sig, current_klass, byte, true, true, CHECK);
       
   773 }
       
   774 
       
   775 void LinkResolver::resolve_field(fieldDescriptor& fd, KlassHandle resolved_klass, Symbol* field, Symbol* sig,
       
   776                                  KlassHandle current_klass, Bytecodes::Code byte, bool check_access, bool initialize_class,
       
   777                                  TRAPS) {
   826                                  TRAPS) {
   778   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
   827   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
   779          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
   828          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
   780          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
   829          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
   781          (byte == Bytecodes::_nop && !check_access), "bad field access bytecode");
   830          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
   782 
   831 
   783   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
   832   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
   784   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
   833   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
   785   // Check if there's a resolved klass containing the field
   834   // Check if there's a resolved klass containing the field
       
   835   KlassHandle resolved_klass = link_info.resolved_klass();
       
   836   Symbol* field = link_info.name();
       
   837   Symbol* sig = link_info.signature();
       
   838 
   786   if (resolved_klass.is_null()) {
   839   if (resolved_klass.is_null()) {
   787     ResourceMark rm(THREAD);
   840     ResourceMark rm(THREAD);
   788     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
   841     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
   789   }
   842   }
   790 
   843 
   794   if (sel_klass.is_null()) {
   847   if (sel_klass.is_null()) {
   795     ResourceMark rm(THREAD);
   848     ResourceMark rm(THREAD);
   796     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
   849     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
   797   }
   850   }
   798 
   851 
   799   if (!check_access)
   852   if (!link_info.check_access())
   800     // Access checking may be turned off when calling from within the VM.
   853     // Access checking may be turned off when calling from within the VM.
   801     return;
   854     return;
   802 
   855 
   803   // check access
   856   // check access
       
   857   KlassHandle current_klass = link_info.current_klass();
   804   check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
   858   check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
   805 
   859 
   806   // check for errors
   860   // check for errors
   807   if (is_static != fd.is_static()) {
   861   if (is_static != fd.is_static()) {
   808     ResourceMark rm(THREAD);
   862     ResourceMark rm(THREAD);
   825   if (is_static && initialize_class) {
   879   if (is_static && initialize_class) {
   826     sel_klass->initialize(CHECK);
   880     sel_klass->initialize(CHECK);
   827   }
   881   }
   828 
   882 
   829   if (sel_klass() != current_klass()) {
   883   if (sel_klass() != current_klass()) {
   830     HandleMark hm(THREAD);
   884     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
   831     Handle ref_loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());
       
   832     Handle sel_loader (THREAD, InstanceKlass::cast(sel_klass())->class_loader());
       
   833     {
       
   834       ResourceMark rm(THREAD);
       
   835       Symbol* failed_type_symbol =
       
   836         SystemDictionary::check_signature_loaders(sig,
       
   837                                                   ref_loader, sel_loader,
       
   838                                                   false,
       
   839                                                   CHECK);
       
   840       if (failed_type_symbol != NULL) {
       
   841         const char* msg = "loader constraint violation: when resolving field"
       
   842           " \"%s\" the class loader (instance of %s) of the referring class, "
       
   843           "%s, and the class loader (instance of %s) for the field's resolved "
       
   844           "type, %s, have different Class objects for that type";
       
   845         char* field_name = field->as_C_string();
       
   846         const char* loader1 = SystemDictionary::loader_name(ref_loader());
       
   847         char* sel = InstanceKlass::cast(sel_klass())->name()->as_C_string();
       
   848         const char* loader2 = SystemDictionary::loader_name(sel_loader());
       
   849         char* failed_type_name = failed_type_symbol->as_C_string();
       
   850         size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1) +
       
   851           strlen(sel) + strlen(loader2) + strlen(failed_type_name) + 1;
       
   852         char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
       
   853         jio_snprintf(buf, buflen, msg, field_name, loader1, sel, loader2,
       
   854                      failed_type_name);
       
   855         THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
       
   856       }
       
   857     }
       
   858   }
   885   }
   859 
   886 
   860   // return information. note that the klass is set to the actual klass containing the
   887   // return information. note that the klass is set to the actual klass containing the
   861   // field, otherwise access of static fields in superclasses will not work.
   888   // field, otherwise access of static fields in superclasses will not work.
   862 }
   889 }
   871 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
   898 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
   872 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
   899 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
   873 // recv_klass         the receiver klass
   900 // recv_klass         the receiver klass
   874 
   901 
   875 
   902 
   876 void LinkResolver::resolve_static_call(CallInfo& result, KlassHandle& resolved_klass, Symbol* method_name,
   903 void LinkResolver::resolve_static_call(CallInfo& result,
   877                                        Symbol* method_signature, KlassHandle current_klass,
   904                                        const LinkInfo& link_info,
   878                                        bool check_access, bool initialize_class, TRAPS) {
   905                                        bool initialize_class, TRAPS) {
   879   methodHandle resolved_method;
   906   methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK);
   880   linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
   907 
   881   resolved_klass = KlassHandle(THREAD, resolved_method->method_holder());
   908   // The resolved class can change as a result of this resolution.
   882 
   909   KlassHandle resolved_klass = KlassHandle(THREAD, resolved_method->method_holder());
       
   910 
       
   911   Method* save_resolved_method = resolved_method();
   883   // Initialize klass (this should only happen if everything is ok)
   912   // Initialize klass (this should only happen if everything is ok)
   884   if (initialize_class && resolved_klass->should_be_initialized()) {
   913   if (initialize_class && resolved_klass->should_be_initialized()) {
   885     resolved_klass->initialize(CHECK);
   914     resolved_klass->initialize(CHECK);
   886     linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
   915     // Use updated LinkInfo (to reresolve with resolved_klass as method_holder?)
   887   }
   916     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
   888 
   917                       link_info.current_klass(), link_info.check_access());
       
   918     resolved_method = linktime_resolve_static_method(new_info, CHECK);
       
   919   }
       
   920 
       
   921   assert(save_resolved_method == resolved_method(), "does this change?");
   889   // setup result
   922   // setup result
   890   result.set_static(resolved_klass, resolved_method, CHECK);
   923   result.set_static(resolved_klass, resolved_method, CHECK);
   891 }
   924 }
   892 
   925 
   893 // throws linktime exceptions
   926 // throws linktime exceptions
   894 void LinkResolver::linktime_resolve_static_method(methodHandle& resolved_method, KlassHandle resolved_klass,
   927 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
   895                                                   Symbol* method_name, Symbol* method_signature,
   928 
   896                                                   KlassHandle current_klass, bool check_access, TRAPS) {
   929   KlassHandle resolved_klass = link_info.resolved_klass();
   897 
   930   methodHandle resolved_method;
   898   if (!resolved_klass->is_interface()) {
   931   if (!resolved_klass->is_interface()) {
   899     resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK);
   932     resolved_method = resolve_method(link_info, /*require_methodref*/false, CHECK_NULL);
   900   } else {
   933   } else {
   901     resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK);
   934     resolved_method = resolve_interface_method(link_info, /*nostatics*/false, CHECK_NULL);
   902   }
   935   }
   903   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
   936   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
   904 
   937 
   905   // check if static
   938   // check if static
   906   if (!resolved_method->is_static()) {
   939   if (!resolved_method->is_static()) {
   907     ResourceMark rm(THREAD);
   940     ResourceMark rm(THREAD);
   908     char buf[200];
   941     char buf[200];
   909     jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
   942     jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
   910                                                       resolved_method->name(),
   943                                                       resolved_method->name(),
   911                                                       resolved_method->signature()));
   944                                                       resolved_method->signature()));
   912     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   945     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   913   }
   946   }
   914 }
   947   return resolved_method;
   915 
   948 }
   916 
   949 
   917 void LinkResolver::resolve_special_call(CallInfo& result, KlassHandle resolved_klass, Symbol* method_name,
   950 
   918                                         Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) {
   951 void LinkResolver::resolve_special_call(CallInfo& result,
   919   methodHandle resolved_method;
   952                                         const LinkInfo& link_info,
   920   linktime_resolve_special_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
   953                                         TRAPS) {
   921   runtime_resolve_special_method(result, resolved_method, resolved_klass, current_klass, check_access, CHECK);
   954   methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK);
       
   955   runtime_resolve_special_method(result, resolved_method,
       
   956                                  link_info.resolved_klass(),
       
   957                                  link_info.current_klass(),
       
   958                                  link_info.check_access(), CHECK);
   922 }
   959 }
   923 
   960 
   924 // throws linktime exceptions
   961 // throws linktime exceptions
   925 void LinkResolver::linktime_resolve_special_method(methodHandle& resolved_method, KlassHandle resolved_klass,
   962 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info,
   926                                                    Symbol* method_name, Symbol* method_signature,
   963                                                            TRAPS) {
   927                                                    KlassHandle current_klass, bool check_access, TRAPS) {
       
   928 
   964 
   929   // Invokespecial is called for multiple special reasons:
   965   // Invokespecial is called for multiple special reasons:
   930   // <init>
   966   // <init>
   931   // local private method invocation, for classes and interfaces
   967   // local private method invocation, for classes and interfaces
   932   // superclass.method, which can also resolve to a default method
   968   // superclass.method, which can also resolve to a default method
   933   // and the selected method is recalculated relative to the direct superclass
   969   // and the selected method is recalculated relative to the direct superclass
   934   // superinterface.method, which explicitly does not check shadowing
   970   // superinterface.method, which explicitly does not check shadowing
       
   971   KlassHandle resolved_klass = link_info.resolved_klass();
       
   972   methodHandle resolved_method;
   935 
   973 
   936   if (!resolved_klass->is_interface()) {
   974   if (!resolved_klass->is_interface()) {
   937     resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK);
   975     resolved_method = resolve_method(link_info, /*require_methodref*/false, CHECK_NULL);
   938   } else {
   976   } else {
   939     resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK);
   977     resolved_method = resolve_interface_method(link_info, /*nostatics*/true, CHECK_NULL);
   940   }
   978   }
   941 
   979 
   942   // check if method name is <init>, that it is found in same klass as static type
   980   // check if method name is <init>, that it is found in same klass as static type
   943   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
   981   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
   944       resolved_method->method_holder() != resolved_klass()) {
   982       resolved_method->method_holder() != resolved_klass()) {
   949       "%s: method %s%s not found",
   987       "%s: method %s%s not found",
   950       resolved_klass->external_name(),
   988       resolved_klass->external_name(),
   951       resolved_method->name()->as_C_string(),
   989       resolved_method->name()->as_C_string(),
   952       resolved_method->signature()->as_C_string()
   990       resolved_method->signature()->as_C_string()
   953     );
   991     );
   954     return;
   992     return NULL;
   955   }
   993   }
   956 
   994 
   957   // check if invokespecial's interface method reference is in an indirect superinterface
   995   // check if invokespecial's interface method reference is in an indirect superinterface
       
   996   KlassHandle current_klass = link_info.current_klass();
   958   if (!current_klass.is_null() && resolved_klass->is_interface()) {
   997   if (!current_klass.is_null() && resolved_klass->is_interface()) {
   959     Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ?
   998     Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ?
   960                                   current_klass() :
   999                                   current_klass() :
   961                                   InstanceKlass::cast(current_klass())->host_klass();
  1000                                   InstanceKlass::cast(current_klass())->host_klass();
   962     // Disable verification for the dynamically-generated reflection bytecodes.
  1001     // Disable verification for the dynamically-generated reflection bytecodes.
   971                    "Interface method reference: %s, is in an indirect superinterface of %s",
  1010                    "Interface method reference: %s, is in an indirect superinterface of %s",
   972                    Method::name_and_sig_as_C_string(resolved_klass(),
  1011                    Method::name_and_sig_as_C_string(resolved_klass(),
   973                                                          resolved_method->name(),
  1012                                                          resolved_method->name(),
   974                                                          resolved_method->signature()),
  1013                                                          resolved_method->signature()),
   975                    current_klass->external_name());
  1014                    current_klass->external_name());
   976       THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1015       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   977     }
  1016     }
   978   }
  1017   }
   979 
  1018 
   980   // check if not static
  1019   // check if not static
   981   if (resolved_method->is_static()) {
  1020   if (resolved_method->is_static()) {
   982     ResourceMark rm(THREAD);
  1021     ResourceMark rm(THREAD);
   983     char buf[200];
  1022     char buf[200];
   984     jio_snprintf(buf, sizeof(buf),
  1023     jio_snprintf(buf, sizeof(buf),
   985                  "Expecting non-static method %s",
  1024                  "Expecting non-static method %s",
   986                  Method::name_and_sig_as_C_string(resolved_klass(),
  1025                  Method::name_and_sig_as_C_string(resolved_klass(),
   987                                                          resolved_method->name(),
  1026                                                   resolved_method->name(),
   988                                                          resolved_method->signature()));
  1027                                                   resolved_method->signature()));
   989     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1028     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   990   }
  1029   }
   991 
  1030 
   992   if (TraceItables && Verbose) {
  1031   if (TraceItables && Verbose) {
   993     ResourceMark rm(THREAD);
  1032     trace_method_resolution("invokespecial resolved method: caller-class:",
   994     tty->print("invokespecial resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
  1033                             current_klass, resolved_klass, resolved_method);
   995                 (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
       
   996                 (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
       
   997                 Method::name_and_sig_as_C_string(resolved_klass(),
       
   998                                                  resolved_method->name(),
       
   999                                                  resolved_method->signature()),
       
  1000                 resolved_method->method_holder()->internal_name()
       
  1001                );
       
  1002     resolved_method->access_flags().print_on(tty);
       
  1003     if (resolved_method->is_default_method()) {
       
  1004       tty->print("default ");
       
  1005     }
       
  1006     if (resolved_method->is_overpass()) {
       
  1007       tty->print("overpass");
       
  1008     }
       
  1009     tty->cr();
  1034     tty->cr();
  1010   }
  1035   }
       
  1036 
       
  1037   return resolved_method;
  1011 }
  1038 }
  1012 
  1039 
  1013 // throws runtime exceptions
  1040 // throws runtime exceptions
  1014 void LinkResolver::runtime_resolve_special_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
  1041 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
  1015                                                   KlassHandle current_klass, bool check_access, TRAPS) {
  1042                                                   const methodHandle& resolved_method,
       
  1043                                                   KlassHandle resolved_klass,
       
  1044                                                   KlassHandle current_klass,
       
  1045                                                   bool check_access, TRAPS) {
  1016 
  1046 
  1017   // resolved method is selected method unless we have an old-style lookup
  1047   // resolved method is selected method unless we have an old-style lookup
  1018   // for a superclass method
  1048   // for a superclass method
  1019   // Invokespecial for a superinterface, resolved method is selected method,
  1049   // Invokespecial for a superinterface, resolved method is selected method,
  1020   // no checks for shadowing
  1050   // no checks for shadowing
  1035         current_klass() != resolved_klass() &&
  1065         current_klass() != resolved_klass() &&
  1036         // c) check if the method is not <init>
  1066         // c) check if the method is not <init>
  1037         resolved_method->name() != vmSymbols::object_initializer_name()) {
  1067         resolved_method->name() != vmSymbols::object_initializer_name()) {
  1038       // Lookup super method
  1068       // Lookup super method
  1039       KlassHandle super_klass(THREAD, current_klass->super());
  1069       KlassHandle super_klass(THREAD, current_klass->super());
  1040       lookup_instance_method_in_klasses(sel_method, super_klass,
  1070       sel_method = lookup_instance_method_in_klasses(super_klass,
  1041                            resolved_method->name(),
  1071                            resolved_method->name(),
  1042                            resolved_method->signature(), CHECK);
  1072                            resolved_method->signature(), CHECK);
  1043       // check if found
  1073       // check if found
  1044       if (sel_method.is_null()) {
  1074       if (sel_method.is_null()) {
  1045         ResourceMark rm(THREAD);
  1075         ResourceMark rm(THREAD);
  1064   // check if abstract
  1094   // check if abstract
  1065   if (sel_method->is_abstract()) {
  1095   if (sel_method->is_abstract()) {
  1066     ResourceMark rm(THREAD);
  1096     ResourceMark rm(THREAD);
  1067     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
  1097     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
  1068               Method::name_and_sig_as_C_string(resolved_klass(),
  1098               Method::name_and_sig_as_C_string(resolved_klass(),
  1069                                                       sel_method->name(),
  1099                                                sel_method->name(),
  1070                                                       sel_method->signature()));
  1100                                                sel_method->signature()));
  1071   }
  1101   }
  1072 
  1102 
  1073   if (TraceItables && Verbose) {
  1103   if (TraceItables && Verbose) {
  1074     ResourceMark rm(THREAD);
  1104     trace_method_resolution("invokespecial selected method: resolved-class:",
  1075     tty->print("invokespecial selected method: resolved-class:%s, method:%s, method_holder:%s, access_flags: ",
  1105                             resolved_klass, resolved_klass, sel_method);
  1076                  (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
       
  1077                  Method::name_and_sig_as_C_string(resolved_klass(),
       
  1078                                                   sel_method->name(),
       
  1079                                                   sel_method->signature()),
       
  1080                  sel_method->method_holder()->internal_name()
       
  1081                 );
       
  1082     sel_method->access_flags().print_on(tty);
       
  1083     if (sel_method->is_default_method()) {
       
  1084       tty->print("default ");
       
  1085     }
       
  1086     if (sel_method->is_overpass()) {
       
  1087       tty->print("overpass");
       
  1088     }
       
  1089     tty->cr();
  1106     tty->cr();
  1090   }
  1107   }
  1091 
  1108 
  1092   // setup result
  1109   // setup result
  1093   result.set_static(resolved_klass, sel_method, CHECK);
  1110   result.set_static(resolved_klass, sel_method, CHECK);
  1094 }
  1111 }
  1095 
  1112 
  1096 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass, KlassHandle resolved_klass,
  1113 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass,
  1097                                         Symbol* method_name, Symbol* method_signature, KlassHandle current_klass,
  1114                                         const LinkInfo& link_info,
  1098                                         bool check_access, bool check_null_and_abstract, TRAPS) {
  1115                                         bool check_null_and_abstract, TRAPS) {
  1099   methodHandle resolved_method;
  1116   methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
  1100   linktime_resolve_virtual_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
  1117   runtime_resolve_virtual_method(result, resolved_method,
  1101   runtime_resolve_virtual_method(result, resolved_method, resolved_klass, recv, receiver_klass, check_null_and_abstract, CHECK);
  1118                                  link_info.resolved_klass(),
       
  1119                                  recv, receiver_klass,
       
  1120                                  check_null_and_abstract, CHECK);
  1102 }
  1121 }
  1103 
  1122 
  1104 // throws linktime exceptions
  1123 // throws linktime exceptions
  1105 void LinkResolver::linktime_resolve_virtual_method(methodHandle &resolved_method, KlassHandle resolved_klass,
  1124 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
  1106                                                    Symbol* method_name, Symbol* method_signature,
  1125                                                            TRAPS) {
  1107                                                    KlassHandle current_klass, bool check_access, TRAPS) {
       
  1108   // normal method resolution
  1126   // normal method resolution
  1109   resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK);
  1127   methodHandle resolved_method = resolve_method(link_info, /*require_methodref*/true, CHECK_NULL);
  1110 
  1128 
  1111   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
  1129   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
  1112   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
  1130   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
  1113 
  1131 
  1114   // check if private interface method
  1132   // check if private interface method
       
  1133   KlassHandle resolved_klass = link_info.resolved_klass();
       
  1134   KlassHandle current_klass = link_info.current_klass();
       
  1135 
  1115   if (resolved_klass->is_interface() && resolved_method->is_private()) {
  1136   if (resolved_klass->is_interface() && resolved_method->is_private()) {
  1116     ResourceMark rm(THREAD);
  1137     ResourceMark rm(THREAD);
  1117     char buf[200];
  1138     char buf[200];
  1118     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
  1139     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
  1119                  Method::name_and_sig_as_C_string(resolved_klass(),
  1140                  Method::name_and_sig_as_C_string(resolved_klass(),
  1120                                                   resolved_method->name(),
  1141                                                   resolved_method->name(),
  1121                                                   resolved_method->signature()),
  1142                                                   resolved_method->signature()),
  1122                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
  1143                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
  1123     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1144     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1124   }
  1145   }
  1125 
  1146 
  1126   // check if not static
  1147   // check if not static
  1127   if (resolved_method->is_static()) {
  1148   if (resolved_method->is_static()) {
  1128     ResourceMark rm(THREAD);
  1149     ResourceMark rm(THREAD);
  1129     char buf[200];
  1150     char buf[200];
  1130     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
  1151     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
  1131                                                                                                              resolved_method->name(),
  1152                                                                                                              resolved_method->name(),
  1132                                                                                                              resolved_method->signature()));
  1153                                                                                                              resolved_method->signature()));
  1133     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1154     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1134   }
  1155   }
  1135 
  1156 
  1136   if (PrintVtables && Verbose) {
  1157   if (PrintVtables && Verbose) {
  1137     ResourceMark rm(THREAD);
  1158     trace_method_resolution("invokevirtual resolved method: caller-class:",
  1138     tty->print("invokevirtual resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
  1159                             current_klass, resolved_klass, resolved_method);
  1139                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
       
  1140                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
       
  1141                    Method::name_and_sig_as_C_string(resolved_klass(),
       
  1142                                                     resolved_method->name(),
       
  1143                                                     resolved_method->signature()),
       
  1144                    resolved_method->method_holder()->internal_name()
       
  1145                   );
       
  1146     resolved_method->access_flags().print_on(tty);
       
  1147     if (resolved_method->is_default_method()) {
       
  1148       tty->print("default ");
       
  1149     }
       
  1150     if (resolved_method->is_overpass()) {
       
  1151       tty->print("overpass");
       
  1152     }
       
  1153     tty->cr();
  1160     tty->cr();
  1154   }
  1161   }
       
  1162 
       
  1163   return resolved_method;
  1155 }
  1164 }
  1156 
  1165 
  1157 // throws runtime exceptions
  1166 // throws runtime exceptions
  1158 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
  1167 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
  1159                                                   methodHandle resolved_method,
  1168                                                   const methodHandle& resolved_method,
  1160                                                   KlassHandle resolved_klass,
  1169                                                   KlassHandle resolved_klass,
  1161                                                   Handle recv,
  1170                                                   Handle recv,
  1162                                                   KlassHandle recv_klass,
  1171                                                   KlassHandle recv_klass,
  1163                                                   bool check_null_and_abstract,
  1172                                                   bool check_null_and_abstract,
  1164                                                   TRAPS) {
  1173                                                   TRAPS) {
  1225                                                       selected_method->name(),
  1234                                                       selected_method->name(),
  1226                                                       selected_method->signature()));
  1235                                                       selected_method->signature()));
  1227   }
  1236   }
  1228 
  1237 
  1229   if (PrintVtables && Verbose) {
  1238   if (PrintVtables && Verbose) {
  1230     ResourceMark rm(THREAD);
  1239     trace_method_resolution("invokevirtual selected method: receiver-class:",
  1231     tty->print("invokevirtual selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, vtable_index:%d, access_flags: ",
  1240                             recv_klass, resolved_klass, selected_method);
  1232                    (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()),
  1241     tty->print_cr("vtable_index:%d", vtable_index);
  1233                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
       
  1234                    Method::name_and_sig_as_C_string(resolved_klass(),
       
  1235                                                     resolved_method->name(),
       
  1236                                                     resolved_method->signature()),
       
  1237                    selected_method->method_holder()->internal_name(),
       
  1238                    vtable_index
       
  1239                   );
       
  1240     selected_method->access_flags().print_on(tty);
       
  1241     if (selected_method->is_default_method()) {
       
  1242       tty->print("default ");
       
  1243     }
       
  1244     if (selected_method->is_overpass()) {
       
  1245       tty->print("overpass");
       
  1246     }
       
  1247     tty->cr();
       
  1248   }
  1242   }
  1249   // setup result
  1243   // setup result
  1250   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
  1244   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
  1251 }
  1245 }
  1252 
  1246 
  1253 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass,
  1247 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass,
  1254                                           Symbol* method_name, Symbol* method_signature, KlassHandle current_klass,
  1248                                           const LinkInfo& link_info,
  1255                                           bool check_access, bool check_null_and_abstract, TRAPS) {
  1249                                           bool check_null_and_abstract, TRAPS) {
  1256   methodHandle resolved_method;
  1250   // throws linktime exceptions
  1257   linktime_resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
  1251   methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK);
  1258   runtime_resolve_interface_method(result, resolved_method, resolved_klass, recv, recv_klass, check_null_and_abstract, CHECK);
  1252   runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(),
  1259 }
  1253                                    recv, recv_klass, check_null_and_abstract, CHECK);
  1260 
  1254 }
  1261 // throws linktime exceptions
  1255 
  1262 void LinkResolver::linktime_resolve_interface_method(methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name,
  1256 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
  1263                                                      Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) {
  1257                                                              TRAPS) {
  1264   // normal interface method resolution
  1258   // normal interface method resolution
  1265   resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK);
  1259   methodHandle resolved_method = resolve_interface_method(link_info, true, CHECK_NULL);
  1266 
       
  1267   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
  1260   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
  1268   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
  1261   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
       
  1262 
       
  1263   return resolved_method;
  1269 }
  1264 }
  1270 
  1265 
  1271 // throws runtime exceptions
  1266 // throws runtime exceptions
  1272 void LinkResolver::runtime_resolve_interface_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
  1267 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
  1273                                                     Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS) {
  1268                                                     const methodHandle& resolved_method,
       
  1269                                                     KlassHandle resolved_klass,
       
  1270                                                     Handle recv,
       
  1271                                                     KlassHandle recv_klass,
       
  1272                                                     bool check_null_and_abstract, TRAPS) {
  1274   // check if receiver exists
  1273   // check if receiver exists
  1275   if (check_null_and_abstract && recv.is_null()) {
  1274   if (check_null_and_abstract && recv.is_null()) {
  1276     THROW(vmSymbols::java_lang_NullPointerException());
  1275     THROW(vmSymbols::java_lang_NullPointerException());
  1277   }
  1276   }
  1278 
  1277 
  1296                  resolved_klass()->external_name());
  1295                  resolved_klass()->external_name());
  1297     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1296     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1298   }
  1297   }
  1299 
  1298 
  1300   // do lookup based on receiver klass
  1299   // do lookup based on receiver klass
  1301   methodHandle sel_method;
       
  1302   // This search must match the linktime preparation search for itable initialization
  1300   // This search must match the linktime preparation search for itable initialization
  1303   // to correctly enforce loader constraints for interface method inheritance
  1301   // to correctly enforce loader constraints for interface method inheritance
  1304   lookup_instance_method_in_klasses(sel_method, recv_klass,
  1302   methodHandle sel_method = lookup_instance_method_in_klasses(recv_klass,
  1305             resolved_method->name(),
  1303                                                   resolved_method->name(),
  1306             resolved_method->signature(), CHECK);
  1304                                                   resolved_method->signature(), CHECK);
  1307   if (sel_method.is_null() && !check_null_and_abstract) {
  1305   if (sel_method.is_null() && !check_null_and_abstract) {
  1308     // In theory this is a harmless placeholder value, but
  1306     // In theory this is a harmless placeholder value, but
  1309     // in practice leaving in null affects the nsk default method tests.
  1307     // in practice leaving in null affects the nsk default method tests.
  1310     // This needs further study.
  1308     // This needs further study.
  1311     sel_method = resolved_method;
  1309     sel_method = resolved_method;
  1312   }
  1310   }
  1313   // check if method exists
  1311   // check if method exists
  1314   if (sel_method.is_null()) {
  1312   if (sel_method.is_null()) {
  1315     ResourceMark rm(THREAD);
  1313     ResourceMark rm(THREAD);
  1316     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
  1314     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
  1317               Method::name_and_sig_as_C_string(recv_klass(),
  1315                    Method::name_and_sig_as_C_string(recv_klass(),
  1318                                                       resolved_method->name(),
  1316                                                     resolved_method->name(),
  1319                                                       resolved_method->signature()));
  1317                                                     resolved_method->signature()));
  1320   }
  1318   }
  1321   // check access
  1319   // check access
  1322   // Throw Illegal Access Error if sel_method is not public.
  1320   // Throw Illegal Access Error if sel_method is not public.
  1323   if (!sel_method->is_public()) {
  1321   if (!sel_method->is_public()) {
  1324     ResourceMark rm(THREAD);
  1322     ResourceMark rm(THREAD);
  1335                                                       sel_method->name(),
  1333                                                       sel_method->name(),
  1336                                                       sel_method->signature()));
  1334                                                       sel_method->signature()));
  1337   }
  1335   }
  1338 
  1336 
  1339   if (TraceItables && Verbose) {
  1337   if (TraceItables && Verbose) {
  1340     ResourceMark rm(THREAD);
  1338     trace_method_resolution("invokeinterface selected method: receiver-class",
  1341     tty->print("invokeinterface selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, access_flags: ",
  1339                             recv_klass, resolved_klass, sel_method);
  1342                    (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()),
       
  1343                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
       
  1344                    Method::name_and_sig_as_C_string(resolved_klass(),
       
  1345                                                     resolved_method->name(),
       
  1346                                                     resolved_method->signature()),
       
  1347                    sel_method->method_holder()->internal_name()
       
  1348                   );
       
  1349     sel_method->access_flags().print_on(tty);
       
  1350     if (sel_method->is_default_method()) {
       
  1351       tty->print("default ");
       
  1352     }
       
  1353     if (sel_method->is_overpass()) {
       
  1354       tty->print("overpass");
       
  1355     }
       
  1356     tty->cr();
  1340     tty->cr();
  1357   }
  1341   }
  1358   // setup result
  1342   // setup result
  1359   if (!resolved_method->has_itable_index()) {
  1343   if (!resolved_method->has_itable_index()) {
  1360     int vtable_index = resolved_method->vtable_index();
  1344     int vtable_index = resolved_method->vtable_index();
  1366   }
  1350   }
  1367 }
  1351 }
  1368 
  1352 
  1369 
  1353 
  1370 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
  1354 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
  1371                                                  KlassHandle resolved_klass,
  1355                                                  const LinkInfo& link_info) {
  1372                                                  Symbol* method_name,
       
  1373                                                  Symbol* method_signature,
       
  1374                                                  KlassHandle current_klass,
       
  1375                                                  bool check_access) {
       
  1376   EXCEPTION_MARK;
  1356   EXCEPTION_MARK;
  1377   methodHandle method_result;
  1357   methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD);
  1378   linktime_resolve_interface_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
       
  1379   if (HAS_PENDING_EXCEPTION) {
  1358   if (HAS_PENDING_EXCEPTION) {
  1380     CLEAR_PENDING_EXCEPTION;
  1359     CLEAR_PENDING_EXCEPTION;
  1381     return methodHandle();
  1360     return methodHandle();
  1382   } else {
  1361   } else {
  1383     return method_result;
  1362     return method_result;
  1384   }
  1363   }
  1385 }
  1364 }
  1386 
  1365 
  1387 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
  1366 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
  1388                                                  KlassHandle resolved_klass,
  1367                                                  const LinkInfo& link_info) {
  1389                                                  Symbol* method_name,
       
  1390                                                  Symbol* method_signature,
       
  1391                                                  KlassHandle current_klass,
       
  1392                                                  bool check_access) {
       
  1393   EXCEPTION_MARK;
  1368   EXCEPTION_MARK;
  1394   methodHandle method_result;
  1369   methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD);
  1395   linktime_resolve_virtual_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
       
  1396   if (HAS_PENDING_EXCEPTION) {
  1370   if (HAS_PENDING_EXCEPTION) {
  1397     CLEAR_PENDING_EXCEPTION;
  1371     CLEAR_PENDING_EXCEPTION;
  1398     return methodHandle();
  1372     return methodHandle();
  1399   } else {
  1373   } else {
  1400     return method_result;
  1374     return method_result;
  1401   }
  1375   }
  1402 }
  1376 }
  1403 
  1377 
  1404 methodHandle LinkResolver::resolve_virtual_call_or_null(
  1378 methodHandle LinkResolver::resolve_virtual_call_or_null(
  1405                                                  KlassHandle receiver_klass,
  1379                                                  KlassHandle receiver_klass,
  1406                                                  KlassHandle resolved_klass,
  1380                                                  const LinkInfo& link_info) {
  1407                                                  Symbol* name,
       
  1408                                                  Symbol* signature,
       
  1409                                                  KlassHandle current_klass,
       
  1410                                                  bool check_access) {
       
  1411   EXCEPTION_MARK;
  1381   EXCEPTION_MARK;
  1412   CallInfo info;
  1382   CallInfo info;
  1413   resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, check_access, false, THREAD);
  1383   resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
  1414   if (HAS_PENDING_EXCEPTION) {
  1384   if (HAS_PENDING_EXCEPTION) {
  1415     CLEAR_PENDING_EXCEPTION;
  1385     CLEAR_PENDING_EXCEPTION;
  1416     return methodHandle();
  1386     return methodHandle();
  1417   }
  1387   }
  1418   return info.selected_method();
  1388   return info.selected_method();
  1419 }
  1389 }
  1420 
  1390 
  1421 methodHandle LinkResolver::resolve_interface_call_or_null(
  1391 methodHandle LinkResolver::resolve_interface_call_or_null(
  1422                                                  KlassHandle receiver_klass,
  1392                                                  KlassHandle receiver_klass,
  1423                                                  KlassHandle resolved_klass,
  1393                                                  const LinkInfo& link_info) {
  1424                                                  Symbol* name,
       
  1425                                                  Symbol* signature,
       
  1426                                                  KlassHandle current_klass,
       
  1427                                                  bool check_access) {
       
  1428   EXCEPTION_MARK;
  1394   EXCEPTION_MARK;
  1429   CallInfo info;
  1395   CallInfo info;
  1430   resolve_interface_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, check_access, false, THREAD);
  1396   resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
  1431   if (HAS_PENDING_EXCEPTION) {
  1397   if (HAS_PENDING_EXCEPTION) {
  1432     CLEAR_PENDING_EXCEPTION;
  1398     CLEAR_PENDING_EXCEPTION;
  1433     return methodHandle();
  1399     return methodHandle();
  1434   }
  1400   }
  1435   return info.selected_method();
  1401   return info.selected_method();
  1436 }
  1402 }
  1437 
  1403 
  1438 int LinkResolver::resolve_virtual_vtable_index(
  1404 int LinkResolver::resolve_virtual_vtable_index(KlassHandle receiver_klass,
  1439                                                KlassHandle receiver_klass,
  1405                                                const LinkInfo& link_info) {
  1440                                                KlassHandle resolved_klass,
       
  1441                                                Symbol* name,
       
  1442                                                Symbol* signature,
       
  1443                                                KlassHandle current_klass) {
       
  1444   EXCEPTION_MARK;
  1406   EXCEPTION_MARK;
  1445   CallInfo info;
  1407   CallInfo info;
  1446   resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
  1408   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
       
  1409                        /*check_null_or_abstract*/false, THREAD);
  1447   if (HAS_PENDING_EXCEPTION) {
  1410   if (HAS_PENDING_EXCEPTION) {
  1448     CLEAR_PENDING_EXCEPTION;
  1411     CLEAR_PENDING_EXCEPTION;
  1449     return Method::invalid_vtable_index;
  1412     return Method::invalid_vtable_index;
  1450   }
  1413   }
  1451   return info.vtable_index();
  1414   return info.vtable_index();
  1452 }
  1415 }
  1453 
  1416 
  1454 methodHandle LinkResolver::resolve_static_call_or_null(
  1417 methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
  1455                                                   KlassHandle resolved_klass,
       
  1456                                                   Symbol* name,
       
  1457                                                   Symbol* signature,
       
  1458                                                   KlassHandle current_klass,
       
  1459                                                   bool check_access) {
       
  1460   EXCEPTION_MARK;
  1418   EXCEPTION_MARK;
  1461   CallInfo info;
  1419   CallInfo info;
  1462   resolve_static_call(info, resolved_klass, name, signature, current_klass, check_access, false, THREAD);
  1420   resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
  1463   if (HAS_PENDING_EXCEPTION) {
  1421   if (HAS_PENDING_EXCEPTION) {
  1464     CLEAR_PENDING_EXCEPTION;
  1422     CLEAR_PENDING_EXCEPTION;
  1465     return methodHandle();
  1423     return methodHandle();
  1466   }
  1424   }
  1467   return info.selected_method();
  1425   return info.selected_method();
  1468 }
  1426 }
  1469 
  1427 
  1470 methodHandle LinkResolver::resolve_special_call_or_null(
  1428 methodHandle LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
  1471                                                   KlassHandle resolved_klass,
       
  1472                                                   Symbol* name,
       
  1473                                                   Symbol* signature,
       
  1474                                                   KlassHandle current_klass,
       
  1475                                                   bool check_access) {
       
  1476   EXCEPTION_MARK;
  1429   EXCEPTION_MARK;
  1477   CallInfo info;
  1430   CallInfo info;
  1478   resolve_special_call(info, resolved_klass, name, signature, current_klass, check_access, THREAD);
  1431   resolve_special_call(info, link_info, THREAD);
  1479   if (HAS_PENDING_EXCEPTION) {
  1432   if (HAS_PENDING_EXCEPTION) {
  1480     CLEAR_PENDING_EXCEPTION;
  1433     CLEAR_PENDING_EXCEPTION;
  1481     return methodHandle();
  1434     return methodHandle();
  1482   }
  1435   }
  1483   return info.selected_method();
  1436   return info.selected_method();
  1498     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
  1451     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
  1499   }
  1452   }
  1500   return;
  1453   return;
  1501 }
  1454 }
  1502 
  1455 
  1503 void LinkResolver::resolve_pool(KlassHandle& resolved_klass, Symbol*& method_name, Symbol*& method_signature,
       
  1504                                 KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS) {
       
  1505    // resolve klass
       
  1506   resolve_klass(resolved_klass, pool, index, CHECK);
       
  1507 
       
  1508   // Get name, signature, and static klass
       
  1509   method_name      = pool->name_ref_at(index);
       
  1510   method_signature = pool->signature_ref_at(index);
       
  1511   current_klass    = KlassHandle(THREAD, pool->pool_holder());
       
  1512 }
       
  1513 
       
  1514 
       
  1515 void LinkResolver::resolve_invokestatic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
  1456 void LinkResolver::resolve_invokestatic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
  1516   KlassHandle  resolved_klass;
  1457   LinkInfo link_info(pool, index, CHECK);
  1517   Symbol* method_name = NULL;
  1458   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
  1518   Symbol* method_signature = NULL;
       
  1519   KlassHandle  current_klass;
       
  1520   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
       
  1521   resolve_static_call(result, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
       
  1522 }
  1459 }
  1523 
  1460 
  1524 
  1461 
  1525 void LinkResolver::resolve_invokespecial(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
  1462 void LinkResolver::resolve_invokespecial(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
  1526   KlassHandle  resolved_klass;
  1463   LinkInfo link_info(pool, index, CHECK);
  1527   Symbol* method_name = NULL;
  1464   resolve_special_call(result, link_info, CHECK);
  1528   Symbol* method_signature = NULL;
       
  1529   KlassHandle  current_klass;
       
  1530   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
       
  1531   resolve_special_call(result, resolved_klass, method_name, method_signature, current_klass, true, CHECK);
       
  1532 }
  1465 }
  1533 
  1466 
  1534 
  1467 
  1535 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
  1468 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
  1536                                           constantPoolHandle pool, int index,
  1469                                           constantPoolHandle pool, int index,
  1537                                           TRAPS) {
  1470                                           TRAPS) {
  1538 
  1471 
  1539   KlassHandle  resolved_klass;
  1472   LinkInfo link_info(pool, index, CHECK);
  1540   Symbol* method_name = NULL;
       
  1541   Symbol* method_signature = NULL;
       
  1542   KlassHandle  current_klass;
       
  1543   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
       
  1544   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
  1473   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
  1545   resolve_virtual_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
  1474   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
  1546 }
  1475 }
  1547 
  1476 
  1548 
  1477 
  1549 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) {
  1478 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) {
  1550   KlassHandle  resolved_klass;
  1479   LinkInfo link_info(pool, index, CHECK);
  1551   Symbol* method_name = NULL;
       
  1552   Symbol* method_signature = NULL;
       
  1553   KlassHandle  current_klass;
       
  1554   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
       
  1555   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
  1480   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
  1556   resolve_interface_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
  1481   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
  1557 }
  1482 }
  1558 
  1483 
  1559 
  1484 
  1560 void LinkResolver::resolve_invokehandle(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
  1485 void LinkResolver::resolve_invokehandle(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
  1561   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
  1486   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
  1562   KlassHandle  resolved_klass;
  1487   LinkInfo link_info(pool, index, CHECK);
  1563   Symbol* method_name = NULL;
       
  1564   Symbol* method_signature = NULL;
       
  1565   KlassHandle  current_klass;
       
  1566   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
       
  1567   if (TraceMethodHandles) {
  1488   if (TraceMethodHandles) {
  1568     ResourceMark rm(THREAD);
  1489     ResourceMark rm(THREAD);
  1569     tty->print_cr("resolve_invokehandle %s %s", method_name->as_C_string(), method_signature->as_C_string());
  1490     tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
  1570   }
  1491                   link_info.signature()->as_C_string());
  1571   resolve_handle_call(result, resolved_klass, method_name, method_signature, current_klass, CHECK);
  1492   }
  1572 }
  1493   resolve_handle_call(result, link_info, CHECK);
  1573 
  1494 }
  1574 void LinkResolver::resolve_handle_call(CallInfo& result, KlassHandle resolved_klass,
  1495 
  1575                                        Symbol* method_name, Symbol* method_signature,
  1496 void LinkResolver::resolve_handle_call(CallInfo& result,
  1576                                        KlassHandle current_klass,
  1497                                        const LinkInfo& link_info,
  1577                                        TRAPS) {
  1498                                        TRAPS) {
  1578   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
  1499   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
  1579   assert(resolved_klass() == SystemDictionary::MethodHandle_klass(), "");
  1500   assert(link_info.resolved_klass()() == SystemDictionary::MethodHandle_klass(), "");
  1580   assert(MethodHandles::is_signature_polymorphic_name(method_name), "");
  1501   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
  1581   methodHandle resolved_method;
       
  1582   Handle       resolved_appendix;
  1502   Handle       resolved_appendix;
  1583   Handle       resolved_method_type;
  1503   Handle       resolved_method_type;
  1584   lookup_polymorphic_method(resolved_method, resolved_klass,
  1504   methodHandle resolved_method = lookup_polymorphic_method(link_info,
  1585                             method_name, method_signature,
  1505                                        &resolved_appendix, &resolved_method_type, CHECK);
  1586                             current_klass, &resolved_appendix, &resolved_method_type, CHECK);
       
  1587   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK);
  1506   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK);
  1588 }
  1507 }
  1589 
  1508 
  1590 static void wrap_invokedynamic_exception(TRAPS) {
  1509 static void wrap_invokedynamic_exception(TRAPS) {
  1591   if (HAS_PENDING_EXCEPTION) {
  1510   if (HAS_PENDING_EXCEPTION) {
  1607     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
  1526     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
  1608   }
  1527   }
  1609 }
  1528 }
  1610 
  1529 
  1611 void LinkResolver::resolve_invokedynamic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
  1530 void LinkResolver::resolve_invokedynamic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
  1612   //resolve_pool(<resolved_klass>, method_name, method_signature, current_klass, pool, index, CHECK);
       
  1613   Symbol* method_name       = pool->name_ref_at(index);
  1531   Symbol* method_name       = pool->name_ref_at(index);
  1614   Symbol* method_signature  = pool->signature_ref_at(index);
  1532   Symbol* method_signature  = pool->signature_ref_at(index);
  1615   KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder());
  1533   KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder());
  1616 
  1534 
  1617   // Resolve the bootstrap specifier (BSM + optional arguments).
  1535   // Resolve the bootstrap specifier (BSM + optional arguments).
  1665   wrap_invokedynamic_exception(CHECK);
  1583   wrap_invokedynamic_exception(CHECK);
  1666   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);
  1584   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);
  1667   wrap_invokedynamic_exception(CHECK);
  1585   wrap_invokedynamic_exception(CHECK);
  1668 }
  1586 }
  1669 
  1587 
  1670 //------------------------------------------------------------------------------------------------------------------------
       
  1671 #ifndef PRODUCT
  1588 #ifndef PRODUCT
  1672 
  1589 void LinkResolver::trace_method_resolution(const char* prefix,
  1673 void CallInfo::print() {
  1590                                            KlassHandle klass,
       
  1591                                            KlassHandle resolved_klass,
       
  1592                                            const methodHandle& method) {
  1674   ResourceMark rm;
  1593   ResourceMark rm;
  1675   const char* kindstr = "unknown";
  1594   tty->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
  1676   switch (_call_kind) {
  1595              prefix,
  1677   case direct_call: kindstr = "direct"; break;
  1596              (klass.is_null() ? "<NULL>" : klass->internal_name()),
  1678   case vtable_call: kindstr = "vtable"; break;
  1597              (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
  1679   case itable_call: kindstr = "itable"; break;
  1598              Method::name_and_sig_as_C_string(resolved_klass(),
  1680   }
  1599                                               method->name(),
  1681   tty->print_cr("Call %s@%d %s", kindstr, _call_index,
  1600                                               method->signature()),
  1682                 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
  1601              method->method_holder()->internal_name()
  1683 }
  1602              );
  1684 
  1603   method->access_flags().print_on(tty);
  1685 #endif
  1604   if (method->is_default_method()) {
       
  1605     tty->print("default ");
       
  1606   }
       
  1607   if (method->is_overpass()) {
       
  1608     tty->print("overpass ");
       
  1609   }
       
  1610 }
       
  1611 #endif // PRODUCT