comparison src/share/vm/classfile/systemDictionary.cpp @ 23143:928e1994ad43 jdk8u51-b10

8075838: Method for typing MethodTypes Reviewed-by: jrose, ahgross, alanb, bmoloden
author vlivanov
date Tue, 14 Apr 2015 19:10:28 +0300
parents 9fa3bf3043a2
children dd9cc155639c 50e62b688ddc
comparison
equal deleted inserted replaced
23142:62c4bd276cbe 23143:928e1994ad43
2347 methodHandle empty; 2347 methodHandle empty;
2348 assert(EnableInvokeDynamic, ""); 2348 assert(EnableInvokeDynamic, "");
2349 assert(!THREAD->is_Compiler_thread(), ""); 2349 assert(!THREAD->is_Compiler_thread(), "");
2350 Handle method_type = 2350 Handle method_type =
2351 SystemDictionary::find_method_handle_type(signature, accessing_klass, CHECK_(empty)); 2351 SystemDictionary::find_method_handle_type(signature, accessing_klass, CHECK_(empty));
2352 if (false) { // FIXME: Decide if the Java upcall should resolve signatures.
2353 method_type = java_lang_String::create_from_symbol(signature, CHECK_(empty));
2354 }
2355 2352
2356 KlassHandle mh_klass = SystemDictionary::MethodHandle_klass(); 2353 KlassHandle mh_klass = SystemDictionary::MethodHandle_klass();
2357 int ref_kind = JVM_REF_invokeVirtual; 2354 int ref_kind = JVM_REF_invokeVirtual;
2358 Handle name_str = StringTable::intern(name, CHECK_(empty)); 2355 Handle name_str = StringTable::intern(name, CHECK_(empty));
2359 objArrayHandle appendix_box = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1, CHECK_(empty)); 2356 objArrayHandle appendix_box = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1, CHECK_(empty));
2381 Handle mname(THREAD, (oop) result.get_jobject()); 2378 Handle mname(THREAD, (oop) result.get_jobject());
2382 (*method_type_result) = method_type; 2379 (*method_type_result) = method_type;
2383 return unpack_method_and_appendix(mname, accessing_klass, appendix_box, appendix_result, THREAD); 2380 return unpack_method_and_appendix(mname, accessing_klass, appendix_box, appendix_result, THREAD);
2384 } 2381 }
2385 2382
2383 // Decide if we can globally cache a lookup of this class, to be returned to any client that asks.
2384 // We must ensure that all class loaders everywhere will reach this class, for any client.
2385 // This is a safe bet for public classes in java.lang, such as Object and String.
2386 // We also include public classes in java.lang.invoke, because they appear frequently in system-level method types.
2387 // Out of an abundance of caution, we do not include any other classes, not even for packages like java.util.
2388 static bool is_always_visible_class(oop mirror) {
2389 Klass* klass = java_lang_Class::as_Klass(mirror);
2390 if (klass->oop_is_objArray()) {
2391 klass = ObjArrayKlass::cast(klass)->bottom_klass(); // check element type
2392 }
2393 if (klass->oop_is_typeArray()) {
2394 return true; // primitive array
2395 }
2396 assert(klass->oop_is_instance(), klass->external_name());
2397 return klass->is_public() &&
2398 (InstanceKlass::cast(klass)->is_same_class_package(SystemDictionary::Object_klass()) || // java.lang
2399 InstanceKlass::cast(klass)->is_same_class_package(SystemDictionary::MethodHandle_klass())); // java.lang.invoke
2400 }
2386 2401
2387 // Ask Java code to find or construct a java.lang.invoke.MethodType for the given 2402 // Ask Java code to find or construct a java.lang.invoke.MethodType for the given
2388 // signature, as interpreted relative to the given class loader. 2403 // signature, as interpreted relative to the given class loader.
2389 // Because of class loader constraints, all method handle usage must be 2404 // Because of class loader constraints, all method handle usage must be
2390 // consistent with this loader. 2405 // consistent with this loader.
2403 warning("SystemDictionary::find_method_handle_type called from compiler thread"); // FIXME 2418 warning("SystemDictionary::find_method_handle_type called from compiler thread"); // FIXME
2404 return Handle(); // do not attempt from within compiler, unless it was cached 2419 return Handle(); // do not attempt from within compiler, unless it was cached
2405 } 2420 }
2406 2421
2407 Handle class_loader, protection_domain; 2422 Handle class_loader, protection_domain;
2408 bool is_on_bcp = true; // keep this true as long as we can materialize from the boot classloader 2423 if (accessing_klass.not_null()) {
2424 class_loader = Handle(THREAD, InstanceKlass::cast(accessing_klass())->class_loader());
2425 protection_domain = Handle(THREAD, InstanceKlass::cast(accessing_klass())->protection_domain());
2426 }
2427 bool can_be_cached = true;
2409 int npts = ArgumentCount(signature).size(); 2428 int npts = ArgumentCount(signature).size();
2410 objArrayHandle pts = oopFactory::new_objArray(SystemDictionary::Class_klass(), npts, CHECK_(empty)); 2429 objArrayHandle pts = oopFactory::new_objArray(SystemDictionary::Class_klass(), npts, CHECK_(empty));
2411 int arg = 0; 2430 int arg = 0;
2412 Handle rt; // the return type from the signature 2431 Handle rt; // the return type from the signature
2413 ResourceMark rm(THREAD); 2432 ResourceMark rm(THREAD);
2414 for (SignatureStream ss(signature); !ss.is_done(); ss.next()) { 2433 for (SignatureStream ss(signature); !ss.is_done(); ss.next()) {
2415 oop mirror = NULL; 2434 oop mirror = NULL;
2416 if (is_on_bcp) { 2435 if (can_be_cached) {
2417 // Note: class_loader & protection_domain are both null at this point. 2436 // Use neutral class loader to lookup candidate classes to be placed in the cache.
2418 mirror = ss.as_java_mirror(class_loader, protection_domain, 2437 mirror = ss.as_java_mirror(Handle(), Handle(),
2419 SignatureStream::ReturnNull, CHECK_(empty)); 2438 SignatureStream::ReturnNull, CHECK_(empty));
2420 if (mirror == NULL) { 2439 if (mirror == NULL || (ss.is_object() && !is_always_visible_class(mirror))) {
2421 // fall back from BCP to accessing_klass 2440 // Fall back to accessing_klass context.
2422 if (accessing_klass.not_null()) { 2441 can_be_cached = false;
2423 class_loader = Handle(THREAD, InstanceKlass::cast(accessing_klass())->class_loader());
2424 protection_domain = Handle(THREAD, InstanceKlass::cast(accessing_klass())->protection_domain());
2425 }
2426 is_on_bcp = false;
2427 } 2442 }
2428 } 2443 }
2429 if (!is_on_bcp) { 2444 if (!can_be_cached) {
2430 // Resolve, throwing a real error if it doesn't work. 2445 // Resolve, throwing a real error if it doesn't work.
2431 mirror = ss.as_java_mirror(class_loader, protection_domain, 2446 mirror = ss.as_java_mirror(class_loader, protection_domain,
2432 SignatureStream::NCDFError, CHECK_(empty)); 2447 SignatureStream::NCDFError, CHECK_(empty));
2433 } 2448 }
2449 assert(!oopDesc::is_null(mirror), ss.as_symbol(THREAD)->as_C_string());
2434 if (ss.at_return_type()) 2450 if (ss.at_return_type())
2435 rt = Handle(THREAD, mirror); 2451 rt = Handle(THREAD, mirror);
2436 else 2452 else
2437 pts->obj_at_put(arg++, mirror); 2453 pts->obj_at_put(arg++, mirror);
2438 2454
2460 vmSymbols::findMethodHandleType_name(), 2476 vmSymbols::findMethodHandleType_name(),
2461 vmSymbols::findMethodHandleType_signature(), 2477 vmSymbols::findMethodHandleType_signature(),
2462 &args, CHECK_(empty)); 2478 &args, CHECK_(empty));
2463 Handle method_type(THREAD, (oop) result.get_jobject()); 2479 Handle method_type(THREAD, (oop) result.get_jobject());
2464 2480
2465 if (is_on_bcp) { 2481 if (can_be_cached) {
2466 // We can cache this MethodType inside the JVM. 2482 // We can cache this MethodType inside the JVM.
2467 MutexLocker ml(SystemDictionary_lock, THREAD); 2483 MutexLocker ml(SystemDictionary_lock, THREAD);
2468 spe = invoke_method_table()->find_entry(index, hash, signature, null_iid); 2484 spe = invoke_method_table()->find_entry(index, hash, signature, null_iid);
2469 if (spe == NULL) 2485 if (spe == NULL)
2470 spe = invoke_method_table()->add_entry(index, hash, signature, null_iid); 2486 spe = invoke_method_table()->add_entry(index, hash, signature, null_iid);