< prev index next >

src/hotspot/share/prims/methodHandles.cpp

Print this page




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


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






 342   if (is_setter)  flags += ((JVM_REF_putField - JVM_REF_getField) << REFERENCE_KIND_SHIFT);
 343   int vmindex        = fd.offset();  // determines the field uniquely when combined with static bit
 344 
 345   oop mname_oop = mname();
 346   java_lang_invoke_MemberName::set_flags  (mname_oop, flags);
 347   java_lang_invoke_MemberName::set_method (mname_oop, NULL);
 348   java_lang_invoke_MemberName::set_vmindex(mname_oop, vmindex);
 349   java_lang_invoke_MemberName::set_clazz  (mname_oop, fd.field_holder()->java_mirror());
 350 
 351   oop type = field_signature_type_or_null(fd.signature());
 352   oop name = field_name_or_null(fd.name());
 353   if (name != NULL)
 354     java_lang_invoke_MemberName::set_name(mname_oop,   name);
 355   if (type != NULL)
 356     java_lang_invoke_MemberName::set_type(mname_oop,   type);
 357   // Note:  name and type can be lazily computed by resolve_MemberName,
 358   // if Java code needs them as resolved String and Class objects.
 359   // Note that the incoming type oop might be pre-resolved (non-null).
 360   // The base clazz and field offset (vmindex) must be eagerly stored,
 361   // because they unambiguously identify the field.


 556     //case 'Z': case 'B': case 'C': case 'S':
 557     default:
 558       return false;
 559     }
 560   }
 561   return true;
 562 }
 563 
 564 Symbol* MethodHandles::lookup_basic_type_signature(Symbol* sig, bool keep_last_arg, TRAPS) {
 565   Symbol* bsig = NULL;
 566   if (sig == NULL) {
 567     return sig;
 568   } else if (is_basic_type_signature(sig)) {
 569     sig->increment_refcount();
 570     return sig;  // that was easy
 571   } else if (sig->char_at(0) != '(') {
 572     BasicType bt = char2type(sig->char_at(0));
 573     if (is_subword_type(bt)) {
 574       bsig = vmSymbols::int_signature();
 575     } else {
 576       assert(bt == T_OBJECT || bt == T_ARRAY, "is_basic_type_signature was false");
 577       bsig = vmSymbols::object_signature();
 578     }
 579   } else {
 580     ResourceMark rm;
 581     stringStream buffer(128);
 582     buffer.put('(');
 583     int arg_pos = 0, keep_arg_pos = -1;
 584     if (keep_last_arg)
 585       keep_arg_pos = ArgumentCount(sig).size() - 1;
 586     for (SignatureStream ss(sig); !ss.is_done(); ss.next()) {
 587       BasicType bt = ss.type();
 588       size_t this_arg_pos = buffer.size();
 589       if (ss.at_return_type()) {
 590         buffer.put(')');
 591       }
 592       if (arg_pos == keep_arg_pos) {
 593         buffer.write((char*) ss.raw_bytes(),
 594                      (int)   ss.raw_length());
 595       } else if (bt == T_OBJECT || bt == T_ARRAY) {
 596         buffer.write(OBJ_SIG, OBJ_SIG_LEN);
 597       } else {
 598         if (is_subword_type(bt))
 599           bt = T_INT;
 600         buffer.put(type2char(bt));
 601       }
 602       arg_pos++;
 603     }
 604     const char* sigstr =       buffer.base();
 605     int         siglen = (int) buffer.size();
 606     bsig = SymbolTable::new_symbol(sigstr, siglen);
 607   }
 608   assert(is_basic_type_signature(bsig) ||
 609          // detune assert in case the injected argument is not a basic type:
 610          keep_last_arg, "");
 611   return bsig;
 612 }
 613 
 614 void MethodHandles::print_as_basic_type_signature_on(outputStream* st,
 615                                                      Symbol* sig,


 814         } else {
 815           assert(false, "ref_kind=%d", ref_kind);
 816         }
 817         if (HAS_PENDING_EXCEPTION) {
 818           if (speculative_resolve) {
 819             CLEAR_PENDING_EXCEPTION;
 820           }
 821           return empty;
 822         }
 823       }
 824       if (result.resolved_appendix().not_null()) {
 825         // The resolved MemberName must not be accompanied by an appendix argument,
 826         // since there is no way to bind this value into the MemberName.
 827         // Caller is responsible to prevent this from happening.
 828         THROW_MSG_(vmSymbols::java_lang_InternalError(), "appendix", empty);
 829       }
 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_CONSTRUCTOR:
 835     {
 836       CallInfo result;
 837       LinkInfo link_info(defc, name, type, caller, access_check);
 838       {
 839         assert(!HAS_PENDING_EXCEPTION, "");
 840         if (name == vmSymbols::object_initializer_name()) {


 841           LinkResolver::resolve_special_call(result, Handle(), link_info, THREAD);
 842         } else {
 843           break;                // will throw after end of switch

 844         }
 845         if (HAS_PENDING_EXCEPTION) {
 846           if (speculative_resolve) {
 847             CLEAR_PENDING_EXCEPTION;
 848           }
 849           return empty;
 850         }
 851       }
 852       assert(result.is_statically_bound(), "");
 853       result.set_resolved_method_name(CHECK_(empty));
 854       oop mname2 = init_method_MemberName(mname, result);
 855       return Handle(THREAD, mname2);
 856     }
 857   case IS_FIELD:
 858     {
 859       fieldDescriptor result; // find_field initializes fd if found
 860       {
 861         assert(!HAS_PENDING_EXCEPTION, "");
 862         LinkInfo link_info(defc, name, type, caller, LinkInfo::skip_access_check);
 863         LinkResolver::resolve_field(result, link_info, Bytecodes::_nop, false, THREAD);


 883 // Resolving it plants a vmtarget/vmindex in it,
 884 // which refers directly to JVM internals.
 885 void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) {
 886   assert(java_lang_invoke_MemberName::is_instance(mname()), "");
 887 
 888   bool have_defc = (java_lang_invoke_MemberName::clazz(mname()) != NULL);
 889   bool have_name = (java_lang_invoke_MemberName::name(mname()) != NULL);
 890   bool have_type = (java_lang_invoke_MemberName::type(mname()) != NULL);
 891   int flags      = java_lang_invoke_MemberName::flags(mname());
 892 
 893   if (suppress != 0) {
 894     if (suppress & _suppress_defc)  have_defc = true;
 895     if (suppress & _suppress_name)  have_name = true;
 896     if (suppress & _suppress_type)  have_type = true;
 897   }
 898 
 899   if (have_defc && have_name && have_type)  return;  // nothing needed
 900 
 901   switch (flags & ALL_KINDS) {
 902   case IS_METHOD:
 903   case IS_CONSTRUCTOR:
 904     {
 905       Method* vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
 906       if (vmtarget == NULL) {
 907         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand");
 908       }
 909       methodHandle m(THREAD, vmtarget);
 910       DEBUG_ONLY(vmtarget = NULL);  // safety
 911       if (!have_defc) {
 912         InstanceKlass* defc = m->method_holder();
 913         java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror());
 914       }
 915       if (!have_name) {
 916         //not java_lang_String::create_from_symbol; let's intern member names
 917         oop name = StringTable::intern(m->name(), CHECK);
 918         java_lang_invoke_MemberName::set_name(mname(), name);
 919       }
 920       if (!have_type) {
 921         Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK);
 922         java_lang_invoke_MemberName::set_type(mname(), type());
 923       }


 965 
 966   if (k == NULL || !k->is_instance_klass())  return -1;
 967 
 968   int rfill = 0, rlimit = results->length(), rskip = skip;
 969   // overflow measurement:
 970   int overflow = 0, overflow_limit = MAX2(1000, rlimit);
 971 
 972   int match_flags = mflags;
 973   bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0);
 974   bool search_intfc  = ((match_flags & SEARCH_INTERFACES)   != 0);
 975   bool local_only = !(search_superc | search_intfc);
 976 
 977   if (name != NULL) {
 978     if (name->utf8_length() == 0)  return 0; // a match is not possible
 979   }
 980   if (sig != NULL) {
 981     if (sig->utf8_length() == 0)  return 0; // a match is not possible
 982     if (sig->char_at(0) == '(')
 983       match_flags &= ~(IS_FIELD | IS_TYPE);
 984     else
 985       match_flags &= ~(IS_CONSTRUCTOR | IS_METHOD);
 986   }
 987 
 988   if ((match_flags & IS_TYPE) != 0) {
 989     // NYI, and Core Reflection works quite well for this query
 990   }
 991 
 992   if ((match_flags & IS_FIELD) != 0) {
 993     InstanceKlass* ik = InstanceKlass::cast(k);
 994     for (FieldStream st(ik, local_only, !search_intfc); !st.eos(); st.next()) {
 995       if (name != NULL && st.name() != name)
 996           continue;
 997       if (sig != NULL && st.signature() != sig)
 998         continue;
 999       // passed the filters
1000       if (rskip > 0) {
1001         --rskip;
1002       } else if (rfill < rlimit) {
1003         Handle result(thread, results->obj_at(rfill++));
1004         if (!java_lang_invoke_MemberName::is_instance(result()))
1005           return -99;  // caller bug!
1006         oop saved = MethodHandles::init_field_MemberName(result, st.field_descriptor());
1007         if (!oopDesc::equals(saved, result()))
1008           results->obj_at_put(rfill-1, saved);  // show saved instance to user
1009       } else if (++overflow >= overflow_limit) {
1010         match_flags = 0; break; // got tired of looking at overflow
1011       }
1012     }
1013   }
1014 
1015   if ((match_flags & (IS_METHOD | IS_CONSTRUCTOR)) != 0) {
1016     // watch out for these guys:
1017     Symbol* init_name   = vmSymbols::object_initializer_name();
1018     Symbol* clinit_name = vmSymbols::class_initializer_name();
1019     if (name == clinit_name)  clinit_name = NULL; // hack for exposing <clinit>
1020     bool negate_name_test = false;
1021     // fix name so that it captures the intention of IS_CONSTRUCTOR
1022     if (!(match_flags & IS_METHOD)) {
1023       // constructors only
1024       if (name == NULL) {
1025         name = init_name;
1026       } else if (name != init_name) {
1027         return 0;               // no constructors of this method name
1028       }
1029     } else if (!(match_flags & IS_CONSTRUCTOR)) {

1030       // methods only
1031       if (name == NULL) {
1032         name = init_name;
1033         negate_name_test = true; // if we see the name, we *omit* the entry
1034       } else if (name == init_name) {
1035         return 0;               // no methods of this constructor name
1036       }
1037     } else {
1038       // caller will accept either sort; no need to adjust name
1039     }
1040     InstanceKlass* ik = InstanceKlass::cast(k);
1041     for (MethodStream st(ik, local_only, !search_intfc); !st.eos(); st.next()) {
1042       Method* m = st.method();
1043       Symbol* m_name = m->name();
1044       if (m_name == clinit_name)
1045         continue;
1046       if (name != NULL && ((m_name != name) ^ negate_name_test))
1047           continue;
1048       if (sig != NULL && m->signature() != sig)
1049         continue;




1050       // passed the filters
1051       if (rskip > 0) {
1052         --rskip;
1053       } else if (rfill < rlimit) {
1054         Handle result(thread, results->obj_at(rfill++));
1055         if (!java_lang_invoke_MemberName::is_instance(result()))
1056           return -99;  // caller bug!
1057         CallInfo info(m, NULL, CHECK_0);
1058         oop saved = MethodHandles::init_method_MemberName(result, info);
1059         if (!oopDesc::equals(saved, result()))
1060           results->obj_at_put(rfill-1, saved);  // show saved instance to user
1061       } else if (++overflow >= overflow_limit) {
1062         match_flags = 0; break; // got tired of looking at overflow
1063       }
1064     }
1065   }
1066 
1067   // return number of elements we at leasted wanted to initialize
1068   return rfill + overflow;
1069 }


