src/share/vm/prims/methodHandles.cpp

Tue, 24 Jul 2012 10:51:00 -0700

author
twisti
date
Tue, 24 Jul 2012 10:51:00 -0700
changeset 3969
1d7922586cf6
parent 3928
56c4f88474b3
child 3974
93c71eb28866
permissions
-rw-r--r--

7023639: JSR 292 method handle invocation needs a fast path for compiled code
6984705: JSR 292 method handle creation should not go through JNI
Summary: remove assembly code for JDK 7 chained method handles
Reviewed-by: jrose, twisti, kvn, mhaupt
Contributed-by: John Rose <john.r.rose@oracle.com>, Christian Thalinger <christian.thalinger@oracle.com>, Michael Haupt <michael.haupt@oracle.com>

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

mercurial