1 /*
   2  * Copyright (c) 2016, 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 // Private interface methods call test
  27 JNIEXPORT jint JNICALL
  28 Java_PrivateInterfaceMethods_callIntVoid(JNIEnv *env, jclass unused, jobject impl, jstring defining_class_name, 
  29                                          jstring method_name, jboolean virtual) {
  30 
  31     // Lookup int method_name() in defining_class_name, and if it exists call impl.method_name()  
  32     // using a virtual or non-virtual invocation as indicated
  33 
  34     jmethodID m_id = NULL;
  35     jclass clazz = NULL;
  36     const char* name = NULL;   
  37 
  38     name = (*env)->GetStringUTFChars(env, defining_class_name, NULL);
  39     if (name == NULL) return -1;
  40     clazz = (*env)->FindClass(env, name);
  41     (*env)->ReleaseStringUTFChars(env, defining_class_name, name);
  42     if ((*env)->ExceptionCheck(env)) return -1;
  43 
  44     name = (*env)->GetStringUTFChars(env, method_name, NULL);
  45     if (name == NULL) return -1;
  46     m_id = (*env)->GetMethodID(env, clazz, name, "()I");
  47     (*env)->ReleaseStringUTFChars(env, method_name, name);
  48     if ((*env)->ExceptionCheck(env)) return -1;
  49 
  50     if (!virtual)
  51         return (*env)->CallNonvirtualIntMethod(env, impl, clazz, m_id);
  52     else 
  53         return (*env)->CallIntMethod(env, impl, m_id);
  54 }