什么是registerNatives()方法呢? [英] What does the registerNatives() method do?

查看:231
本文介绍了什么是registerNatives()方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,什么是私人静态方法 registerNatives() Object类的吗?

In java, what does the private static method registerNatives() of the Object class do?

推荐答案

其他答案是技术上是正确的,但不能有人没有JNI经验非常有用的。 : - )

The other answers are technically correct, but not very useful for someone with no JNI experience. :-)

通常情况下,为了使JVM找到你的本地函数,它们被命名为一定的方式。例如,对于 java.lang.Object.registerNatives ,对应的C函数被命名为 Java_java_lang_Object_registerNatives 。通过使用 registerNatives (或者说,JNI函数 RegisterNatives ),您可以命名任何你想要你的C函数。

Normally, in order for the JVM to find your native functions, they have to be named a certain way. e.g., for java.lang.Object.registerNatives, the corresponding C function is named Java_java_lang_Object_registerNatives. By using registerNatives (or rather, the JNI function RegisterNatives), you can name your C functions whatever you want.

下面是相关的C code(OpenJDK的距离6):

Here's the associated C code (from OpenJDK 6):

static JNINativeMethod methods[] = {
    {"hashCode",    "()I",                    (void *)&JVM_IHashCode},
    {"wait",        "(J)V",                   (void *)&JVM_MonitorWait},
    {"notify",      "()V",                    (void *)&JVM_MonitorNotify},
    {"notifyAll",   "()V",                    (void *)&JVM_MonitorNotifyAll},
    {"clone",       "()Ljava/lang/Object;",   (void *)&JVM_Clone},
};

JNIEXPORT void JNICALL
Java_java_lang_Object_registerNatives(JNIEnv *env, jclass cls)
{
    (*env)->RegisterNatives(env, cls,
                            methods, sizeof(methods)/sizeof(methods[0]));
}

(注意: Object.getClass 不在列表中,它会仍然 Java_java_lang_Object_getClass )。对于列出的功能,相关的C函数是作为上市在该表中,这是比写一堆转发功能更加便利。

(Notice that Object.getClass is not in the list; it will still be called by the "standard" name of Java_java_lang_Object_getClass.) For the functions listed, the associated C functions are as listed in that table, which is handier than writing a bunch of forwarding functions.

本地注册功能也是有用的,如果你在你的C程序中嵌入Java和希望应用程序本身(如共享库中的反对)内链接的功能,或者正在使用的功能都没有,否则出口的,因为这些通常不会通过标准方法查找机制找到。注册本地函数也可以用来重新绑定到另一个C函数一个本地方法(有用的,如果你的程序支持动态加载和卸载模块,例如)。

Registering native functions is also useful if you are embedding Java in your C program and want to link to functions within the application itself (as opposed to within a shared library), or the functions being used aren't otherwise "exported", since these would not normally be found by the standard method lookup mechanism. Registering native functions can also be used to "rebind" a native method to another C function (useful if your program supports dynamically loading and unloading modules, for example).

我鼓励大家读<一个href=\"http://www.informit.com/store/java-native-interface-programmers-guide-and-specification-9780201325775\">JNI本书,其中谈到了这一点,更。 : - )

I encourage everybody to read the JNI book, which talks about this and much more. :-)

这篇关于什么是registerNatives()方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