1129     }
1130     jio_snprintf(qname, len, "MethodHandle::interpreter_entry::%s%s", name, suffix);
1131     trace_method_handle(_masm, qname);
1132     // Note:  Don't free the allocated char array because it's used
1133     // during runtime.
1134   }
1135 }
1136 
1137 //
1138 // Here are the native methods in java.lang.invoke.MethodHandleNatives
1139 // They are the private interface between this JVM and the HotSpot-specific
1140 // Java code that implements JSR 292 method handles.
1141 //
1142 // Note:  We use a JVM_ENTRY macro to define each of these, for this is the way
1143 // that intrinsic (non-JNI) native methods are defined in HotSpot.
1144 //
1145 
1146 #ifndef PRODUCT
1147 #define EACH_NAMED_CON(template, requirement) \
1148     template(java_lang_invoke_MemberName,MN_IS_METHOD) \
1149     template(java_lang_invoke_MemberName,MN_IS_CONSTRUCTOR) \
1150     template(java_lang_invoke_MemberName,MN_IS_FIELD) \
1151     template(java_lang_invoke_MemberName,MN_IS_TYPE) \
1152     template(java_lang_invoke_MemberName,MN_CALLER_SENSITIVE) \
1153     template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \
1154     template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \
1155     template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_SHIFT) \
1156     template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_MASK) \
1157     /*end*/
1158 
1159 #define IGNORE_REQ(req_expr) /* req_expr */
1160 #define ONE_PLUS(scope,value) 1+
1161 static const int con_value_count = EACH_NAMED_CON(ONE_PLUS, IGNORE_REQ) 0;
1162 #define VALUE_COMMA(scope,value) scope::value,
1163 static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA, IGNORE_REQ) 0 };
1164 #define STRING_NULL(scope,value) #value "\0"
1165 static const char con_names[] = { EACH_NAMED_CON(STRING_NULL, IGNORE_REQ) };
1166 
1167 static bool advertise_con_value(int which) {
1168   if (which < 0)  return false;
1169   bool ok = true;


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




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


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


 562     //case 'Z': case 'B': case 'C': case 'S':
 563     default:
 564       return false;
 565     }
 566   }
 567   return true;
 568 }
 569 
 570 Symbol* MethodHandles::lookup_basic_type_signature(Symbol* sig, bool keep_last_arg, TRAPS) {
 571   Symbol* bsig = NULL;
 572   if (sig == NULL) {
 573     return sig;
 574   } else if (is_basic_type_signature(sig)) {
 575     sig->increment_refcount();
 576     return sig;  // that was easy
 577   } else if (sig->char_at(0) != '(') {
 578     BasicType bt = char2type(sig->char_at(0));
 579     if (is_subword_type(bt)) {
 580       bsig = vmSymbols::int_signature();
 581     } else {
 582       assert(bt == T_OBJECT || bt == T_ARRAY || bt == T_VALUETYPE, "is_basic_type_signature was false");
 583       bsig = vmSymbols::object_signature();
 584     }
 585   } else {
 586     ResourceMark rm;
 587     stringStream buffer(128);
 588     buffer.put('(');
 589     int arg_pos = 0, keep_arg_pos = -1;
 590     if (keep_last_arg)
 591       keep_arg_pos = ArgumentCount(sig).size() - 1;
 592     for (SignatureStream ss(sig); !ss.is_done(); ss.next()) {
 593       BasicType bt = ss.type();
 594       size_t this_arg_pos = buffer.size();
 595       if (ss.at_return_type()) {
 596         buffer.put(')');
 597       }
 598       if (arg_pos == keep_arg_pos) {
 599         buffer.write((char*) ss.raw_bytes(),
 600                      (int)   ss.raw_length());
 601       } else if (bt == T_OBJECT || bt == T_ARRAY || bt == T_VALUETYPE) {
 602         buffer.write(OBJ_SIG, OBJ_SIG_LEN);
 603       } else {
 604         if (is_subword_type(bt))
 605           bt = T_INT;
 606         buffer.put(type2char(bt));
 607       }
 608       arg_pos++;
 609     }
 610     const char* sigstr =       buffer.base();
 611     int         siglen = (int) buffer.size();
 612     bsig = SymbolTable::new_symbol(sigstr, siglen);
 613   }
 614   assert(is_basic_type_signature(bsig) ||
 615          // detune assert in case the injected argument is not a basic type:
 616          keep_last_arg, "");
 617   return bsig;
 618 }
 619 
 620 void MethodHandles::print_as_basic_type_signature_on(outputStream* st,
 621                                                      Symbol* sig,


 820         } else {
 821           assert(false, "ref_kind=%d", ref_kind);
 822         }
 823         if (HAS_PENDING_EXCEPTION) {
 824           if (speculative_resolve) {
 825             CLEAR_PENDING_EXCEPTION;
 826           }
 827           return empty;
 828         }
 829       }
 830       if (result.resolved_appendix().not_null()) {
 831         // The resolved MemberName must not be accompanied by an appendix argument,
 832         // since there is no way to bind this value into the MemberName.
 833         // Caller is responsible to prevent this from happening.
 834         THROW_MSG_(vmSymbols::java_lang_InternalError(), "appendix", empty);
 835       }
 836       result.set_resolved_method_name(CHECK_(empty));
 837       oop mname2 = init_method_MemberName(mname, result);
 838       return Handle(THREAD, mname2);
 839     }
 840   case IS_OBJECT_CONSTRUCTOR:
 841     {
 842       CallInfo result;
 843       LinkInfo link_info(defc, name, type, caller, access_check);
 844       {
 845         assert(!HAS_PENDING_EXCEPTION, "");
 846         if (name != vmSymbols::object_initializer_name()) {
 847           break;                // will throw after end of switch
 848         } else if (type->is_void_method_signature()) {
 849           LinkResolver::resolve_special_call(result, Handle(), link_info, THREAD);
 850         } else {
 851           // LinkageError unless it returns something reasonable
 852           LinkResolver::resolve_static_call(result, link_info, false, THREAD);
 853         }
 854         if (HAS_PENDING_EXCEPTION) {
 855           if (speculative_resolve) {
 856             CLEAR_PENDING_EXCEPTION;
 857           }
 858           return empty;
 859         }
 860       }
 861       assert(result.is_statically_bound(), "");
 862       result.set_resolved_method_name(CHECK_(empty));
 863       oop mname2 = init_method_MemberName(mname, result);
 864       return Handle(THREAD, mname2);
 865     }
 866   case IS_FIELD:
 867     {
 868       fieldDescriptor result; // find_field initializes fd if found
 869       {
 870         assert(!HAS_PENDING_EXCEPTION, "");
 871         LinkInfo link_info(defc, name, type, caller, LinkInfo::skip_access_check);
 872         LinkResolver::resolve_field(result, link_info, Bytecodes::_nop, false, THREAD);


 892 // Resolving it plants a vmtarget/vmindex in it,
 893 // which refers directly to JVM internals.
 894 void MethodHandles::expand_MemberName(Handle mname, int suppress, TRAPS) {
 895   assert(java_lang_invoke_MemberName::is_instance(mname()), "");
 896 
 897   bool have_defc = (java_lang_invoke_MemberName::clazz(mname()) != NULL);
 898   bool have_name = (java_lang_invoke_MemberName::name(mname()) != NULL);
 899   bool have_type = (java_lang_invoke_MemberName::type(mname()) != NULL);
 900   int flags      = java_lang_invoke_MemberName::flags(mname());
 901 
 902   if (suppress != 0) {
 903     if (suppress & _suppress_defc)  have_defc = true;
 904     if (suppress & _suppress_name)  have_name = true;
 905     if (suppress & _suppress_type)  have_type = true;
 906   }
 907 
 908   if (have_defc && have_name && have_type)  return;  // nothing needed
 909 
 910   switch (flags & ALL_KINDS) {
 911   case IS_METHOD:
 912   case IS_OBJECT_CONSTRUCTOR:
 913     {
 914       Method* vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
 915       if (vmtarget == NULL) {
 916         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "nothing to expand");
 917       }
 918       methodHandle m(THREAD, vmtarget);
 919       DEBUG_ONLY(vmtarget = NULL);  // safety
 920       if (!have_defc) {
 921         InstanceKlass* defc = m->method_holder();
 922         java_lang_invoke_MemberName::set_clazz(mname(), defc->java_mirror());
 923       }
 924       if (!have_name) {
 925         //not java_lang_String::create_from_symbol; let's intern member names
 926         oop name = StringTable::intern(m->name(), CHECK);
 927         java_lang_invoke_MemberName::set_name(mname(), name);
 928       }
 929       if (!have_type) {
 930         Handle type = java_lang_String::create_from_symbol(m->signature(), CHECK);
 931         java_lang_invoke_MemberName::set_type(mname(), type());
 932       }


 974 
 975   if (k == NULL || !k->is_instance_klass())  return -1;
 976 
 977   int rfill = 0, rlimit = results->length(), rskip = skip;
 978   // overflow measurement:
 979   int overflow = 0, overflow_limit = MAX2(1000, rlimit);
 980 
 981   int match_flags = mflags;
 982   bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0);
 983   bool search_intfc  = ((match_flags & SEARCH_INTERFACES)   != 0);
 984   bool local_only = !(search_superc | search_intfc);
 985 
 986   if (name != NULL) {
 987     if (name->utf8_length() == 0)  return 0; // a match is not possible
 988   }
 989   if (sig != NULL) {
 990     if (sig->utf8_length() == 0)  return 0; // a match is not possible
 991     if (sig->char_at(0) == '(')
 992       match_flags &= ~(IS_FIELD | IS_TYPE);
 993     else
 994       match_flags &= ~(IS_OBJECT_CONSTRUCTOR | IS_METHOD);
 995   }
 996 
 997   if ((match_flags & IS_TYPE) != 0) {
 998     // NYI, and Core Reflection works quite well for this query
 999   }
1000 
1001   if ((match_flags & IS_FIELD) != 0) {
1002     InstanceKlass* ik = InstanceKlass::cast(k);
1003     for (FieldStream st(ik, local_only, !search_intfc); !st.eos(); st.next()) {
1004       if (name != NULL && st.name() != name)
1005           continue;
1006       if (sig != NULL && st.signature() != sig)
1007         continue;
1008       // passed the filters
1009       if (rskip > 0) {
1010         --rskip;
1011       } else if (rfill < rlimit) {
1012         Handle result(thread, results->obj_at(rfill++));
1013         if (!java_lang_invoke_MemberName::is_instance(result()))
1014           return -99;  // caller bug!
1015         oop saved = MethodHandles::init_field_MemberName(result, st.field_descriptor());
1016         if (!oopDesc::equals(saved, result()))
1017           results->obj_at_put(rfill-1, saved);  // show saved instance to user
1018       } else if (++overflow >= overflow_limit) {
1019         match_flags = 0; break; // got tired of looking at overflow
1020       }
1021     }
1022   }
1023 
1024   if ((match_flags & (IS_METHOD | IS_OBJECT_CONSTRUCTOR)) != 0) {
1025     // watch out for these guys:
1026     Symbol* init_name   = vmSymbols::object_initializer_name();
1027     Symbol* clinit_name = vmSymbols::class_initializer_name();
1028     if (name == clinit_name)  clinit_name = NULL; // hack for exposing <clinit>
1029     bool ctor_ok = true, sfac_ok = true;
1030     // fix name so that it captures the intention of IS_OBJECT_CONSTRUCTOR
1031     if (!(match_flags & IS_METHOD)) {
1032       // constructors only
1033       if (name == NULL) {
1034         name = init_name;
1035       } else if (name != init_name) {
1036         return 0;               // no constructors of this method name
1037       }
1038       sfac_ok = false;
1039     } else if (!(match_flags & IS_OBJECT_CONSTRUCTOR)) {
1040       // methods only
1041       ctor_ok = false;  // but sfac_ok is true, so we might find <init>





1042     } else {
1043       // caller will accept either sort; no need to adjust name
1044     }
1045     InstanceKlass* ik = InstanceKlass::cast(k);
1046     for (MethodStream st(ik, local_only, !search_intfc); !st.eos(); st.next()) {
1047       Method* m = st.method();
1048       Symbol* m_name = m->name();
1049       if (m_name == clinit_name)
1050         continue;
1051       if (name != NULL && m_name != name)
1052           continue;
1053       if (sig != NULL && m->signature() != sig)
1054         continue;
1055       if (m_name == init_name) {  // might be either ctor or sfac
1056         if (m->is_object_constructor()  && !ctor_ok)  continue;
1057         if (m->is_static_init_factory() && !sfac_ok)  continue;
1058       }
1059       // passed the filters
1060       if (rskip > 0) {
1061         --rskip;
1062       } else if (rfill < rlimit) {
1063         Handle result(thread, results->obj_at(rfill++));
1064         if (!java_lang_invoke_MemberName::is_instance(result()))
1065           return -99;  // caller bug!
1066         CallInfo info(m, NULL, CHECK_0);
1067         oop saved = MethodHandles::init_method_MemberName(result, info);
1068         if (!oopDesc::equals(saved, result()))
1069           results->obj_at_put(rfill-1, saved);  // show saved instance to user
1070       } else if (++overflow >= overflow_limit) {
1071         match_flags = 0; break; // got tired of looking at overflow
1072       }
1073     }
1074   }
1075 
1076   // return number of elements we at leasted wanted to initialize
1077   return rfill + overflow;
1078 }


1138     }
1139     jio_snprintf(qname, len, "MethodHandle::interpreter_entry::%s%s", name, suffix);
1140     trace_method_handle(_masm, qname);
1141     // Note:  Don't free the allocated char array because it's used
1142     // during runtime.
1143   }
1144 }
1145 
1146 //
1147 // Here are the native methods in java.lang.invoke.MethodHandleNatives
1148 // They are the private interface between this JVM and the HotSpot-specific
1149 // Java code that implements JSR 292 method handles.
1150 //
1151 // Note:  We use a JVM_ENTRY macro to define each of these, for this is the way
1152 // that intrinsic (non-JNI) native methods are defined in HotSpot.
1153 //
1154 
1155 #ifndef PRODUCT
1156 #define EACH_NAMED_CON(template, requirement) \
1157     template(java_lang_invoke_MemberName,MN_IS_METHOD) \
1158     template(java_lang_invoke_MemberName,MN_IS_OBJECT_CONSTRUCTOR) \
1159     template(java_lang_invoke_MemberName,MN_IS_FIELD) \
1160     template(java_lang_invoke_MemberName,MN_IS_TYPE) \
1161     template(java_lang_invoke_MemberName,MN_CALLER_SENSITIVE) \
1162     template(java_lang_invoke_MemberName,MN_SEARCH_SUPERCLASSES) \
1163     template(java_lang_invoke_MemberName,MN_SEARCH_INTERFACES) \
1164     template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_SHIFT) \
1165     template(java_lang_invoke_MemberName,MN_REFERENCE_KIND_MASK) \
1166     /*end*/
1167 
1168 #define IGNORE_REQ(req_expr) /* req_expr */
1169 #define ONE_PLUS(scope,value) 1+
1170 static const int con_value_count = EACH_NAMED_CON(ONE_PLUS, IGNORE_REQ) 0;
1171 #define VALUE_COMMA(scope,value) scope::value,
1172 static const int con_values[con_value_count+1] = { EACH_NAMED_CON(VALUE_COMMA, IGNORE_REQ) 0 };
1173 #define STRING_NULL(scope,value) #value "\0"
1174 static const char con_names[] = { EACH_NAMED_CON(STRING_NULL, IGNORE_REQ) };
1175 
1176 static bool advertise_con_value(int which) {
1177   if (which < 0)  return false;
1178   bool ok = true;


1263   }
1264 
1265   Klass* caller = caller_jh == NULL ? NULL :
1266                      java_lang_Class::as_Klass(JNIHandles::resolve_non_null(caller_jh));
1267   Handle resolved = MethodHandles::resolve_MemberName(mname, caller, speculative_resolve == JNI_TRUE,
1268                                                       CHECK_NULL);
1269 
1270   if (resolved.is_null()) {
1271     int flags = java_lang_invoke_MemberName::flags(mname());
1272     int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK;
1273     if (!MethodHandles::ref_kind_is_valid(ref_kind)) {
1274       THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "obsolete MemberName format");
1275     }
1276     if (speculative_resolve) {
1277       assert(!HAS_PENDING_EXCEPTION, "No exceptions expected when resolving speculatively");
1278       return NULL;
1279     }
1280     if ((flags & ALL_KINDS) == IS_FIELD) {
1281       THROW_MSG_NULL(vmSymbols::java_lang_NoSuchFieldError(), "field resolution failed");
1282     } else if ((flags & ALL_KINDS) == IS_METHOD ||
1283                (flags & ALL_KINDS) == IS_OBJECT_CONSTRUCTOR) {
1284       THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), "method resolution failed");
1285     } else {
1286       THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "resolution failed");
1287     }
1288   }
1289 
1290   return JNIHandles::make_local(THREAD, resolved());
1291 }
1292 JVM_END
1293 
1294 static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) {
1295   if (mname == NULL ||
1296       java_lang_invoke_MemberName::clazz(mname) == NULL) {
1297     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved");
1298   } else {
1299     int flags = java_lang_invoke_MemberName::flags(mname);
1300     if ((flags & IS_FIELD) != 0 &&
1301         (must_be_static
1302          ? (flags & JVM_ACC_STATIC) != 0
1303          : (flags & JVM_ACC_STATIC) == 0)) {


< prev index next >