< prev index next >

src/hotspot/share/prims/methodHandles.cpp

Print this page

 109     StubCodeMark mark(this, "MethodHandle::interpreter_entry", vmIntrinsics::name_at(iid));
 110     address entry = MethodHandles::generate_method_handle_interpreter_entry(_masm, iid);
 111     if (entry != NULL) {
 112       Interpreter::set_entry_for_kind(mk, entry);
 113     }
 114     // If the entry is not set, it will throw AbstractMethodError.
 115   }
 116 }
 117 
 118 void MethodHandles::set_enabled(bool z) {
 119   if (_enabled != z) {
 120     guarantee(z, "can only enable once");
 121     _enabled = z;
 122   }
 123 }
 124 
 125 // MemberName support
 126 
 127 // import java_lang_invoke_MemberName.*
 128 enum {
 129   IS_METHOD            = java_lang_invoke_MemberName::MN_IS_METHOD,
 130   IS_CONSTRUCTOR       = java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR,
 131   IS_FIELD             = java_lang_invoke_MemberName::MN_IS_FIELD,
 132   IS_TYPE              = java_lang_invoke_MemberName::MN_IS_TYPE,
 133   CALLER_SENSITIVE     = java_lang_invoke_MemberName::MN_CALLER_SENSITIVE,
 134   TRUSTED_FINAL        = java_lang_invoke_MemberName::MN_TRUSTED_FINAL,
 135   REFERENCE_KIND_SHIFT = java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT,
 136   REFERENCE_KIND_MASK  = java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK,
 137   SEARCH_SUPERCLASSES  = java_lang_invoke_MemberName::MN_SEARCH_SUPERCLASSES,
 138   SEARCH_INTERFACES    = java_lang_invoke_MemberName::MN_SEARCH_INTERFACES,
 139   LM_UNCONDITIONAL     = java_lang_invoke_MemberName::MN_UNCONDITIONAL_MODE,
 140   LM_MODULE            = java_lang_invoke_MemberName::MN_MODULE_MODE,
 141   LM_TRUSTED           = java_lang_invoke_MemberName::MN_TRUSTED_MODE,
 142   ALL_KINDS      = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE
 143 };
 144 
 145 int MethodHandles::ref_kind_to_flags(int ref_kind) {
 146   assert(ref_kind_is_valid(ref_kind), "%d", ref_kind);
 147   int flags = (ref_kind << REFERENCE_KIND_SHIFT);
 148   if (ref_kind_is_field(ref_kind)) {
 149     flags |= IS_FIELD;
 150   } else if (ref_kind_is_method(ref_kind)) {
 151     flags |= IS_METHOD;
 152   } else if (ref_kind == JVM_REF_newInvokeSpecial) {
 153     flags |= IS_CONSTRUCTOR;
 154   }
 155   return flags;
 156 }
 157 
 158 Handle MethodHandles::resolve_MemberName_type(Handle mname, Klass* caller, TRAPS) {
 159   Handle empty;
 160   Handle type(THREAD, java_lang_invoke_MemberName::type(mname()));
 161   if (!java_lang_String::is_instance(type())) {
 162     return type; // already resolved
 163   }
 164   Symbol* signature = java_lang_String::as_symbol_or_null(type());
 165   if (signature == NULL) {
 166     return empty;  // no such signature exists in the VM
 167   }
 168   Handle resolved;
 169   int flags = java_lang_invoke_MemberName::flags(mname());
 170   switch (flags & ALL_KINDS) {
 171     case IS_METHOD:
 172     case IS_CONSTRUCTOR:
 173       resolved = SystemDictionary::find_method_handle_type(signature, caller, CHECK_(empty));
 174       break;
 175     case IS_FIELD:
 176       resolved = SystemDictionary::find_field_handle_type(signature, caller, CHECK_(empty));
 177       break;
 178     default:
 179       THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format", empty);
 180   }
 181   if (resolved.is_null()) {
 182     THROW_MSG_(vmSymbols::java_lang_InternalError(), "bad MemberName type", empty);
 183   }
 184   return resolved;
 185 }
 186 
 187 oop MethodHandles::init_MemberName(Handle mname, Handle target, TRAPS) {
 188   // This method is used from java.lang.invoke.MemberName constructors.
 189   // It fills in the new MemberName from a java.lang.reflect.Member.
 190   oop target_oop = target();
 191   Klass* target_klass = target_oop->klass();
 192   if (target_klass == vmClasses::reflect_Field_klass()) {

 294       m_klass = m_klass_non_interface;
 295     }
 296     if (lt_indy.is_enabled()) {
 297       ResourceMark rm;
 298       LogStream ls(lt_indy);
 299       ls.print_cr("memberName: invokevirtual method_holder::method: %s, receiver: %s, vtableindex: %d, access_flags:",
 300                   Method::name_and_sig_as_C_string(m->method_holder(), m->name(), m->signature()),
 301                   m_klass->internal_name(), vmindex);
 302        m->access_flags().print_on(&ls);
 303        if (m->is_default_method()) {
 304          ls.print("default");
 305        }
 306        ls.cr();
 307     }
 308     break;
 309 
 310   case CallInfo::direct_call:
 311     vmindex = Method::nonvirtual_vtable_index;
 312     if (m->is_static()) {
 313       flags |= IS_METHOD      | (JVM_REF_invokeStatic  << REFERENCE_KIND_SHIFT);
 314     } else if (m->is_initializer()) {
 315       flags |= IS_CONSTRUCTOR | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT);
 316     } else {
 317       // "special" reflects that this is a direct call, not that it
 318       // necessarily originates from an invokespecial. We can also do
 319       // direct calls for private and/or final non-static methods.
 320       flags |= IS_METHOD      | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT);
 321     }
 322     break;
 323 
 324   default:  assert(false, "bad CallInfo");  return NULL;
 325   }
 326 
 327   // @CallerSensitive annotation detected
 328   if (m->caller_sensitive()) {
 329     flags |= CALLER_SENSITIVE;
 330   }
 331 
 332   Handle resolved_method = info.resolved_method_name();
 333   assert(java_lang_invoke_ResolvedMethodName::vmtarget(resolved_method()) == m() || m->is_old(),
 334          "Should not change after link resolution");
 335 
 336   oop mname_oop = mname();
 337   java_lang_invoke_MemberName::set_flags  (mname_oop, flags);
 338   java_lang_invoke_MemberName::set_method (mname_oop, resolved_method());
 339   java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex);   // vtable/itable index
 340   java_lang_invoke_MemberName::set_clazz  (mname_oop, m_klass->java_mirror());
 341   // Note:  name and type can be lazily computed by resolve_MemberName,
 342   // if Java code needs them as resolved String and MethodType objects.
 343   // If relevant, the vtable or itable value is stored as vmindex.
 344   // This is done eagerly, since it is readily available without
 345   // constructing any new objects.
 346   return mname();
 347 }
 348 
 349 oop MethodHandles::init_field_MemberName(Handle mname, fieldDescriptor& fd, bool is_setter) {
 350   InstanceKlass* ik = fd.field_holder();
 351   int flags = (jushort)( fd.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS );
 352   flags |= IS_FIELD | ((fd.is_static() ? JVM_REF_getStatic : JVM_REF_getField) << REFERENCE_KIND_SHIFT);



 353   if (fd.is_trusted_final()) flags |= TRUSTED_FINAL;
 354   if (is_setter)  flags += ((JVM_REF_putField - JVM_REF_getField) << REFERENCE_KIND_SHIFT);
 355   int vmindex        = fd.offset();  // determines the field uniquely when combined with static bit
 356 
 357   oop mname_oop = mname();
 358   java_lang_invoke_MemberName::set_flags  (mname_oop, flags);
 359   java_lang_invoke_MemberName::set_method (mname_oop, NULL);
 360   java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex);
 361   java_lang_invoke_MemberName::set_clazz  (mname_oop, ik->java_mirror());
 362 
 363   oop type = field_signature_type_or_null(fd.signature());
 364   oop name = field_name_or_null(fd.name());
 365   if (name != NULL)
 366     java_lang_invoke_MemberName::set_name(mname_oop,   name);
 367   if (type != NULL)
 368     java_lang_invoke_MemberName::set_type(mname_oop,   type);
 369   // Note:  name and type can be lazily computed by resolve_MemberName,
 370   // if Java code needs them as resolved String and Class objects.
 371   // Note that the incoming type oop might be pre-resolved (non-null).
 372   // The base clazz and field offset (vmindex) must be eagerly stored,

 787         } else {
 788           assert(false, "ref_kind=%d", ref_kind);
 789         }
 790         if (HAS_PENDING_EXCEPTION) {
 791           if (speculative_resolve) {
 792             CLEAR_PENDING_EXCEPTION;
 793           }
 794           return empty;
 795         }
 796       }
 797       if (result.resolved_appendix().not_null()) {
 798         // The resolved MemberName must not be accompanied by an appendix argument,
 799         // since there is no way to bind this value into the MemberName.
 800         // Caller is responsible to prevent this from happening.
 801         THROW_MSG_(vmSymbols::java_lang_InternalError(), "appendix", empty);
 802       }
 803       result.set_resolved_method_name(CHECK_(empty));
 804       oop mname2 = init_method_MemberName(mname, result);
 805       return Handle(THREAD, mname2);
 806     }
 807   case IS_CONSTRUCTOR:
 808     {
 809       CallInfo result;
 810       LinkInfo link_info(defc, name, type, caller, access_check, loader_constraint_check);
 811       {
 812         assert(!HAS_PENDING_EXCEPTION, "");
 813         if (name == vmSymbols::object_initializer_name()) {


 814           LinkResolver::resolve_special_call(result, Handle(), link_info, THREAD);
 815         } else {
 816           break;                // will throw after end of switch

 817         }
 818         if (HAS_PENDING_EXCEPTION) {
 819           if (speculative_resolve) {
 820             CLEAR_PENDING_EXCEPTION;
 821           }
 822           return empty;
 823         }
 824       }
 825       assert(result.is_statically_bound(), "");
 826       result.set_resolved_method_name(CHECK_(empty));
 827       oop mname2 = init_method_MemberName(mname, result);
 828       return Handle(THREAD, mname2);
 829     }
 830   case IS_FIELD:
 831     {
 832       fieldDescriptor result; // find_field initializes fd if found
 833       {
 834         assert(!HAS_PENDING_EXCEPTION, "");
 835         LinkInfo link_info(defc, name, type, caller, LinkInfo::AccessCheck::skip, loader_constraint_check);
 836         LinkResolver::resolve_field(result, link_info, Bytecodes::_nop, false, THREAD);

 856 // Resolving it plants a vmtarget/vmindex in it,
 857 // which refers directly to JVM internals.
 858 void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) {
 859   assert(java_lang_invoke_MemberName::is_instance(mname()), "");
 860 
 861   bool have_defc = (java_lang_invoke_MemberName::clazz(mname()) != NULL);
 862   bool have_name = (java_lang_invoke_MemberName::name(mname()) != NULL);
 863   bool have_type = (java_lang_invoke_MemberName::type(mname()) != NULL);
 864   int flags      = java_lang_invoke_MemberName::flags(mname());
 865 
 866   if (suppress != 0) {
 867     if (suppress & _suppress_defc)  have_defc = true;
 868     if (suppress & _suppress_name)  have_name = true;
 869     if (suppress & _suppress_type)  have_type = true;
 870   }
 871 
 872   if (have_defc && have_name && have_type)  return;  // nothing needed
 873 
 874   switch (flags & ALL_KINDS) {
 875   case IS_METHOD:
 876   case IS_CONSTRUCTOR:
 877     {
 878       Method* vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
 879       if (vmtarget == NULL) {
 880         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand");
 881       }
 882       methodHandle m(THREAD, vmtarget);
 883       DEBUG_ONLY(vmtarget = NULL);  // safety
 884       if (!have_defc) {
 885         InstanceKlass* defc = m->method_holder();
 886         java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror());
 887       }
 888       if (!have_name) {
 889         //not java_lang_String::create_from_symbol; let's intern member names
 890         oop name = StringTable::intern(m->name(), CHECK);
 891         java_lang_invoke_MemberName::set_name(mname(), name);
 892       }
 893       if (!have_type) {
 894         Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK);
 895         java_lang_invoke_MemberName::set_type(mname(), type());
 896       }

 935   // %%% take caller into account!
 936 
 937   if (k == NULL || !k->is_instance_klass())  return -1;
 938 
 939   int rfill = 0, rlimit = results->length(), rskip = skip;
 940   // overflow measurement:
 941   int overflow = 0, overflow_limit = MAX2(1000, rlimit);
 942 
 943   int match_flags = mflags;
 944   bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0);
 945   bool search_intfc  = ((match_flags & SEARCH_INTERFACES)   != 0);
 946   bool local_only = !(search_superc | search_intfc);
 947 
 948   if (name != NULL) {
 949     if (name->utf8_length() == 0)  return 0; // a match is not possible
 950   }
 951   if (sig != NULL) {
 952     if (sig->starts_with(JVM_SIGNATURE_FUNC))
 953       match_flags &= ~(IS_FIELD | IS_TYPE);
 954     else
 955       match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD);
 956   }
 957 
 958   if ((match_flags & IS_TYPE) != 0) {
 959     // NYI, and Core Reflection works quite well for this query
 960   }
 961 
 962   if ((match_flags & IS_FIELD) != 0) {
 963     InstanceKlass* ik = InstanceKlass::cast(k);
 964     for (FieldStream st(ik, local_only, !search_intfc); !st.eos(); st.next()) {
 965       if (name != NULL && st.name() != name)
 966           continue;
 967       if (sig != NULL && st.signature() != sig)
 968         continue;
 969       // passed the filters
 970       if (rskip > 0) {
 971         --rskip;
 972       } else if (rfill < rlimit) {
 973         Handle result(THREAD, results->obj_at(rfill++));
 974         if (!java_lang_invoke_MemberName::is_instance(result()))
 975           return -99;  // caller bug!
 976         oop saved = MethodHandles::init_field_MemberName(result, st.field_descriptor());
 977         if (saved != result())
 978           results->obj_at_put(rfill-1, saved);  // show saved instance to user
 979       } else if (++overflow >= overflow_limit) {
 980         match_flags = 0; break; // got tired of looking at overflow
 981       }
 982     }
 983   }
 984 
 985   if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) {
 986     // watch out for these guys:
 987     Symbol* init_name   = vmSymbols::object_initializer_name();
 988     Symbol* clinit_name = vmSymbols::class_initializer_name();
 989     if (name == clinit_name)  clinit_name = NULL; // hack for exposing <clinit>
 990     bool negate_name_test = false;
 991     // fix name so that it captures the intention of IS_CONSTRUCTOR
 992     if (!(match_flags & IS_METHOD)) {
 993       // constructors only
 994       if (name == NULL) {
 995         name = init_name;
 996       } else if (name != init_name) {
 997         return 0;               // no constructors of this method name
 998       }
 999     } else if (!(match_flags & IS_CONSTRUCTOR)) {

1000       // methods only
1001       if (name == NULL) {
1002         name = init_name;
1003         negate_name_test = true; // if we see the name, we *omit* the entry
1004       } else if (name == init_name) {
1005         return 0;               // no methods of this constructor name
1006       }
1007     } else {
1008       // caller will accept either sort; no need to adjust name
1009     }
1010     InstanceKlass* ik = InstanceKlass::cast(k);
1011     for (MethodStream st(ik, local_only, !search_intfc); !st.eos(); st.next()) {
1012       Method* m = st.method();
1013       Symbol* m_name = m->name();
1014       if (m_name == clinit_name)
1015         continue;
1016       if (name != NULL && ((m_name != name) ^ negate_name_test))
1017           continue;
1018       if (sig != NULL && m->signature() != sig)
1019         continue;




1020       // passed the filters
1021       if (rskip > 0) {
1022         --rskip;
1023       } else if (rfill < rlimit) {
1024         Handle result(THREAD, results->obj_at(rfill++));
1025         if (!java_lang_invoke_MemberName::is_instance(result()))
1026           return -99;  // caller bug!
1027         CallInfo info(m, NULL, CHECK_0);
1028         oop saved = MethodHandles::init_method_MemberName(result, info);
1029         if (saved != result())
1030           results->obj_at_put(rfill-1, saved);  // show saved instance to user
1031       } else if (++overflow >= overflow_limit) {
1032         match_flags = 0; break; // got tired of looking at overflow
1033       }
1034     }
1035   }
1036 
1037   // return number of elements we at leasted wanted to initialize
1038   return rfill + overflow;
1039 }

