src/share/vm/prims/methodHandles.cpp

Fri, 13 Sep 2013 22:38:02 -0400

author
drchase
date
Fri, 13 Sep 2013 22:38:02 -0400
changeset 5732
b2e698d2276c
parent 5354
c1bd7b5bdc70
child 5848
ac9cb1d5a202
permissions
-rw-r--r--

8014013: CallInfo structure no longer accurately reports the result of a LinkResolver operation
Summary: Enhance method resolution and resulting data structures, plus some refactoring.
Reviewed-by: twisti, acorn, jrose

jrose@1145 1 /*
sspitsyn@5178 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
jrose@1145 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jrose@1145 4 *
jrose@1145 5 * This code is free software; you can redistribute it and/or modify it
jrose@1145 6 * under the terms of the GNU General Public License version 2 only, as
jrose@1145 7 * published by the Free Software Foundation.
jrose@1145 8 *
jrose@1145 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jrose@1145 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jrose@1145 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jrose@1145 12 * version 2 for more details (a copy is included in the LICENSE file that
jrose@1145 13 * accompanied this code).
jrose@1145 14 *
jrose@1145 15 * You should have received a copy of the GNU General Public License version
jrose@1145 16 * 2 along with this work; if not, write to the Free Software Foundation,
jrose@1145 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jrose@1145 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
jrose@1145 22 *
jrose@1145 23 */
jrose@1145 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/symbolTable.hpp"
jrose@2982 27 #include "compiler/compileBroker.hpp"
stefank@2314 28 #include "interpreter/interpreter.hpp"
never@2920 29 #include "interpreter/oopMapCache.hpp"
stefank@2314 30 #include "memory/allocation.inline.hpp"
stefank@2314 31 #include "memory/oopFactory.hpp"
sspitsyn@4965 32 #include "prims/jvmtiRedefineClassesTrace.hpp"
stefank@2314 33 #include "prims/methodHandles.hpp"
jrose@2982 34 #include "runtime/compilationPolicy.hpp"
stefank@2314 35 #include "runtime/javaCalls.hpp"
stefank@2314 36 #include "runtime/reflection.hpp"
stefank@2314 37 #include "runtime/signature.hpp"
stefank@2314 38 #include "runtime/stubRoutines.hpp"
stefank@2314 39
twisti@3969 40
jrose@1145 41 /*
jrose@1145 42 * JSR 292 reference implementation: method handles
twisti@3969 43 * The JDK 7 reference implementation represented method handle
twisti@3969 44 * combinations as chains. Each link in the chain had a "vmentry"
twisti@3969 45 * field which pointed at a bit of assembly code which performed
twisti@3969 46 * one transformation before dispatching to the next link in the chain.
twisti@3969 47 *
twisti@3969 48 * The current reference implementation pushes almost all code generation
twisti@3969 49 * responsibility to (trusted) Java code. A method handle contains a
twisti@3969 50 * pointer to its "LambdaForm", which embodies all details of the method
twisti@3969 51 * handle's behavior. The LambdaForm is a normal Java object, managed
twisti@3969 52 * by a runtime coded in Java.
jrose@1145 53 */
jrose@1145 54
jrose@1145 55 bool MethodHandles::_enabled = false; // set true after successful native linkage
never@2950 56 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = NULL;
twisti@1734 57
twisti@1734 58 //------------------------------------------------------------------------------
twisti@1734 59 // MethodHandles::generate_adapters
twisti@1734 60 //
twisti@2436 61 void MethodHandles::generate_adapters() {
twisti@2698 62 if (!EnableInvokeDynamic || SystemDictionary::MethodHandle_klass() == NULL) return;
twisti@1734 63
twisti@1734 64 assert(_adapter_code == NULL, "generate only once");
twisti@1734 65
twisti@1734 66 ResourceMark rm;
twisti@1734 67 TraceTime timer("MethodHandles adapters generation", TraceStartupTime);
never@2950 68 _adapter_code = MethodHandlesAdapterBlob::create(adapter_code_size);
twisti@1734 69 if (_adapter_code == NULL)
ccheung@4993 70 vm_exit_out_of_memory(adapter_code_size, OOM_MALLOC_ERROR,
ccheung@4993 71 "CodeCache: no room for MethodHandles adapters");
never@3255 72 {
never@3255 73 CodeBuffer code(_adapter_code);
never@3255 74 MethodHandlesAdapterGenerator g(&code);
never@3255 75 g.generate();
never@3255 76 code.log_section_sizes("MethodHandlesAdapterBlob");
never@3255 77 }
twisti@1734 78 }
twisti@1734 79
twisti@1734 80 //------------------------------------------------------------------------------
twisti@1734 81 // MethodHandlesAdapterGenerator::generate
twisti@1734 82 //
twisti@2436 83 void MethodHandlesAdapterGenerator::generate() {
twisti@1734 84 // Generate generic method handle adapters.
twisti@3969 85 // Generate interpreter entries
twisti@3969 86 for (Interpreter::MethodKind mk = Interpreter::method_handle_invoke_FIRST;
twisti@3969 87 mk <= Interpreter::method_handle_invoke_LAST;
twisti@3969 88 mk = Interpreter::MethodKind(1 + (int)mk)) {
twisti@3969 89 vmIntrinsics::ID iid = Interpreter::method_handle_intrinsic(mk);
twisti@3969 90 StubCodeMark mark(this, "MethodHandle::interpreter_entry", vmIntrinsics::name_at(iid));
twisti@3969 91 address entry = MethodHandles::generate_method_handle_interpreter_entry(_masm, iid);
twisti@3969 92 if (entry != NULL) {
twisti@3969 93 Interpreter::set_entry_for_kind(mk, entry);
never@2895 94 }
twisti@3969 95 // If the entry is not set, it will throw AbstractMethodError.
twisti@1734 96 }
twisti@1734 97 }
twisti@1734 98
jrose@1145 99 void MethodHandles::set_enabled(bool z) {
jrose@1145 100 if (_enabled != z) {
twisti@2698 101 guarantee(z && EnableInvokeDynamic, "can only enable once, and only if -XX:+EnableInvokeDynamic");
jrose@1145 102 _enabled = z;
jrose@1145 103 }
jrose@1145 104 }
jrose@1145 105
jrose@1145 106 // MemberName support
jrose@1145 107
jrose@2639 108 // import java_lang_invoke_MemberName.*
jrose@1145 109 enum {
twisti@4866 110 IS_METHOD = java_lang_invoke_MemberName::MN_IS_METHOD,
twisti@4866 111 IS_CONSTRUCTOR = java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR,
twisti@4866 112 IS_FIELD = java_lang_invoke_MemberName::MN_IS_FIELD,
twisti@4866 113 IS_TYPE = java_lang_invoke_MemberName::MN_IS_TYPE,
twisti@4866 114 CALLER_SENSITIVE = java_lang_invoke_MemberName::MN_CALLER_SENSITIVE,
twisti@3969 115 REFERENCE_KIND_SHIFT = java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT,
twisti@3969 116 REFERENCE_KIND_MASK = java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK,
twisti@4866 117 SEARCH_SUPERCLASSES = java_lang_invoke_MemberName::MN_SEARCH_SUPERCLASSES,
twisti@4866 118 SEARCH_INTERFACES = java_lang_invoke_MemberName::MN_SEARCH_INTERFACES,
twisti@3969 119 ALL_KINDS = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE
jrose@1145 120 };
jrose@1145 121
jrose@1862 122 Handle MethodHandles::new_MemberName(TRAPS) {
jrose@1862 123 Handle empty;
jrose@1862 124 instanceKlassHandle k(THREAD, SystemDictionary::MemberName_klass());
jrose@1862 125 if (!k->is_initialized()) k->initialize(CHECK_(empty));
jrose@1862 126 return Handle(THREAD, k->allocate_instance(THREAD));
jrose@1862 127 }
jrose@1862 128
sspitsyn@4965 129 oop MethodHandles::init_MemberName(Handle mname, Handle target) {
drchase@5732 130 // This method is used from java.lang.invoke.MemberName constructors.
drchase@5732 131 // It fills in the new MemberName from a java.lang.reflect.Member.
sspitsyn@4965 132 Thread* thread = Thread::current();
sspitsyn@4965 133 oop target_oop = target();
coleenp@4037 134 Klass* target_klass = target_oop->klass();
twisti@3969 135 if (target_klass == SystemDictionary::reflect_Field_klass()) {
jrose@1145 136 oop clazz = java_lang_reflect_Field::clazz(target_oop); // fd.field_holder()
jrose@1145 137 int slot = java_lang_reflect_Field::slot(target_oop); // fd.index()
sspitsyn@4965 138 KlassHandle k(thread, java_lang_Class::as_Klass(clazz));
drchase@5732 139 if (!k.is_null() && k->oop_is_instance()) {
drchase@5732 140 fieldDescriptor fd(InstanceKlass::cast(k()), slot);
drchase@5732 141 oop mname2 = init_field_MemberName(mname, fd);
drchase@5732 142 if (mname2 != NULL) {
drchase@5732 143 // Since we have the reified name and type handy, add them to the result.
drchase@5732 144 if (java_lang_invoke_MemberName::name(mname2) == NULL)
drchase@5732 145 java_lang_invoke_MemberName::set_name(mname2, java_lang_reflect_Field::name(target_oop));
drchase@5732 146 if (java_lang_invoke_MemberName::type(mname2) == NULL)
drchase@5732 147 java_lang_invoke_MemberName::set_type(mname2, java_lang_reflect_Field::type(target_oop));
drchase@5732 148 }
drchase@5732 149 return mname2;
drchase@5732 150 }
twisti@3969 151 } else if (target_klass == SystemDictionary::reflect_Method_klass()) {
twisti@3969 152 oop clazz = java_lang_reflect_Method::clazz(target_oop);
twisti@3969 153 int slot = java_lang_reflect_Method::slot(target_oop);
sspitsyn@4965 154 KlassHandle k(thread, java_lang_Class::as_Klass(clazz));
sspitsyn@4965 155 if (!k.is_null() && k->oop_is_instance()) {
sspitsyn@4965 156 Method* m = InstanceKlass::cast(k())->method_with_idnum(slot);
drchase@5732 157 if (m == NULL || is_signature_polymorphic(m->intrinsic_id()))
drchase@5732 158 return NULL; // do not resolve unless there is a concrete signature
drchase@5732 159 CallInfo info(m, k());
drchase@5732 160 return init_method_MemberName(mname, info);
twisti@3969 161 }
twisti@3969 162 } else if (target_klass == SystemDictionary::reflect_Constructor_klass()) {
twisti@3969 163 oop clazz = java_lang_reflect_Constructor::clazz(target_oop);
twisti@3969 164 int slot = java_lang_reflect_Constructor::slot(target_oop);
sspitsyn@4965 165 KlassHandle k(thread, java_lang_Class::as_Klass(clazz));
sspitsyn@4965 166 if (!k.is_null() && k->oop_is_instance()) {
sspitsyn@4965 167 Method* m = InstanceKlass::cast(k())->method_with_idnum(slot);
drchase@5732 168 if (m == NULL) return NULL;
drchase@5732 169 CallInfo info(m, k());
drchase@5732 170 return init_method_MemberName(mname, info);
twisti@3969 171 }
jrose@1145 172 }
twisti@3969 173 return NULL;
jrose@1145 174 }
jrose@1145 175
drchase@5732 176 oop MethodHandles::init_method_MemberName(Handle mname, CallInfo& info) {
drchase@5732 177 assert(info.resolved_appendix().is_null(), "only normal methods here");
drchase@5732 178 KlassHandle receiver_limit = info.resolved_klass();
drchase@5732 179 methodHandle m = info.resolved_method();
drchase@5732 180 int flags = (jushort)( m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS );
drchase@5732 181 int vmindex = Method::invalid_vtable_index;
drchase@5732 182
drchase@5732 183 switch (info.call_kind()) {
drchase@5732 184 case CallInfo::itable_call:
drchase@5732 185 vmindex = info.itable_index();
drchase@5732 186 // More importantly, the itable index only works with the method holder.
drchase@5732 187 receiver_limit = m->method_holder();
drchase@5732 188 assert(receiver_limit->verify_itable_index(vmindex), "");
twisti@3969 189 flags |= IS_METHOD | (JVM_REF_invokeInterface << REFERENCE_KIND_SHIFT);
drchase@5732 190 break;
drchase@5732 191
drchase@5732 192 case CallInfo::vtable_call:
drchase@5732 193 vmindex = info.vtable_index();
twisti@3969 194 flags |= IS_METHOD | (JVM_REF_invokeVirtual << REFERENCE_KIND_SHIFT);
drchase@5732 195 assert(receiver_limit->is_subtype_of(m->method_holder()), "virtual call must be type-safe");
drchase@5732 196 break;
drchase@5732 197
drchase@5732 198 case CallInfo::direct_call:
drchase@5732 199 vmindex = Method::nonvirtual_vtable_index;
drchase@5732 200 if (m->is_static()) {
drchase@5732 201 flags |= IS_METHOD | (JVM_REF_invokeStatic << REFERENCE_KIND_SHIFT);
drchase@5732 202 } else if (m->is_initializer()) {
drchase@5732 203 flags |= IS_CONSTRUCTOR | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT);
drchase@5732 204 assert(receiver_limit == m->method_holder(), "constructor call must be exactly typed");
drchase@5732 205 } else {
drchase@5732 206 flags |= IS_METHOD | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT);
drchase@5732 207 assert(receiver_limit->is_subtype_of(m->method_holder()), "special call must be type-safe");
drchase@5732 208 }
drchase@5732 209 break;
drchase@5732 210
drchase@5732 211 default: assert(false, "bad CallInfo"); return NULL;
twisti@3969 212 }
twisti@3969 213
twisti@4866 214 // @CallerSensitive annotation detected
twisti@4866 215 if (m->caller_sensitive()) {
twisti@4866 216 flags |= CALLER_SENSITIVE;
twisti@4866 217 }
twisti@4866 218
sspitsyn@4965 219 oop mname_oop = mname();
twisti@4866 220 java_lang_invoke_MemberName::set_flags( mname_oop, flags);
drchase@5732 221 java_lang_invoke_MemberName::set_vmtarget(mname_oop, m());
twisti@4866 222 java_lang_invoke_MemberName::set_vmindex( mname_oop, vmindex); // vtable/itable index
twisti@4866 223 java_lang_invoke_MemberName::set_clazz( mname_oop, receiver_limit->java_mirror());
twisti@3969 224 // Note: name and type can be lazily computed by resolve_MemberName,
twisti@3969 225 // if Java code needs them as resolved String and MethodType objects.
twisti@3969 226 // The clazz must be eagerly stored, because it provides a GC
coleenp@4037 227 // root to help keep alive the Method*.
twisti@3969 228 // If relevant, the vtable or itable value is stored as vmindex.
twisti@3969 229 // This is done eagerly, since it is readily available without
twisti@3969 230 // constructing any new objects.
twisti@3969 231 // TO DO: maybe intern mname_oop
sspitsyn@5178 232 m->method_holder()->add_member_name(m->method_idnum(), mname);
sspitsyn@5178 233
sspitsyn@4965 234 return mname();
twisti@3969 235 }
twisti@3969 236
drchase@5732 237 oop MethodHandles::init_field_MemberName(Handle mname, fieldDescriptor& fd, bool is_setter) {
drchase@5732 238 int flags = (jushort)( fd.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS );
drchase@5732 239 flags |= IS_FIELD | ((fd.is_static() ? JVM_REF_getStatic : JVM_REF_getField) << REFERENCE_KIND_SHIFT);
twisti@3969 240 if (is_setter) flags += ((JVM_REF_putField - JVM_REF_getField) << REFERENCE_KIND_SHIFT);
drchase@5732 241 Metadata* vmtarget = fd.field_holder();
drchase@5732 242 int vmindex = fd.offset(); // determines the field uniquely when combined with static bit
sspitsyn@4965 243 oop mname_oop = mname();
twisti@3969 244 java_lang_invoke_MemberName::set_flags(mname_oop, flags);
jrose@2639 245 java_lang_invoke_MemberName::set_vmtarget(mname_oop, vmtarget);
jrose@2639 246 java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex);
drchase@5732 247 java_lang_invoke_MemberName::set_clazz(mname_oop, fd.field_holder()->java_mirror());
drchase@5732 248 oop type = field_signature_type_or_null(fd.signature());
drchase@5732 249 oop name = field_name_or_null(fd.name());
twisti@3969 250 if (name != NULL)
twisti@3969 251 java_lang_invoke_MemberName::set_name(mname_oop, name);
twisti@3969 252 if (type != NULL)
twisti@3969 253 java_lang_invoke_MemberName::set_type(mname_oop, type);
twisti@3969 254 // Note: name and type can be lazily computed by resolve_MemberName,
twisti@3969 255 // if Java code needs them as resolved String and Class objects.
twisti@3969 256 // Note that the incoming type oop might be pre-resolved (non-null).
twisti@3969 257 // The base clazz and field offset (vmindex) must be eagerly stored,
twisti@3969 258 // because they unambiguously identify the field.
twisti@3969 259 // Although the fieldDescriptor::_index would also identify the field,
twisti@3969 260 // we do not use it, because it is harder to decode.
twisti@3969 261 // TO DO: maybe intern mname_oop
sspitsyn@4965 262 return mname();
jrose@1145 263 }
jrose@1145 264
twisti@3969 265 // JVM 2.9 Special Methods:
twisti@3969 266 // A method is signature polymorphic if and only if all of the following conditions hold :
twisti@3969 267 // * It is declared in the java.lang.invoke.MethodHandle class.
twisti@3969 268 // * It has a single formal parameter of type Object[].
twisti@3969 269 // * It has a return type of Object.
twisti@3969 270 // * It has the ACC_VARARGS and ACC_NATIVE flags set.
coleenp@4037 271 bool MethodHandles::is_method_handle_invoke_name(Klass* klass, Symbol* name) {
twisti@3969 272 if (klass == NULL)
twisti@3969 273 return false;
twisti@3969 274 // The following test will fail spuriously during bootstrap of MethodHandle itself:
twisti@3969 275 // if (klass != SystemDictionary::MethodHandle_klass())
twisti@3969 276 // Test the name instead:
hseigel@4278 277 if (klass->name() != vmSymbols::java_lang_invoke_MethodHandle())
twisti@3969 278 return false;
twisti@3969 279 Symbol* poly_sig = vmSymbols::object_array_object_signature();
coleenp@4037 280 Method* m = InstanceKlass::cast(klass)->find_method(name, poly_sig);
twisti@3969 281 if (m == NULL) return false;
twisti@3969 282 int required = JVM_ACC_NATIVE | JVM_ACC_VARARGS;
twisti@3969 283 int flags = m->access_flags().as_int();
twisti@3969 284 return (flags & required) == required;
jrose@1145 285 }
jrose@1145 286
twisti@3969 287
twisti@3969 288 Symbol* MethodHandles::signature_polymorphic_intrinsic_name(vmIntrinsics::ID iid) {
twisti@3969 289 assert(is_signature_polymorphic_intrinsic(iid), err_msg("iid=%d", iid));
twisti@3969 290 switch (iid) {
twisti@3969 291 case vmIntrinsics::_invokeBasic: return vmSymbols::invokeBasic_name();
twisti@3969 292 case vmIntrinsics::_linkToVirtual: return vmSymbols::linkToVirtual_name();
twisti@3969 293 case vmIntrinsics::_linkToStatic: return vmSymbols::linkToStatic_name();
twisti@3969 294 case vmIntrinsics::_linkToSpecial: return vmSymbols::linkToSpecial_name();
twisti@3969 295 case vmIntrinsics::_linkToInterface: return vmSymbols::linkToInterface_name();
twisti@3969 296 }
twisti@3969 297 assert(false, "");
twisti@3969 298 return 0;
twisti@3969 299 }
twisti@3969 300
twisti@3969 301 int MethodHandles::signature_polymorphic_intrinsic_ref_kind(vmIntrinsics::ID iid) {
twisti@3969 302 switch (iid) {
twisti@3969 303 case vmIntrinsics::_invokeBasic: return 0;
twisti@3969 304 case vmIntrinsics::_linkToVirtual: return JVM_REF_invokeVirtual;
twisti@3969 305 case vmIntrinsics::_linkToStatic: return JVM_REF_invokeStatic;
twisti@3969 306 case vmIntrinsics::_linkToSpecial: return JVM_REF_invokeSpecial;
twisti@3969 307 case vmIntrinsics::_linkToInterface: return JVM_REF_invokeInterface;
twisti@3969 308 }
twisti@3969 309 assert(false, err_msg("iid=%d", iid));
twisti@3969 310 return 0;
twisti@3969 311 }
twisti@3969 312
twisti@3969 313 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Symbol* name) {
twisti@3969 314 vmSymbols::SID name_id = vmSymbols::find_sid(name);
twisti@3969 315 switch (name_id) {
twisti@3969 316 // The ID _invokeGeneric stands for all non-static signature-polymorphic methods, except built-ins.
twisti@3969 317 case vmSymbols::VM_SYMBOL_ENUM_NAME(invoke_name): return vmIntrinsics::_invokeGeneric;
twisti@3969 318 // The only built-in non-static signature-polymorphic method is MethodHandle.invokeBasic:
twisti@3969 319 case vmSymbols::VM_SYMBOL_ENUM_NAME(invokeBasic_name): return vmIntrinsics::_invokeBasic;
twisti@3969 320
twisti@3969 321 // There is one static signature-polymorphic method for each JVM invocation mode.
twisti@3969 322 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToVirtual_name): return vmIntrinsics::_linkToVirtual;
twisti@3969 323 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToStatic_name): return vmIntrinsics::_linkToStatic;
twisti@3969 324 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToSpecial_name): return vmIntrinsics::_linkToSpecial;
twisti@3969 325 case vmSymbols::VM_SYMBOL_ENUM_NAME(linkToInterface_name): return vmIntrinsics::_linkToInterface;
twisti@3969 326 }
twisti@3969 327
twisti@3969 328 // Cover the case of invokeExact and any future variants of invokeFoo.
coleenp@4037 329 Klass* mh_klass = SystemDictionary::well_known_klass(
twisti@3969 330 SystemDictionary::WK_KLASS_ENUM_NAME(MethodHandle_klass) );
twisti@3969 331 if (mh_klass != NULL && is_method_handle_invoke_name(mh_klass, name))
twisti@3969 332 return vmIntrinsics::_invokeGeneric;
twisti@3969 333
twisti@3969 334 // Note: The pseudo-intrinsic _compiledLambdaForm is never linked against.
twisti@3969 335 // Instead it is used to mark lambda forms bound to invokehandle or invokedynamic.
twisti@3969 336 return vmIntrinsics::_none;
twisti@3969 337 }
twisti@3969 338
coleenp@4037 339 vmIntrinsics::ID MethodHandles::signature_polymorphic_name_id(Klass* klass, Symbol* name) {
twisti@3969 340 if (klass != NULL &&
hseigel@4278 341 klass->name() == vmSymbols::java_lang_invoke_MethodHandle()) {
twisti@3969 342 vmIntrinsics::ID iid = signature_polymorphic_name_id(name);
twisti@3969 343 if (iid != vmIntrinsics::_none)
twisti@3969 344 return iid;
twisti@3969 345 if (is_method_handle_invoke_name(klass, name))
twisti@3969 346 return vmIntrinsics::_invokeGeneric;
twisti@3969 347 }
twisti@3969 348 return vmIntrinsics::_none;
twisti@3969 349 }
twisti@3969 350
twisti@3969 351
coleenp@2497 352 // convert the external string or reflective type to an internal signature
twisti@3969 353 Symbol* MethodHandles::lookup_signature(oop type_str, bool intern_if_not_found, TRAPS) {
jrose@2639 354 if (java_lang_invoke_MethodType::is_instance(type_str)) {
twisti@3969 355 return java_lang_invoke_MethodType::as_signature(type_str, intern_if_not_found, CHECK_NULL);
coleenp@2497 356 } else if (java_lang_Class::is_instance(type_str)) {
coleenp@2497 357 return java_lang_Class::as_signature(type_str, false, CHECK_NULL);
coleenp@2497 358 } else if (java_lang_String::is_instance(type_str)) {
twisti@3969 359 if (intern_if_not_found) {
coleenp@2497 360 return java_lang_String::as_symbol(type_str, CHECK_NULL);
coleenp@2497 361 } else {
coleenp@2497 362 return java_lang_String::as_symbol_or_null(type_str);
coleenp@2497 363 }
coleenp@2497 364 } else {
coleenp@2497 365 THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized type", NULL);
coleenp@2497 366 }
coleenp@2497 367 }
coleenp@2497 368
twisti@3969 369 static const char OBJ_SIG[] = "Ljava/lang/Object;";
twisti@3969 370 enum { OBJ_SIG_LEN = 18 };
twisti@3969 371
twisti@3969 372 bool MethodHandles::is_basic_type_signature(Symbol* sig) {
twisti@3969 373 assert(vmSymbols::object_signature()->utf8_length() == (int)OBJ_SIG_LEN, "");
twisti@3969 374 assert(vmSymbols::object_signature()->equals(OBJ_SIG), "");
twisti@3969 375 const int len = sig->utf8_length();
twisti@3969 376 for (int i = 0; i < len; i++) {
twisti@3969 377 switch (sig->byte_at(i)) {
twisti@3969 378 case 'L':
twisti@3969 379 // only java/lang/Object is valid here
twisti@3969 380 if (sig->index_of_at(i, OBJ_SIG, OBJ_SIG_LEN) != i)
twisti@3969 381 return false;
twisti@3969 382 i += OBJ_SIG_LEN-1; //-1 because of i++ in loop
twisti@3969 383 continue;
twisti@3969 384 case '(': case ')': case 'V':
twisti@3969 385 case 'I': case 'J': case 'F': case 'D':
twisti@3969 386 continue;
twisti@3969 387 //case '[':
twisti@3969 388 //case 'Z': case 'B': case 'C': case 'S':
twisti@3969 389 default:
twisti@3969 390 return false;
twisti@3969 391 }
twisti@3969 392 }
twisti@3969 393 return true;
twisti@3969 394 }
twisti@3969 395
twisti@3969 396 Symbol* MethodHandles::lookup_basic_type_signature(Symbol* sig, bool keep_last_arg, TRAPS) {
twisti@3969 397 Symbol* bsig = NULL;
twisti@3969 398 if (sig == NULL) {
twisti@3969 399 return sig;
twisti@3969 400 } else if (is_basic_type_signature(sig)) {
twisti@3969 401 sig->increment_refcount();
twisti@3969 402 return sig; // that was easy
twisti@3969 403 } else if (sig->byte_at(0) != '(') {
twisti@3969 404 BasicType bt = char2type(sig->byte_at(0));
twisti@3969 405 if (is_subword_type(bt)) {
twisti@3969 406 bsig = vmSymbols::int_signature();
twisti@3969 407 } else {
twisti@3969 408 assert(bt == T_OBJECT || bt == T_ARRAY, "is_basic_type_signature was false");
twisti@3969 409 bsig = vmSymbols::object_signature();
twisti@3969 410 }
twisti@3969 411 } else {
twisti@3969 412 ResourceMark rm;
twisti@3969 413 stringStream buffer(128);
twisti@3969 414 buffer.put('(');
twisti@3969 415 int arg_pos = 0, keep_arg_pos = -1;
twisti@3969 416 if (keep_last_arg)
twisti@3969 417 keep_arg_pos = ArgumentCount(sig).size() - 1;
twisti@3969 418 for (SignatureStream ss(sig); !ss.is_done(); ss.next()) {
twisti@3969 419 BasicType bt = ss.type();
twisti@3969 420 size_t this_arg_pos = buffer.size();
twisti@3969 421 if (ss.at_return_type()) {
twisti@3969 422 buffer.put(')');
twisti@3969 423 }
twisti@3969 424 if (arg_pos == keep_arg_pos) {
twisti@3969 425 buffer.write((char*) ss.raw_bytes(),
twisti@3969 426 (int) ss.raw_length());
twisti@3969 427 } else if (bt == T_OBJECT || bt == T_ARRAY) {
twisti@3969 428 buffer.write(OBJ_SIG, OBJ_SIG_LEN);
twisti@3969 429 } else {
twisti@3969 430 if (is_subword_type(bt))
twisti@3969 431 bt = T_INT;
twisti@3969 432 buffer.put(type2char(bt));
twisti@3969 433 }
twisti@3969 434 arg_pos++;
twisti@3969 435 }
twisti@3969 436 const char* sigstr = buffer.base();
twisti@3969 437 int siglen = (int) buffer.size();
twisti@3969 438 bsig = SymbolTable::new_symbol(sigstr, siglen, THREAD);
twisti@3969 439 }
twisti@3969 440 assert(is_basic_type_signature(bsig) ||
twisti@3969 441 // detune assert in case the injected argument is not a basic type:
twisti@3969 442 keep_last_arg, "");
twisti@3969 443 return bsig;
twisti@3969 444 }
twisti@3969 445
twisti@3969 446 void MethodHandles::print_as_basic_type_signature_on(outputStream* st,
twisti@3969 447 Symbol* sig,
twisti@3969 448 bool keep_arrays,
twisti@3969 449 bool keep_basic_names) {
twisti@3969 450 st = st ? st : tty;
twisti@3969 451 int len = sig->utf8_length();
twisti@3969 452 int array = 0;
twisti@3969 453 bool prev_type = false;
twisti@3969 454 for (int i = 0; i < len; i++) {
twisti@3969 455 char ch = sig->byte_at(i);
twisti@3969 456 switch (ch) {
twisti@3969 457 case '(': case ')':
twisti@3969 458 prev_type = false;
twisti@3969 459 st->put(ch);
twisti@3969 460 continue;
twisti@3969 461 case '[':
twisti@3969 462 if (!keep_basic_names && keep_arrays)
twisti@3969 463 st->put(ch);
twisti@3969 464 array++;
twisti@3969 465 continue;
twisti@3969 466 case 'L':
twisti@3969 467 {
twisti@3969 468 if (prev_type) st->put(',');
twisti@3969 469 int start = i+1, slash = start;
twisti@3969 470 while (++i < len && (ch = sig->byte_at(i)) != ';') {
twisti@3969 471 if (ch == '/' || ch == '.' || ch == '$') slash = i+1;
twisti@3969 472 }
twisti@3969 473 if (slash < i) start = slash;
twisti@3969 474 if (!keep_basic_names) {
twisti@3969 475 st->put('L');
twisti@3969 476 } else {
twisti@3969 477 for (int j = start; j < i; j++)
twisti@3969 478 st->put(sig->byte_at(j));
twisti@3969 479 prev_type = true;
twisti@3969 480 }
twisti@3969 481 break;
twisti@3969 482 }
twisti@3969 483 default:
twisti@3969 484 {
twisti@3969 485 if (array && char2type(ch) != T_ILLEGAL && !keep_arrays) {
twisti@3969 486 ch = '[';
twisti@3969 487 array = 0;
twisti@3969 488 }
twisti@3969 489 if (prev_type) st->put(',');
twisti@3969 490 const char* n = NULL;
twisti@3969 491 if (keep_basic_names)
twisti@3969 492 n = type2name(char2type(ch));
twisti@3969 493 if (n == NULL) {
twisti@3969 494 // unknown letter, or we don't want to know its name
twisti@3969 495 st->put(ch);
twisti@3969 496 } else {
twisti@3969 497 st->print(n);
twisti@3969 498 prev_type = true;
twisti@3969 499 }
twisti@3969 500 break;
twisti@3969 501 }
twisti@3969 502 }
twisti@3969 503 // Switch break goes here to take care of array suffix:
twisti@3969 504 if (prev_type) {
twisti@3969 505 while (array > 0) {
twisti@3969 506 st->print("[]");
twisti@3969 507 --array;
twisti@3969 508 }
twisti@3969 509 }
twisti@3969 510 array = 0;
twisti@3969 511 }
twisti@3969 512 }
twisti@3969 513
twisti@3969 514
twisti@3969 515
twisti@3969 516 static oop object_java_mirror() {
hseigel@4278 517 return SystemDictionary::Object_klass()->java_mirror();
twisti@3969 518 }
twisti@3969 519
drchase@5732 520 oop MethodHandles::field_name_or_null(Symbol* s) {
twisti@3969 521 if (s == NULL) return NULL;
twisti@3969 522 return StringTable::lookup(s);
twisti@3969 523 }
twisti@3969 524
drchase@5732 525 oop MethodHandles::field_signature_type_or_null(Symbol* s) {
twisti@3969 526 if (s == NULL) return NULL;
twisti@3969 527 BasicType bt = FieldType::basic_type(s);
twisti@3969 528 if (is_java_primitive(bt)) {
twisti@3969 529 assert(s->utf8_length() == 1, "");
twisti@3969 530 return java_lang_Class::primitive_mirror(bt);
twisti@3969 531 }
twisti@3969 532 // Here are some more short cuts for common types.
twisti@3969 533 // They are optional, since reference types can be resolved lazily.
twisti@3969 534 if (bt == T_OBJECT) {
twisti@3969 535 if (s == vmSymbols::object_signature()) {
twisti@3969 536 return object_java_mirror();
twisti@3969 537 } else if (s == vmSymbols::class_signature()) {
hseigel@4278 538 return SystemDictionary::Class_klass()->java_mirror();
twisti@3969 539 } else if (s == vmSymbols::string_signature()) {
hseigel@4278 540 return SystemDictionary::String_klass()->java_mirror();
twisti@3969 541 }
twisti@3969 542 }
twisti@3969 543 return NULL;
twisti@3969 544 }
twisti@3969 545
coleenp@4037 546
jrose@1145 547 // An unresolved member name is a mere symbolic reference.
jrose@1145 548 // Resolving it plants a vmtarget/vmindex in it,
coleenp@4037 549 // which refers directly to JVM internals.
twisti@3969 550 Handle MethodHandles::resolve_MemberName(Handle mname, TRAPS) {
twisti@3969 551 Handle empty;
jrose@2639 552 assert(java_lang_invoke_MemberName::is_instance(mname()), "");
twisti@3969 553
twisti@3969 554 if (java_lang_invoke_MemberName::vmtarget(mname()) != NULL) {
twisti@3969 555 // Already resolved.
twisti@3969 556 DEBUG_ONLY(int vmindex = java_lang_invoke_MemberName::vmindex(mname()));
coleenp@4037 557 assert(vmindex >= Method::nonvirtual_vtable_index, "");
twisti@3969 558 return mname;
twisti@3969 559 }
twisti@3969 560
twisti@2806 561 Handle defc_oop(THREAD, java_lang_invoke_MemberName::clazz(mname()));
twisti@2806 562 Handle name_str(THREAD, java_lang_invoke_MemberName::name( mname()));
twisti@2806 563 Handle type_str(THREAD, java_lang_invoke_MemberName::type( mname()));
twisti@2806 564 int flags = java_lang_invoke_MemberName::flags(mname());
twisti@3969 565 int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK;
twisti@3969 566 if (!ref_kind_is_valid(ref_kind)) {
twisti@3969 567 THROW_MSG_(vmSymbols::java_lang_InternalError(), "obsolete MemberName format", empty);
twisti@3969 568 }
twisti@3969 569
twisti@3969 570 DEBUG_ONLY(int old_vmindex);
twisti@3969 571 assert((old_vmindex = java_lang_invoke_MemberName::vmindex(mname())) == 0, "clean input");
jrose@1145 572
twisti@2806 573 if (defc_oop.is_null() || name_str.is_null() || type_str.is_null()) {
twisti@3969 574 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "nothing to resolve", empty);
jrose@1145 575 }
twisti@2806 576
twisti@2806 577 instanceKlassHandle defc;
twisti@2806 578 {
coleenp@4037 579 Klass* defc_klass = java_lang_Class::as_Klass(defc_oop());
coleenp@4037 580 if (defc_klass == NULL) return empty; // a primitive; no resolution possible
hseigel@4278 581 if (!defc_klass->oop_is_instance()) {
hseigel@4278 582 if (!defc_klass->oop_is_array()) return empty;
coleenp@4037 583 defc_klass = SystemDictionary::Object_klass();
twisti@2806 584 }
coleenp@4037 585 defc = instanceKlassHandle(THREAD, defc_klass);
jrose@1145 586 }
jrose@1145 587 if (defc.is_null()) {
twisti@3969 588 THROW_MSG_(vmSymbols::java_lang_InternalError(), "primitive class", empty);
jrose@1145 589 }
twisti@3969 590 defc->link_class(CHECK_(empty)); // possible safepoint
jrose@1145 591
jrose@1145 592 // convert the external string name to an internal symbol
twisti@2806 593 TempNewSymbol name = java_lang_String::as_symbol_or_null(name_str());
twisti@3969 594 if (name == NULL) return empty; // no such name
never@2978 595 if (name == vmSymbols::class_initializer_name())
twisti@3969 596 return empty; // illegal name
jrose@1145 597
twisti@3969 598 vmIntrinsics::ID mh_invoke_id = vmIntrinsics::_none;
jrose@1863 599 if ((flags & ALL_KINDS) == IS_METHOD &&
twisti@3969 600 (defc() == SystemDictionary::MethodHandle_klass()) &&
twisti@3969 601 (ref_kind == JVM_REF_invokeVirtual ||
twisti@3969 602 ref_kind == JVM_REF_invokeSpecial ||
twisti@3969 603 // static invocation mode is required for _linkToVirtual, etc.:
twisti@3969 604 ref_kind == JVM_REF_invokeStatic)) {
twisti@3969 605 vmIntrinsics::ID iid = signature_polymorphic_name_id(name);
twisti@3969 606 if (iid != vmIntrinsics::_none &&
twisti@3969 607 ((ref_kind == JVM_REF_invokeStatic) == is_signature_polymorphic_static(iid))) {
twisti@3969 608 // Virtual methods invoke and invokeExact, plus internal invokers like _invokeBasic.
twisti@3969 609 // For a static reference it could an internal linkage routine like _linkToVirtual, etc.
twisti@3969 610 mh_invoke_id = iid;
twisti@3969 611 }
twisti@2806 612 }
jrose@1863 613
jrose@1145 614 // convert the external string or reflective type to an internal signature
twisti@3969 615 TempNewSymbol type = lookup_signature(type_str(), (mh_invoke_id != vmIntrinsics::_none), CHECK_(empty));
twisti@3969 616 if (type == NULL) return empty; // no such signature exists in the VM
jrose@1145 617
jrose@1145 618 // Time to do the lookup.
jrose@1145 619 switch (flags & ALL_KINDS) {
jrose@1145 620 case IS_METHOD:
jrose@1145 621 {
jrose@1145 622 CallInfo result;
jrose@1145 623 {
twisti@3969 624 assert(!HAS_PENDING_EXCEPTION, "");
twisti@3969 625 if (ref_kind == JVM_REF_invokeStatic) {
jrose@1145 626 LinkResolver::resolve_static_call(result,
jrose@1145 627 defc, name, type, KlassHandle(), false, false, THREAD);
twisti@3969 628 } else if (ref_kind == JVM_REF_invokeInterface) {
jrose@1145 629 LinkResolver::resolve_interface_call(result, Handle(), defc,
jrose@1145 630 defc, name, type, KlassHandle(), false, false, THREAD);
twisti@3969 631 } else if (mh_invoke_id != vmIntrinsics::_none) {
twisti@3969 632 assert(!is_signature_polymorphic_static(mh_invoke_id), "");
twisti@3969 633 LinkResolver::resolve_handle_call(result,
twisti@3969 634 defc, name, type, KlassHandle(), THREAD);
twisti@3969 635 } else if (ref_kind == JVM_REF_invokeSpecial) {
twisti@3969 636 LinkResolver::resolve_special_call(result,
twisti@3969 637 defc, name, type, KlassHandle(), false, THREAD);
twisti@3969 638 } else if (ref_kind == JVM_REF_invokeVirtual) {
jrose@1145 639 LinkResolver::resolve_virtual_call(result, Handle(), defc,
jrose@1145 640 defc, name, type, KlassHandle(), false, false, THREAD);
twisti@3969 641 } else {
twisti@3969 642 assert(false, err_msg("ref_kind=%d", ref_kind));
jrose@1145 643 }
jrose@1145 644 if (HAS_PENDING_EXCEPTION) {
twisti@3969 645 return empty;
jrose@1145 646 }
jrose@1145 647 }
drchase@5732 648 if (result.resolved_appendix().not_null()) {
drchase@5732 649 // The resolved MemberName must not be accompanied by an appendix argument,
drchase@5732 650 // since there is no way to bind this value into the MemberName.
drchase@5732 651 // Caller is responsible to prevent this from happening.
drchase@5732 652 THROW_MSG_(vmSymbols::java_lang_InternalError(), "appendix", empty);
drchase@5732 653 }
drchase@5732 654 oop mname2 = init_method_MemberName(mname, result);
drchase@5732 655 return Handle(THREAD, mname2);
jrose@1145 656 }
jrose@1145 657 case IS_CONSTRUCTOR:
jrose@1145 658 {
jrose@1145 659 CallInfo result;
jrose@1145 660 {
twisti@3969 661 assert(!HAS_PENDING_EXCEPTION, "");
coleenp@2497 662 if (name == vmSymbols::object_initializer_name()) {
jrose@1145 663 LinkResolver::resolve_special_call(result,
jrose@1145 664 defc, name, type, KlassHandle(), false, THREAD);
jrose@1145 665 } else {
jrose@1145 666 break; // will throw after end of switch
jrose@1145 667 }
jrose@1145 668 if (HAS_PENDING_EXCEPTION) {
twisti@3969 669 return empty;
jrose@1145 670 }
jrose@1145 671 }
jrose@1145 672 assert(result.is_statically_bound(), "");
drchase@5732 673 oop mname2 = init_method_MemberName(mname, result);
drchase@5732 674 return Handle(THREAD, mname2);
jrose@1145 675 }
jrose@1145 676 case IS_FIELD:
jrose@1145 677 {
drchase@5732 678 fieldDescriptor result; // find_field initializes fd if found
drchase@5732 679 {
drchase@5732 680 assert(!HAS_PENDING_EXCEPTION, "");
drchase@5732 681 LinkResolver::resolve_field(result, defc, name, type, KlassHandle(), Bytecodes::_nop, false, false, THREAD);
drchase@5732 682 if (HAS_PENDING_EXCEPTION) {
drchase@5732 683 return empty;
drchase@5732 684 }
drchase@5732 685 }
drchase@5732 686 oop mname2 = init_field_MemberName(mname, result, ref_kind_is_setter(ref_kind));
drchase@5732 687 return Handle(THREAD, mname2);
jrose@1145 688 }
jrose@1863 689 default:
twisti@3969 690 THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format", empty);
jrose@1145 691 }
jrose@1863 692
twisti@3969 693 return empty;
jrose@1145 694 }
jrose@1145 695
jrose@1145 696 // Conversely, a member name which is only initialized from JVM internals
jrose@1145 697 // may have null defc, name, and type fields.
jrose@1145 698 // Resolving it plants a vmtarget/vmindex in it,
jrose@1145 699 // which refers directly to JVM internals.
jrose@1145 700 void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) {
jrose@2639 701 assert(java_lang_invoke_MemberName::is_instance(mname()), "");
coleenp@4037 702 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
jrose@2639 703 int vmindex = java_lang_invoke_MemberName::vmindex(mname());
twisti@3969 704 if (vmtarget == NULL) {
jrose@1145 705 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand");
jrose@1145 706 }
jrose@1145 707
jrose@2639 708 bool have_defc = (java_lang_invoke_MemberName::clazz(mname()) != NULL);
jrose@2639 709 bool have_name = (java_lang_invoke_MemberName::name(mname()) != NULL);
jrose@2639 710 bool have_type = (java_lang_invoke_MemberName::type(mname()) != NULL);
jrose@2639 711 int flags = java_lang_invoke_MemberName::flags(mname());
jrose@1145 712
jrose@1145 713 if (suppress != 0) {
jrose@1145 714 if (suppress & _suppress_defc) have_defc = true;
jrose@1145 715 if (suppress & _suppress_name) have_name = true;
jrose@1145 716 if (suppress & _suppress_type) have_type = true;
jrose@1145 717 }
jrose@1145 718
jrose@1145 719 if (have_defc && have_name && have_type) return; // nothing needed
jrose@1145 720
jrose@1145 721 switch (flags & ALL_KINDS) {
jrose@1145 722 case IS_METHOD:
jrose@1145 723 case IS_CONSTRUCTOR:
jrose@1145 724 {
coleenp@4037 725 assert(vmtarget->is_method(), "method or constructor vmtarget is Method*");
coleenp@4037 726 methodHandle m(THREAD, (Method*)vmtarget);
twisti@3969 727 DEBUG_ONLY(vmtarget = NULL); // safety
jrose@1145 728 if (m.is_null()) break;
jrose@1145 729 if (!have_defc) {
coleenp@4251 730 InstanceKlass* defc = m->method_holder();
coleenp@4251 731 java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror());
jrose@1145 732 }
jrose@1145 733 if (!have_name) {
jrose@1145 734 //not java_lang_String::create_from_symbol; let's intern member names
jrose@1145 735 Handle name = StringTable::intern(m->name(), CHECK);
jrose@2639 736 java_lang_invoke_MemberName::set_name(mname(), name());
jrose@1145 737 }
jrose@1145 738 if (!have_type) {
jrose@1145 739 Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK);
jrose@2639 740 java_lang_invoke_MemberName::set_type(mname(), type());
jrose@1145 741 }
jrose@1145 742 return;
jrose@1145 743 }
jrose@1145 744 case IS_FIELD:
jrose@1145 745 {
coleenp@4037 746 assert(vmtarget->is_klass(), "field vmtarget is Klass*");
hseigel@4278 747 if (!((Klass*) vmtarget)->oop_is_instance()) break;
coleenp@4037 748 instanceKlassHandle defc(THREAD, (Klass*) vmtarget);
twisti@3969 749 DEBUG_ONLY(vmtarget = NULL); // safety
jrose@1145 750 bool is_static = ((flags & JVM_ACC_STATIC) != 0);
jrose@1145 751 fieldDescriptor fd; // find_field initializes fd if found
jrose@1145 752 if (!defc->find_field_from_offset(vmindex, is_static, &fd))
jrose@1145 753 break; // cannot expand
jrose@1145 754 if (!have_defc) {
jrose@2639 755 java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror());
jrose@1145 756 }
jrose@1145 757 if (!have_name) {
jrose@1145 758 //not java_lang_String::create_from_symbol; let's intern member names
jrose@1145 759 Handle name = StringTable::intern(fd.name(), CHECK);
jrose@2639 760 java_lang_invoke_MemberName::set_name(mname(), name());
jrose@1145 761 }
jrose@1145 762 if (!have_type) {
twisti@3969 763 // If it is a primitive field type, don't mess with short strings like "I".
twisti@3969 764 Handle type = field_signature_type_or_null(fd.signature());
twisti@3969 765 if (type.is_null()) {
twisti@3969 766 java_lang_String::create_from_symbol(fd.signature(), CHECK);
twisti@3969 767 }
jrose@2639 768 java_lang_invoke_MemberName::set_type(mname(), type());
jrose@1145 769 }
jrose@1145 770 return;
jrose@1145 771 }
jrose@1145 772 }
jrose@1145 773 THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format");
jrose@1145 774 }
jrose@1145 775
sspitsyn@4965 776 int MethodHandles::find_MemberNames(KlassHandle k,
coleenp@2497 777 Symbol* name, Symbol* sig,
sspitsyn@4965 778 int mflags, KlassHandle caller,
sspitsyn@4965 779 int skip, objArrayHandle results) {
jrose@1145 780 // %%% take caller into account!
jrose@1145 781
sspitsyn@4965 782 Thread* thread = Thread::current();
sspitsyn@4965 783
sspitsyn@4965 784 if (k.is_null() || !k->oop_is_instance()) return -1;
jrose@1145 785
jrose@1145 786 int rfill = 0, rlimit = results->length(), rskip = skip;
jrose@1145 787 // overflow measurement:
jrose@1145 788 int overflow = 0, overflow_limit = MAX2(1000, rlimit);
jrose@1145 789
jrose@1145 790 int match_flags = mflags;
jrose@1145 791 bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0);
jrose@1145 792 bool search_intfc = ((match_flags & SEARCH_INTERFACES) != 0);
jrose@1145 793 bool local_only = !(search_superc | search_intfc);
jrose@1145 794 bool classes_only = false;
jrose@1145 795
jrose@1145 796 if (name != NULL) {
jrose@1145 797 if (name->utf8_length() == 0) return 0; // a match is not possible
jrose@1145 798 }
jrose@1145 799 if (sig != NULL) {
jrose@1145 800 if (sig->utf8_length() == 0) return 0; // a match is not possible
jrose@1145 801 if (sig->byte_at(0) == '(')
jrose@1145 802 match_flags &= ~(IS_FIELD | IS_TYPE);
jrose@1145 803 else
jrose@1145 804 match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD);
jrose@1145 805 }
jrose@1145 806
jrose@1145 807 if ((match_flags & IS_TYPE) != 0) {
jrose@1145 808 // NYI, and Core Reflection works quite well for this query
jrose@1145 809 }
jrose@1145 810
jrose@1145 811 if ((match_flags & IS_FIELD) != 0) {
sspitsyn@4965 812 for (FieldStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) {
jrose@1145 813 if (name != NULL && st.name() != name)
jrose@1145 814 continue;
jrose@1145 815 if (sig != NULL && st.signature() != sig)
jrose@1145 816 continue;
jrose@1145 817 // passed the filters
jrose@1145 818 if (rskip > 0) {
jrose@1145 819 --rskip;
jrose@1145 820 } else if (rfill < rlimit) {
sspitsyn@4965 821 Handle result(thread, results->obj_at(rfill++));
sspitsyn@4965 822 if (!java_lang_invoke_MemberName::is_instance(result()))
jrose@1145 823 return -99; // caller bug!
drchase@5732 824 oop saved = MethodHandles::init_field_MemberName(result, st.field_descriptor());
sspitsyn@4965 825 if (saved != result())
twisti@3969 826 results->obj_at_put(rfill-1, saved); // show saved instance to user
jrose@1145 827 } else if (++overflow >= overflow_limit) {
jrose@1145 828 match_flags = 0; break; // got tired of looking at overflow
jrose@1145 829 }
jrose@1145 830 }
jrose@1145 831 }
jrose@1145 832
jrose@1145 833 if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) {
jrose@1145 834 // watch out for these guys:
coleenp@2497 835 Symbol* init_name = vmSymbols::object_initializer_name();
coleenp@2497 836 Symbol* clinit_name = vmSymbols::class_initializer_name();
jrose@1145 837 if (name == clinit_name) clinit_name = NULL; // hack for exposing <clinit>
jrose@1145 838 bool negate_name_test = false;
jrose@1145 839 // fix name so that it captures the intention of IS_CONSTRUCTOR
jrose@1145 840 if (!(match_flags & IS_METHOD)) {
jrose@1145 841 // constructors only
jrose@1145 842 if (name == NULL) {
jrose@1145 843 name = init_name;
jrose@1145 844 } else if (name != init_name) {
jrose@1145 845 return 0; // no constructors of this method name
jrose@1145 846 }
jrose@1145 847 } else if (!(match_flags & IS_CONSTRUCTOR)) {
jrose@1145 848 // methods only
jrose@1145 849 if (name == NULL) {
jrose@1145 850 name = init_name;
jrose@1145 851 negate_name_test = true; // if we see the name, we *omit* the entry
jrose@1145 852 } else if (name == init_name) {
jrose@1145 853 return 0; // no methods of this constructor name
jrose@1145 854 }
jrose@1145 855 } else {
jrose@1145 856 // caller will accept either sort; no need to adjust name
jrose@1145 857 }
sspitsyn@4965 858 for (MethodStream st(k(), local_only, !search_intfc); !st.eos(); st.next()) {
coleenp@4037 859 Method* m = st.method();
coleenp@2497 860 Symbol* m_name = m->name();
jrose@1145 861 if (m_name == clinit_name)
jrose@1145 862 continue;
jrose@1145 863 if (name != NULL && ((m_name != name) ^ negate_name_test))
jrose@1145 864 continue;
jrose@1145 865 if (sig != NULL && m->signature() != sig)
jrose@1145 866 continue;
jrose@1145 867 // passed the filters
jrose@1145 868 if (rskip > 0) {
jrose@1145 869 --rskip;
jrose@1145 870 } else if (rfill < rlimit) {
sspitsyn@4965 871 Handle result(thread, results->obj_at(rfill++));
sspitsyn@4965 872 if (!java_lang_invoke_MemberName::is_instance(result()))
jrose@1145 873 return -99; // caller bug!
drchase@5732 874 CallInfo info(m);
drchase@5732 875 oop saved = MethodHandles::init_method_MemberName(result, info);
sspitsyn@4965 876 if (saved != result())
twisti@3969 877 results->obj_at_put(rfill-1, saved); // show saved instance to user
jrose@1145 878 } else if (++overflow >= overflow_limit) {
jrose@1145 879 match_flags = 0; break; // got tired of looking at overflow
jrose@1145 880 }
jrose@1145 881 }
jrose@1145 882 }
jrose@1145 883
jrose@1145 884 // return number of elements we at leasted wanted to initialize
jrose@1145 885 return rfill + overflow;
jrose@1145 886 }
sspitsyn@4965 887
sspitsyn@4965 888 //------------------------------------------------------------------------------
sspitsyn@4965 889 // MemberNameTable
sspitsyn@4965 890 //
sspitsyn@4965 891
sspitsyn@5178 892 MemberNameTable::MemberNameTable(int methods_cnt)
sspitsyn@5178 893 : GrowableArray<jweak>(methods_cnt, true) {
sspitsyn@4965 894 assert_locked_or_safepoint(MemberNameTable_lock);
sspitsyn@4965 895 }
sspitsyn@4965 896
sspitsyn@4965 897 MemberNameTable::~MemberNameTable() {
sspitsyn@4965 898 assert_locked_or_safepoint(MemberNameTable_lock);
sspitsyn@4965 899 int len = this->length();
sspitsyn@4965 900
sspitsyn@4965 901 for (int idx = 0; idx < len; idx++) {
sspitsyn@4965 902 jweak ref = this->at(idx);
sspitsyn@4965 903 JNIHandles::destroy_weak_global(ref);
sspitsyn@4965 904 }
sspitsyn@4965 905 }
sspitsyn@4965 906
sspitsyn@5178 907 void MemberNameTable::add_member_name(int index, jweak mem_name_wref) {
sspitsyn@4965 908 assert_locked_or_safepoint(MemberNameTable_lock);
sspitsyn@5178 909 this->at_put_grow(index, mem_name_wref);
sspitsyn@4965 910 }
sspitsyn@4965 911
sspitsyn@5178 912 // Return a member name oop or NULL.
sspitsyn@5178 913 oop MemberNameTable::get_member_name(int index) {
sspitsyn@4965 914 assert_locked_or_safepoint(MemberNameTable_lock);
sspitsyn@4965 915
sspitsyn@5178 916 jweak ref = this->at(index);
sspitsyn@5178 917 oop mem_name = JNIHandles::resolve(ref);
sspitsyn@5178 918 return mem_name;
sspitsyn@4965 919 }
sspitsyn@4965 920
sspitsyn@4965 921 #if INCLUDE_JVMTI
sspitsyn@4965 922 oop MemberNameTable::find_member_name_by_method(Method* old_method) {
sspitsyn@4965 923 assert_locked_or_safepoint(MemberNameTable_lock);
sspitsyn@4965 924 oop found = NULL;
sspitsyn@4965 925 int len = this->length();
sspitsyn@4965 926
sspitsyn@4965 927 for (int idx = 0; idx < len; idx++) {
sspitsyn@4965 928 oop mem_name = JNIHandles::resolve(this->at(idx));
sspitsyn@4965 929 if (mem_name == NULL) {
sspitsyn@4965 930 continue;
sspitsyn@4965 931 }
sspitsyn@4965 932 Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mem_name);
sspitsyn@4965 933 if (method == old_method) {
sspitsyn@4965 934 found = mem_name;
sspitsyn@4965 935 break;
sspitsyn@4965 936 }
sspitsyn@4965 937 }
sspitsyn@4965 938 return found;
sspitsyn@4965 939 }
sspitsyn@4965 940
sspitsyn@4965 941 // It is called at safepoint only
sspitsyn@4965 942 void MemberNameTable::adjust_method_entries(Method** old_methods, Method** new_methods,
sspitsyn@4965 943 int methods_length, bool *trace_name_printed) {
sspitsyn@4965 944 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
sspitsyn@4965 945 // search the MemberNameTable for uses of either obsolete or EMCP methods
sspitsyn@4965 946 for (int j = 0; j < methods_length; j++) {
sspitsyn@4965 947 Method* old_method = old_methods[j];
sspitsyn@4965 948 Method* new_method = new_methods[j];
sspitsyn@4965 949 oop mem_name = find_member_name_by_method(old_method);
sspitsyn@4965 950 if (mem_name != NULL) {
sspitsyn@4965 951 java_lang_invoke_MemberName::adjust_vmtarget(mem_name, new_method);
sspitsyn@4965 952
sspitsyn@4965 953 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
sspitsyn@4965 954 if (!(*trace_name_printed)) {
sspitsyn@4965 955 // RC_TRACE_MESG macro has an embedded ResourceMark
sspitsyn@4965 956 RC_TRACE_MESG(("adjust: name=%s",
sspitsyn@4965 957 old_method->method_holder()->external_name()));
sspitsyn@4965 958 *trace_name_printed = true;
sspitsyn@4965 959 }
sspitsyn@4965 960 // RC_TRACE macro has an embedded ResourceMark
sspitsyn@4965 961 RC_TRACE(0x00400000, ("MemberName method update: %s(%s)",
sspitsyn@4965 962 new_method->name()->as_C_string(),
sspitsyn@4965 963 new_method->signature()->as_C_string()));
sspitsyn@4965 964 }
sspitsyn@4965 965 }
sspitsyn@4965 966 }
sspitsyn@4965 967 }
sspitsyn@4965 968 #endif // INCLUDE_JVMTI
sspitsyn@4965 969
jrose@1145 970 //
twisti@3969 971 // Here are the native methods in java.lang.invoke.MethodHandleNatives
jrose@1145 972 // They are the private interface between this JVM and the HotSpot-specific
jrose@1145 973 // Java code that implements JSR 292 method handles.
jrose@1145 974 //
jrose@1145 975 // Note: We use a JVM_ENTRY macro to define each of these, for this is the way
jrose@1145 976 // that intrinsic (non-JNI) native methods are defined in HotSpot.
jrose@1145 977 //
coleenp@4037 978
jrose@2639 979 JVM_ENTRY(jint, MHN_getConstant(JNIEnv *env, jobject igcls, jint which)) {
jrose@1145 980 switch (which) {
never@3105 981 case MethodHandles::GC_COUNT_GWT:
never@3105 982 #ifdef COMPILER2
never@3105 983 return true;
never@3105 984 #else
never@3105 985 return false;
never@3105 986 #endif
jrose@1145 987 }
jrose@1145 988 return 0;
jrose@1145 989 }
jrose@1145 990 JVM_END
jrose@1145 991
jrose@1145 992 #ifndef PRODUCT
twisti@3969 993 #define EACH_NAMED_CON(template, requirement) \
twisti@3969 994 template(MethodHandles,GC_COUNT_GWT) \
jrose@2639 995 template(java_lang_invoke_MemberName,MN_IS_METHOD) \
jrose@2639 996 template(java_lang_invoke_MemberName,MN_IS_CONSTRUCTOR) \
jrose@2639 997 template(java_lang_invoke_MemberName,MN_IS_FIELD) \
jrose@2639 998 template(java_lang_invoke_MemberName,MN_IS_TYPE) \
twisti@4866 999 template(java_lang_invoke_MemberName,MN_CALLER_SENSITIVE) \
jrose@2639 1000 template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \
jrose@2639 1001 template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \
twisti@3969 1002 template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_SHIFT) \
twisti@3969 1003 template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_MASK) \
twisti@3969 1004 template(MethodHandles,GC_LAMBDA_SUPPORT) \
jrose@1145 1005 /*end*/
jrose@1145 1006
twisti@3969 1007 #define IGNORE_REQ(req_expr) /* req_expr */
jrose@1145 1008 #define ONE_PLUS(scope,value) 1+
twisti@3969 1009 static const int con_value_count = EACH_NAMED_CON(ONE_PLUS, IGNORE_REQ) 0;
jrose@1145 1010 #define VALUE_COMMA(scope,value) scope::value,
twisti@3969 1011 static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA, IGNORE_REQ) 0 };
jrose@1145 1012 #define STRING_NULL(scope,value) #value "\0"
twisti@3969 1013 static const char con_names[] = { EACH_NAMED_CON(STRING_NULL, IGNORE_REQ) };
twisti@3969 1014
twisti@3969 1015 static bool advertise_con_value(int which) {
twisti@3969 1016 if (which < 0) return false;
twisti@3969 1017 bool ok = true;
twisti@3969 1018 int count = 0;
twisti@3969 1019 #define INC_COUNT(scope,value) \
twisti@3969 1020 ++count;
twisti@3969 1021 #define CHECK_REQ(req_expr) \
twisti@3969 1022 if (which < count) return ok; \
twisti@3969 1023 ok = (req_expr);
twisti@3969 1024 EACH_NAMED_CON(INC_COUNT, CHECK_REQ);
twisti@3969 1025 #undef INC_COUNT
twisti@3969 1026 #undef CHECK_REQ
twisti@3969 1027 assert(count == con_value_count, "");
twisti@3969 1028 if (which < count) return ok;
twisti@3969 1029 return false;
twisti@3969 1030 }
jrose@1145 1031
jrose@1145 1032 #undef ONE_PLUS
jrose@1145 1033 #undef VALUE_COMMA
jrose@1145 1034 #undef STRING_NULL
jrose@1145 1035 #undef EACH_NAMED_CON
twisti@3969 1036 #endif // PRODUCT
jrose@1145 1037
jrose@2639 1038 JVM_ENTRY(jint, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) {
jrose@1145 1039 #ifndef PRODUCT
twisti@3969 1040 if (advertise_con_value(which)) {
twisti@3969 1041 assert(which >= 0 && which < con_value_count, "");
jrose@1145 1042 int con = con_values[which];
twisti@2806 1043 objArrayHandle box(THREAD, (objArrayOop) JNIHandles::resolve(box_jh));
twisti@2806 1044 if (box.not_null() && box->klass() == Universe::objectArrayKlassObj() && box->length() > 0) {
jrose@1145 1045 const char* str = &con_names[0];
jrose@1145 1046 for (int i = 0; i < which; i++)
jrose@1145 1047 str += strlen(str) + 1; // skip name and null
twisti@2806 1048 oop name = java_lang_String::create_oop_from_str(str, CHECK_0); // possible safepoint
jrose@1145 1049 box->obj_at_put(0, name);
jrose@1145 1050 }
jrose@1145 1051 return con;
jrose@1145 1052 }
jrose@1145 1053 #endif
jrose@1145 1054 return 0;
jrose@1145 1055 }
jrose@1145 1056 JVM_END
jrose@1145 1057
jrose@1145 1058 // void init(MemberName self, AccessibleObject ref)
jrose@2639 1059 JVM_ENTRY(void, MHN_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) {
never@2937 1060 if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); }
never@2937 1061 if (target_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); }
jrose@1145 1062 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
sspitsyn@4965 1063 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh));
sspitsyn@4965 1064 MethodHandles::init_MemberName(mname, target);
jrose@1145 1065 }
jrose@1145 1066 JVM_END
jrose@1145 1067
jrose@1145 1068 // void expand(MemberName self)
jrose@2639 1069 JVM_ENTRY(void, MHN_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) {
never@2937 1070 if (mname_jh == NULL) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); }
jrose@1145 1071 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
jrose@1145 1072 MethodHandles::expand_MemberName(mname, 0, CHECK);
jrose@1145 1073 }
jrose@1145 1074 JVM_END
jrose@1145 1075
jrose@1145 1076 // void resolve(MemberName self, Class<?> caller)
twisti@3969 1077 JVM_ENTRY(jobject, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh)) {
twisti@3969 1078 if (mname_jh == NULL) { THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "mname is null"); }
jrose@1145 1079 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
jrose@1862 1080
jrose@1862 1081 // The trusted Java code that calls this method should already have performed
jrose@1862 1082 // access checks on behalf of the given caller. But, we can verify this.
twisti@3969 1083 if (VerifyMethodHandles && caller_jh != NULL &&
twisti@3969 1084 java_lang_invoke_MemberName::clazz(mname()) != NULL) {
coleenp@4037 1085 Klass* reference_klass = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname()));
twisti@5354 1086 if (reference_klass != NULL && reference_klass->oop_is_objArray()) {
twisti@5354 1087 reference_klass = ObjArrayKlass::cast(reference_klass)->bottom_klass();
twisti@5354 1088 }
twisti@5354 1089
twisti@5354 1090 // Reflection::verify_class_access can only handle instance classes.
twisti@5354 1091 if (reference_klass != NULL && reference_klass->oop_is_instance()) {
jrose@1862 1092 // Emulate LinkResolver::check_klass_accessability.
coleenp@4037 1093 Klass* caller = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh));
jrose@1862 1094 if (!Reflection::verify_class_access(caller,
jrose@1862 1095 reference_klass,
jrose@1862 1096 true)) {
hseigel@4278 1097 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), reference_klass->external_name());
jrose@1862 1098 }
jrose@1862 1099 }
jrose@1862 1100 }
jrose@1862 1101
twisti@3969 1102 Handle resolved = MethodHandles::resolve_MemberName(mname, CHECK_NULL);
twisti@3969 1103 if (resolved.is_null()) {
twisti@3969 1104 int flags = java_lang_invoke_MemberName::flags(mname());
twisti@3969 1105 int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK;
twisti@3969 1106 if (!MethodHandles::ref_kind_is_valid(ref_kind)) {
twisti@3969 1107 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "obsolete MemberName format");
twisti@3969 1108 }
twisti@3969 1109 if ((flags & ALL_KINDS) == IS_FIELD) {
twisti@3969 1110 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), "field resolution failed");
twisti@3969 1111 } else if ((flags & ALL_KINDS) == IS_METHOD ||
twisti@3969 1112 (flags & ALL_KINDS) == IS_CONSTRUCTOR) {
twisti@3969 1113 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchFieldError(), "method resolution failed");
twisti@3969 1114 } else {
twisti@3969 1115 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "resolution failed");
twisti@3969 1116 }
twisti@3969 1117 }
twisti@3969 1118
twisti@3969 1119 return JNIHandles::make_local(THREAD, resolved());
jrose@1145 1120 }
jrose@1145 1121 JVM_END
jrose@1145 1122
twisti@3969 1123 static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) {
twisti@3969 1124 if (mname == NULL ||
twisti@3969 1125 java_lang_invoke_MemberName::vmtarget(mname) == NULL) {
twisti@3969 1126 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved");
twisti@3969 1127 } else {
twisti@3969 1128 int flags = java_lang_invoke_MemberName::flags(mname);
twisti@3969 1129 if ((flags & IS_FIELD) != 0 &&
twisti@3969 1130 (must_be_static
twisti@3969 1131 ? (flags & JVM_ACC_STATIC) != 0
twisti@3969 1132 : (flags & JVM_ACC_STATIC) == 0)) {
twisti@3969 1133 int vmindex = java_lang_invoke_MemberName::vmindex(mname);
twisti@3969 1134 return (jlong) vmindex;
twisti@3969 1135 }
twisti@3969 1136 }
twisti@3969 1137 const char* msg = (must_be_static ? "static field required" : "non-static field required");
twisti@3969 1138 THROW_MSG_0(vmSymbols::java_lang_InternalError(), msg);
twisti@3969 1139 return 0;
twisti@3969 1140 }
twisti@3969 1141
twisti@3969 1142 JVM_ENTRY(jlong, MHN_objectFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) {
twisti@3969 1143 return find_member_field_offset(JNIHandles::resolve(mname_jh), false, THREAD);
twisti@3969 1144 }
twisti@3969 1145 JVM_END
twisti@3969 1146
twisti@3969 1147 JVM_ENTRY(jlong, MHN_staticFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) {
twisti@3969 1148 return find_member_field_offset(JNIHandles::resolve(mname_jh), true, THREAD);
twisti@3969 1149 }
twisti@3969 1150 JVM_END
twisti@3969 1151
twisti@3969 1152 JVM_ENTRY(jobject, MHN_staticFieldBase(JNIEnv *env, jobject igcls, jobject mname_jh)) {
twisti@3969 1153 // use the other function to perform sanity checks:
twisti@3969 1154 jlong ignore = find_member_field_offset(JNIHandles::resolve(mname_jh), true, CHECK_NULL);
twisti@3969 1155 oop clazz = java_lang_invoke_MemberName::clazz(JNIHandles::resolve_non_null(mname_jh));
twisti@3969 1156 return JNIHandles::make_local(THREAD, clazz);
twisti@3969 1157 }
twisti@3969 1158 JVM_END
twisti@3969 1159
twisti@3969 1160 JVM_ENTRY(jobject, MHN_getMemberVMInfo(JNIEnv *env, jobject igcls, jobject mname_jh)) {
twisti@3969 1161 if (mname_jh == NULL) return NULL;
twisti@3969 1162 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
twisti@3969 1163 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname());
coleenp@4037 1164 Metadata* vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
twisti@3969 1165 objArrayHandle result = oopFactory::new_objArray(SystemDictionary::Object_klass(), 2, CHECK_NULL);
twisti@3969 1166 jvalue vmindex_value; vmindex_value.j = (long)vmindex;
twisti@3969 1167 oop x = java_lang_boxing_object::create(T_LONG, &vmindex_value, CHECK_NULL);
twisti@3969 1168 result->obj_at_put(0, x);
twisti@3969 1169 x = NULL;
coleenp@4037 1170 if (vmtarget == NULL) {
coleenp@4037 1171 x = NULL;
twisti@3969 1172 } else if (vmtarget->is_klass()) {
hseigel@4278 1173 x = ((Klass*) vmtarget)->java_mirror();
coleenp@4037 1174 } else if (vmtarget->is_method()) {
twisti@3969 1175 Handle mname2 = MethodHandles::new_MemberName(CHECK_NULL);
drchase@5732 1176 CallInfo info((Method*)vmtarget);
drchase@5732 1177 x = MethodHandles::init_method_MemberName(mname2, info);
twisti@3969 1178 }
twisti@3969 1179 result->obj_at_put(1, x);
twisti@3969 1180 return JNIHandles::make_local(env, result());
twisti@3969 1181 }
twisti@3969 1182 JVM_END
twisti@3969 1183
twisti@3969 1184
twisti@3969 1185
jrose@1145 1186 // static native int getMembers(Class<?> defc, String matchName, String matchSig,
jrose@1145 1187 // int matchFlags, Class<?> caller, int skip, MemberName[] results);
jrose@2639 1188 JVM_ENTRY(jint, MHN_getMembers(JNIEnv *env, jobject igcls,
jrose@1145 1189 jclass clazz_jh, jstring name_jh, jstring sig_jh,
jrose@1145 1190 int mflags, jclass caller_jh, jint skip, jobjectArray results_jh)) {
jrose@1145 1191 if (clazz_jh == NULL || results_jh == NULL) return -1;
coleenp@4037 1192 KlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz_jh)));
jrose@1145 1193
twisti@2806 1194 objArrayHandle results(THREAD, (objArrayOop) JNIHandles::resolve(results_jh));
twisti@2806 1195 if (results.is_null() || !results->is_objArray()) return -1;
jrose@1145 1196
coleenp@2497 1197 TempNewSymbol name = NULL;
coleenp@2497 1198 TempNewSymbol sig = NULL;
jrose@1145 1199 if (name_jh != NULL) {
jrose@1145 1200 name = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(name_jh));
jrose@1145 1201 if (name == NULL) return 0; // a match is not possible
jrose@1145 1202 }
jrose@1145 1203 if (sig_jh != NULL) {
jrose@1145 1204 sig = java_lang_String::as_symbol_or_null(JNIHandles::resolve_non_null(sig_jh));
jrose@1145 1205 if (sig == NULL) return 0; // a match is not possible
jrose@1145 1206 }
jrose@1145 1207
twisti@2806 1208 KlassHandle caller;
jrose@1145 1209 if (caller_jh != NULL) {
jrose@1145 1210 oop caller_oop = JNIHandles::resolve_non_null(caller_jh);
jrose@1145 1211 if (!java_lang_Class::is_instance(caller_oop)) return -1;
coleenp@4037 1212 caller = KlassHandle(THREAD, java_lang_Class::as_Klass(caller_oop));
jrose@1145 1213 }
jrose@1145 1214
twisti@2806 1215 if (name != NULL && sig != NULL && results.not_null()) {
jrose@1145 1216 // try a direct resolve
jrose@1145 1217 // %%% TO DO
jrose@1145 1218 }
jrose@1145 1219
sspitsyn@4965 1220 int res = MethodHandles::find_MemberNames(k, name, sig, mflags,
sspitsyn@4965 1221 caller, skip, results);
jrose@1145 1222 // TO DO: expand at least some of the MemberNames, to avoid massive callbacks
jrose@1145 1223 return res;
jrose@1145 1224 }
jrose@1145 1225 JVM_END
jrose@1145 1226
twisti@3131 1227 JVM_ENTRY(void, MHN_setCallSiteTargetNormal(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
twisti@3239 1228 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
twisti@3239 1229 Handle target (THREAD, JNIHandles::resolve(target_jh));
twisti@3131 1230 {
twisti@3131 1231 // Walk all nmethods depending on this call site.
twisti@3131 1232 MutexLocker mu(Compile_lock, thread);
twisti@3131 1233 Universe::flush_dependents_on(call_site, target);
twisti@4354 1234 java_lang_invoke_CallSite::set_target(call_site(), target());
twisti@3131 1235 }
twisti@3131 1236 }
twisti@3131 1237 JVM_END
twisti@3131 1238
twisti@3131 1239 JVM_ENTRY(void, MHN_setCallSiteTargetVolatile(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
twisti@3239 1240 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
twisti@3239 1241 Handle target (THREAD, JNIHandles::resolve(target_jh));
twisti@3131 1242 {
twisti@3131 1243 // Walk all nmethods depending on this call site.
twisti@3131 1244 MutexLocker mu(Compile_lock, thread);
twisti@3131 1245 Universe::flush_dependents_on(call_site, target);
twisti@4354 1246 java_lang_invoke_CallSite::set_target_volatile(call_site(), target());
twisti@3131 1247 }
twisti@3131 1248 }
twisti@3131 1249 JVM_END
twisti@3131 1250
twisti@5108 1251 /**
twisti@5108 1252 * Throws a java/lang/UnsupportedOperationException unconditionally.
twisti@5108 1253 * This is required by the specification of MethodHandle.invoke if
twisti@5108 1254 * invoked directly.
twisti@5108 1255 */
twisti@5108 1256 JVM_ENTRY(jobject, MH_invoke_UOE(JNIEnv* env, jobject mh, jobjectArray args)) {
twisti@5108 1257 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invoke cannot be invoked reflectively");
twisti@5108 1258 return NULL;
twisti@5108 1259 }
twisti@5108 1260 JVM_END
twisti@5108 1261
twisti@5108 1262 /**
twisti@5108 1263 * Throws a java/lang/UnsupportedOperationException unconditionally.
twisti@5108 1264 * This is required by the specification of MethodHandle.invokeExact if
twisti@5108 1265 * invoked directly.
twisti@5108 1266 */
twisti@5108 1267 JVM_ENTRY(jobject, MH_invokeExact_UOE(JNIEnv* env, jobject mh, jobjectArray args)) {
twisti@5108 1268 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invokeExact cannot be invoked reflectively");
twisti@5108 1269 return NULL;
twisti@5108 1270 }
twisti@5108 1271 JVM_END
twisti@5108 1272
jrose@1145 1273 /// JVM_RegisterMethodHandleMethods
jrose@1145 1274
twisti@3131 1275 #undef CS // Solaris builds complain
twisti@3131 1276
jrose@1145 1277 #define LANG "Ljava/lang/"
jrose@2742 1278 #define JLINV "Ljava/lang/invoke/"
jrose@1145 1279
jrose@1145 1280 #define OBJ LANG"Object;"
jrose@1145 1281 #define CLS LANG"Class;"
jrose@1145 1282 #define STRG LANG"String;"
twisti@3131 1283 #define CS JLINV"CallSite;"
jrose@2742 1284 #define MT JLINV"MethodType;"
jrose@2742 1285 #define MH JLINV"MethodHandle;"
jrose@2742 1286 #define MEM JLINV"MemberName;"
jrose@1145 1287
jrose@1145 1288 #define CC (char*) /*cast a literal from (const char*)*/
jrose@1145 1289 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
jrose@1145 1290
twisti@3131 1291 // These are the native methods on java.lang.invoke.MethodHandleNatives.
twisti@5108 1292 static JNINativeMethod MHN_methods[] = {
twisti@3131 1293 {CC"init", CC"("MEM""OBJ")V", FN_PTR(MHN_init_Mem)},
twisti@3131 1294 {CC"expand", CC"("MEM")V", FN_PTR(MHN_expand_Mem)},
twisti@3969 1295 {CC"resolve", CC"("MEM""CLS")"MEM, FN_PTR(MHN_resolve_Mem)},
twisti@3131 1296 {CC"getConstant", CC"(I)I", FN_PTR(MHN_getConstant)},
jrose@1145 1297 // static native int getNamedCon(int which, Object[] name)
twisti@3131 1298 {CC"getNamedCon", CC"(I["OBJ")I", FN_PTR(MHN_getNamedCon)},
jrose@1145 1299 // static native int getMembers(Class<?> defc, String matchName, String matchSig,
jrose@1145 1300 // int matchFlags, Class<?> caller, int skip, MemberName[] results);
twisti@3969 1301 {CC"getMembers", CC"("CLS""STRG""STRG"I"CLS"I["MEM")I", FN_PTR(MHN_getMembers)},
twisti@3969 1302 {CC"objectFieldOffset", CC"("MEM")J", FN_PTR(MHN_objectFieldOffset)},
twisti@3131 1303 {CC"setCallSiteTargetNormal", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetNormal)},
twisti@3969 1304 {CC"setCallSiteTargetVolatile", CC"("CS""MH")V", FN_PTR(MHN_setCallSiteTargetVolatile)},
twisti@3969 1305 {CC"staticFieldOffset", CC"("MEM")J", FN_PTR(MHN_staticFieldOffset)},
twisti@3969 1306 {CC"staticFieldBase", CC"("MEM")"OBJ, FN_PTR(MHN_staticFieldBase)},
twisti@3969 1307 {CC"getMemberVMInfo", CC"("MEM")"OBJ, FN_PTR(MHN_getMemberVMInfo)}
jrose@1145 1308 };
jrose@1145 1309
twisti@5108 1310 static JNINativeMethod MH_methods[] = {
twisti@5108 1311 // UnsupportedOperationException throwers
twisti@5108 1312 {CC"invoke", CC"(["OBJ")"OBJ, FN_PTR(MH_invoke_UOE)},
twisti@5108 1313 {CC"invokeExact", CC"(["OBJ")"OBJ, FN_PTR(MH_invokeExact_UOE)}
twisti@5108 1314 };
jrose@1145 1315
twisti@5108 1316 /**
twisti@5108 1317 * Helper method to register native methods.
twisti@5108 1318 */
twisti@5108 1319 static bool register_natives(JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods) {
twisti@5108 1320 int status = env->RegisterNatives(clazz, methods, nMethods);
twisti@5108 1321 if (status != JNI_OK || env->ExceptionOccurred()) {
twisti@5108 1322 warning("JSR 292 method handle code is mismatched to this JVM. Disabling support.");
twisti@5108 1323 env->ExceptionClear();
twisti@5108 1324 return false;
twisti@5108 1325 }
twisti@5108 1326 return true;
twisti@5108 1327 }
twisti@5108 1328
twisti@5108 1329 /**
twisti@5108 1330 * This one function is exported, used by NativeLookup.
twisti@5108 1331 */
jrose@1145 1332 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) {
twisti@2698 1333 if (!EnableInvokeDynamic) {
twisti@2698 1334 warning("JSR 292 is disabled in this JVM. Use -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic to enable.");
jrose@1145 1335 return; // bind nothing
jrose@1145 1336 }
jrose@1145 1337
twisti@3131 1338 assert(!MethodHandles::enabled(), "must not be enabled");
jrose@1474 1339 bool enable_MH = true;
jrose@1474 1340
twisti@3969 1341 jclass MH_class = NULL;
twisti@3969 1342 if (SystemDictionary::MethodHandle_klass() == NULL) {
twisti@3969 1343 enable_MH = false;
twisti@3969 1344 } else {
hseigel@4278 1345 oop mirror = SystemDictionary::MethodHandle_klass()->java_mirror();
twisti@3969 1346 MH_class = (jclass) JNIHandles::make_local(env, mirror);
twisti@3969 1347 }
twisti@3969 1348
twisti@3969 1349 if (enable_MH) {
jrose@1145 1350 ThreadToNativeFromVM ttnfv(thread);
twisti@3969 1351
twisti@5108 1352 if (enable_MH) {
twisti@5108 1353 enable_MH = register_natives(env, MHN_class, MHN_methods, sizeof(MHN_methods)/sizeof(JNINativeMethod));
twisti@5108 1354 }
twisti@5108 1355 if (enable_MH) {
twisti@5108 1356 enable_MH = register_natives(env, MH_class, MH_methods, sizeof(MH_methods)/sizeof(JNINativeMethod));
jrose@1145 1357 }
jrose@1145 1358 }
jrose@1161 1359
twisti@3969 1360 if (TraceInvokeDynamic) {
twisti@3969 1361 tty->print_cr("MethodHandle support loaded (using LambdaForms)");
jrose@1474 1362 }
jrose@1474 1363
jrose@1474 1364 if (enable_MH) {
twisti@2436 1365 MethodHandles::generate_adapters();
jrose@1474 1366 MethodHandles::set_enabled(true);
jrose@1474 1367 }
jrose@1145 1368 }
jrose@1145 1369 JVM_END

mercurial