src/share/vm/prims/methodHandles.cpp

Tue, 02 Jul 2013 20:27:00 -0700

author
twisti
date
Tue, 02 Jul 2013 20:27:00 -0700
changeset 5354
c1bd7b5bdc70
parent 5178
b7fa10a3a69a
child 5732
b2e698d2276c
permissions
-rw-r--r--

8017571: JSR292: JVM crashing on assert "cast to instanceKlass" while producing MethodHandle for array methods with MethodHandle.findVirtual
Reviewed-by: kvn

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

mercurial