1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/defaultMethods.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "compiler/compileBroker.hpp"
  30 #include "gc_interface/collectedHeap.inline.hpp"
  31 #include "interpreter/bytecode.hpp"
  32 #include "interpreter/interpreterRuntime.hpp"
  33 #include "interpreter/linkResolver.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "memory/universe.inline.hpp"
  36 #include "oops/instanceKlass.hpp"
  37 #include "oops/objArrayOop.hpp"
  38 #include "prims/methodHandles.hpp"
  39 #include "prims/nativeLookup.hpp"
  40 #include "runtime/compilationPolicy.hpp"
  41 #include "runtime/fieldDescriptor.hpp"
  42 #include "runtime/frame.inline.hpp"
  43 #include "runtime/handles.inline.hpp"
  44 #include "runtime/reflection.hpp"
  45 #include "runtime/signature.hpp"
  46 #include "runtime/thread.inline.hpp"
  47 #include "runtime/vmThread.hpp"
  48 
  49 
  50 //------------------------------------------------------------------------------------------------------------------------
  51 // Implementation of CallInfo
  52 
  53 
  54 void CallInfo::set_static(KlassHandle resolved_klass, methodHandle resolved_method, TRAPS) {
  55   int vtable_index = Method::nonvirtual_vtable_index;
  56   set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
  57 }
  58 
  59 
  60 void CallInfo::set_interface(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int itable_index, TRAPS) {
  61   // This is only called for interface methods. If the resolved_method
  62   // comes from java/lang/Object, it can be the subject of a virtual call, so
  63   // we should pick the vtable index from the resolved method.
  64   // In that case, the caller must call set_virtual instead of set_interface.
  65   assert(resolved_method->method_holder()->is_interface(), "");
  66   assert(itable_index == resolved_method()->itable_index(), "");
  67   set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);
  68 }
  69 
  70 void CallInfo::set_virtual(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index, TRAPS) {
  71   assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");
  72   assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");
  73   CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);
  74   set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK);
  75   assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");
  76 }
  77 
  78 void CallInfo::set_handle(methodHandle resolved_method, Handle resolved_appendix, Handle resolved_method_type, TRAPS) {
  79   if (resolved_method.is_null()) {
  80     THROW_MSG(vmSymbols::java_lang_InternalError(), "resolved method is null");
  81   }
  82   KlassHandle resolved_klass = SystemDictionary::MethodHandle_klass();
  83   assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
  84          resolved_method->is_compiled_lambda_form(),
  85          "linkMethod must return one of these");
  86   int vtable_index = Method::nonvirtual_vtable_index;
  87   assert(!resolved_method->has_vtable_index(), "");
  88   set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
  89   _resolved_appendix    = resolved_appendix;
  90   _resolved_method_type = resolved_method_type;
  91 }
  92 
  93 void CallInfo::set_common(KlassHandle resolved_klass,
  94                           KlassHandle selected_klass,
  95                           methodHandle resolved_method,
  96                           methodHandle selected_method,
  97                           CallKind kind,
  98                           int index,
  99                           TRAPS) {
 100   assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
 101   _resolved_klass  = resolved_klass;
 102   _selected_klass  = selected_klass;
 103   _resolved_method = resolved_method;
 104   _selected_method = selected_method;
 105   _call_kind       = kind;
 106   _call_index      = index;
 107   _resolved_appendix = Handle();
 108   DEBUG_ONLY(verify());  // verify before making side effects
 109 
 110   if (CompilationPolicy::must_be_compiled(selected_method)) {
 111     // This path is unusual, mostly used by the '-Xcomp' stress test mode.
 112 
 113     // Note: with several active threads, the must_be_compiled may be true
 114     //       while can_be_compiled is false; remove assert
 115     // assert(CompilationPolicy::can_be_compiled(selected_method), "cannot compile");
 116     if (THREAD->is_Compiler_thread()) {
 117       // don't force compilation, resolve was on behalf of compiler
 118       return;
 119     }
 120     if (selected_method->method_holder()->is_not_initialized()) {
 121       // 'is_not_initialized' means not only '!is_initialized', but also that
 122       // initialization has not been started yet ('!being_initialized')
 123       // Do not force compilation of methods in uninitialized classes.
 124       // Note that doing this would throw an assert later,
 125       // in CompileBroker::compile_method.
 126       // We sometimes use the link resolver to do reflective lookups
 127       // even before classes are initialized.
 128       return;
 129     }
 130     CompileBroker::compile_method(selected_method, InvocationEntryBci,
 131                                   CompilationPolicy::policy()->initial_compile_level(),
 132                                   methodHandle(), 0, "must_be_compiled", CHECK);
 133   }
 134 }
 135 
 136 // utility query for unreflecting a method
 137 CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass) {
 138   Klass* resolved_method_holder = resolved_method->method_holder();
 139   if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st
 140     resolved_klass = resolved_method_holder;
 141   }
 142   _resolved_klass  = resolved_klass;
 143   _selected_klass  = resolved_klass;
 144   _resolved_method = resolved_method;
 145   _selected_method = resolved_method;
 146   // classify:
 147   CallKind kind = CallInfo::unknown_kind;
 148   int index = resolved_method->vtable_index();
 149   if (resolved_method->can_be_statically_bound()) {
 150     kind = CallInfo::direct_call;
 151   } else if (!resolved_method_holder->is_interface()) {
 152     // Could be an Object method inherited into an interface, but still a vtable call.
 153     kind = CallInfo::vtable_call;
 154   } else if (!resolved_klass->is_interface()) {
 155     // A default or miranda method.  Compute the vtable index.
 156     ResourceMark rm;
 157     klassVtable* vt = InstanceKlass::cast(resolved_klass)->vtable();
 158     index = LinkResolver::vtable_index_of_interface_method(resolved_klass,
 159                            resolved_method);
 160     assert(index >= 0 , "we should have valid vtable index at this point");
 161 
 162     kind = CallInfo::vtable_call;
 163   } else if (resolved_method->has_vtable_index()) {
 164     // Can occur if an interface redeclares a method of Object.
 165 
 166 #ifdef ASSERT
 167     // Ensure that this is really the case.
 168     KlassHandle object_klass = SystemDictionary::Object_klass();
 169     Method * object_resolved_method = object_klass()->vtable()->method_at(index);
 170     assert(object_resolved_method->name() == resolved_method->name(),
 171       err_msg("Object and interface method names should match at vtable index %d, %s != %s",
 172       index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string()));
 173     assert(object_resolved_method->signature() == resolved_method->signature(),
 174       err_msg("Object and interface method signatures should match at vtable index %d, %s != %s",
 175       index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string()));
 176 #endif // ASSERT
 177 
 178     kind = CallInfo::vtable_call;
 179   } else {
 180     // A regular interface call.
 181     kind = CallInfo::itable_call;
 182     index = resolved_method->itable_index();
 183   }
 184   assert(index == Method::nonvirtual_vtable_index || index >= 0, err_msg("bad index %d", index));
 185   _call_kind  = kind;
 186   _call_index = index;
 187   _resolved_appendix = Handle();
 188   DEBUG_ONLY(verify());
 189 }
 190 
 191 #ifdef ASSERT
 192 void CallInfo::verify() {
 193   switch (call_kind()) {  // the meaning and allowed value of index depends on kind
 194   case CallInfo::direct_call:
 195     if (_call_index == Method::nonvirtual_vtable_index)  break;
 196     // else fall through to check vtable index:
 197   case CallInfo::vtable_call:
 198     assert(resolved_klass()->verify_vtable_index(_call_index), "");
 199     break;
 200   case CallInfo::itable_call:
 201     assert(resolved_method()->method_holder()->verify_itable_index(_call_index), "");
 202     break;
 203   case CallInfo::unknown_kind:
 204     assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set");
 205     break;
 206   default:
 207     fatal(err_msg_res("Unexpected call kind %d", call_kind()));
 208   }
 209 }
 210 #endif //ASSERT
 211 
 212 
 213 
 214 //------------------------------------------------------------------------------------------------------------------------
 215 // Klass resolution
 216 
 217 void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) {
 218   if (!Reflection::verify_class_access(ref_klass(),
 219                                        sel_klass(),
 220                                        true)) {
 221     ResourceMark rm(THREAD);
 222     Exceptions::fthrow(
 223       THREAD_AND_LOCATION,
 224       vmSymbols::java_lang_IllegalAccessError(),
 225       "tried to access class %s from class %s",
 226       sel_klass->external_name(),
 227       ref_klass->external_name()
 228     );
 229     return;
 230   }
 231 }
 232 
 233 void LinkResolver::resolve_klass(KlassHandle& result, constantPoolHandle pool, int index, TRAPS) {
 234   Klass* result_oop = pool->klass_ref_at(index, CHECK);
 235   result = KlassHandle(THREAD, result_oop);
 236 }
 237 
 238 //------------------------------------------------------------------------------------------------------------------------
 239 // Method resolution
 240 //
 241 // According to JVM spec. $5.4.3c & $5.4.3d
 242 
 243 // Look up method in klasses, including static methods
 244 // Then look up local default methods
 245 void LinkResolver::lookup_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) {
 246   Method* result_oop = klass->uncached_lookup_method(name, signature);
 247   if (result_oop == NULL) {
 248     Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();
 249     if (default_methods != NULL) {
 250       result_oop = InstanceKlass::find_method(default_methods, name, signature);
 251     }
 252   }
 253 
 254   if (EnableInvokeDynamic && result_oop != NULL) {
 255     vmIntrinsics::ID iid = result_oop->intrinsic_id();
 256     if (MethodHandles::is_signature_polymorphic(iid)) {
 257       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
 258       return;
 259     }
 260   }
 261   result = methodHandle(THREAD, result_oop);
 262 }
 263 
 264 // returns first instance method
 265 // Looks up method in classes, then looks up local default methods
 266 void LinkResolver::lookup_instance_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) {
 267   Method* result_oop = klass->uncached_lookup_method(name, signature);
 268   result = methodHandle(THREAD, result_oop);
 269   while (!result.is_null() && result->is_static() && result->method_holder()->super() != NULL) {
 270     klass = KlassHandle(THREAD, result->method_holder()->super());
 271     result = methodHandle(THREAD, klass->uncached_lookup_method(name, signature));
 272   }
 273 
 274   if (result.is_null()) {
 275     Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();
 276     if (default_methods != NULL) {
 277       result = methodHandle(InstanceKlass::find_method(default_methods, name, signature));
 278       assert(result.is_null() || !result->is_static(), "static defaults not allowed");
 279     }
 280   }
 281 }
 282 
 283 int LinkResolver::vtable_index_of_interface_method(KlassHandle klass,
 284                                           methodHandle resolved_method) {
 285 
 286   int vtable_index = Method::invalid_vtable_index;
 287   Symbol* name = resolved_method->name();
 288   Symbol* signature = resolved_method->signature();
 289 
 290   // First check in default method array
 291   if (!resolved_method->is_abstract()  &&
 292     (InstanceKlass::cast(klass())->default_methods() != NULL)) {
 293     int index = InstanceKlass::find_method_index(InstanceKlass::cast(klass())->default_methods(), name, signature);
 294     if (index >= 0 ) {
 295       vtable_index = InstanceKlass::cast(klass())->default_vtable_indices()->at(index);
 296     }
 297   }
 298   if (vtable_index == Method::invalid_vtable_index) {
 299     // get vtable_index for miranda methods
 300     ResourceMark rm;
 301     klassVtable *vt = InstanceKlass::cast(klass())->vtable();
 302     vtable_index = vt->index_of_miranda(name, signature);
 303   }
 304   return vtable_index;
 305 }
 306 
 307 void LinkResolver::lookup_method_in_interfaces(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) {
 308   InstanceKlass *ik = InstanceKlass::cast(klass());
 309   result = methodHandle(THREAD, ik->lookup_method_in_all_interfaces(name, signature));
 310 }
 311 
 312 void LinkResolver::lookup_polymorphic_method(methodHandle& result,
 313                                              KlassHandle klass, Symbol* name, Symbol* full_signature,
 314                                              KlassHandle current_klass,
 315                                              Handle *appendix_result_or_null,
 316                                              Handle *method_type_result,
 317                                              TRAPS) {
 318   vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
 319   if (TraceMethodHandles) {
 320     ResourceMark rm(THREAD);
 321     tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
 322                   vmIntrinsics::name_at(iid), klass->external_name(),
 323                   name->as_C_string(), full_signature->as_C_string());
 324   }
 325   if (EnableInvokeDynamic &&
 326       klass() == SystemDictionary::MethodHandle_klass() &&
 327       iid != vmIntrinsics::_none) {
 328     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
 329       // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
 330       // Do not erase last argument type (MemberName) if it is a static linkTo method.
 331       bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
 332       TempNewSymbol basic_signature =
 333         MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK);
 334       if (TraceMethodHandles) {
 335         ResourceMark rm(THREAD);
 336         tty->print_cr("lookup_polymorphic_method %s %s => basic %s",
 337                       name->as_C_string(),
 338                       full_signature->as_C_string(),
 339                       basic_signature->as_C_string());
 340       }
 341       result = SystemDictionary::find_method_handle_intrinsic(iid,
 342                                                               basic_signature,
 343                                                               CHECK);
 344       if (result.not_null()) {
 345         assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
 346         assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
 347         assert(basic_signature == result->signature(), "predict the result signature");
 348         if (TraceMethodHandles) {
 349           tty->print("lookup_polymorphic_method => intrinsic ");
 350           result->print_on(tty);
 351         }
 352         return;
 353       }
 354     } else if (iid == vmIntrinsics::_invokeGeneric
 355                && !THREAD->is_Compiler_thread()
 356                && appendix_result_or_null != NULL) {
 357       // This is a method with type-checking semantics.
 358       // We will ask Java code to spin an adapter method for it.
 359       if (!MethodHandles::enabled()) {
 360         // Make sure the Java part of the runtime has been booted up.
 361         Klass* natives = SystemDictionary::MethodHandleNatives_klass();
 362         if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {
 363           SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
 364                                             Handle(),
 365                                             Handle(),
 366                                             true,
 367                                             CHECK);
 368         }
 369       }
 370 
 371       Handle appendix;
 372       Handle method_type;
 373       result = SystemDictionary::find_method_handle_invoker(name,
 374                                                             full_signature,
 375                                                             current_klass,
 376                                                             &appendix,
 377                                                             &method_type,
 378                                                             CHECK);
 379       if (TraceMethodHandles) {
 380         tty->print("lookup_polymorphic_method => (via Java) ");
 381         result->print_on(tty);
 382         tty->print("  lookup_polymorphic_method => appendix = ");
 383         if (appendix.is_null())  tty->print_cr("(none)");
 384         else                     appendix->print_on(tty);
 385       }
 386       if (result.not_null()) {
 387 #ifdef ASSERT
 388         ResourceMark rm(THREAD);
 389 
 390         TempNewSymbol basic_signature =
 391           MethodHandles::lookup_basic_type_signature(full_signature, CHECK);
 392         int actual_size_of_params = result->size_of_parameters();
 393         int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
 394         // +1 for MethodHandle.this, +1 for trailing MethodType
 395         if (!MethodHandles::is_signature_polymorphic_static(iid))  expected_size_of_params += 1;
 396         if (appendix.not_null())                                   expected_size_of_params += 1;
 397         if (actual_size_of_params != expected_size_of_params) {
 398           tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
 399           tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
 400           result->print();
 401         }
 402         assert(actual_size_of_params == expected_size_of_params,
 403                err_msg("%d != %d", actual_size_of_params, expected_size_of_params));
 404 #endif //ASSERT
 405 
 406         assert(appendix_result_or_null != NULL, "");
 407         (*appendix_result_or_null) = appendix;
 408         (*method_type_result)      = method_type;
 409         return;
 410       }
 411     }
 412   }
 413 }
 414 
 415 void LinkResolver::check_method_accessability(KlassHandle ref_klass,
 416                                               KlassHandle resolved_klass,
 417                                               KlassHandle sel_klass,
 418                                               methodHandle sel_method,
 419                                               TRAPS) {
 420 
 421   AccessFlags flags = sel_method->access_flags();
 422 
 423   // Special case #1:  arrays always override "clone". JVMS 2.15.
 424   // If the resolved klass is an array class, and the declaring class
 425   // is java.lang.Object and the method is "clone", set the flags
 426   // to public.
 427   // Special case #2:  If the resolved klass is an interface, and
 428   // the declaring class is java.lang.Object, and the method is
 429   // "clone" or "finalize", set the flags to public. If the
 430   // resolved interface does not contain "clone" or "finalize"
 431   // methods, the method/interface method resolution looks to
 432   // the interface's super class, java.lang.Object.  With JDK 8
 433   // interface accessability check requirement, special casing
 434   // this scenario is necessary to avoid an IAE.
 435   //
 436   // We'll check for each method name first and then java.lang.Object
 437   // to best short-circuit out of these tests.
 438   if (((sel_method->name() == vmSymbols::clone_name() &&
 439         (resolved_klass->oop_is_array() || resolved_klass->is_interface())) ||
 440        (sel_method->name() == vmSymbols::finalize_method_name() &&
 441         resolved_klass->is_interface())) &&
 442       sel_klass() == SystemDictionary::Object_klass()) {
 443     // We need to change "protected" to "public".
 444     assert(flags.is_protected(), "clone or finalize not protected?");
 445     jint new_flags = flags.as_int();
 446     new_flags = new_flags & (~JVM_ACC_PROTECTED);
 447     new_flags = new_flags | JVM_ACC_PUBLIC;
 448     flags.set_flags(new_flags);
 449   }
 450 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
 451 
 452   if (!Reflection::verify_field_access(ref_klass(),
 453                                        resolved_klass(),
 454                                        sel_klass(),
 455                                        flags,
 456                                        true)) {
 457     ResourceMark rm(THREAD);
 458     Exceptions::fthrow(
 459       THREAD_AND_LOCATION,
 460       vmSymbols::java_lang_IllegalAccessError(),
 461       "tried to access method %s.%s%s from class %s",
 462       sel_klass->external_name(),
 463       sel_method->name()->as_C_string(),
 464       sel_method->signature()->as_C_string(),
 465       ref_klass->external_name()
 466     );
 467     return;
 468   }
 469 }
 470 
 471 void LinkResolver::resolve_method_statically(methodHandle& resolved_method, KlassHandle& resolved_klass,
 472                                              Bytecodes::Code code, constantPoolHandle pool, int index, TRAPS) {
 473   // This method is used only
 474   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 475   // and
 476   // (2) in Bytecode_invoke::static_target
 477   // It appears to fail when applied to an invokeinterface call site.
 478   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
 479   // resolve klass
 480   if (code == Bytecodes::_invokedynamic) {
 481     resolved_klass = SystemDictionary::MethodHandle_klass();
 482     Symbol* method_name = vmSymbols::invoke_name();
 483     Symbol* method_signature = pool->signature_ref_at(index);
 484     KlassHandle  current_klass(THREAD, pool->pool_holder());
 485     resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK);
 486     return;
 487   }
 488 
 489   resolve_klass(resolved_klass, pool, index, CHECK);
 490 
 491   Symbol*  method_name       = pool->name_ref_at(index);
 492   Symbol*  method_signature  = pool->signature_ref_at(index);
 493   KlassHandle  current_klass(THREAD, pool->pool_holder());
 494 
 495   if (pool->has_preresolution()
 496       || (resolved_klass() == SystemDictionary::MethodHandle_klass() &&
 497           MethodHandles::is_signature_polymorphic_name(resolved_klass(), method_name))) {
 498     Method* result_oop = ConstantPool::method_at_if_loaded(pool, index);
 499     if (result_oop != NULL) {
 500       resolved_method = methodHandle(THREAD, result_oop);
 501       return;
 502     }
 503   }
 504 
 505   if (code == Bytecodes::_invokeinterface) {
 506     resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, CHECK);
 507   } else if (code == Bytecodes::_invokevirtual) {
 508     resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
 509   } else {
 510     resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK);
 511   }
 512 }
 513 
 514 void LinkResolver::resolve_method(methodHandle& resolved_method, KlassHandle resolved_klass,
 515                                   Symbol* method_name, Symbol* method_signature,
 516                                   KlassHandle current_klass, bool check_access,
 517                                   bool require_methodref, TRAPS) {
 518 
 519   Handle nested_exception;
 520 
 521   // 1. check if methodref required, that resolved_klass is not interfacemethodref
 522   if (require_methodref && resolved_klass->is_interface()) {
 523     ResourceMark rm(THREAD);
 524     char buf[200];
 525     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 526         resolved_klass()->external_name());
 527     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 528   }
 529 
 530   // 2. lookup method in resolved klass and its super klasses
 531   lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, CHECK);
 532 
 533   if (resolved_method.is_null()) { // not found in the class hierarchy
 534     // 3. lookup method in all the interfaces implemented by the resolved klass
 535     lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);
 536 
 537     if (resolved_method.is_null()) {
 538       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 539       lookup_polymorphic_method(resolved_method, resolved_klass, method_name, method_signature,
 540                                 current_klass, (Handle*)NULL, (Handle*)NULL, THREAD);
 541       if (HAS_PENDING_EXCEPTION) {
 542         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 543         CLEAR_PENDING_EXCEPTION;
 544       }
 545     }
 546 
 547     if (resolved_method.is_null()) {
 548       // 4. method lookup failed
 549       ResourceMark rm(THREAD);
 550       THROW_MSG_CAUSE(vmSymbols::java_lang_NoSuchMethodError(),
 551                       Method::name_and_sig_as_C_string(resolved_klass(),
 552                                                               method_name,
 553                                                               method_signature),
 554                       nested_exception);
 555     }
 556   }
 557 
 558   // 5. check if method is concrete
 559   if (resolved_method->is_abstract() && !resolved_klass->is_abstract()) {
 560     ResourceMark rm(THREAD);
 561     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
 562               Method::name_and_sig_as_C_string(resolved_klass(),
 563                                                       method_name,
 564                                                       method_signature));
 565   }
 566 
 567   // 6. access checks, access checking may be turned off when calling from within the VM.
 568   if (check_access) {
 569     assert(current_klass.not_null() , "current_klass should not be null");
 570 
 571     // check if method can be accessed by the referring class
 572     check_method_accessability(current_klass,
 573                                resolved_klass,
 574                                KlassHandle(THREAD, resolved_method->method_holder()),
 575                                resolved_method,
 576                                CHECK);
 577 
 578     // check loader constraints
 579     Handle loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());
 580     Handle class_loader (THREAD, resolved_method->method_holder()->class_loader());
 581     {
 582       ResourceMark rm(THREAD);
 583       Symbol* failed_type_symbol =
 584         SystemDictionary::check_signature_loaders(method_signature, loader,
 585                                                   class_loader, true, CHECK);
 586       if (failed_type_symbol != NULL) {
 587         const char* msg = "loader constraint violation: when resolving method"
 588           " \"%s\" the class loader (instance of %s) of the current class, %s,"
 589           " and the class loader (instance of %s) for the method's defining class, %s, have"
 590           " different Class objects for the type %s used in the signature";
 591         char* sig = Method::name_and_sig_as_C_string(resolved_klass(),method_name,method_signature);
 592         const char* loader1 = SystemDictionary::loader_name(loader());
 593         char* current = InstanceKlass::cast(current_klass())->name()->as_C_string();
 594         const char* loader2 = SystemDictionary::loader_name(class_loader());
 595         char* target = InstanceKlass::cast(resolved_method->method_holder())
 596                        ->name()->as_C_string();
 597         char* failed_type_name = failed_type_symbol->as_C_string();
 598         size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
 599           strlen(current) + strlen(loader2) + strlen(target) +
 600           strlen(failed_type_name) + 1;
 601         char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 602         jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
 603                      target, failed_type_name);
 604         THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 605       }
 606     }
 607   }
 608 }
 609 
 610 void LinkResolver::resolve_interface_method(methodHandle& resolved_method,
 611                                             KlassHandle resolved_klass,
 612                                             Symbol* method_name,
 613                                             Symbol* method_signature,
 614                                             KlassHandle current_klass,
 615                                             bool check_access, TRAPS) {
 616 
 617  // check if klass is interface
 618   if (!resolved_klass->is_interface()) {
 619     ResourceMark rm(THREAD);
 620     char buf[200];
 621     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name());
 622     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 623   }
 624 
 625   // lookup method in this interface or its super, java.lang.Object
 626   lookup_instance_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, CHECK);
 627 
 628   if (resolved_method.is_null()) {
 629     // lookup method in all the super-interfaces
 630     lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);
 631     if (resolved_method.is_null()) {
 632       // no method found
 633       ResourceMark rm(THREAD);
 634       THROW_MSG(vmSymbols::java_lang_NoSuchMethodError(),
 635                 Method::name_and_sig_as_C_string(resolved_klass(),
 636                                                         method_name,
 637                                                         method_signature));
 638     }
 639   }
 640 
 641   if (check_access) {
 642     // JDK8 adds non-public interface methods, and accessability check requirement
 643     assert(current_klass.not_null() , "current_klass should not be null");
 644 
 645     // check if method can be accessed by the referring class
 646     check_method_accessability(current_klass,
 647                                resolved_klass,
 648                                KlassHandle(THREAD, resolved_method->method_holder()),
 649                                resolved_method,
 650                                CHECK);
 651 
 652     HandleMark hm(THREAD);
 653     Handle loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());
 654     Handle class_loader (THREAD, resolved_method->method_holder()->class_loader());
 655     {
 656       ResourceMark rm(THREAD);
 657       Symbol* failed_type_symbol =
 658         SystemDictionary::check_signature_loaders(method_signature, loader,
 659                                                   class_loader, true, CHECK);
 660       if (failed_type_symbol != NULL) {
 661         const char* msg = "loader constraint violation: when resolving "
 662           "interface method \"%s\" the class loader (instance of %s) of the "
 663           "current class, %s, and the class loader (instance of %s) for "
 664           "the method's defining class, %s, have different Class objects for the type %s "
 665           "used in the signature";
 666         char* sig = Method::name_and_sig_as_C_string(resolved_klass(),method_name,method_signature);
 667         const char* loader1 = SystemDictionary::loader_name(loader());
 668         char* current = InstanceKlass::cast(current_klass())->name()->as_C_string();
 669         const char* loader2 = SystemDictionary::loader_name(class_loader());
 670         char* target = InstanceKlass::cast(resolved_method->method_holder())
 671                        ->name()->as_C_string();
 672         char* failed_type_name = failed_type_symbol->as_C_string();
 673         size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
 674           strlen(current) + strlen(loader2) + strlen(target) +
 675           strlen(failed_type_name) + 1;
 676         char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 677         jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
 678                      target, failed_type_name);
 679         THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 680       }
 681     }
 682   }
 683 
 684   if (TraceItables && Verbose) {
 685     ResourceMark rm(THREAD);
 686     tty->print("invokeinterface resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 687                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
 688                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
 689                    Method::name_and_sig_as_C_string(resolved_klass(),
 690                                                     resolved_method->name(),
 691                                                     resolved_method->signature()),
 692                    resolved_method->method_holder()->internal_name()
 693                   );
 694     resolved_method->access_flags().print_on(tty);
 695     if (resolved_method->is_default_method()) {
 696       tty->print("default ");
 697     }
 698     if (resolved_method->is_overpass()) {
 699       tty->print("overpass");
 700     }
 701     tty->cr();
 702   }
 703 }
 704 
 705 //------------------------------------------------------------------------------------------------------------------------
 706 // Field resolution
 707 
 708 void LinkResolver::check_field_accessability(KlassHandle ref_klass,
 709                                              KlassHandle resolved_klass,
 710                                              KlassHandle sel_klass,
 711                                              fieldDescriptor& fd,
 712                                              TRAPS) {
 713   if (!Reflection::verify_field_access(ref_klass(),
 714                                        resolved_klass(),
 715                                        sel_klass(),
 716                                        fd.access_flags(),
 717                                        true)) {
 718     ResourceMark rm(THREAD);
 719     Exceptions::fthrow(
 720       THREAD_AND_LOCATION,
 721       vmSymbols::java_lang_IllegalAccessError(),
 722       "tried to access field %s.%s from class %s",
 723       sel_klass->external_name(),
 724       fd.name()->as_C_string(),
 725       ref_klass->external_name()
 726     );
 727     return;
 728   }
 729 }
 730 
 731 void LinkResolver::resolve_field_access(fieldDescriptor& result, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {
 732   // Load these early in case the resolve of the containing klass fails
 733   Symbol* field = pool->name_ref_at(index);
 734   Symbol* sig   = pool->signature_ref_at(index);
 735 
 736   // resolve specified klass
 737   KlassHandle resolved_klass;
 738   resolve_klass(resolved_klass, pool, index, CHECK);
 739 
 740   KlassHandle  current_klass(THREAD, pool->pool_holder());
 741   resolve_field(result, resolved_klass, field, sig, current_klass, byte, true, true, CHECK);
 742 }
 743 
 744 void LinkResolver::resolve_field(fieldDescriptor& fd, KlassHandle resolved_klass, Symbol* field, Symbol* sig,
 745                                  KlassHandle current_klass, Bytecodes::Code byte, bool check_access, bool initialize_class,
 746                                  TRAPS) {
 747   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 748          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 749          (byte == Bytecodes::_nop && !check_access), "bad field access bytecode");
 750 
 751   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 752   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic);
 753 
 754   // Check if there's a resolved klass containing the field
 755   if (resolved_klass.is_null()) {
 756     ResourceMark rm(THREAD);
 757     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 758   }
 759 
 760   // Resolve instance field
 761   KlassHandle sel_klass(THREAD, InstanceKlass::cast(resolved_klass())->find_field(field, sig, &fd));
 762   // check if field exists; i.e., if a klass containing the field def has been selected
 763   if (sel_klass.is_null()) {
 764     ResourceMark rm(THREAD);
 765     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 766   }
 767 
 768   if (!check_access)
 769     // Access checking may be turned off when calling from within the VM.
 770     return;
 771 
 772   // check access
 773   check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
 774 
 775   // check for errors
 776   if (is_static != fd.is_static()) {
 777     ResourceMark rm(THREAD);
 778     char msg[200];
 779     jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string());
 780     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
 781   }
 782 
 783   // Final fields can only be accessed from its own class.
 784   if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass()) {
 785     THROW(vmSymbols::java_lang_IllegalAccessError());
 786   }
 787 
 788   // initialize resolved_klass if necessary
 789   // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
 790   //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
 791   //
 792   // note 2: we don't want to force initialization if we are just checking
 793   //         if the field access is legal; e.g., during compilation
 794   if (is_static && initialize_class) {
 795     sel_klass->initialize(CHECK);
 796   }
 797 
 798   if (sel_klass() != current_klass()) {
 799     HandleMark hm(THREAD);
 800     Handle ref_loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());
 801     Handle sel_loader (THREAD, InstanceKlass::cast(sel_klass())->class_loader());
 802     {
 803       ResourceMark rm(THREAD);
 804       Symbol* failed_type_symbol =
 805         SystemDictionary::check_signature_loaders(sig,
 806                                                   ref_loader, sel_loader,
 807                                                   false,
 808                                                   CHECK);
 809       if (failed_type_symbol != NULL) {
 810         const char* msg = "loader constraint violation: when resolving field"
 811           " \"%s\" the class loader (instance of %s) of the referring class, "
 812           "%s, and the class loader (instance of %s) for the field's resolved "
 813           "type, %s, have different Class objects for that type";
 814         char* field_name = field->as_C_string();
 815         const char* loader1 = SystemDictionary::loader_name(ref_loader());
 816         char* sel = InstanceKlass::cast(sel_klass())->name()->as_C_string();
 817         const char* loader2 = SystemDictionary::loader_name(sel_loader());
 818         char* failed_type_name = failed_type_symbol->as_C_string();
 819         size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1) +
 820           strlen(sel) + strlen(loader2) + strlen(failed_type_name) + 1;
 821         char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 822         jio_snprintf(buf, buflen, msg, field_name, loader1, sel, loader2,
 823                      failed_type_name);
 824         THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 825       }
 826     }
 827   }
 828 
 829   // return information. note that the klass is set to the actual klass containing the
 830   // field, otherwise access of static fields in superclasses will not work.
 831 }
 832 
 833 
 834 //------------------------------------------------------------------------------------------------------------------------
 835 // Invoke resolution
 836 //
 837 // Naming conventions:
 838 //
 839 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
 840 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
 841 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
 842 // recv_klass         the receiver klass
 843 
 844 
 845 void LinkResolver::resolve_static_call(CallInfo& result, KlassHandle& resolved_klass, Symbol* method_name,
 846                                        Symbol* method_signature, KlassHandle current_klass,
 847                                        bool check_access, bool initialize_class, TRAPS) {
 848   methodHandle resolved_method;
 849   linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
 850   resolved_klass = KlassHandle(THREAD, resolved_method->method_holder());
 851 
 852   // Initialize klass (this should only happen if everything is ok)
 853   if (initialize_class && resolved_klass->should_be_initialized()) {
 854     resolved_klass->initialize(CHECK);
 855     linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
 856   }
 857 
 858   // setup result
 859   result.set_static(resolved_klass, resolved_method, CHECK);
 860 }
 861 
 862 // throws linktime exceptions
 863 void LinkResolver::linktime_resolve_static_method(methodHandle& resolved_method, KlassHandle resolved_klass,
 864                                                   Symbol* method_name, Symbol* method_signature,
 865                                                   KlassHandle current_klass, bool check_access, TRAPS) {
 866 
 867   resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK);
 868   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
 869 
 870   // check if static
 871   if (!resolved_method->is_static()) {
 872     ResourceMark rm(THREAD);
 873     char buf[200];
 874     jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
 875                                                       resolved_method->name(),
 876                                                       resolved_method->signature()));
 877     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 878   }
 879 }
 880 
 881 
 882 void LinkResolver::resolve_special_call(CallInfo& result, KlassHandle resolved_klass, Symbol* method_name,
 883                                         Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) {
 884   methodHandle resolved_method;
 885   linktime_resolve_special_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
 886   runtime_resolve_special_method(result, resolved_method, resolved_klass, current_klass, check_access, CHECK);
 887 }
 888 
 889 // throws linktime exceptions
 890 void LinkResolver::linktime_resolve_special_method(methodHandle& resolved_method, KlassHandle resolved_klass,
 891                                                    Symbol* method_name, Symbol* method_signature,
 892                                                    KlassHandle current_klass, bool check_access, TRAPS) {
 893 
 894   // Invokespecial is called for multiple special reasons:
 895   // <init>
 896   // local private method invocation, for classes and interfaces
 897   // superclass.method, which can also resolve to a default method
 898   // and the selected method is recalculated relative to the direct superclass
 899   // superinterface.method, which explicitly does not check shadowing
 900 
 901   resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK);
 902 
 903   // check if method name is <init>, that it is found in same klass as static type
 904   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
 905       resolved_method->method_holder() != resolved_klass()) {
 906     ResourceMark rm(THREAD);
 907     Exceptions::fthrow(
 908       THREAD_AND_LOCATION,
 909       vmSymbols::java_lang_NoSuchMethodError(),
 910       "%s: method %s%s not found",
 911       resolved_klass->external_name(),
 912       resolved_method->name()->as_C_string(),
 913       resolved_method->signature()->as_C_string()
 914     );
 915     return;
 916   }
 917 
 918   // check if not static
 919   if (resolved_method->is_static()) {
 920     ResourceMark rm(THREAD);
 921     char buf[200];
 922     jio_snprintf(buf, sizeof(buf),
 923                  "Expecting non-static method %s",
 924                  Method::name_and_sig_as_C_string(resolved_klass(),
 925                                                          resolved_method->name(),
 926                                                          resolved_method->signature()));
 927     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 928   }
 929 
 930   if (TraceItables && Verbose) {
 931     ResourceMark rm(THREAD);
 932     tty->print("invokespecial resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 933                 (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
 934                 (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
 935                 Method::name_and_sig_as_C_string(resolved_klass(),
 936                                                  resolved_method->name(),
 937                                                  resolved_method->signature()),
 938                 resolved_method->method_holder()->internal_name()
 939                );
 940     resolved_method->access_flags().print_on(tty);
 941     if (resolved_method->is_default_method()) {
 942       tty->print("default ");
 943     }
 944     if (resolved_method->is_overpass()) {
 945       tty->print("overpass");
 946     }
 947     tty->cr();
 948   }
 949 }
 950 
 951 // throws runtime exceptions
 952 void LinkResolver::runtime_resolve_special_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
 953                                                   KlassHandle current_klass, bool check_access, TRAPS) {
 954 
 955   // resolved method is selected method unless we have an old-style lookup
 956   // for a superclass method
 957   // Invokespecial for a superinterface, resolved method is selected method,
 958   // no checks for shadowing
 959   methodHandle sel_method(THREAD, resolved_method());
 960 
 961   // check if this is an old-style super call and do a new lookup if so
 962   { KlassHandle method_klass  = KlassHandle(THREAD,
 963                                             resolved_method->method_holder());
 964 
 965     if (check_access &&
 966         // a) check if ACC_SUPER flag is set for the current class
 967         (current_klass->is_super() || !AllowNonVirtualCalls) &&
 968         // b) check if the class of the resolved_klass is a superclass
 969         // (not supertype in order to exclude interface classes) of the current class.
 970         // This check is not performed for super.invoke for interface methods
 971         // in super interfaces.
 972         current_klass->is_subclass_of(resolved_klass()) &&
 973         current_klass() != resolved_klass() &&
 974         // c) check if the method is not <init>
 975         resolved_method->name() != vmSymbols::object_initializer_name()) {
 976       // Lookup super method
 977       KlassHandle super_klass(THREAD, current_klass->super());
 978       lookup_instance_method_in_klasses(sel_method, super_klass,
 979                            resolved_method->name(),
 980                            resolved_method->signature(), CHECK);
 981       // check if found
 982       if (sel_method.is_null()) {
 983         ResourceMark rm(THREAD);
 984         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
 985                   Method::name_and_sig_as_C_string(resolved_klass(),
 986                                             resolved_method->name(),
 987                                             resolved_method->signature()));
 988       }
 989     }
 990   }
 991 
 992   // check if not static
 993   if (sel_method->is_static()) {
 994     ResourceMark rm(THREAD);
 995     char buf[200];
 996     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
 997                                                                                                              resolved_method->name(),
 998                                                                                                              resolved_method->signature()));
 999     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1000   }