1099     }
1100     jio_snprintf(qname, len, "MethodHandle::interpreter_entry::%s%s", name, suffix);
1101     trace_method_handle(_masm, qname);
1102     // Note:  Don't free the allocated char array because it's used
1103     // during runtime.
1104   }
1105 }
1106 
1107 //
1108 // Here are the native methods in java.lang.invoke.MethodHandleNatives
1109 // They are the private interface between this JVM and the HotSpot-specific
1110 // Java code that implements JSR 292 method handles.
1111 //
1112 // Note:  We use a JVM_ENTRY macro to define each of these, for this is the way
1113 // that intrinsic (non-JNI) native methods are defined in HotSpot.
1114 //
1115 
1116 #ifndef PRODUCT
1117 #define EACH_NAMED_CON(template, requirement) \
1118     template(java_lang_invoke_MemberName,MN_IS_METHOD) \
1119     template(java_lang_invoke_MemberName,MN_IS_CONSTRUCTOR) \
1120     template(java_lang_invoke_MemberName,MN_IS_FIELD) \
1121     template(java_lang_invoke_MemberName,MN_IS_TYPE) \
1122     template(java_lang_invoke_MemberName,MN_CALLER_SENSITIVE) \
1123     template(java_lang_invoke_MemberName,MN_TRUSTED_FINAL) \
1124     template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \
1125     template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \
1126     template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_SHIFT) \
1127     template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_MASK) \
1128     template(java_lang_invoke_MemberName,MN_NESTMATE_CLASS) \
1129     template(java_lang_invoke_MemberName,MN_HIDDEN_CLASS) \
1130     template(java_lang_invoke_MemberName,MN_STRONG_LOADER_LINK) \
1131     template(java_lang_invoke_MemberName,MN_ACCESS_VM_ANNOTATIONS) \
1132     template(java_lang_invoke_MemberName,MN_MODULE_MODE) \
1133     template(java_lang_invoke_MemberName,MN_UNCONDITIONAL_MODE) \
1134     template(java_lang_invoke_MemberName,MN_TRUSTED_MODE) \
1135     /*end*/
1136 
1137 #define IGNORE_REQ(req_expr) /* req_expr */
1138 #define ONE_PLUS(scope,value) 1+
1139 static const int con_value_count = EACH_NAMED_CON(ONE_PLUS, IGNORE_REQ) 0;

