src/share/vm/interpreter/linkResolver.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial