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 */ 23 24#include <jni.h> 25 26/* 27 * Class: CallWithJNIWeak 28 * Method: testJNIFieldAccessors 29 * Signature: (LCallWithJNIWeak;)V 30 */ 31JNIEXPORT void JNICALL 32Java_CallWithJNIWeak_testJNIFieldAccessors(JNIEnv *env, jclass clazz, jobject this) { 33 // Make sure that we have a weak reference to the receiver 34 35 jweak self = (*env)->NewWeakGlobalRef(env, this); 36 37 jclass this_class = (*env)->GetObjectClass(env, self); 38 39 jclass exception = (*env)->FindClass(env, "java/lang/RuntimeException"); 40 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; 50 51#define CHECK(variable, expected) \ 52 do { \ 53 if ((variable) != (expected)) { \ 54 (*env)->ThrowNew(env, exception, #variable" != " #expected); \ 55 return; \ 56 } \ 57 } while(0) 58 59 // The values checked below must be kept in sync with the Java source file. 60 61 v.i = (*env)->GetIntField(env, self, id_i); 62 CHECK(v.i, 1); 63 64 v.j = (*env)->GetLongField(env, self, id_j); 65 CHECK(v.j, 2); 66 67 v.z = (*env)->GetBooleanField(env, self, id_z); 68 CHECK(v.z, JNI_TRUE); 69 70 v.c = (*env)->GetCharField(env, self, id_c); 71 CHECK(v.c, 'a'); 72 73 v.s = (*env)->GetShortField(env, self, id_s); 74 CHECK(v.s, 3); 75 76 v.f = (*env)->GetFloatField(env, self, id_f); 77 CHECK(v.f, 1.0f); 78 79 v.d = (*env)->GetDoubleField(env, self, id_d); 80 CHECK(v.d, 2.0); 81 82#undef CHECK 83 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 } 95 96 (*env)->DeleteWeakGlobalRef(env, self); 97} 98 99/* 100 * Class: CallWithJNIWeak 101 * Method: runTests 102 * Signature: (LCallWithJNIWeak;)V 103 */ 104JNIEXPORT void JNICALL 105Java_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 } 115 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 } 123 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} 133 134/* 135 * Class: CallWithJNIWeak 136 * Method: weakReceiverTest0 137 * Signature: ()V 138 */ 139JNIEXPORT void JNICALL 140Java_CallWithJNIWeak_weakReceiverTest0(JNIEnv *env, jobject obj) { 141 (*env)->GetObjectClass(env, obj); 142} 143