1243 
1244   Klass* caller = caller_jh == NULL ? NULL :
1245                      java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh));
1246   Handle resolved = MethodHandles::resolve_MemberName(mname, caller, lookup_mode,
1247                                                       speculative_resolve == JNI_TRUE,
1248                                                       CHECK_NULL);
1249 
1250   if (resolved.is_null()) {
1251     int flags = java_lang_invoke_MemberName::flags(mname());
1252     int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK;
1253     if (!MethodHandles::ref_kind_is_valid(ref_kind)) {
1254       THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "obsolete MemberName format");
1255     }
1256     if (speculative_resolve) {
1257       assert(!HAS_PENDING_EXCEPTION, "No exceptions expected when resolving speculatively");
1258       return NULL;
1259     }
1260     if ((flags & ALL_KINDS) == IS_FIELD) {
1261       THROW_MSG_NULL(vmSymbols::java_lang_NoSuchFieldError(), "field resolution failed");
1262     } else if ((flags & ALL_KINDS) == IS_METHOD ||
1263                (flags & ALL_KINDS) == IS_CONSTRUCTOR) {
1264       THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), "method resolution failed");
1265     } else {
1266       THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "resolution failed");
1267     }
1268   }
1269 
1270   return JNIHandles::make_local(THREAD, resolved());
1271 }
1272 JVM_END
1273 
1274 static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) {
1275   if (mname == NULL ||
1276       java_lang_invoke_MemberName::clazz(mname) == NULL) {
1277     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved");
1278   } else {
1279     int flags = java_lang_invoke_MemberName::flags(mname);
1280     if ((flags & IS_FIELD) != 0 &&
1281         (must_be_static
1282          ? (flags & JVM_ACC_STATIC) != 0
1283          : (flags & JVM_ACC_STATIC) == 0)) {

 109     StubCodeMark mark(this, "MethodHandle::interpreter_entry", vmIntrinsics::name_at(iid));
 110     address entry = MethodHandles::generate_method_handle_interpreter_entry(_masm, iid);
 111     if (entry != NULL) {
 112       Interpreter::set_entry_for_kind(mk, entry);
 113     }
 114     // If the entry is not set, it will throw AbstractMethodError.
 115   }
 116 }
 117 
 118 void MethodHandles::set_enabled(bool z) {
 119   if (_enabled != z) {
 120     guarantee(z, "can only enable once");
 121     _enabled = z;
 122   }
 123 }
 124 
 125 // MemberName support
 126 
 127 // import java_lang_invoke_MemberName.*
 128 enum {
 129   IS_METHOD             = java_lang_invoke_MemberName::MN_IS_METHOD,
 130   IS_OBJECT_CONSTRUCTOR = java_lang_invoke_MemberName::MN_IS_OBJECT_CONSTRUCTOR,
 131   IS_FIELD              = java_lang_invoke_MemberName::MN_IS_FIELD,
 132   IS_TYPE               = java_lang_invoke_MemberName::MN_IS_TYPE,
 133   CALLER_SENSITIVE      = java_lang_invoke_MemberName::MN_CALLER_SENSITIVE,
 134   TRUSTED_FINAL        = java_lang_invoke_MemberName::MN_TRUSTED_FINAL,
 135   REFERENCE_KIND_SHIFT  = java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT,
 136   REFERENCE_KIND_MASK   = java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK,
 137   SEARCH_SUPERCLASSES   = java_lang_invoke_MemberName::MN_SEARCH_SUPERCLASSES,
 138   SEARCH_INTERFACES     = java_lang_invoke_MemberName::MN_SEARCH_INTERFACES,
 139   LM_UNCONDITIONAL      = java_lang_invoke_MemberName::MN_UNCONDITIONAL_MODE,
 140   LM_MODULE             = java_lang_invoke_MemberName::MN_MODULE_MODE,
 141   LM_TRUSTED            = java_lang_invoke_MemberName::MN_TRUSTED_MODE,
 142   ALL_KINDS      = IS_METHOD | IS_OBJECT_CONSTRUCTOR | IS_FIELD | IS_TYPE
 143 };
 144 
 145 int MethodHandles::ref_kind_to_flags(int ref_kind) {
 146   assert(ref_kind_is_valid(ref_kind), "%d", ref_kind);
 147   int flags = (ref_kind << REFERENCE_KIND_SHIFT);
 148   if (ref_kind_is_field(ref_kind)) {
 149     flags |= IS_FIELD;
 150   } else if (ref_kind_is_method(ref_kind)) {
 151     flags |= IS_METHOD;
 152   } else if (ref_kind == JVM_REF_newInvokeSpecial) {
 153     flags |= IS_OBJECT_CONSTRUCTOR;
 154   }
 155   return flags;
 156 }
 157 
 158 Handle MethodHandles::resolve_MemberName_type(Handle mname, Klass* caller, TRAPS) {
 159   Handle empty;
 160   Handle type(THREAD, java_lang_invoke_MemberName::type(mname()));
 161   if (!java_lang_String::is_instance(type())) {
 162     return type; // already resolved
 163   }
 164   Symbol* signature = java_lang_String::as_symbol_or_null(type());
 165   if (signature == NULL) {
 166     return empty;  // no such signature exists in the VM
 167   }
 168   Handle resolved;
 169   int flags = java_lang_invoke_MemberName::flags(mname());
 170   switch (flags & ALL_KINDS) {
 171     case IS_METHOD:
 172     case IS_OBJECT_CONSTRUCTOR:
 173       resolved = SystemDictionary::find_method_handle_type(signature, caller, CHECK_(empty));
 174       break;
 175     case IS_FIELD:
 176       resolved = SystemDictionary::find_field_handle_type(signature, caller, CHECK_(empty));
 177       break;
 178     default:
 179       THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format", empty);
 180   }
 181   if (resolved.is_null()) {
 182     THROW_MSG_(vmSymbols::java_lang_InternalError(), "bad MemberName type", empty);
 183   }
 184   return resolved;
 185 }
 186 
 187 oop MethodHandles::init_MemberName(Handle mname, Handle target, TRAPS) {
 188   // This method is used from java.lang.invoke.MemberName constructors.
 189   // It fills in the new MemberName from a java.lang.reflect.Member.
 190   oop target_oop = target();
 191   Klass* target_klass = target_oop->klass();
 192   if (target_klass == vmClasses::reflect_Field_klass()) {

 294       m_klass = m_klass_non_interface;
 295     }
 296     if (lt_indy.is_enabled()) {
 297       ResourceMark rm;
 298       LogStream ls(lt_indy);
 299       ls.print_cr("memberName: invokevirtual method_holder::method: %s, receiver: %s, vtableindex: %d, access_flags:",
 300                   Method::name_and_sig_as_C_string(m->method_holder(), m->name(), m->signature()),
 301                   m_klass->internal_name(), vmindex);
 302        m->access_flags().print_on(&ls);
 303        if (m->is_default_method()) {
 304          ls.print("default");
 305        }
 306        ls.cr();
 307     }
 308     break;
 309 
 310   case CallInfo::direct_call:
 311     vmindex = Method::nonvirtual_vtable_index;
 312     if (m->is_static()) {
 313       flags |= IS_METHOD      | (JVM_REF_invokeStatic  << REFERENCE_KIND_SHIFT);
 314     } else if (m->is_object_constructor()) {
 315       flags |= IS_OBJECT_CONSTRUCTOR | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT);
 316     } else {
 317       // "special" reflects that this is a direct call, not that it
 318       // necessarily originates from an invokespecial. We can also do
 319       // direct calls for private and/or final non-static methods.
 320       flags |= IS_METHOD      | (JVM_REF_invokeSpecial << REFERENCE_KIND_SHIFT);
 321     }
 322     break;
 323 
 324   default:  assert(false, "bad CallInfo");  return NULL;
 325   }
 326 
 327   // @CallerSensitive annotation detected
 328   if (m->caller_sensitive()) {
 329     flags |= CALLER_SENSITIVE;
 330   }
 331 
 332   Handle resolved_method = info.resolved_method_name();
 333   assert(java_lang_invoke_ResolvedMethodName::vmtarget(resolved_method()) == m() || m->is_old(),
 334          "Should not change after link resolution");
 335 
 336   oop mname_oop = mname();
 337   java_lang_invoke_MemberName::set_flags  (mname_oop, flags);
 338   java_lang_invoke_MemberName::set_method (mname_oop, resolved_method());
 339   java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex);   // vtable/itable index
 340   java_lang_invoke_MemberName::set_clazz  (mname_oop, m_klass->java_mirror());
 341   // Note:  name and type can be lazily computed by resolve_MemberName,
 342   // if Java code needs them as resolved String and MethodType objects.
 343   // If relevant, the vtable or itable value is stored as vmindex.
 344   // This is done eagerly, since it is readily available without
 345   // constructing any new objects.
 346   return mname();
 347 }
 348 
 349 oop MethodHandles::init_field_MemberName(Handle mname, fieldDescriptor& fd, bool is_setter) {
 350   InstanceKlass* ik = fd.field_holder();
 351   int flags = (jushort)( fd.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS );
 352   flags |= IS_FIELD | ((fd.is_static() ? JVM_REF_getStatic : JVM_REF_getField) << REFERENCE_KIND_SHIFT);
 353   if (fd.is_inlined()) {
 354     flags |= JVM_ACC_FIELD_INLINED;
 355   }
 356   if (fd.is_trusted_final()) flags |= TRUSTED_FINAL;
 357   if (is_setter)  flags += ((JVM_REF_putField - JVM_REF_getField) << REFERENCE_KIND_SHIFT);
 358   int vmindex        = fd.offset();  // determines the field uniquely when combined with static bit
 359 
 360   oop mname_oop = mname();
 361   java_lang_invoke_MemberName::set_flags  (mname_oop, flags);
 362   java_lang_invoke_MemberName::set_method (mname_oop, NULL);
 363   java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex);
 364   java_lang_invoke_MemberName::set_clazz  (mname_oop, ik->java_mirror());
 365 
 366   oop type = field_signature_type_or_null(fd.signature());
 367   oop name = field_name_or_null(fd.name());
 368   if (name != NULL)
 369     java_lang_invoke_MemberName::set_name(mname_oop,   name);
 370   if (type != NULL)
 371     java_lang_invoke_MemberName::set_type(mname_oop,   type);
 372   // Note:  name and type can be lazily computed by resolve_MemberName,
 373   // if Java code needs them as resolved String and Class objects.
 374   // Note that the incoming type oop might be pre-resolved (non-null).
 375   // The base clazz and field offset (vmindex) must be eagerly stored,

 790         } else {
 791           assert(false, "ref_kind=%d", ref_kind);
 792         }
 793         if (HAS_PENDING_EXCEPTION) {
 794           if (speculative_resolve) {
 795             CLEAR_PENDING_EXCEPTION;
 796           }
 797           return empty;
 798         }
 799       }
 800       if (result.resolved_appendix().not_null()) {
 801         // The resolved MemberName must not be accompanied by an appendix argument,
 802         // since there is no way to bind this value into the MemberName.
 803         // Caller is responsible to prevent this from happening.
 804         THROW_MSG_(vmSymbols::java_lang_InternalError(), "appendix", empty);
 805       }
 806       result.set_resolved_method_name(CHECK_(empty));
 807       oop mname2 = init_method_MemberName(mname, result);
 808       return Handle(THREAD, mname2);
 809     }
 810   case IS_OBJECT_CONSTRUCTOR:
 811     {
 812       CallInfo result;
 813       LinkInfo link_info(defc, name, type, caller, access_check, loader_constraint_check);
 814       {
 815         assert(!HAS_PENDING_EXCEPTION, "");
 816         if (name != vmSymbols::object_initializer_name()) {
 817           break;                // will throw after end of switch
 818         } else if (type->is_void_method_signature()) {
 819           LinkResolver::resolve_special_call(result, Handle(), link_info, THREAD);
 820         } else {
 821           // LinkageError unless it returns something reasonable
 822           LinkResolver::resolve_static_call(result, link_info, false, THREAD);
 823         }
 824         if (HAS_PENDING_EXCEPTION) {
 825           if (speculative_resolve) {
 826             CLEAR_PENDING_EXCEPTION;
 827           }
 828           return empty;
 829         }
 830       }
 831       assert(result.is_statically_bound(), "");
 832       result.set_resolved_method_name(CHECK_(empty));
 833       oop mname2 = init_method_MemberName(mname, result);
 834       return Handle(THREAD, mname2);
 835     }
 836   case IS_FIELD:
 837     {
 838       fieldDescriptor result; // find_field initializes fd if found
 839       {
 840         assert(!HAS_PENDING_EXCEPTION, "");
 841         LinkInfo link_info(defc, name, type, caller, LinkInfo::AccessCheck::skip, loader_constraint_check);
 842         LinkResolver::resolve_field(result, link_info, Bytecodes::_nop, false, THREAD);

 862 // Resolving it plants a vmtarget/vmindex in it,
 863 // which refers directly to JVM internals.
 864 void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) {
 865   assert(java_lang_invoke_MemberName::is_instance(mname()), "");
 866 
 867   bool have_defc = (java_lang_invoke_MemberName::clazz(mname()) != NULL);
 868   bool have_name = (java_lang_invoke_MemberName::name(mname()) != NULL);
 869   bool have_type = (java_lang_invoke_MemberName::type(mname()) != NULL);
 870   int flags      = java_lang_invoke_MemberName::flags(mname());
 871 
 872   if (suppress != 0) {
 873     if (suppress & _suppress_defc)  have_defc = true;
 874     if (suppress & _suppress_name)  have_name = true;
 875     if (suppress & _suppress_type)  have_type = true;
 876   }
 877 
 878   if (have_defc && have_name && have_type)  return;  // nothing needed
 879 
 880   switch (flags & ALL_KINDS) {
 881   case IS_METHOD:
 882   case IS_OBJECT_CONSTRUCTOR:
 883     {
 884       Method* vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
 885       if (vmtarget == NULL) {
 886         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand");
 887       }
 888       methodHandle m(THREAD, vmtarget);
 889       DEBUG_ONLY(vmtarget = NULL);  // safety
 890       if (!have_defc) {
 891         InstanceKlass* defc = m->method_holder();
 892         java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror());
 893       }
 894       if (!have_name) {
 895         //not java_lang_String::create_from_symbol; let's intern member names
 896         oop name = StringTable::intern(m->name(), CHECK);
 897         java_lang_invoke_MemberName::set_name(mname(), name);
 898       }
 899       if (!have_type) {
 900         Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK);
 901         java_lang_invoke_MemberName::set_type(mname(), type());
 902       }

 941   // %%% take caller into account!
 942 
 943   if (k == NULL || !k->is_instance_klass())  return -1;
 944 
 945   int rfill = 0, rlimit = results->length(), rskip = skip;
 946   // overflow measurement:
 947   int overflow = 0, overflow_limit = MAX2(1000, rlimit);
 948 
 949   int match_flags = mflags;
 950   bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0);
 951   bool search_intfc  = ((match_flags & SEARCH_INTERFACES)   != 0);
 952   bool local_only = !(search_superc | search_intfc);
 953 
 954   if (name != NULL) {
 955     if (name->utf8_length() == 0)  return 0; // a match is not possible
 956   }
 957   if (sig != NULL) {
 958     if (sig->starts_with(JVM_SIGNATURE_FUNC))
 959       match_flags &= ~(IS_FIELD | IS_TYPE);
 960     else
 961       match_flags &= ~(IS_OBJECT_CONSTRUCTOR | IS_METHOD);
 962   }
 963 
 964   if ((match_flags & IS_TYPE) != 0) {
 965     // NYI, and Core Reflection works quite well for this query
 966   }
 967 
 968   if ((match_flags & IS_FIELD) != 0) {
 969     InstanceKlass* ik = InstanceKlass::cast(k);
 970     for (FieldStream st(ik, local_only, !search_intfc); !st.eos(); st.next()) {
 971       if (name != NULL && st.name() != name)
 972           continue;
 973       if (sig != NULL && st.signature() != sig)
 974         continue;
 975       // passed the filters
 976       if (rskip > 0) {
 977         --rskip;
 978       } else if (rfill < rlimit) {
 979         Handle result(THREAD, results->obj_at(rfill++));
 980         if (!java_lang_invoke_MemberName::is_instance(result()))
 981           return -99;  // caller bug!
 982         oop saved = MethodHandles::init_field_MemberName(result, st.field_descriptor());
 983         if (saved != result())
 984           results->obj_at_put(rfill-1, saved);  // show saved instance to user
 985       } else if (++overflow >= overflow_limit) {
 986         match_flags = 0; break; // got tired of looking at overflow
 987       }
 988     }
 989   }
 990 
 991   if ((match_flags & (IS_METHOD | IS_OBJECT_CONSTRUCTOR)) != 0) {
 992     // watch out for these guys:
 993     Symbol* init_name   = vmSymbols::object_initializer_name();
 994     Symbol* clinit_name = vmSymbols::class_initializer_name();
 995     if (name == clinit_name)  clinit_name = NULL; // hack for exposing <clinit>
 996     bool ctor_ok = true, sfac_ok = true;
 997     // fix name so that it captures the intention of IS_OBJECT_CONSTRUCTOR
 998     if (!(match_flags & IS_METHOD)) {
 999       // constructors only
1000       if (name == NULL) {
1001         name = init_name;
1002       } else if (name != init_name) {
1003         return 0;               // no constructors of this method name
1004       }
1005       sfac_ok = false;
1006     } else if (!(match_flags & IS_OBJECT_CONSTRUCTOR)) {
1007       // methods only
1008       ctor_ok = false;  // but sfac_ok is true, so we might find <init>





1009     } else {
1010       // caller will accept either sort; no need to adjust name
1011     }
1012     InstanceKlass* ik = InstanceKlass::cast(k);
1013     for (MethodStream st(ik, local_only, !search_intfc); !st.eos(); st.next()) {
1014       Method* m = st.method();
1015       Symbol* m_name = m->name();
1016       if (m_name == clinit_name)
1017         continue;
1018       if (name != NULL && m_name != name)
1019           continue;
1020       if (sig != NULL && m->signature() != sig)
1021         continue;
1022       if (m_name == init_name) {  // might be either ctor or sfac
1023         if (m->is_object_constructor()  && !ctor_ok)  continue;
1024         if (m->is_static_init_factory() && !sfac_ok)  continue;
1025       }
1026       // passed the filters
1027       if (rskip > 0) {
1028         --rskip;
1029       } else if (rfill < rlimit) {
1030         Handle result(THREAD, results->obj_at(rfill++));
1031         if (!java_lang_invoke_MemberName::is_instance(result()))
1032           return -99;  // caller bug!
1033         CallInfo info(m, NULL, CHECK_0);
1034         oop saved = MethodHandles::init_method_MemberName(result, info);
1035         if (saved != result())
1036           results->obj_at_put(rfill-1, saved);  // show saved instance to user
1037       } else if (++overflow >= overflow_limit) {
1038         match_flags = 0; break; // got tired of looking at overflow
1039       }
1040     }
1041   }
1042 
1043   // return number of elements we at leasted wanted to initialize
1044   return rfill + overflow;
1045 }

