test/runtime/jni/CallWithJNIWeak/CallWithJNIWeak.c

Fri, 10 May 2019 18:50:40 +0000

author
phh
date
Fri, 10 May 2019 18:50:40 +0000
changeset 9671
e86bc9786d83
permissions
-rw-r--r--

8223664: Add jtreg tests for 8223528, backport to jdk8u of 8176100
Summary: Add hotspot/test/runtime/jni directory with two tests for 8176100
Reviewed-by: kbarrett, coleenp, tschatzl

     1 /*
     2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    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  */
    24 #include <jni.h>
    26 /*
    27  * Class:     CallWithJNIWeak
    28  * Method:    testJNIFieldAccessors
    29  * Signature: (LCallWithJNIWeak;)V
    30  */
    31 JNIEXPORT void JNICALL
    32 Java_CallWithJNIWeak_testJNIFieldAccessors(JNIEnv *env, jclass clazz, jobject this) {
    33   // Make sure that we have a weak reference to the receiver
    35   jweak self = (*env)->NewWeakGlobalRef(env, this);
    37   jclass this_class = (*env)->GetObjectClass(env, self);
    39   jclass exception = (*env)->FindClass(env, "java/lang/RuntimeException");
    41   jfieldID id_i = (*env)->GetFieldID(env, this_class, "i", "I");
    42   jfieldID id_j = (*env)->GetFieldID(env, this_class, "j", "J");
    43   jfieldID id_z = (*env)->GetFieldID(env, this_class, "z", "Z");
    44   jfieldID id_c = (*env)->GetFieldID(env, this_class, "c", "C");
    45   jfieldID id_s = (*env)->GetFieldID(env, this_class, "s", "S");
    46   jfieldID id_f = (*env)->GetFieldID(env, this_class, "f", "F");
    47   jfieldID id_d = (*env)->GetFieldID(env, this_class, "d", "D");
    48   jfieldID id_l = (*env)->GetFieldID(env, this_class, "l", "Ljava/lang/Object;");
    49   jvalue v;
    51 #define CHECK(variable, expected)                                   \
    52   do {                                                              \
    53     if ((variable) != (expected)) {                                 \
    54       (*env)->ThrowNew(env, exception,  #variable" != " #expected); \
    55       return;                                                       \
    56     }                                                               \
    57   } while(0)
    59   // The values checked below must be kept in sync with the Java source file.
    61   v.i = (*env)->GetIntField(env, self, id_i);
    62   CHECK(v.i, 1);
    64   v.j = (*env)->GetLongField(env, self, id_j);
    65   CHECK(v.j, 2);
    67   v.z = (*env)->GetBooleanField(env, self, id_z);
    68   CHECK(v.z, JNI_TRUE);
    70   v.c = (*env)->GetCharField(env, self, id_c);
    71   CHECK(v.c, 'a');
    73   v.s = (*env)->GetShortField(env, self, id_s);
    74   CHECK(v.s, 3);
    76   v.f = (*env)->GetFloatField(env, self, id_f);
    77   CHECK(v.f, 1.0f);
    79   v.d = (*env)->GetDoubleField(env, self, id_d);
    80   CHECK(v.d, 2.0);
    82 #undef CHECK
    84   v.l = (*env)->GetObjectField(env, self, id_l);
    85   if (v.l == NULL) {
    86     (*env)->ThrowNew(env, exception, "Object field was null");
    87     return;
    88   }
    89   {
    90     jclass clz = (*env)->GetObjectClass(env, v.l);
    91     if (!(*env)->IsSameObject(env, clazz, clz)) {
    92       (*env)->ThrowNew(env, exception, "Bad object class");
    93     }
    94   }
    96   (*env)->DeleteWeakGlobalRef(env, self);
    97 }
    99 /*
   100  * Class:     CallWithJNIWeak
   101  * Method:    runTests
   102  * Signature: (LCallWithJNIWeak;)V
   103  */
   104 JNIEXPORT void JNICALL
   105 Java_CallWithJNIWeak_runTests(JNIEnv *env, jclass clazz, jobject this) {
   106   jweak that = (*env)->NewWeakGlobalRef(env, this);
   107   {
   108     jmethodID method = (*env)->GetStaticMethodID(env,
   109                                                  clazz, "testJNIFieldAccessors", "(LCallWithJNIWeak;)V");
   110     (*env)->CallStaticVoidMethod(env, clazz, method, that);
   111     if ((*env)->ExceptionCheck(env)) {
   112       return;
   113     }
   114   }
   116   {
   117     jmethodID method = (*env)->GetMethodID(env, clazz, "weakReceiverTest", "()V");
   118     (*env)->CallVoidMethod(env, that, method);
   119     if ((*env)->ExceptionCheck(env)) {
   120       return;
   121     }
   122   }
   124   {
   125     jmethodID method = (*env)->GetMethodID(env, clazz, "synchonizedWeakReceiverTest", "()V");
   126     (*env)->CallVoidMethod(env, that, method);
   127     if ((*env)->ExceptionCheck(env)) {
   128       return;
   129     }
   130   }
   131   (*env)->DeleteWeakGlobalRef(env, that);
   132 }
   134 /*
   135  * Class:     CallWithJNIWeak
   136  * Method:    weakReceiverTest0
   137  * Signature: ()V
   138  */
   139 JNIEXPORT void JNICALL
   140 Java_CallWithJNIWeak_weakReceiverTest0(JNIEnv *env, jobject obj) {
   141   (*env)->GetObjectClass(env, obj);
   142 }

mercurial