1 /* 2 * Copyright (c) 2005, 2018, 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 #include <jni.h> 24 #include <stdio.h> 25 26 extern "C" { 27 28 /* A C function that takes a reference to java Object(a circular Linked list) 29 and utilizes this reference to do a java method callback to determine the 30 number of elements in the linked list */ 31 JNIEXPORT jint JNICALL 32 Java_gc_gctests_nativeGC02_nativeGC02_nativeMethod02 33 (JNIEnv *env, jobject obj, jobject linked_list) { 34 jclass cls, clss; 35 jmethodID mid, mid2; 36 jfieldID fid; 37 jobject llist; 38 int elementCount; 39 40 /* Store a reference to the linked list in the C stack */ 41 42 clss = env->GetObjectClass(obj); 43 fid = env->GetFieldID(clss, "cl", "Lnsk/share/gc/CircularLinkedList;"); 44 if (fid == 0) { 45 printf("could not locate field - cl\n"); 46 return -1; 47 } 48 49 llist = env->GetObjectField(obj, fid); 50 51 /* force a GC by invoking a callback where System.gc() is called 52 */ 53 54 cls = env->GetObjectClass(obj); 55 mid = env->GetMethodID(cls, "callbackGC", "()V"); 56 if (mid == 0) { 57 printf("couldnt locate method callbackGC()\n"); 58 return -1; 59 } 60 env->CallVoidMethod(obj,mid); 61 62 /* Now that a GC has been done, invoke the callback 63 that counts the number of elements in the 64 circular linked list 65 */ 66 67 clss = env->GetObjectClass(linked_list); 68 mid2 = env->GetMethodID(clss, "getLength", "(Lnsk/share/gc/CircularLinkedList;)I"); 69 if (mid2 == 0) { 70 printf("couldnt locate method getLength(CircularLinkedList)\n"); 71 return -1; 72 } 73 elementCount = env->CallIntMethod(linked_list, mid2, llist); 74 return elementCount; 75 } 76 77 }