1105     }
1106     jio_snprintf(qname, len, "MethodHandle::interpreter_entry::%s%s", name, suffix);
1107     trace_method_handle(_masm, qname);
1108     // Note:  Don't free the allocated char array because it's used
1109     // during runtime.
1110   }
1111 }
1112 
1113 //
1114 // Here are the native methods in java.lang.invoke.MethodHandleNatives
1115 // They are the private interface between this JVM and the HotSpot-specific
1116 // Java code that implements JSR 292 method handles.
1117 //
1118 // Note:  We use a JVM_ENTRY macro to define each of these, for this is the way
1119 // that intrinsic (non-JNI) native methods are defined in HotSpot.
1120 //
1121 
1122 #ifndef PRODUCT
1123 #define EACH_NAMED_CON(template, requirement) \
1124     template(java_lang_invoke_MemberName,MN_IS_METHOD) \
1125     template(java_lang_invoke_MemberName,MN_IS_OBJECT_CONSTRUCTOR) \
1126     template(java_lang_invoke_MemberName,MN_IS_FIELD) \
1127     template(java_lang_invoke_MemberName,MN_IS_TYPE) \
1128     template(java_lang_invoke_MemberName,MN_CALLER_SENSITIVE) \
1129     template(java_lang_invoke_MemberName,MN_TRUSTED_FINAL) \
1130     template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \
1131     template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \
1132     template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_SHIFT) \
1133     template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_MASK) \
1134     template(java_lang_invoke_MemberName,MN_NESTMATE_CLASS) \
1135     template(java_lang_invoke_MemberName,MN_HIDDEN_CLASS) \
1136     template(java_lang_invoke_MemberName,MN_STRONG_LOADER_LINK) \
1137     template(java_lang_invoke_MemberName,MN_ACCESS_VM_ANNOTATIONS) \
1138     template(java_lang_invoke_MemberName,MN_MODULE_MODE) \
1139     template(java_lang_invoke_MemberName,MN_UNCONDITIONAL_MODE) \
1140     template(java_lang_invoke_MemberName,MN_TRUSTED_MODE) \
1141     /*end*/
1142 
1143 #define IGNORE_REQ(req_expr) /* req_expr */
1144 #define ONE_PLUS(scope,value) 1+
1145 static const int con_value_count = EACH_NAMED_CON(ONE_PLUS, IGNORE_REQ) 0;

