< prev index next >

src/hotspot/share/prims/methodHandles.cpp

Print this page

 113     StubCodeMark mark(this, "MethodHandle::interpreter_entry", vmIntrinsics::name_at(iid));
 114     address entry = MethodHandles::generate_method_handle_interpreter_entry(_masm, iid);
 115     if (entry != NULL) {
 116       Interpreter::set_entry_for_kind(mk, entry);
 117     }
 118     // If the entry is not set, it will throw AbstractMethodError.
 119   }
 120 }
 121 
 122 void MethodHandles::set_enabled(bool z) {
 123   if (_enabled != z) {
 124     guarantee(z, "can only enable once");
 125     _enabled = z;
 126   }
 127 }
 128 
 129 // MemberName support
 130 
 131 // import java_lang_invoke_MemberName.*
 132 enum {
 133   IS_METHOD            = java_lang_invoke_MemberName::MN_IS_METHOD,
 134   IS_CONSTRUCTOR       = java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR,
 135   IS_FIELD             = java_lang_invoke_MemberName::MN_IS_FIELD,
 136   IS_TYPE              = java_lang_invoke_MemberName::MN_IS_TYPE,
 137   CALLER_SENSITIVE     = java_lang_invoke_MemberName::MN_CALLER_SENSITIVE,
 138   TRUSTED_FINAL        = java_lang_invoke_MemberName::MN_TRUSTED_FINAL,
 139   REFERENCE_KIND_SHIFT = java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT,
 140   REFERENCE_KIND_MASK  = java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK,
 141   SEARCH_SUPERCLASSES  = java_lang_invoke_MemberName::MN_SEARCH_SUPERCLASSES,
 142   SEARCH_INTERFACES    = java_lang_invoke_MemberName::MN_SEARCH_INTERFACES,
 143   LM_UNCONDITIONAL     = java_lang_invoke_MemberName::MN_UNCONDITIONAL_MODE,
 144   LM_MODULE            = java_lang_invoke_MemberName::MN_MODULE_MODE,
 145   LM_TRUSTED           = java_lang_invoke_MemberName::MN_TRUSTED_MODE,
 146   ALL_KINDS      = IS_METHOD | IS_CONSTRUCTOR | IS_FIELD | IS_TYPE

 147 };
 148 
 149 int MethodHandles::ref_kind_to_flags(int ref_kind) {
 150   assert(ref_kind_is_valid(ref_kind), "%d", ref_kind);
 151   int flags = (ref_kind << REFERENCE_KIND_SHIFT);
 152   if (ref_kind_is_field(ref_kind)) {
 153     flags |= IS_FIELD;
 154   } else if (ref_kind_is_method(ref_kind)) {
 155     flags |= IS_METHOD;
 156   } else if (ref_kind == JVM_REF_newInvokeSpecial) {
 157     flags |= IS_CONSTRUCTOR;
 158   }
 159   return flags;
 160 }
 161 
 162 Handle MethodHandles::resolve_MemberName_type(Handle mname, Klass* caller, TRAPS) {
 163   Handle empty;
 164   Handle type(THREAD, java_lang_invoke_MemberName::type(mname()));
 165   if (!java_lang_String::is_instance(type())) {
 166     return type; // already resolved
 167   }
 168   Symbol* signature = java_lang_String::as_symbol_or_null(type());
 169   if (signature == NULL) {
 170     return empty;  // no such signature exists in the VM
 171   }
 172   Handle resolved;
 173   int flags = java_lang_invoke_MemberName::flags(mname());
 174   switch (flags & ALL_KINDS) {
 175     case IS_METHOD:
 176     case IS_CONSTRUCTOR:
 177       resolved = SystemDictionary::find_method_handle_type(signature, caller, CHECK_(empty));
 178       break;
 179     case IS_FIELD:
 180       resolved = SystemDictionary::find_field_handle_type(signature, caller, CHECK_(empty));
 181       break;
 182     default:
 183       THROW_MSG_(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format", empty);
 184   }
 185   if (resolved.is_null()) {
 186     THROW_MSG_(vmSymbols::java_lang_InternalError(), "bad MemberName type", empty);
 187   }
 188   return resolved;
 189 }
 190 
 191 oop MethodHandles::init_MemberName(Handle mname, Handle target, TRAPS) {
 192   // This method is used from java.lang.invoke.MemberName constructors.
 193   // It fills in the new MemberName from a java.lang.reflect.Member.
 194   oop target_oop = target();
 195   Klass* target_klass = target_oop->klass();
 196   if (target_klass == vmClasses::reflect_Field_klass()) {

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

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

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


 818           LinkResolver::resolve_special_call(result, Handle(), link_info, THREAD);
 819         } else {
 820           break;                // will throw after end of switch

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

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

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

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




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

1095     }
1096     jio_snprintf(qname, len, "MethodHandle::interpreter_entry::%s%s", name, suffix);
1097     trace_method_handle(_masm, qname);
1098     // Note:  Don't free the allocated char array because it's used
1099     // during runtime.
1100   }
1101 }
1102 
1103 //
1104 // Here are the native methods in java.lang.invoke.MethodHandleNatives
1105 // They are the private interface between this JVM and the HotSpot-specific
1106 // Java code that implements JSR 292 method handles.
1107 //
1108 // Note:  We use a JVM_ENTRY macro to define each of these, for this is the way
1109 // that intrinsic (non-JNI) native methods are defined in HotSpot.
1110 //
1111 
1112 #ifndef PRODUCT
1113 #define EACH_NAMED_CON(template, requirement) \
1114     template(java_lang_invoke_MemberName,MN_IS_METHOD) \
1115     template(java_lang_invoke_MemberName,MN_IS_CONSTRUCTOR) \
1116     template(java_lang_invoke_MemberName,MN_IS_FIELD) \
1117     template(java_lang_invoke_MemberName,MN_IS_TYPE) \
1118     template(java_lang_invoke_MemberName,MN_CALLER_SENSITIVE) \
1119     template(java_lang_invoke_MemberName,MN_TRUSTED_FINAL) \

1120     template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \
1121     template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \
1122     template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_SHIFT) \
1123     template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_MASK) \
1124     template(java_lang_invoke_MemberName,MN_NESTMATE_CLASS) \
1125     template(java_lang_invoke_MemberName,MN_HIDDEN_CLASS) \
1126     template(java_lang_invoke_MemberName,MN_STRONG_LOADER_LINK) \
1127     template(java_lang_invoke_MemberName,MN_ACCESS_VM_ANNOTATIONS) \
1128     template(java_lang_invoke_MemberName,MN_MODULE_MODE) \
1129     template(java_lang_invoke_MemberName,MN_UNCONDITIONAL_MODE) \
1130     template(java_lang_invoke_MemberName,MN_TRUSTED_MODE) \
1131     /*end*/
1132 
1133 #define IGNORE_REQ(req_expr) /* req_expr */
1134 #define ONE_PLUS(scope,value) 1+
1135 static const int con_value_count = EACH_NAMED_CON(ONE_PLUS, IGNORE_REQ) 0;
1136 #define VALUE_COMMA(scope,value) scope::value,
1137 static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA, IGNORE_REQ) 0 };
1138 #define STRING_NULL(scope,value) #value "\0"
1139 static const char con_names[] = { EACH_NAMED_CON(STRING_NULL, IGNORE_REQ) };

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

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

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

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

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

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





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

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

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