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 invokespecial's interface method reference is in an indirect superinterface
 919   if (!current_klass.is_null() && resolved_klass->is_interface()) {
 920     Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ?
 921                                   current_klass() :
 922                                   InstanceKlass::cast(current_klass())->host_klass();
 923 
 924     if (!InstanceKlass::cast(klass_to_check)->is_same_or_direct_interface(resolved_klass())) {
 925       ResourceMark rm(THREAD);
 926       char buf[200];
 927       jio_snprintf(buf, sizeof(buf),
 928                    "Interface method reference: %s, is in an indirect superinterface of %s",
 929                    Method::name_and_sig_as_C_string(resolved_klass(),
 930                                                          resolved_method->name(),
 931                                                          resolved_method->signature()),
 932                    current_klass->external_name());
 933       THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 934     }
 935   }
 936 
 937   // check if not static
 938   if (resolved_method->is_static()) {
 939     ResourceMark rm(THREAD);
 940     char buf[200];
 941     jio_snprintf(buf, sizeof(buf),
 942                  "Expecting non-static method %s",
 943                  Method::name_and_sig_as_C_string(resolved_klass(),
 944                                                          resolved_method->name(),
 945                                                          resolved_method->signature()));
 946     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 947   }
 948 
 949   if (TraceItables && Verbose) {
 950     ResourceMark rm(THREAD);
 951     tty->print("invokespecial resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 952                 (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
 953                 (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
 954                 Method::name_and_sig_as_C_string(resolved_klass(),
 955                                                  resolved_method->name(),
 956                                                  resolved_method->signature()),
 957                 resolved_method->method_holder()->internal_name()
 958                );
 959     resolved_method->access_flags().print_on(tty);
 960     if (resolved_method->is_default_method()) {
 961       tty->print("default ");
 962     }
 963     if (resolved_method->is_overpass()) {
 964       tty->print("overpass");
 965     }
 966     tty->cr();
 967   }
 968 }
 969 
 970 // throws runtime exceptions
 971 void LinkResolver::runtime_resolve_special_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
 972                                                   KlassHandle current_klass, bool check_access, TRAPS) {
 973 
 974   // resolved method is selected method unless we have an old-style lookup
 975   // for a superclass method
 976   // Invokespecial for a superinterface, resolved method is selected method,
 977   // no checks for shadowing
 978   methodHandle sel_method(THREAD, resolved_method());
 979 
 980   // check if this is an old-style super call and do a new lookup if so
 981   { KlassHandle method_klass  = KlassHandle(THREAD,
 982                                             resolved_method->method_holder());
 983 
 984     if (check_access &&
 985         // a) check if ACC_SUPER flag is set for the current class
 986         (current_klass->is_super() || !AllowNonVirtualCalls) &&
 987         // b) check if the class of the resolved_klass is a superclass
 988         // (not supertype in order to exclude interface classes) of the current class.
 989         // This check is not performed for super.invoke for interface methods
 990         // in super interfaces.
 991         current_klass->is_subclass_of(resolved_klass()) &&
 992         current_klass() != resolved_klass() &&
 993         // c) check if the method is not <init>
 994         resolved_method->name() != vmSymbols::object_initializer_name()) {
 995       // Lookup super method
 996       KlassHandle super_klass(THREAD, current_klass->super());
 997       lookup_instance_method_in_klasses(sel_method, super_klass,
 998                            resolved_method->name(),
 999                            resolved_method->signature(), CHECK);
1000       // check if found
1001       if (sel_method.is_null()) {
1002         ResourceMark rm(THREAD);
1003         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1004                   Method::name_and_sig_as_C_string(resolved_klass(),
1005                                             resolved_method->name(),
1006                                             resolved_method->signature()));
1007       }
1008     }
1009   }
1010 
1011   // check if not static
1012   if (sel_method->is_static()) {
1013     ResourceMark rm(THREAD);
1014     char buf[200];
1015     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1016                                                                                                              resolved_method->name(),
1017                                                                                                              resolved_method->signature()));
1018     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1019   }
1020 
1021   // check if abstract
1022   if (sel_method->is_abstract()) {
1023     ResourceMark rm(THREAD);
1024     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1025               Method::name_and_sig_as_C_string(resolved_klass(),
1026                                                       sel_method->name(),
1027                                                       sel_method->signature()));
1028   }
1029 
1030   if (TraceItables && Verbose) {
1031     ResourceMark rm(THREAD);
1032     tty->print("invokespecial selected method: resolved-class:%s, method:%s, method_holder:%s, access_flags: ",
1033                  (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
1034                  Method::name_and_sig_as_C_string(resolved_klass(),
1035                                                   sel_method->name(),
1036                                                   sel_method->signature()),
1037                  sel_method->method_holder()->internal_name()
1038                 );
1039     sel_method->access_flags().print_on(tty);
1040     if (sel_method->is_default_method()) {
1041       tty->print("default ");
1042     }
1043     if (sel_method->is_overpass()) {
1044       tty->print("overpass");
1045     }
1046     tty->cr();
1047   }
1048 
1049   // setup result
1050   result.set_static(resolved_klass, sel_method, CHECK);
1051 }
1052 
1053 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass, KlassHandle resolved_klass,
1054                                         Symbol* method_name, Symbol* method_signature, KlassHandle current_klass,
1055                                         bool check_access, bool check_null_and_abstract, TRAPS) {
1056   methodHandle resolved_method;
1057   linktime_resolve_virtual_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
1058   runtime_resolve_virtual_method(result, resolved_method, resolved_klass, recv, receiver_klass, check_null_and_abstract, CHECK);
1059 }
1060 
1061 // throws linktime exceptions
1062 void LinkResolver::linktime_resolve_virtual_method(methodHandle &resolved_method, KlassHandle resolved_klass,
1063                                                    Symbol* method_name, Symbol* method_signature,
1064                                                    KlassHandle current_klass, bool check_access, TRAPS) {
1065   // normal method resolution
1066   resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK);
1067 
1068   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1069   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1070 
1071   // check if private interface method
1072   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1073     ResourceMark rm(THREAD);
1074     char buf[200];
1075     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
1076                  Method::name_and_sig_as_C_string(resolved_klass(),
1077                                                   resolved_method->name(),
1078                                                   resolved_method->signature()),
1079                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
1080     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1081   }
1082 
1083   // check if not static
1084   if (resolved_method->is_static()) {
1085     ResourceMark rm(THREAD);
1086     char buf[200];
1087     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1088                                                                                                              resolved_method->name(),
1089                                                                                                              resolved_method->signature()));
1090     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1091   }
1092 
1093   if (PrintVtables && Verbose) {
1094     ResourceMark rm(THREAD);
1095     tty->print("invokevirtual resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
1096                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
1097                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
1098                    Method::name_and_sig_as_C_string(resolved_klass(),
1099                                                     resolved_method->name(),
1100                                                     resolved_method->signature()),
1101                    resolved_method->method_holder()->internal_name()
1102                   );
1103     resolved_method->access_flags().print_on(tty);
1104     if (resolved_method->is_default_method()) {
1105       tty->print("default ");
1106     }
1107     if (resolved_method->is_overpass()) {
1108       tty->print("overpass");
1109     }
1110     tty->cr();
1111   }
1112 }
1113 
1114 // throws runtime exceptions
1115 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1116                                                   methodHandle resolved_method,
1117                                                   KlassHandle resolved_klass,
1118                                                   Handle recv,
1119                                                   KlassHandle recv_klass,
1120                                                   bool check_null_and_abstract,
1121                                                   TRAPS) {
1122 
1123   // setup default return values
1124   int vtable_index = Method::invalid_vtable_index;
1125   methodHandle selected_method;
1126 
1127   assert(recv.is_null() || recv->is_oop(), "receiver is not an oop");
1128 
1129   // runtime method resolution
1130   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1131     THROW(vmSymbols::java_lang_NullPointerException());
1132   }
1133 
1134   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1135   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1136   // a missing receiver might result in a bogus lookup.
1137   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1138 
1139   // do lookup based on receiver klass using the vtable index
1140   if (resolved_method->method_holder()->is_interface()) { // miranda method
1141     vtable_index = vtable_index_of_interface_method(resolved_klass,
1142                            resolved_method);
1143     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1144 
1145     InstanceKlass* inst = InstanceKlass::cast(recv_klass());
1146     selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));
1147   } else {
1148     // at this point we are sure that resolved_method is virtual and not
1149     // a miranda method; therefore, it must have a valid vtable index.
1150     assert(!resolved_method->has_itable_index(), "");
1151     vtable_index = resolved_method->vtable_index();
1152     // We could get a negative vtable_index for final methods,
1153     // because as an optimization they are they are never put in the vtable,
1154     // unless they override an existing method.
1155     // If we do get a negative, it means the resolved method is the the selected
1156     // method, and it can never be changed by an override.
1157     if (vtable_index == Method::nonvirtual_vtable_index) {
1158       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1159       selected_method = resolved_method;
1160     } else {
1161       // recv_klass might be an arrayKlassOop but all vtables start at
1162       // the same place. The cast is to avoid virtual call and assertion.
1163       InstanceKlass* inst = (InstanceKlass*)recv_klass();
1164       selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));
1165     }
1166   }
1167 
1168   // check if method exists
1169   if (selected_method.is_null()) {
1170     ResourceMark rm(THREAD);
1171     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1172               Method::name_and_sig_as_C_string(resolved_klass(),
1173                                                       resolved_method->name(),
1174                                                       resolved_method->signature()));
1175   }
1176 
1177   // check if abstract
1178   if (check_null_and_abstract && selected_method->is_abstract()) {
1179     ResourceMark rm(THREAD);
1180     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1181               Method::name_and_sig_as_C_string(resolved_klass(),
1182                                                       selected_method->name(),
1183                                                       selected_method->signature()));
1184   }
1185 
1186   if (PrintVtables && Verbose) {
1187     ResourceMark rm(THREAD);
1188     tty->print("invokevirtual selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, vtable_index:%d, access_flags: ",
1189                    (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()),
1190                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
1191                    Method::name_and_sig_as_C_string(resolved_klass(),
1192                                                     resolved_method->name(),
1193                                                     resolved_method->signature()),
1194                    selected_method->method_holder()->internal_name(),
1195                    vtable_index
1196                   );
1197     selected_method->access_flags().print_on(tty);
1198     if (selected_method->is_default_method()) {
1199       tty->print("default ");
1200     }
1201     if (selected_method->is_overpass()) {
1202       tty->print("overpass");
1203     }
1204     tty->cr();
1205   }
1206   // setup result
1207   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1208 }
1209 
1210 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass,
1211                                           Symbol* method_name, Symbol* method_signature, KlassHandle current_klass,
1212                                           bool check_access, bool check_null_and_abstract, TRAPS) {
1213   methodHandle resolved_method;
1214   linktime_resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
1215   runtime_resolve_interface_method(result, resolved_method, resolved_klass, recv, recv_klass, check_null_and_abstract, CHECK);
1216 }
1217 
1218 // throws linktime exceptions
1219 void LinkResolver::linktime_resolve_interface_method(methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name,
1220                                                      Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) {
1221   // normal interface method resolution
1222   resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
1223 
1224   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1225   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1226 }
1227 
1228 // throws runtime exceptions
1229 void LinkResolver::runtime_resolve_interface_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
1230                                                     Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS) {
1231   // check if receiver exists
1232   if (check_null_and_abstract && recv.is_null()) {
1233     THROW(vmSymbols::java_lang_NullPointerException());
1234   }
1235 
1236   // check if private interface method
1237   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1238     ResourceMark rm(THREAD);
1239     char buf[200];
1240     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s",
1241                  Method::name_and_sig_as_C_string(resolved_klass(),
1242                                                   resolved_method->name(),
1243                                                   resolved_method->signature()));
1244     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1245   }
1246 
1247   // check if receiver klass implements the resolved interface
1248   if (!recv_klass->is_subtype_of(resolved_klass())) {
1249     ResourceMark rm(THREAD);
1250     char buf[200];
1251     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1252                  recv_klass()->external_name(),
1253                  resolved_klass()->external_name());
1254     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1255   }
1256   // do lookup based on receiver klass
1257   methodHandle sel_method;
1258   lookup_instance_method_in_klasses(sel_method, recv_klass,
1259             resolved_method->name(),
1260             resolved_method->signature(), CHECK);
1261   if (sel_method.is_null() && !check_null_and_abstract) {
1262     // In theory this is a harmless placeholder value, but
1263     // in practice leaving in null affects the nsk default method tests.
1264     // This needs further study.
1265     sel_method = resolved_method;
1266   }
1267   // check if method exists
1268   if (sel_method.is_null()) {
1269     ResourceMark rm(THREAD);
1270     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1271               Method::name_and_sig_as_C_string(recv_klass(),
1272                                                       resolved_method->name(),
1273                                                       resolved_method->signature()));
1274   }
1275   // check access
1276   // Throw Illegal Access Error if sel_method is not public.
1277   if (!sel_method->is_public()) {
1278     ResourceMark rm(THREAD);
1279     THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1280               Method::name_and_sig_as_C_string(recv_klass(),
1281                                                sel_method->name(),
1282                                                sel_method->signature()));
1283   }
1284   // check if abstract
1285   if (check_null_and_abstract && sel_method->is_abstract()) {
1286     ResourceMark rm(THREAD);
1287     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1288               Method::name_and_sig_as_C_string(recv_klass(),
1289                                                       sel_method->name(),
1290                                                       sel_method->signature()));
1291   }
1292 
1293   if (TraceItables && Verbose) {
1294     ResourceMark rm(THREAD);
1295     tty->print("invokeinterface selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, access_flags: ",
1296                    (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()),
1297                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
1298                    Method::name_and_sig_as_C_string(resolved_klass(),
1299                                                     resolved_method->name(),
1300                                                     resolved_method->signature()),
1301                    sel_method->method_holder()->internal_name()
1302                   );
1303     sel_method->access_flags().print_on(tty);
1304     if (sel_method->is_default_method()) {
1305       tty->print("default ");
1306     }
1307     if (sel_method->is_overpass()) {
1308       tty->print("overpass");
1309     }
1310     tty->cr();
1311   }
1312   // setup result
1313   if (!resolved_method->has_itable_index()) {
1314     int vtable_index = resolved_method->vtable_index();
1315     assert(vtable_index == sel_method->vtable_index(), "sanity check");
1316     result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK);
1317   } else {
1318     int itable_index = resolved_method()->itable_index();
1319     result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK);
1320   }
1321 }
1322 
1323 
1324 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
1325                                                  KlassHandle resolved_klass,
1326                                                  Symbol* method_name,
1327                                                  Symbol* method_signature,
1328                                                  KlassHandle current_klass,
1329                                                  bool check_access) {
1330   EXCEPTION_MARK;
1331   methodHandle method_result;
1332   linktime_resolve_interface_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
1333   if (HAS_PENDING_EXCEPTION) {
1334     CLEAR_PENDING_EXCEPTION;
1335     return methodHandle();
1336   } else {
1337     return method_result;
1338   }
1339 }
1340 
1341 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1342                                                  KlassHandle resolved_klass,
1343                                                  Symbol* method_name,
1344                                                  Symbol* method_signature,
1345                                                  KlassHandle current_klass,
1346                                                  bool check_access) {
1347   EXCEPTION_MARK;
1348   methodHandle method_result;
1349   linktime_resolve_virtual_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
1350   if (HAS_PENDING_EXCEPTION) {
1351     CLEAR_PENDING_EXCEPTION;
1352     return methodHandle();
1353   } else {
1354     return method_result;
1355   }
1356 }
1357 
1358 methodHandle LinkResolver::resolve_virtual_call_or_null(
1359                                                  KlassHandle receiver_klass,
1360                                                  KlassHandle resolved_klass,
1361                                                  Symbol* name,
1362                                                  Symbol* signature,
1363                                                  KlassHandle current_klass) {
1364   EXCEPTION_MARK;
1365   CallInfo info;
1366   resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
1367   if (HAS_PENDING_EXCEPTION) {
1368     CLEAR_PENDING_EXCEPTION;
1369     return methodHandle();
1370   }
1371   return info.selected_method();
1372 }
1373 
1374 methodHandle LinkResolver::resolve_interface_call_or_null(
1375                                                  KlassHandle receiver_klass,
1376                                                  KlassHandle resolved_klass,
1377                                                  Symbol* name,
1378                                                  Symbol* signature,
1379                                                  KlassHandle current_klass) {
1380   EXCEPTION_MARK;
1381   CallInfo info;
1382   resolve_interface_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
1383   if (HAS_PENDING_EXCEPTION) {
1384     CLEAR_PENDING_EXCEPTION;
1385     return methodHandle();
1386   }
1387   return info.selected_method();
1388 }
1389 
1390 int LinkResolver::resolve_virtual_vtable_index(
1391                                                KlassHandle receiver_klass,
1392                                                KlassHandle resolved_klass,
1393                                                Symbol* name,
1394                                                Symbol* signature,
1395                                                KlassHandle current_klass) {
1396   EXCEPTION_MARK;
1397   CallInfo info;
1398   resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
1399   if (HAS_PENDING_EXCEPTION) {
1400     CLEAR_PENDING_EXCEPTION;
1401     return Method::invalid_vtable_index;
1402   }
1403   return info.vtable_index();
1404 }
1405 
1406 methodHandle LinkResolver::resolve_static_call_or_null(
1407                                                   KlassHandle resolved_klass,
1408                                                   Symbol* name,
1409                                                   Symbol* signature,
1410                                                   KlassHandle current_klass) {
1411   EXCEPTION_MARK;
1412   CallInfo info;
1413   resolve_static_call(info, resolved_klass, name, signature, current_klass, true, false, THREAD);
1414   if (HAS_PENDING_EXCEPTION) {
1415     CLEAR_PENDING_EXCEPTION;
1416     return methodHandle();
1417   }
1418   return info.selected_method();
1419 }
1420 
1421 methodHandle LinkResolver::resolve_special_call_or_null(KlassHandle resolved_klass, Symbol* name, Symbol* signature,
1422                                                         KlassHandle current_klass) {
1423   EXCEPTION_MARK;
1424   CallInfo info;
1425   resolve_special_call(info, resolved_klass, name, signature, current_klass, true, THREAD);
1426   if (HAS_PENDING_EXCEPTION) {
1427     CLEAR_PENDING_EXCEPTION;
1428     return methodHandle();
1429   }
1430   return info.selected_method();
1431 }
1432 
1433 
1434 
1435 //------------------------------------------------------------------------------------------------------------------------
1436 // ConstantPool entries
1437 
1438 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {
1439   switch (byte) {
1440     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1441     case Bytecodes::_invokespecial  : resolve_invokespecial  (result,       pool, index, CHECK); break;
1442     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1443     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1444     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1445     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1446   }
1447   return;
1448 }
1449 
1450 void LinkResolver::resolve_pool(KlassHandle& resolved_klass, Symbol*& method_name, Symbol*& method_signature,
1451                                 KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS) {
1452    // resolve klass
1453   resolve_klass(resolved_klass, pool, index, CHECK);
1454 
1455   // Get name, signature, and static klass
1456   method_name      = pool->name_ref_at(index);
1457   method_signature = pool->signature_ref_at(index);
1458   current_klass    = KlassHandle(THREAD, pool->pool_holder());
1459 }
1460 
1461 
1462 void LinkResolver::resolve_invokestatic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
1463   KlassHandle  resolved_klass;
1464   Symbol* method_name = NULL;
1465   Symbol* method_signature = NULL;
1466   KlassHandle  current_klass;
1467   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
1468   resolve_static_call(result, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
1469 }
1470 
1471 
1472 void LinkResolver::resolve_invokespecial(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
1473   KlassHandle  resolved_klass;
1474   Symbol* method_name = NULL;
1475   Symbol* method_signature = NULL;
1476   KlassHandle  current_klass;
1477   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
1478   resolve_special_call(result, resolved_klass, method_name, method_signature, current_klass, true, CHECK);
1479 }
1480 
1481 
1482 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1483                                           constantPoolHandle pool, int index,
1484                                           TRAPS) {
1485 
1486   KlassHandle  resolved_klass;
1487   Symbol* method_name = NULL;
1488   Symbol* method_signature = NULL;
1489   KlassHandle  current_klass;
1490   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
1491   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1492   resolve_virtual_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
1493 }
1494 
1495 
1496 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) {
1497   KlassHandle  resolved_klass;
1498   Symbol* method_name = NULL;
1499   Symbol* method_signature = NULL;
1500   KlassHandle  current_klass;
1501   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
1502   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1503   resolve_interface_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
1504 }
1505 
1506 
1507 void LinkResolver::resolve_invokehandle(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
1508   assert(EnableInvokeDynamic, "");
1509   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1510   KlassHandle  resolved_klass;
1511   Symbol* method_name = NULL;
1512   Symbol* method_signature = NULL;
1513   KlassHandle  current_klass;
1514   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
1515   if (TraceMethodHandles) {
1516     ResourceMark rm(THREAD);
1517     tty->print_cr("resolve_invokehandle %s %s", method_name->as_C_string(), method_signature->as_C_string());
1518   }
1519   resolve_handle_call(result, resolved_klass, method_name, method_signature, current_klass, CHECK);
1520 }
1521 
1522 void LinkResolver::resolve_handle_call(CallInfo& result, KlassHandle resolved_klass,
1523                                        Symbol* method_name, Symbol* method_signature,
1524                                        KlassHandle current_klass,
1525                                        TRAPS) {
1526   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1527   assert(resolved_klass() == SystemDictionary::MethodHandle_klass(), "");
1528   assert(MethodHandles::is_signature_polymorphic_name(method_name), "");
1529   methodHandle resolved_method;
1530   Handle       resolved_appendix;
1531   Handle       resolved_method_type;
1532   lookup_polymorphic_method(resolved_method, resolved_klass,
1533                             method_name, method_signature,
1534                             current_klass, &resolved_appendix, &resolved_method_type, CHECK);
1535   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK);
1536 }
1537 
1538 
1539 void LinkResolver::resolve_invokedynamic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
1540   assert(EnableInvokeDynamic, "");
1541 
1542   //resolve_pool(<resolved_klass>, method_name, method_signature, current_klass, pool, index, CHECK);
1543   Symbol* method_name       = pool->name_ref_at(index);
1544   Symbol* method_signature  = pool->signature_ref_at(index);
1545   KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder());
1546 
1547   // Resolve the bootstrap specifier (BSM + optional arguments).
1548   Handle bootstrap_specifier;
1549   // Check if CallSite has been bound already:
1550   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
1551   if (cpce->is_f1_null()) {
1552     int pool_index = cpce->constant_pool_index();
1553     oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, CHECK);
1554     assert(bsm_info != NULL, "");
1555     // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
1556     bootstrap_specifier = Handle(THREAD, bsm_info);
1557   }
1558   if (!cpce->is_f1_null()) {
1559     methodHandle method(     THREAD, cpce->f1_as_method());
1560     Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
1561     Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
1562     result.set_handle(method, appendix, method_type, CHECK);
1563     return;
1564   }
1565 
1566   if (TraceMethodHandles) {
1567       ResourceMark rm(THREAD);
1568       tty->print_cr("resolve_invokedynamic #%d %s %s",
1569                   ConstantPool::decode_invokedynamic_index(index),
1570                   method_name->as_C_string(), method_signature->as_C_string());
1571     tty->print("  BSM info: "); bootstrap_specifier->print();
1572   }
1573 
1574   resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK);
1575 }
1576 
1577 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1578                                         Handle bootstrap_specifier,
1579                                         Symbol* method_name, Symbol* method_signature,
1580                                         KlassHandle current_klass,
1581                                         TRAPS) {
1582   // JSR 292:  this must resolve to an implicitly generated method MH.linkToCallSite(*...)
1583   // The appendix argument is likely to be a freshly-created CallSite.
1584   Handle       resolved_appendix;
1585   Handle       resolved_method_type;
1586   methodHandle resolved_method =
1587     SystemDictionary::find_dynamic_call_site_invoker(current_klass,
1588                                                      bootstrap_specifier,
1589                                                      method_name, method_signature,
1590                                                      &resolved_appendix,
1591                                                      &resolved_method_type,
1592                                                      THREAD);
1593   if (HAS_PENDING_EXCEPTION) {
1594     if (TraceMethodHandles) {
1595       tty->print_cr("invokedynamic throws BSME for "INTPTR_FORMAT, (void *)PENDING_EXCEPTION);
1596       PENDING_EXCEPTION->print();
1597     }
1598     if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) {
1599       // throw these guys, since they are already wrapped
1600       return;
1601     }
1602     if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
1603       // intercept only LinkageErrors which might have failed to wrap
1604       return;
1605     }
1606     // See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS.
1607     Handle nested_exception(THREAD, PENDING_EXCEPTION);
1608     CLEAR_PENDING_EXCEPTION;
1609     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
1610   }
1611   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK);
1612 }
1613 
1614 //------------------------------------------------------------------------------------------------------------------------
1615 #ifndef PRODUCT
1616 
1617 void CallInfo::print() {
1618   ResourceMark rm;
1619   const char* kindstr = "unknown";
1620   switch (_call_kind) {
1621   case direct_call: kindstr = "direct"; break;
1622   case vtable_call: kindstr = "vtable"; break;
1623   case itable_call: kindstr = "itable"; break;
1624   }
1625   tty->print_cr("Call %s@%d %s", kindstr, _call_index,
1626                 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
1627 }
1628 
1629 #endif