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 
  24 #include <jni.h>
  25 #include <stdio.h>
  26 
  27 extern "C" {
  28 
  29 /*
  30  * A C function that takes a reference to java Object( a circular Linked list)
  31  * and utilizes this reference to do a java method callback to determine the
  32  * number of elements in the linked list
  33  */
  34 JNIEXPORT jint JNICALL
  35 Java_gc_gctests_nativeGC01_nativeGC01_nativeMethod01
  36 (JNIEnv *env, jobject obj, jobject linked_list) {
  37         jclass cls, clss;
  38         jmethodID mid, mid2;
  39         int elementCount;
  40 
  41         /* Before doing anything force a GC by
  42            invoking a callback where System.gc() is called
  43            */
  44         cls  = env->GetObjectClass(obj);
  45         mid = env->GetMethodID(cls, "callbackGC", "()V");
  46         if (mid == 0) {
  47                 printf("couldnt locate method callbackGC()");
  48                 return -1;
  49         }
  50         env->CallVoidMethod(obj,mid);
  51 
  52         /* Now that a GC has been done, invoke the callback
  53            that counts the number of elements in the
  54            circular linked list
  55            */
  56 
  57         clss = env->GetObjectClass(linked_list);
  58         mid2 = env->GetMethodID(clss, "getLength", "()I" );
  59         if (mid2  == 0) {
  60                 printf("couldnt locate method getLength()");
  61                 return -1;
  62         }
  63         elementCount  = env->CallIntMethod(linked_list, mid2);
  64         return elementCount;
  65 }
  66 
  67 }