src/share/vm/prims/methodHandles.cpp

Tue, 07 Apr 2015 10:53:51 +0200

author
tschatzl
date
Tue, 07 Apr 2015 10:53:51 +0200
changeset 7781
33e421924c67
parent 7391
fe34c5ab0b35
child 7535
7ae4e26cb1e0
child 7795
157895117ad5
permissions
-rw-r--r--

8058354: SPECjvm2008-Derby -2.7% performance regression on Solaris-X64 starting with 9-b29
Summary: Allow use of large pages for auxiliary data structures in G1. Clean up existing interfaces.
Reviewed-by: jmasa, pliden, stefank

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

mercurial