java 我可以知道调用 JNI C 方法的类的名称吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12719766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:57:30  来源:igfitidea点击:

Can I know the name of the class that calls a JNI C method?

javacclassjava-native-interface

提问by Y.E.P

Is there any way I can know the name of the class that called a method in JNI C code ? I can obtain a reference to the class using the following statement :

有什么办法可以知道在 JNI C 代码中调用方法的类的名称吗?我可以使用以下语句获取对类的引用:

jclass cls = (*env)->GetObjectClass(env,obj);

But is there any way I can know the name of the class ? .

但是有什么办法可以知道班级的名称吗?.

回答by maba

This code will give you the calling class name:

此代码将为您提供调用类名称:

jclass cls = env->GetObjectClass(obj);

// First get the class object
jmethodID mid = env->GetMethodID(cls, "getClass", "()Ljava/lang/Class;");
jobject clsObj = env->CallObjectMethod(obj, mid);

// Now get the class object's class descriptor
cls = env->GetObjectClass(clsObj);

// Find the getName() method on the class object
mid = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");

// Call the getName() to get a jstring object back
jstring strObj = (jstring)env->CallObjectMethod(clsObj, mid);

// Now get the c string from the java jstring object
const char* str = env->GetStringUTFChars(strObj, NULL);

// Print the class name
printf("\nCalling class is: %s\n", str);

// Release the memory pinned char array
env->ReleaseStringUTFChars(strObj, str);

Note that I haven't taken any actions to check for errors. This is just a small code snippet describing how it could be done.

请注意,我没有采取任何措施来检查错误。这只是一个描述如何完成的小代码片段。



Alternatively you could do this instead of using the GetStringUTFChars/ReleaseStringUTFChars:

或者,您可以这样做而不是使用GetStringUTFChars/ReleaseStringUTFChars

// Make sure that the buffer is large enough
char str[128];
jint strlen = env->GetStringUTFLength(strObj);
env->GetStringUTFRegion(strObj, 0, strlen, str);
printf("\nCalling class is: %s\n", str);

No need to release since the string is copied to local buffer.

由于字符串已复制到本地缓冲区,因此无需释放。

回答by Alexander Haroldo da Rocha

In my case, I haven't had the object to get the class. Instead, I wanted to get the name of a given class, based on its signature.

就我而言,我没有对象来获取课程。相反,我想根据其签名获取给定类的名称。

So, this worked for me. I hope it can help:

所以,这对我有用。我希望它可以帮助:

// Find the class by its JNI signature
jclass cls = env->FindClass(expectedType);

// Get the class object's class descriptor
jclass clsClazz = env->GetObjectClass(cls);

// Find the getSimpleName() method in the class object
jmethodID methodId = env->GetMethodID(clsClazz, "getSimpleName", "()Ljava/lang/String;");
jstring className = (jstring) env->CallObjectMethod(cls, methodId);

// And finally, don't forget to release the JNI objects after usage!!!!
env->DeleteLocalRef(clsClazz);
env->DeleteLocalRef(cls);

回答by user207421

Just invoke getName()on the jclass, also via JNI.

只需调用getName()jclass,也可以通过JNI。