Home > Archive > Java Help > January 2008 > JNI and Reflection
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
JNI and Reflection
|
|
| CLIPSLurker 2008-01-12, 7:19 pm |
| 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) throws
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/lang/
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.
| |
| CLIPSLurker 2008-01-12, 10:29 pm |
| 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 =3D (*env)->FindClass(env,"java/lang/Long");
classClass =3D (*env)->GetObjectClass(env,theClass);
mid =3D (*env)->GetMethodID(env,classClass,"getConstructors",
"()[Ljava/lang/reflect/Constructor;");
constructors =3D (jarray) (*env)->CallObjectMethod(env,theClass,mid);
theClass =3D (*env)->FindClass(env,"java/lang/reflect/
Constructor");
mid =3D (*env)->GetMethodID(env,theClass,"toString",
"()Ljava/lang/String;");
theSize =3D (*env)->GetArrayLength(env,constructors);
for (i =3D 0; i < theSize; i++)
{
theObject =3D (*env)-> GetObjectArrayElement(env,constructors,i
);
theString =3D (jstring) (*env)-
>CallObjectMethod(env,theObject,mid);
theCString =3D (*env)->GetStringUTFChars(env,theString,NULL);
printf("%s\n",theCString);
(*env)-> ReleaseStringUTFChars(env,theString,theC
String);
}
}
On Jan 12, 6:09=A0pm, 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:
>
> =A0 =A0import java.lang.reflect.Constructor;
>
> =A0 =A0class ReflectExample
> =A0 =A0 =A0{
> =A0 =A0 =A0 public static void main(String args[])
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0try
> =A0 =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 Class theClass =3D Class.forName("java.lang.Long")=
;
>
> =A0 =A0 =A0 =A0 =A0 =A0 Constructor constructorList[] =3D
> theClass.getConstructors();
>
> =A0 =A0 =A0 =A0 =A0 =A0 for (int i =3D 0; i < constructorList.length; i++)=
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0System.out.println("theConstructor[" + i + =
"] =3D " +
> constructorList[i]);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0 catch (Exception e)
> =A0 =A0 =A0 =A0 =A0 =A0{ System.out.println("Exception: " + e); }
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0}
>
> When I run the program, I get the output I expected:
>
> =A0 =A0theConstructor[0] =3D public java.lang.Long(java.lang.String) throw=
s
> java.lang.NumberFormatException
> =A0 =A0theConstructor[1] =3D public java.lang.Long(long)
>
> I then wrote another program (ReflectJNIExample.java) which calls out
> to a native library:
>
> =A0 =A0public class ReflectJNIExample
> =A0 =A0 =A0{
> =A0 =A0 =A0 static { System.loadLibrary("ReflectJNI"); }
>
> =A0 =A0 =A0 public ReflectJNIExample()
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0super();
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 private native void callOut();
>
> =A0 =A0 =A0 public static void main(String args[])
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0ReflectJNIExample me;
>
> =A0 =A0 =A0 =A0 =A0me =3D new ReflectJNIExample();
>
> =A0 =A0 =A0 =A0 =A0me.callOut();
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0}
>
> The native library code (ReflectJNIExample.c) is:
>
> =A0 =A0#include "ReflectJNIExample.h"
> =A0 =A0#include <stdio.h>
>
> =A0 =A0JNIEXPORT void JNICALL Java_ReflectJNIExample_callOut(
> =A0 =A0 =A0JNIEnv *env,
> =A0 =A0 =A0jobject obj)
> =A0 =A0 =A0{
> =A0 =A0 =A0 jclass theClass;
> =A0 =A0 =A0 jmethodID mid;
>
> =A0 =A0 =A0 theClass =3D (*env)->FindClass(env,"java/lang/reflect/
> Constructor");
> =A0 =A0 =A0 printf("java/lang/reflect/Constructor =3D %p\n",theClass);
>
> =A0 =A0 =A0 theClass =3D (*env)->FindClass(env,"java/lang/Long");
> =A0 =A0 =A0 printf("java/lang/Long =3D %p\n",theClass);
>
> =A0 =A0 =A0 if (theClass =3D=3D NULL)
> =A0 =A0 =A0 =A0 { return; }
>
> =A0 =A0 =A0 mid =3D (*env)->GetMethodID(env,theClass,"<init>","(J)V");
> =A0 =A0 =A0 printf("Long init mid =3D %ld\n",(long) mid);
>
> =A0 =A0 =A0 mid =3D (*env)->GetMethodID(env,theClass,"toString","()Ljava/l=
ang/
> String;");
> =A0 =A0 =A0 printf("Long toString mid =3D %ld\n",(long) mid);
>
> =A0 =A0 =A0 mid =3D (*env)->GetMethodID(env,theClass,"getConstructors","()=
> [Ljava/lang/reflect/Constructor;");
> =A0 =A0 =A0 printf("Long getConstructors mid =3D %ld\n",(long) mid);
> =A0 =A0 =A0}
>
> The jni header file (ReflectJNIExample.h) used with the native library
> is:
>
> =A0 =A0/* DO NOT EDIT THIS FILE - it is machine generated */
> =A0 =A0#include <jni.h>
> =A0 =A0/* Header for class ReflectJNIExample */
>
> =A0 =A0#ifndef _Included_ReflectJNIExample
> =A0 =A0#define _Included_ReflectJNIExample
> =A0 =A0#ifdef __cplusplus
> =A0 =A0extern "C" {
> =A0 =A0#endif
> =A0 =A0/*
> =A0 =A0 * Class: =A0 =A0 ReflectJNIExample
> =A0 =A0 * Method: =A0 =A0callOut
> =A0 =A0 * Signature: ()V
> =A0 =A0 */
> =A0 =A0JNIEXPORT void JNICALL Java_ReflectJNIExample_callOut
> =A0 =A0 =A0(JNIEnv *, jobject);
>
> =A0 =A0#ifdef __cplusplus
> =A0 =A0}
> =A0 =A0#endif
> =A0 =A0#endif
>
> When I run the program, I get the following output:
>
> =A0 =A0java/lang/reflect/Constructor =3D 0x1008750
> =A0 =A0java/lang/Long =3D 0x1008754
> =A0 =A0Long init mid =3D 16827995
> =A0 =A0Long toString mid =3D 16827988
> =A0 =A0Long getConstructors mid =3D 0
> =A0 =A0Exception in thread "main" java.lang.NoSuchMethodError:
> getConstructors
> =A0 =A0 =A0 at ReflectJNIExample.callOut(Native Method)
> =A0 =A0 =A0 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.
| |
| Roedy Green 2008-01-13, 8:19 am |
| On Sat, 12 Jan 2008 16:09:12 -0800 (PST), CLIPSLurker
<gdronline2008@swbell.net> wrote, quoted or indirectly quoted someone
who said :
>
>Any help on how to accomplish this task would be greatly appreciated.
see http://mindprod.com/jgloss/jni.html#REFLECTION
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
| |
| Roedy Green 2008-01-13, 8:19 am |
| On Sat, 12 Jan 2008 18:45:43 -0800 (PST), CLIPSLurker
<gdronline2008@swbell.net> wrote, quoted or indirectly quoted someone
who said :
>I figured it out:
If you learned about a gotcha you could encapsulate into a paragraph,
I would like to include it in the JNI essay to warn others.
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
| |
| CLIPSLurker 2008-01-13, 7:21 pm |
| On Jan 13, 5:46=A0am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Sat, 12 Jan 2008 18:45:43 -0800 (PST), CLIPSLurker
> <gdronline2...@swbell.net> wrote, quoted or indirectly quoted someone
> who said :
>
>
> If you learned about a gotcha you could encapsulate into a paragraph,
> I would like to include it in the JNI essay to warn others.
> --
> Roedy Green, Canadian Mind Products
> The Java Glossary,http://mindprod.com
The relevant fragment from the Java program to be converted is this:
Class theClass =3D Class.forName("java.lang.Long");
Constructor constructorList[] =3D theClass.getConstructors();
In the JNI code, the first line from the Java code translates to this:
jclass theClass =3D (*env)->FindClass(env,"java/lang/Long");
The second line of the Java code does three things that must be
handled in separate steps in the JNI code. First, it takes the object
java.lang.Long and determines the class to which it belongs
(java.lang.Class):
jclass classClass =3D (*env)->GetObjectClass(env,theClass);
Second, it finds the getConstructors method from the java.lang.Class
class:
jmethodID mid =3D (*env)-
>GetMethodID(env,classClass,"getConstructors",
"()[Ljava/lang/reflect/
Constructor;");
Third, it invokes the getConstructors method on the java.lang.Long
object:
jarray constructorList =3D (jarray) (*env)-
>CallObjectMethod(env,theClass,mid);
|
|
|
|
|