Re: JNI and Reflection
I figured it out:
#include "ReflectJNIExample.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_ReflectJNIExample_callOut(
JNIEnv *env,
jobject obj)
{
jobject theObject;
jstring theString;
jclass theClass, classClass;
jmethodID mid;
jarray constructors;
jsize theSize, i;
char *theCString;
theClass = (*env)->FindClass(env,"java/lang/Long");
classClass = (*env)->GetObjectClass(env,theClass);
mid = (*env)->GetMethodID(env,classClass,"getConstructors",
"()[Ljava/lang/reflect/Constructor;");
constructors = (jarray) (*env)->CallObjectMethod(env,theClass,mid);
theClass = (*env)->FindClass(env,"java/lang/reflect/
Constructor");
mid = (*env)->GetMethodID(env,theClass,"toString",
"()Ljava/lang/String;");
theSize = (*env)->GetArrayLength(env,constructors);
for (i = 0; i < theSize; i++)
{
theObject = (*env)->GetObjectArrayElement(env,constructors,i);
theString = (jstring) (*env)-
CallObjectMethod(env,theObject,mid);
theCString = (*env)->GetStringUTFChars(env,theString,NULL);
printf("%s\n",theCString);
(*env)->ReleaseStringUTFChars(env,theString,theCString);
}
}
On Jan 12, 6:09 pm, CLIPSLurker <gdronline2...@swbell.net> wrote:
I'm trying to figure out how to get a list of constructors for a Java
class from a native library using reflection. I first wrote a program
just in Java (ReflectExample.java) to see if I could use reflection:
import java.lang.reflect.Constructor;
class ReflectExample
{
public static void main(String args[])
{
try
{
Class theClass = Class.forName("java.lang.Long")=
;
Constructor constructorList[] =
theClass.getConstructors();
for (int i = 0; i < constructorList.length; i++)=
{
System.out.println("theConstructor[" + i + =
"] = " +
constructorList[i]);
}
}
catch (Exception e)
{ System.out.println("Exception: " + e); }
}
}
When I run the program, I get the output I expected:
theConstructor[0] = public java.lang.Long(java.lang.String) throw=
s
java.lang.NumberFormatException
theConstructor[1] = public java.lang.Long(long)
I then wrote another program (ReflectJNIExample.java) which calls out
to a native library:
public class ReflectJNIExample
{
static { System.loadLibrary("ReflectJNI"); }
public ReflectJNIExample()
{
super();
}
private native void callOut();
public static void main(String args[])
{
ReflectJNIExample me;
me = new ReflectJNIExample();
me.callOut();
}
}
The native library code (ReflectJNIExample.c) is:
#include "ReflectJNIExample.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_ReflectJNIExample_callOut(
JNIEnv *env,
jobject obj)
{
jclass theClass;
jmethodID mid;
theClass = (*env)->FindClass(env,"java/lang/reflect/
Constructor");
printf("java/lang/reflect/Constructor = %p\n",theClass);
theClass = (*env)->FindClass(env,"java/lang/Long");
printf("java/lang/Long = %p\n",theClass);
if (theClass == NULL)
{ return; }
mid = (*env)->GetMethodID(env,theClass,"<init>","(J)V");
printf("Long init mid = %ld\n",(long) mid);
mid = (*env)->GetMethodID(env,theClass,"toString","()Ljava/l=
ang/
String;");
printf("Long toString mid = %ld\n",(long) mid);
mid = (*env)->GetMethodID(env,theClass,"getConstructors","()=
[Ljava/lang/reflect/Constructor;");
printf("Long getConstructors mid = %ld\n",(long) mid);
}
The jni header file (ReflectJNIExample.h) used with the native library
is:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class ReflectJNIExample */
#ifndef _Included_ReflectJNIExample
#define _Included_ReflectJNIExample
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: ReflectJNIExample
* Method: callOut
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_ReflectJNIExample_callOut
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
When I run the program, I get the following output:
java/lang/reflect/Constructor = 0x1008750
java/lang/Long = 0x1008754
Long init mid = 16827995
Long toString mid = 16827988
Long getConstructors mid = 0
Exception in thread "main" java.lang.NoSuchMethodError:
getConstructors
at ReflectJNIExample.callOut(Native Method)
at ReflectJNIExample.main(ReflectJNIExample.java:18)
So it's finding the classes and methods queried with the exception of
the getConstructors method. I've verified that the method signature is
correct. The documentation for GetMethodID indicates that it will
search superclasses to find a method and the method sought isn't a
static method so GetMethodID should be used rather than
GetStaticMethodID. I've searched for documentation and examples
illustrating how to use reflection from native libraries but haven't
found anything useful.
Any help on how to accomplish this task would be greatly appreciated.