1249 
1250   Klass* caller = caller_jh == NULL ? NULL :
1251                      java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh));
1252   Handle resolved = MethodHandles::resolve_MemberName(mname, caller, lookup_mode,
1253                                                       speculative_resolve == JNI_TRUE,
1254                                                       CHECK_NULL);
1255 
1256   if (resolved.is_null()) {
1257     int flags = java_lang_invoke_MemberName::flags(mname());
1258     int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK;
1259     if (!MethodHandles::ref_kind_is_valid(ref_kind)) {
1260       THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "obsolete MemberName format");
1261     }
1262     if (speculative_resolve) {
1263       assert(!HAS_PENDING_EXCEPTION, "No exceptions expected when resolving speculatively");
1264       return NULL;
1265     }
1266     if ((flags & ALL_KINDS) == IS_FIELD) {
1267       THROW_MSG_NULL(vmSymbols::java_lang_NoSuchFieldError(), "field resolution failed");
1268     } else if ((flags & ALL_KINDS) == IS_METHOD ||
1269                (flags & ALL_KINDS) == IS_OBJECT_CONSTRUCTOR) {
1270       THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), "method resolution failed");
1271     } else {
1272       THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "resolution failed");
1273     }
1274   }
1275 
1276   return JNIHandles::make_local(THREAD, resolved());
1277 }
1278 JVM_END
1279 
1280 static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) {
1281   if (mname == NULL ||
1282       java_lang_invoke_MemberName::clazz(mname) == NULL) {
1283     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved");
1284   } else {
1285     int flags = java_lang_invoke_MemberName::flags(mname);
1286     if ((flags & IS_FIELD) != 0 &&
1287         (must_be_static
1288          ? (flags & JVM_ACC_STATIC) != 0
1289          : (flags & JVM_ACC_STATIC) == 0)) {
< prev index next >