src/share/vm/prims/methodHandles.cpp

Print this page




  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/symbolTable.hpp"
  27 #include "compiler/compileBroker.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "interpreter/oopMapCache.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/oopFactory.hpp"

  32 #include "prims/methodHandles.hpp"
  33 #include "runtime/compilationPolicy.hpp"
  34 #include "runtime/javaCalls.hpp"
  35 #include "runtime/reflection.hpp"
  36 #include "runtime/signature.hpp"
  37 #include "runtime/stubRoutines.hpp"
  38 
  39 
  40 /*
  41  * JSR 292 reference implementation: method handles
  42  * The JDK 7 reference implementation represented method handle
  43  * combinations as chains.  Each link in the chain had a "vmentry"
  44  * field which pointed at a bit of assembly code which performed
  45  * one transformation before dispatching to the next link in the chain.
  46  *
  47  * The current reference implementation pushes almost all code generation
  48  * responsibility to (trusted) Java code.  A method handle contains a
  49  * pointer to its "LambdaForm", which embodies all details of the method
  50  * handle's behavior.  The LambdaForm is a normal Java object, managed
  51  * by a runtime coded in Java.


 783         java_lang_invoke_MemberName::set_name(mname(), name());
 784       }
 785       if (!have_type) {
 786         // If it is a primitive field type, don't mess with short strings like "I".
 787         Handle type = field_signature_type_or_null(fd.signature());
 788         if (type.is_null()) {
 789           java_lang_String::create_from_symbol(fd.signature(), CHECK);
 790         }
 791         java_lang_invoke_MemberName::set_type(mname(), type());
 792       }
 793       return;
 794     }
 795   }
 796   THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format");
 797 }
 798 
 799 int MethodHandles::find_MemberNames(Klass* k,
 800                                     Symbol* name, Symbol* sig,
 801                                     int mflags, Klass* caller,
 802                                     int skip, objArrayOop results) {
 803   DEBUG_ONLY(No_Safepoint_Verifier nsv);
 804   // this code contains no safepoints!
 805 
 806   // %%% take caller into account!
 807 
 808   if (k == NULL || !k->oop_is_instance())  return -1;
 809 
 810   int rfill = 0, rlimit = results->length(), rskip = skip;
 811   // overflow measurement:
 812   int overflow = 0, overflow_limit = MAX2(1000, rlimit);
 813 
 814   int match_flags = mflags;
 815   bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0);
 816   bool search_intfc  = ((match_flags & SEARCH_INTERFACES)   != 0);
 817   bool local_only = !(search_superc | search_intfc);
 818   bool classes_only = false;
 819 
 820   if (name != NULL) {
 821     if (name->utf8_length() == 0)  return 0; // a match is not possible
 822   }
 823   if (sig != NULL) {
 824     if (sig->utf8_length() == 0)  return 0; // a match is not possible
 825     if (sig->byte_at(0) == '(')


 894         continue;
 895       // passed the filters
 896       if (rskip > 0) {
 897         --rskip;
 898       } else if (rfill < rlimit) {
 899         oop result = results->obj_at(rfill++);
 900         if (!java_lang_invoke_MemberName::is_instance(result))
 901           return -99;  // caller bug!
 902         oop saved = MethodHandles::init_method_MemberName(result, m, true, NULL);
 903         if (saved != result)
 904           results->obj_at_put(rfill-1, saved);  // show saved instance to user
 905       } else if (++overflow >= overflow_limit) {
 906         match_flags = 0; break; // got tired of looking at overflow
 907       }
 908     }
 909   }
 910 
 911   // return number of elements we at leasted wanted to initialize
 912   return rfill + overflow;
 913 }



 914 //




















































































 915 // Here are the native methods in java.lang.invoke.MethodHandleNatives
 916 // They are the private interface between this JVM and the HotSpot-specific
 917 // Java code that implements JSR 292 method handles.
 918 //
 919 // Note:  We use a JVM_ENTRY macro to define each of these, for this is the way
 920 // that intrinsic (non-JNI) native methods are defined in HotSpot.
 921 //
 922 
 923 JVM_ENTRY(jint, MHN_getConstant(JNIEnv *env, jobject igcls, jint which)) {
 924   switch (which) {
 925   case MethodHandles::GC_COUNT_GWT:
 926 #ifdef COMPILER2
 927     return true;
 928 #else
 929     return false;
 930 #endif
 931   }
 932   return 0;
 933 }
 934 JVM_END




  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/symbolTable.hpp"
  27 #include "compiler/compileBroker.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "interpreter/oopMapCache.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/oopFactory.hpp"
  32 #include "prims/jvmtiRedefineClassesTrace.hpp"
  33 #include "prims/methodHandles.hpp"
  34 #include "runtime/compilationPolicy.hpp"
  35 #include "runtime/javaCalls.hpp"
  36 #include "runtime/reflection.hpp"
  37 #include "runtime/signature.hpp"
  38 #include "runtime/stubRoutines.hpp"
  39 
  40 
  41 /*
  42  * JSR 292 reference implementation: method handles
  43  * The JDK 7 reference implementation represented method handle
  44  * combinations as chains.  Each link in the chain had a "vmentry"
  45  * field which pointed at a bit of assembly code which performed
  46  * one transformation before dispatching to the next link in the chain.
  47  *
  48  * The current reference implementation pushes almost all code generation
  49  * responsibility to (trusted) Java code.  A method handle contains a
  50  * pointer to its "LambdaForm", which embodies all details of the method
  51  * handle's behavior.  The LambdaForm is a normal Java object, managed
  52  * by a runtime coded in Java.


 784         java_lang_invoke_MemberName::set_name(mname(), name());
 785       }
 786       if (!have_type) {
 787         // If it is a primitive field type, don't mess with short strings like "I".
 788         Handle type = field_signature_type_or_null(fd.signature());
 789         if (type.is_null()) {
 790           java_lang_String::create_from_symbol(fd.signature(), CHECK);
 791         }
 792         java_lang_invoke_MemberName::set_type(mname(), type());
 793       }
 794       return;
 795     }
 796   }
 797   THROW_MSG(vmSymbols::java_lang_InternalError(), "unrecognized MemberName format");
 798 }
 799 
 800 int MethodHandles::find_MemberNames(Klass* k,
 801                                     Symbol* name, Symbol* sig,
 802                                     int mflags, Klass* caller,
 803                                     int skip, objArrayOop results) {



 804   // %%% take caller into account!
 805 
 806   if (k == NULL || !k->oop_is_instance())  return -1;
 807 
 808   int rfill = 0, rlimit = results->length(), rskip = skip;
 809   // overflow measurement:
 810   int overflow = 0, overflow_limit = MAX2(1000, rlimit);
 811 
 812   int match_flags = mflags;
 813   bool search_superc = ((match_flags & SEARCH_SUPERCLASSES) != 0);
 814   bool search_intfc  = ((match_flags & SEARCH_INTERFACES)   != 0);
 815   bool local_only = !(search_superc | search_intfc);
 816   bool classes_only = false;
 817 
 818   if (name != NULL) {
 819     if (name->utf8_length() == 0)  return 0; // a match is not possible
 820   }
 821   if (sig != NULL) {
 822     if (sig->utf8_length() == 0)  return 0; // a match is not possible
 823     if (sig->byte_at(0) == '(')


 892         continue;
 893       // passed the filters
 894       if (rskip > 0) {
 895         --rskip;
 896       } else if (rfill < rlimit) {
 897         oop result = results->obj_at(rfill++);
 898         if (!java_lang_invoke_MemberName::is_instance(result))
 899           return -99;  // caller bug!
 900         oop saved = MethodHandles::init_method_MemberName(result, m, true, NULL);
 901         if (saved != result)
 902           results->obj_at_put(rfill-1, saved);  // show saved instance to user
 903       } else if (++overflow >= overflow_limit) {
 904         match_flags = 0; break; // got tired of looking at overflow
 905       }
 906     }
 907   }
 908 
 909   // return number of elements we at leasted wanted to initialize
 910   return rfill + overflow;
 911 }
 912 
 913 //------------------------------------------------------------------------------
 914 // MemberNameTable
 915 //
 916 
 917 MemberNameTable::MemberNameTable() : GrowableArray<jweak>(10, true) {
 918 }
 919 
 920 MemberNameTable::~MemberNameTable() {
 921   int len = this->length();
 922 
 923   for (int idx = 0; idx < len; idx++) {
 924     jweak ref = this->at(idx);
 925     JNIHandles::destroy_weak_global(ref);
 926   }
 927 }
 928 
 929 // Return entry index if found, return -1 otherwise.
 930 int MemberNameTable::find_member_name(oop mem_name) {
 931   int len = this->length();
 932 
 933   for (int idx = 0; idx < len; idx++) {
 934     jweak ref = this->at(idx);
 935     oop entry = JNIHandles::resolve(ref);
 936     if (entry == mem_name) {
 937       return idx;
 938     }
 939   }
 940   return -1;
 941 }
 942 
 943 void MemberNameTable::add_member_name(oop mem_name) {
 944   // Each member name may appear just once: add only if not found
 945   if (find_member_name(mem_name) == -1) {
 946     this->append(JNIHandles::make_weak_global(mem_name));
 947   }
 948 }
 949 
 950 #if INCLUDE_JVMTI
 951 // It is called at safepoint only from the MemberNameTable::adjust_method_entries().
 952 // No synchronization is needed.
 953 oop MemberNameTable::find_member_name_by_method(Method* old_method) {
 954   oop found = NULL;
 955   int len = this->length();
 956 
 957   for (int idx = 0; idx < len; idx++) {
 958     oop mem_name = JNIHandles::resolve(this->at(idx));
 959     if (mem_name == NULL) {
 960       continue;
 961     }
 962     Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mem_name);
 963     if (method == old_method) {
 964       found = mem_name;
 965       break;
 966     }
 967   }
 968   return found;
 969 }
 970 
 971 // It is called at safepoint only
 972 void MemberNameTable::adjust_method_entries(Method** old_methods, Method** new_methods,
 973                                             int methods_length, bool *trace_name_printed) {
 974   // search the MemberNameTable for uses of either obsolete or EMCP methods
 975   for (int j = 0; j < methods_length; j++) {
 976     Method* old_method = old_methods[j];
 977     Method* new_method = new_methods[j];
 978     oop mem_name = find_member_name_by_method(old_method);
 979     if (mem_name != NULL) {
 980       java_lang_invoke_MemberName::adjust_vmtarget(mem_name, new_method);
 981 
 982       if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
 983         if (!(*trace_name_printed)) {
 984           // RC_TRACE_MESG macro has an embedded ResourceMark
 985           RC_TRACE_MESG(("adjust: name=%s",
 986                          old_method->method_holder()->external_name()));
 987           *trace_name_printed = true;
 988         }
 989         // RC_TRACE macro has an embedded ResourceMark
 990         RC_TRACE(0x00400000, ("MemberName method update: %s(%s)",
 991                               new_method->name()->as_C_string(),
 992                               new_method->signature()->as_C_string()));
 993       }
 994     }
 995   }
 996 }
 997 #endif // INCLUDE_JVMTI
 998 
 999 //
1000 // Here are the native methods in java.lang.invoke.MethodHandleNatives
1001 // They are the private interface between this JVM and the HotSpot-specific
1002 // Java code that implements JSR 292 method handles.
1003 //
1004 // Note:  We use a JVM_ENTRY macro to define each of these, for this is the way
1005 // that intrinsic (non-JNI) native methods are defined in HotSpot.
1006 //
1007 
1008 JVM_ENTRY(jint, MHN_getConstant(JNIEnv *env, jobject igcls, jint which)) {
1009   switch (which) {
1010   case MethodHandles::GC_COUNT_GWT:
1011 #ifdef COMPILER2
1012     return true;
1013 #else
1014     return false;
1015 #endif
1016   }
1017   return 0;
1018 }
1019 JVM_END