I try to get from a C++ program, with JNI, the list of fields of a
class, the goal being to get some reflection information on the C++
side, with this program:
int main( int argC, const char ** argV )
{
JavaVM *jvm;
JNIEnv *pEnv;
JavaVMInitArgs vm_args;
vm_args.version = JNI_VERSION_1_2;
vm_args.nOptions = 0;
vm_args.ignoreUnrecognized = true;
jint res = JNI_CreateJavaVM(&jvm, (void **)&pEnv, &vm_args);
jclass tstClass = pEnv->FindClass("MyClass");
jmethodID initId = pEnv->GetMethodID(tstClass,"<init>","()V");
jobject tstObjct = pEnv->NewObject(tstClass, initId );
jclass jlClss = pEnv->FindClass("java/lang/Class");
jmethodID getFlds = pEnv->GetMethodID(jlClss, "getFields", "()
[Ljava/lang/reflect/Field;");
/// 'Repeated allocation' starts from here...
jobjectArray fields = (jobjectArray)pEnv-
>CallObjectMethod(tstObjct, getFlds);
...
jvm->DestroyJavaVM();
};
... and this class :
class MyClass {
public String Nam = null;
public int Num = 0;
public MyClass() {
this("Alex", 123);
}
}
... and I get this output after GetMethodId("getFields"):
GC Warning: Repeated allocation of very large block (appr. size
512000):
May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size
512000):
May lead to memory leak and poor performance.
... etc ... (Endless)
I checked all returned values of JNI functions, they are fine.
gcj 4.2.2
gcc 4.2.2
Any idea, please ? Thanks.
Gordon Beaton - 09 Jan 2008 08:14 GMT
> /// 'Repeated allocation' starts from here...
> jobjectArray fields = (jobjectArray)pEnv->CallObjectMethod(tstObjct, getFlds);
Your tstObjct is a MyClass, not a java/lang/Class, so it doesn't have
the getFields method you're attempting to invoke here. Specify
tstClass instead.
/gordon
--
remi.chateauneu@gmail.com - 09 Jan 2008 08:46 GMT
> > /// 'Repeated allocation' starts from here...
> > jobjectArray fields = (jobjectArray)pEnv->CallObjectMethod(tstObjct, getFlds);
[quoted text clipped - 6 lines]
>
> --
Many thanks, well done - Wonder why it is not detected at runtime...
Gordon Beaton - 09 Jan 2008 09:17 GMT
> Many thanks, well done - Wonder why it is not detected at runtime...
Like most C libraries, JNI functions do not check for programming
errors!
Also, both tstObjct and tstClass are valid first arguments to
CallObjectMethod(), since they're both object references.
/gordon
--
Lew - 09 Jan 2008 14:07 GMT
>> Many thanks, well done - Wonder why it is not detected at runtime...
>
> Like most C libraries, JNI functions do not check for programming
> errors!
And because Java does check for many common programming errors, some
programmers whine that it's too "restrictive".
So we complain when Java forces us to be careful, and more when it doesn't.

Signature
Lew