1001 
1002   // check if abstract
1003   if (sel_method->is_abstract()) {
1004     ResourceMark rm(THREAD);
1005     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1006               Method::name_and_sig_as_C_string(resolved_klass(),
1007                                                       sel_method->name(),
1008                                                       sel_method->signature()));
1009   }
1010 
1011   if (TraceItables && Verbose) {
1012     ResourceMark rm(THREAD);
1013     tty->print("invokespecial selected method: resolved-class:%s, method:%s, method_holder:%s, access_flags: ",
1014                  (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
1015                  Method::name_and_sig_as_C_string(resolved_klass(),
1016                                                   sel_method->name(),
1017                                                   sel_method->signature()),
1018                  sel_method->method_holder()->internal_name()
1019                 );
1020     sel_method->access_flags().print_on(tty);
1021     if (sel_method->is_default_method()) {
1022       tty->print("default ");
1023     }
1024     if (sel_method->is_overpass()) {
1025       tty->print("overpass");
1026     }
1027     tty->cr();
1028   }
1029 
1030   // setup result
1031   result.set_static(resolved_klass, sel_method, CHECK);
1032 }
1033 
1034 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass, KlassHandle resolved_klass,
1035                                         Symbol* method_name, Symbol* method_signature, KlassHandle current_klass,
1036                                         bool check_access, bool check_null_and_abstract, TRAPS) {
1037   methodHandle resolved_method;
1038   linktime_resolve_virtual_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
1039   runtime_resolve_virtual_method(result, resolved_method, resolved_klass, recv, receiver_klass, check_null_and_abstract, CHECK);
1040 }
1041 
1042 // throws linktime exceptions
1043 void LinkResolver::linktime_resolve_virtual_method(methodHandle &resolved_method, KlassHandle resolved_klass,
1044                                                    Symbol* method_name, Symbol* method_signature,
1045                                                    KlassHandle current_klass, bool check_access, TRAPS) {
1046   // normal method resolution
1047   resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK);
1048 
1049   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1050   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1051 
1052   // check if private interface method
1053   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1054     ResourceMark rm(THREAD);
1055     char buf[200];
1056     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
1057                  Method::name_and_sig_as_C_string(resolved_klass(),
1058                                                   resolved_method->name(),
1059                                                   resolved_method->signature()),
1060                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
1061     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1062   }
1063 
1064   // check if not static
1065   if (resolved_method->is_static()) {
1066     ResourceMark rm(THREAD);
1067     char buf[200];
1068     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1069                                                                                                              resolved_method->name(),
1070                                                                                                              resolved_method->signature()));
1071     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1072   }
1073 
1074   if (PrintVtables && Verbose) {
1075     ResourceMark rm(THREAD);
1076     tty->print("invokevirtual resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
1077                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
1078                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
1079                    Method::name_and_sig_as_C_string(resolved_klass(),
1080                                                     resolved_method->name(),
1081                                                     resolved_method->signature()),
1082                    resolved_method->method_holder()->internal_name()
1083                   );
1084     resolved_method->access_flags().print_on(tty);
1085     if (resolved_method->is_default_method()) {
1086       tty->print("default ");
1087     }
1088     if (resolved_method->is_overpass()) {
1089       tty->print("overpass");
1090     }
1091     tty->cr();
1092   }
1093 }
1094 
1095 // throws runtime exceptions
1096 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1097                                                   methodHandle resolved_method,
1098                                                   KlassHandle resolved_klass,
1099                                                   Handle recv,
1100                                                   KlassHandle recv_klass,
1101                                                   bool check_null_and_abstract,
1102                                                   TRAPS) {
1103 
1104   // setup default return values
1105   int vtable_index = Method::invalid_vtable_index;
1106   methodHandle selected_method;
1107 
1108   assert(recv.is_null() || recv->is_oop(), "receiver is not an oop");
1109 
1110   // runtime method resolution
1111   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1112     THROW(vmSymbols::java_lang_NullPointerException());
1113   }
1114 
1115   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1116   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1117   // a missing receiver might result in a bogus lookup.
1118   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1119 
1120   // do lookup based on receiver klass using the vtable index
1121   if (resolved_method->method_holder()->is_interface()) { // miranda method
1122     vtable_index = vtable_index_of_interface_method(resolved_klass,
1123                            resolved_method);
1124     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1125 
1126     InstanceKlass* inst = InstanceKlass::cast(recv_klass());
1127     selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));
1128   } else {
1129     // at this point we are sure that resolved_method is virtual and not
1130     // a miranda method; therefore, it must have a valid vtable index.
1131     assert(!resolved_method->has_itable_index(), "");
1132     vtable_index = resolved_method->vtable_index();
1133     // We could get a negative vtable_index for final methods,
1134     // because as an optimization they are they are never put in the vtable,
1135     // unless they override an existing method.
1136     // If we do get a negative, it means the resolved method is the the selected
1137     // method, and it can never be changed by an override.
1138     if (vtable_index == Method::nonvirtual_vtable_index) {
1139       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1140       selected_method = resolved_method;
1141     } else {
1142       // recv_klass might be an arrayKlassOop but all vtables start at
1143       // the same place. The cast is to avoid virtual call and assertion.
1144       InstanceKlass* inst = (InstanceKlass*)recv_klass();
1145       selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));
1146     }
1147   }
1148 
1149   // check if method exists
1150   if (selected_method.is_null()) {
1151     ResourceMark rm(THREAD);
1152     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1153               Method::name_and_sig_as_C_string(resolved_klass(),
1154                                                       resolved_method->name(),
1155                                                       resolved_method->signature()));
1156   }
1157 
1158   // check if abstract
1159   if (check_null_and_abstract && selected_method->is_abstract()) {
1160     ResourceMark rm(THREAD);
1161     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1162               Method::name_and_sig_as_C_string(resolved_klass(),
1163                                                       selected_method->name(),
1164                                                       selected_method->signature()));
1165   }
1166 
1167   if (PrintVtables && Verbose) {
1168     ResourceMark rm(THREAD);
1169     tty->print("invokevirtual selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, vtable_index:%d, access_flags: ",
1170                    (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()),
1171                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
1172                    Method::name_and_sig_as_C_string(resolved_klass(),
1173                                                     resolved_method->name(),
1174                                                     resolved_method->signature()),
1175                    selected_method->method_holder()->internal_name(),
1176                    vtable_index
1177                   );
1178     selected_method->access_flags().print_on(tty);
1179     if (selected_method->is_default_method()) {
1180       tty->print("default ");
1181     }
1182     if (selected_method->is_overpass()) {
1183       tty->print("overpass");
1184     }
1185     tty->cr();
1186   }
1187   // setup result
1188   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1189 }
1190 
1191 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass,
1192                                           Symbol* method_name, Symbol* method_signature, KlassHandle current_klass,
1193                                           bool check_access, bool check_null_and_abstract, TRAPS) {
1194   methodHandle resolved_method;
1195   linktime_resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
1196   runtime_resolve_interface_method(result, resolved_method, resolved_klass, recv, recv_klass, check_null_and_abstract, CHECK);
1197 }
1198 
1199 // throws linktime exceptions
1200 void LinkResolver::linktime_resolve_interface_method(methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name,
1201                                                      Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) {
1202   // normal interface method resolution
1203   resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
1204 
1205   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1206   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1207 }
1208 
1209 // throws runtime exceptions
1210 void LinkResolver::runtime_resolve_interface_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
1211                                                     Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS) {
1212   // check if receiver exists
1213   if (check_null_and_abstract && recv.is_null()) {
1214     THROW(vmSymbols::java_lang_NullPointerException());
1215   }
1216 
1217   // check if private interface method
1218   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1219     ResourceMark rm(THREAD);
1220     char buf[200];
1221     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s",
1222                  Method::name_and_sig_as_C_string(resolved_klass(),
1223                                                   resolved_method->name(),
1224                                                   resolved_method->signature()));
1225     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1226   }
1227 
1228   // check if receiver klass implements the resolved interface
1229   if (!recv_klass->is_subtype_of(resolved_klass())) {
1230     ResourceMark rm(THREAD);
1231     char buf[200];
1232     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1233                  recv_klass()->external_name(),
1234                  resolved_klass()->external_name());
1235     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1236   }
1237   // do lookup based on receiver klass
1238   methodHandle sel_method;
1239   lookup_instance_method_in_klasses(sel_method, recv_klass,
1240             resolved_method->name(),
1241             resolved_method->signature(), CHECK);
1242   if (sel_method.is_null() && !check_null_and_abstract) {
1243     // In theory this is a harmless placeholder value, but
1244     // in practice leaving in null affects the nsk default method tests.
1245     // This needs further study.
1246     sel_method = resolved_method;
1247   }
1248   // check if method exists
1249   if (sel_method.is_null()) {
1250     ResourceMark rm(THREAD);
1251     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1252               Method::name_and_sig_as_C_string(recv_klass(),
1253                                                       resolved_method->name(),
1254                                                       resolved_method->signature()));
1255   }
1256   // check access
1257   // Throw Illegal Access Error if sel_method is not public.
1258   if (!sel_method->is_public()) {
1259     ResourceMark rm(THREAD);
1260     THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1261               Method::name_and_sig_as_C_string(recv_klass(),
1262                                                sel_method->name(),
1263                                                sel_method->signature()));
1264   }
1265   // check if abstract
1266   if (check_null_and_abstract && sel_method->is_abstract()) {
1267     ResourceMark rm(THREAD);
1268     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1269               Method::name_and_sig_as_C_string(recv_klass(),
1270                                                       sel_method->name(),
1271                                                       sel_method->signature()));
1272   }
1273 
1274   if (TraceItables && Verbose) {
1275     ResourceMark rm(THREAD);
1276     tty->print("invokeinterface selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, access_flags: ",
1277                    (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()),
1278                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
1279                    Method::name_and_sig_as_C_string(resolved_klass(),
1280                                                     resolved_method->name(),
1281                                                     resolved_method->signature()),
1282                    sel_method->method_holder()->internal_name()
1283                   );
1284     sel_method->access_flags().print_on(tty);
1285     if (sel_method->is_default_method()) {
1286       tty->print("default ");
1287     }
1288     if (sel_method->is_overpass()) {
1289       tty->print("overpass");
1290     }
1291     tty->cr();
1292   }
1293   // setup result
1294   if (!resolved_method->has_itable_index()) {
1295     int vtable_index = resolved_method->vtable_index();
1296     assert(vtable_index == sel_method->vtable_index(), "sanity check");
1297     result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK);
1298   } else {
1299     int itable_index = resolved_method()->itable_index();
1300     result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK);
1301   }
1302 }
1303 
1304 
1305 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
1306                                                  KlassHandle resolved_klass,
1307                                                  Symbol* method_name,
1308                                                  Symbol* method_signature,
1309                                                  KlassHandle current_klass,
1310                                                  bool check_access) {
1311   EXCEPTION_MARK;
1312   methodHandle method_result;
1313   linktime_resolve_interface_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
1314   if (HAS_PENDING_EXCEPTION) {
1315     CLEAR_PENDING_EXCEPTION;
1316     return methodHandle();
1317   } else {
1318     return method_result;
1319   }
1320 }
1321 
1322 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1323                                                  KlassHandle resolved_klass,
1324                                                  Symbol* method_name,
1325                                                  Symbol* method_signature,
1326                                                  KlassHandle current_klass,
1327                                                  bool check_access) {
1328   EXCEPTION_MARK;
1329   methodHandle method_result;
1330   linktime_resolve_virtual_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
1331   if (HAS_PENDING_EXCEPTION) {
1332     CLEAR_PENDING_EXCEPTION;
1333     return methodHandle();
1334   } else {
1335     return method_result;
1336   }
1337 }
1338 
1339 methodHandle LinkResolver::resolve_virtual_call_or_null(
1340                                                  KlassHandle receiver_klass,
1341                                                  KlassHandle resolved_klass,
1342                                                  Symbol* name,
1343                                                  Symbol* signature,
1344                                                  KlassHandle current_klass) {
1345   EXCEPTION_MARK;
1346   CallInfo info;
1347   resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
1348   if (HAS_PENDING_EXCEPTION) {
1349     CLEAR_PENDING_EXCEPTION;
1350     return methodHandle();
1351   }
1352   return info.selected_method();
1353 }
1354 
1355 methodHandle LinkResolver::resolve_interface_call_or_null(
1356                                                  KlassHandle receiver_klass,
1357                                                  KlassHandle resolved_klass,
1358                                                  Symbol* name,
1359                                                  Symbol* signature,
1360                                                  KlassHandle current_klass) {
1361   EXCEPTION_MARK;
1362   CallInfo info;
1363   resolve_interface_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
1364   if (HAS_PENDING_EXCEPTION) {
1365     CLEAR_PENDING_EXCEPTION;
1366     return methodHandle();
1367   }
1368   return info.selected_method();
1369 }
1370 
1371 int LinkResolver::resolve_virtual_vtable_index(
1372                                                KlassHandle receiver_klass,
1373                                                KlassHandle resolved_klass,
1374                                                Symbol* name,
1375                                                Symbol* signature,
1376                                                KlassHandle current_klass) {
1377   EXCEPTION_MARK;
1378   CallInfo info;
1379   resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
1380   if (HAS_PENDING_EXCEPTION) {
1381     CLEAR_PENDING_EXCEPTION;
1382     return Method::invalid_vtable_index;
1383   }
1384   return info.vtable_index();
1385 }
1386 
1387 methodHandle LinkResolver::resolve_static_call_or_null(
1388                                                   KlassHandle resolved_klass,
1389                                                   Symbol* name,
1390                                                   Symbol* signature,
1391                                                   KlassHandle current_klass) {
1392   EXCEPTION_MARK;
1393   CallInfo info;
1394   resolve_static_call(info, resolved_klass, name, signature, current_klass, true, false, THREAD);
1395   if (HAS_PENDING_EXCEPTION) {
1396     CLEAR_PENDING_EXCEPTION;
1397     return methodHandle();
1398   }
1399   return info.selected_method();
1400 }
1401 
1402 methodHandle LinkResolver::resolve_special_call_or_null(KlassHandle resolved_klass, Symbol* name, Symbol* signature,
1403                                                         KlassHandle current_klass) {
1404   EXCEPTION_MARK;
1405   CallInfo info;
1406   resolve_special_call(info, resolved_klass, name, signature, current_klass, true, THREAD);
1407   if (HAS_PENDING_EXCEPTION) {
1408     CLEAR_PENDING_EXCEPTION;
1409     return methodHandle();
1410   }
1411   return info.selected_method();
1412 }
1413 
1414 
1415 
1416 //------------------------------------------------------------------------------------------------------------------------
1417 // ConstantPool entries
1418 
1419 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {
1420   switch (byte) {
1421     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1422     case Bytecodes::_invokespecial  : resolve_invokespecial  (result,       pool, index, CHECK); break;
1423     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1424     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1425     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1426     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1427   }
1428   return;
1429 }
1430 
1431 void LinkResolver::resolve_pool(KlassHandle& resolved_klass, Symbol*& method_name, Symbol*& method_signature,
1432                                 KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS) {
1433    // resolve klass
1434   resolve_klass(resolved_klass, pool, index, CHECK);
1435 
1436   // Get name, signature, and static klass
1437   method_name      = pool->name_ref_at(index);
1438   method_signature = pool->signature_ref_at(index);
1439   current_klass    = KlassHandle(THREAD, pool->pool_holder());
1440 }
1441 
1442 
1443 void LinkResolver::resolve_invokestatic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
1444   KlassHandle  resolved_klass;
1445   Symbol* method_name = NULL;
1446   Symbol* method_signature = NULL;
1447   KlassHandle  current_klass;
1448   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
1449   resolve_static_call(result, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
1450 }
1451 
1452 
1453 void LinkResolver::resolve_invokespecial(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
1454   KlassHandle  resolved_klass;
1455   Symbol* method_name = NULL;
1456   Symbol* method_signature = NULL;
1457   KlassHandle  current_klass;
1458   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
1459   resolve_special_call(result, resolved_klass, method_name, method_signature, current_klass, true, CHECK);
1460 }
1461 
1462 
1463 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1464                                           constantPoolHandle pool, int index,
1465                                           TRAPS) {
1466 
1467   KlassHandle  resolved_klass;
1468   Symbol* method_name = NULL;
1469   Symbol* method_signature = NULL;
1470   KlassHandle  current_klass;
1471   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
1472   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1473   resolve_virtual_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
1474 }
1475 
1476 
1477 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) {
1478   KlassHandle  resolved_klass;
1479   Symbol* method_name = NULL;
1480   Symbol* method_signature = NULL;
1481   KlassHandle  current_klass;
1482   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
1483   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1484   resolve_interface_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
1485 }
1486 
1487 
1488 void LinkResolver::resolve_invokehandle(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
1489   assert(EnableInvokeDynamic, "");
1490   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1491   KlassHandle  resolved_klass;
1492   Symbol* method_name = NULL;
1493   Symbol* method_signature = NULL;
1494   KlassHandle  current_klass;
1495   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
1496   if (TraceMethodHandles) {
1497     ResourceMark rm(THREAD);
1498     tty->print_cr("resolve_invokehandle %s %s", method_name->as_C_string(), method_signature->as_C_string());
1499   }
1500   resolve_handle_call(result, resolved_klass, method_name, method_signature, current_klass, CHECK);
1501 }
1502 
1503 void LinkResolver::resolve_handle_call(CallInfo& result, KlassHandle resolved_klass,
1504                                        Symbol* method_name, Symbol* method_signature,
1505                                        KlassHandle current_klass,
1506                                        TRAPS) {
1507   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1508   assert(resolved_klass() == SystemDictionary::MethodHandle_klass(), "");
1509   assert(MethodHandles::is_signature_polymorphic_name(method_name), "");
1510   methodHandle resolved_method;
1511   Handle       resolved_appendix;
1512   Handle       resolved_method_type;
1513   lookup_polymorphic_method(resolved_method, resolved_klass,
1514                             method_name, method_signature,
1515                             current_klass, &resolved_appendix, &resolved_method_type, CHECK);
1516   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK);
1517 }
1518 
1519 
1520 void LinkResolver::resolve_invokedynamic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
1521   assert(EnableInvokeDynamic, "");
1522 
1523   //resolve_pool(<resolved_klass>, method_name, method_signature, current_klass, pool, index, CHECK);
1524   Symbol* method_name       = pool->name_ref_at(index);
1525   Symbol* method_signature  = pool->signature_ref_at(index);
1526   KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder());
1527 
1528   // Resolve the bootstrap specifier (BSM + optional arguments).
1529   Handle bootstrap_specifier;
1530   // Check if CallSite has been bound already:
1531   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
1532   if (cpce->is_f1_null()) {
1533     int pool_index = cpce->constant_pool_index();
1534     oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, CHECK);
1535     assert(bsm_info != NULL, "");
1536     // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
1537     bootstrap_specifier = Handle(THREAD, bsm_info);
1538   }
1539   if (!cpce->is_f1_null()) {
1540     methodHandle method(     THREAD, cpce->f1_as_method());
1541     Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
1542     Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
1543     result.set_handle(method, appendix, method_type, CHECK);
1544     return;
1545   }
1546 
1547   if (TraceMethodHandles) {
1548       ResourceMark rm(THREAD);
1549       tty->print_cr("resolve_invokedynamic #%d %s %s",
1550                   ConstantPool::decode_invokedynamic_index(index),
1551                   method_name->as_C_string(), method_signature->as_C_string());
1552     tty->print("  BSM info: "); bootstrap_specifier->print();
1553   }
1554 
1555   resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK);
1556 }
1557 
1558 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1559                                         Handle bootstrap_specifier,
1560                                         Symbol* method_name, Symbol* method_signature,
1561                                         KlassHandle current_klass,
1562                                         TRAPS) {
1563   // JSR 292:  this must resolve to an implicitly generated method MH.linkToCallSite(*...)
1564   // The appendix argument is likely to be a freshly-created CallSite.
1565   Handle       resolved_appendix;
1566   Handle       resolved_method_type;
1567   methodHandle resolved_method =
1568     SystemDictionary::find_dynamic_call_site_invoker(current_klass,
1569                                                      bootstrap_specifier,
1570                                                      method_name, method_signature,
1571                                                      &resolved_appendix,
1572                                                      &resolved_method_type,
1573                                                      THREAD);
1574   if (HAS_PENDING_EXCEPTION) {
1575     if (TraceMethodHandles) {
1576       tty->print_cr("invokedynamic throws BSME for "INTPTR_FORMAT, (void *)PENDING_EXCEPTION);
1577       PENDING_EXCEPTION->print();
1578     }
1579     if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) {
1580       // throw these guys, since they are already wrapped
1581       return;
1582     }
1583     if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
1584       // intercept only LinkageErrors which might have failed to wrap
1585       return;
1586     }
1587     // See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS.
1588     Handle nested_exception(THREAD, PENDING_EXCEPTION);
1589     CLEAR_PENDING_EXCEPTION;
1590     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
1591   }
1592   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK);
1593 }
1594 
1595 //------------------------------------------------------------------------------------------------------------------------
1596 #ifndef PRODUCT
1597 
1598 void CallInfo::print() {
1599   ResourceMark rm;
1600   const char* kindstr = "unknown";
1601   switch (_call_kind) {
1602   case direct_call: kindstr = "direct"; break;
1603   case vtable_call: kindstr = "vtable"; break;
1604   case itable_call: kindstr = "itable"; break;
1605   }
1606   tty->print_cr("Call %s@%d %s", kindstr, _call_index,
1607                 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
1608 }
1609 
1610 #endif