Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

getting list of methods/ fields using JNI ( REPOST)

843829Sep 23 2004 — edited Sep 23 2004
Hi All,
Since I did noy get any response , I am reposting this....
I am trying to get a list of all public methods & fields in a given class. But I am having trouble. Listing below shows the code I am using. In the example I am trying to get all the public methods of java.lang.String .

But the code always prints out the 44 methods of java.lang.Class. . Can anyone help me out please ??
jclass cls = env->FindClass("java/lang/String");
	if (cls == 0) {
		fprintf(stderr, "Can't find hello class\n");
		exit(1);
	}
	else
	{
		jclass jCls = env->GetObjectClass(cls);  // Get the java.lang.Class for String
								
               // Get Method ID of getMethods()
		jmethodID midGetFields = env->GetMethodID(jCls, "getMethods","()[Ljava/lang/reflect/Method;");
		env->ExceptionDescribe();
		jobjectArray jobjArray = (jobjectArray)env->CallObjectMethod(jCls, midGetFields);
		
		jsize len = env->GetArrayLength(jobjArray);
		for(jsize i = 0 ; i < len ; i++)
		{
			jobject _strMethod = env->GetObjectArrayElement(jobjArray , i) ;
			jclass _methodClazz = env->GetObjectClass(_strMethod) ;
			jmethodID mid = env->GetMethodID(_methodClazz , "getName" , "()Ljava/lang/String;") ;
			jstring _name = (jstring) env->CallObjectMethod(_strMethod , mid ) ;
			char buf[128];
			const char *str = env->GetStringUTFChars(_name, 0);
			printf("\n%s", str);
			env->ReleaseStringUTFChars(_name, str);
		}
Cheers,
KHK
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 21 2004
Added on Sep 23 2004
4 comments
2,394 views