src/share/vm/prims/methodHandles.cpp

changeset 5108
f0bc60565ba8
parent 4993
746b070f5022
child 5115
e484fe2abebd
     1.1 --- a/src/share/vm/prims/methodHandles.cpp	Fri May 03 15:35:30 2013 -0700
     1.2 +++ b/src/share/vm/prims/methodHandles.cpp	Mon May 06 13:53:13 2013 -0700
     1.3 @@ -1298,6 +1298,28 @@
     1.4  }
     1.5  JVM_END
     1.6  
     1.7 +/**
     1.8 + * Throws a java/lang/UnsupportedOperationException unconditionally.
     1.9 + * This is required by the specification of MethodHandle.invoke if
    1.10 + * invoked directly.
    1.11 + */
    1.12 +JVM_ENTRY(jobject, MH_invoke_UOE(JNIEnv* env, jobject mh, jobjectArray args)) {
    1.13 +  THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invoke cannot be invoked reflectively");
    1.14 +  return NULL;
    1.15 +}
    1.16 +JVM_END
    1.17 +
    1.18 +/**
    1.19 + * Throws a java/lang/UnsupportedOperationException unconditionally.
    1.20 + * This is required by the specification of MethodHandle.invokeExact if
    1.21 + * invoked directly.
    1.22 + */
    1.23 +JVM_ENTRY(jobject, MH_invokeExact_UOE(JNIEnv* env, jobject mh, jobjectArray args)) {
    1.24 +  THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invokeExact cannot be invoked reflectively");
    1.25 +  return NULL;
    1.26 +}
    1.27 +JVM_END
    1.28 +
    1.29  /// JVM_RegisterMethodHandleMethods
    1.30  
    1.31  #undef CS  // Solaris builds complain
    1.32 @@ -1317,7 +1339,7 @@
    1.33  #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
    1.34  
    1.35  // These are the native methods on java.lang.invoke.MethodHandleNatives.
    1.36 -static JNINativeMethod required_methods_JDK8[] = {
    1.37 +static JNINativeMethod MHN_methods[] = {
    1.38    {CC"init",                      CC"("MEM""OBJ")V",                     FN_PTR(MHN_init_Mem)},
    1.39    {CC"expand",                    CC"("MEM")V",                          FN_PTR(MHN_expand_Mem)},
    1.40    {CC"resolve",                   CC"("MEM""CLS")"MEM,                   FN_PTR(MHN_resolve_Mem)},
    1.41 @@ -1335,8 +1357,28 @@
    1.42    {CC"getMemberVMInfo",           CC"("MEM")"OBJ,                        FN_PTR(MHN_getMemberVMInfo)}
    1.43  };
    1.44  
    1.45 -// This one function is exported, used by NativeLookup.
    1.46 +static JNINativeMethod MH_methods[] = {
    1.47 +  // UnsupportedOperationException throwers
    1.48 +  {CC"invoke",                    CC"(["OBJ")"OBJ,                       FN_PTR(MH_invoke_UOE)},
    1.49 +  {CC"invokeExact",               CC"(["OBJ")"OBJ,                       FN_PTR(MH_invokeExact_UOE)}
    1.50 +};
    1.51  
    1.52 +/**
    1.53 + * Helper method to register native methods.
    1.54 + */
    1.55 +static bool register_natives(JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods) {
    1.56 +  int status = env->RegisterNatives(clazz, methods, nMethods);
    1.57 +  if (status != JNI_OK || env->ExceptionOccurred()) {
    1.58 +    warning("JSR 292 method handle code is mismatched to this JVM.  Disabling support.");
    1.59 +    env->ExceptionClear();
    1.60 +    return false;
    1.61 +  }
    1.62 +  return true;
    1.63 +}
    1.64 +
    1.65 +/**
    1.66 + * This one function is exported, used by NativeLookup.
    1.67 + */
    1.68  JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) {
    1.69    if (!EnableInvokeDynamic) {
    1.70      warning("JSR 292 is disabled in this JVM.  Use -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic to enable.");
    1.71 @@ -1354,16 +1396,14 @@
    1.72      MH_class = (jclass) JNIHandles::make_local(env, mirror);
    1.73    }
    1.74  
    1.75 -  int status;
    1.76 -
    1.77    if (enable_MH) {
    1.78      ThreadToNativeFromVM ttnfv(thread);
    1.79  
    1.80 -    status = env->RegisterNatives(MHN_class, required_methods_JDK8, sizeof(required_methods_JDK8)/sizeof(JNINativeMethod));
    1.81 -    if (status != JNI_OK || env->ExceptionOccurred()) {
    1.82 -      warning("JSR 292 method handle code is mismatched to this JVM.  Disabling support.");
    1.83 -      enable_MH = false;
    1.84 -      env->ExceptionClear();
    1.85 +    if (enable_MH) {
    1.86 +      enable_MH = register_natives(env, MHN_class, MHN_methods, sizeof(MHN_methods)/sizeof(JNINativeMethod));
    1.87 +    }
    1.88 +    if (enable_MH) {
    1.89 +      enable_MH = register_natives(env, MH_class, MH_methods, sizeof(MH_methods)/sizeof(JNINativeMethod));
    1.90      }
    1.91    }
    1.92  

mercurial