test/runtime/jni/CallWithJNIWeak/CallWithJNIWeak.c

changeset 9671
e86bc9786d83
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/runtime/jni/CallWithJNIWeak/CallWithJNIWeak.c	Fri May 10 18:50:40 2019 +0000
     1.3 @@ -0,0 +1,142 @@
     1.4 +/*
     1.5 + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +#include <jni.h>
    1.28 +
    1.29 +/*
    1.30 + * Class:     CallWithJNIWeak
    1.31 + * Method:    testJNIFieldAccessors
    1.32 + * Signature: (LCallWithJNIWeak;)V
    1.33 + */
    1.34 +JNIEXPORT void JNICALL
    1.35 +Java_CallWithJNIWeak_testJNIFieldAccessors(JNIEnv *env, jclass clazz, jobject this) {
    1.36 +  // Make sure that we have a weak reference to the receiver
    1.37 +
    1.38 +  jweak self = (*env)->NewWeakGlobalRef(env, this);
    1.39 +
    1.40 +  jclass this_class = (*env)->GetObjectClass(env, self);
    1.41 +
    1.42 +  jclass exception = (*env)->FindClass(env, "java/lang/RuntimeException");
    1.43 +
    1.44 +  jfieldID id_i = (*env)->GetFieldID(env, this_class, "i", "I");
    1.45 +  jfieldID id_j = (*env)->GetFieldID(env, this_class, "j", "J");
    1.46 +  jfieldID id_z = (*env)->GetFieldID(env, this_class, "z", "Z");
    1.47 +  jfieldID id_c = (*env)->GetFieldID(env, this_class, "c", "C");
    1.48 +  jfieldID id_s = (*env)->GetFieldID(env, this_class, "s", "S");
    1.49 +  jfieldID id_f = (*env)->GetFieldID(env, this_class, "f", "F");
    1.50 +  jfieldID id_d = (*env)->GetFieldID(env, this_class, "d", "D");
    1.51 +  jfieldID id_l = (*env)->GetFieldID(env, this_class, "l", "Ljava/lang/Object;");
    1.52 +  jvalue v;
    1.53 +
    1.54 +#define CHECK(variable, expected)                                   \
    1.55 +  do {                                                              \
    1.56 +    if ((variable) != (expected)) {                                 \
    1.57 +      (*env)->ThrowNew(env, exception,  #variable" != " #expected); \
    1.58 +      return;                                                       \
    1.59 +    }                                                               \
    1.60 +  } while(0)
    1.61 +
    1.62 +  // The values checked below must be kept in sync with the Java source file.
    1.63 +
    1.64 +  v.i = (*env)->GetIntField(env, self, id_i);
    1.65 +  CHECK(v.i, 1);
    1.66 +
    1.67 +  v.j = (*env)->GetLongField(env, self, id_j);
    1.68 +  CHECK(v.j, 2);
    1.69 +
    1.70 +  v.z = (*env)->GetBooleanField(env, self, id_z);
    1.71 +  CHECK(v.z, JNI_TRUE);
    1.72 +
    1.73 +  v.c = (*env)->GetCharField(env, self, id_c);
    1.74 +  CHECK(v.c, 'a');
    1.75 +
    1.76 +  v.s = (*env)->GetShortField(env, self, id_s);
    1.77 +  CHECK(v.s, 3);
    1.78 +
    1.79 +  v.f = (*env)->GetFloatField(env, self, id_f);
    1.80 +  CHECK(v.f, 1.0f);
    1.81 +
    1.82 +  v.d = (*env)->GetDoubleField(env, self, id_d);
    1.83 +  CHECK(v.d, 2.0);
    1.84 +
    1.85 +#undef CHECK
    1.86 +
    1.87 +  v.l = (*env)->GetObjectField(env, self, id_l);
    1.88 +  if (v.l == NULL) {
    1.89 +    (*env)->ThrowNew(env, exception, "Object field was null");
    1.90 +    return;
    1.91 +  }
    1.92 +  {
    1.93 +    jclass clz = (*env)->GetObjectClass(env, v.l);
    1.94 +    if (!(*env)->IsSameObject(env, clazz, clz)) {
    1.95 +      (*env)->ThrowNew(env, exception, "Bad object class");
    1.96 +    }
    1.97 +  }
    1.98 +
    1.99 +  (*env)->DeleteWeakGlobalRef(env, self);
   1.100 +}
   1.101 +
   1.102 +/*
   1.103 + * Class:     CallWithJNIWeak
   1.104 + * Method:    runTests
   1.105 + * Signature: (LCallWithJNIWeak;)V
   1.106 + */
   1.107 +JNIEXPORT void JNICALL
   1.108 +Java_CallWithJNIWeak_runTests(JNIEnv *env, jclass clazz, jobject this) {
   1.109 +  jweak that = (*env)->NewWeakGlobalRef(env, this);
   1.110 +  {
   1.111 +    jmethodID method = (*env)->GetStaticMethodID(env,
   1.112 +                                                 clazz, "testJNIFieldAccessors", "(LCallWithJNIWeak;)V");
   1.113 +    (*env)->CallStaticVoidMethod(env, clazz, method, that);
   1.114 +    if ((*env)->ExceptionCheck(env)) {
   1.115 +      return;
   1.116 +    }
   1.117 +  }
   1.118 +
   1.119 +  {
   1.120 +    jmethodID method = (*env)->GetMethodID(env, clazz, "weakReceiverTest", "()V");
   1.121 +    (*env)->CallVoidMethod(env, that, method);
   1.122 +    if ((*env)->ExceptionCheck(env)) {
   1.123 +      return;
   1.124 +    }
   1.125 +  }
   1.126 +
   1.127 +  {
   1.128 +    jmethodID method = (*env)->GetMethodID(env, clazz, "synchonizedWeakReceiverTest", "()V");
   1.129 +    (*env)->CallVoidMethod(env, that, method);
   1.130 +    if ((*env)->ExceptionCheck(env)) {
   1.131 +      return;
   1.132 +    }
   1.133 +  }
   1.134 +  (*env)->DeleteWeakGlobalRef(env, that);
   1.135 +}
   1.136 +
   1.137 +/*
   1.138 + * Class:     CallWithJNIWeak
   1.139 + * Method:    weakReceiverTest0
   1.140 + * Signature: ()V
   1.141 + */
   1.142 +JNIEXPORT void JNICALL
   1.143 +Java_CallWithJNIWeak_weakReceiverTest0(JNIEnv *env, jobject obj) {
   1.144 +  (*env)->GetObjectClass(env, obj);
   1.145 +}

mercurial