什么是本机对象?

发布于 2024-10-09 16:35:53 字数 38 浏览 0 评论 0 原文

什么是本机对象意味着我发现java具有与本机对象接口的对等类?

what is a native object means i found the that java has peer class to interface with native objets?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淤浪 2024-10-16 16:35:53

Java 程序可以使用 JNI 访问本机代码中实现的函数(任何编译为机器代码的东西)。与面向对象的本机代码接口需要一个 java 类,该类使用 jni 将方法调用从 java 转发到本机类的实例。该类是本机类的 java 对等体。

一个例子:
我们有 C++ 中的 print_hello 类,我们需要在 java 程序中使用它,为此我们需要在 java 中定义它的对等体。

本机类

  class print_hello{
  public:
      void do_stuff(){std::cout<<"hello"<<std::endl;}
  } 

java 中的对等类

  class PrintHello{
    //Address of the native instance (the native object)
    long pointer;

    //ctor. calls native method to create
    //instance of print_hello
    PrintHello(){pointer = newNative();}

    ////////////////////////////
    //This whole class is for the following method
    //which provides access to the functionality 
    //of the native class
    public void doStuff(){do_stuff(pointer);}

    //Calls a jni wrapper for print_hello.do_stuff()
    //has to pass the address of the instance.
    //the native keyword keeps the compiler from 
    //complaining about the missing method body
    private native void do_stuff(long p);

    //
    //Methods for management of native resources.
    //

    //Native instance creation/destruction
    private native long newNative();
    private native deleteNative(long p);

    //Method for manual disposal of native resources
    public void dispose(){deleteNative(pointer);pointer = 0;}
  }

JNI 代码(不完整)

所有声明为 native 的方法都需要本机 jni 实现。以下仅实现上面声明的本机方法之一。

//the method name is generated by the javah tool
//and is required for jni to identify it.
void JNIEXPORT Java_PrintHello_do_stuff(JNIEnv* e,jobject i, jlong pointer){
    print_hello* printer = (print_hello*)pointer;
    printer->do_stuff();
} 

Java programs can use JNI to access to functions implemented in native code (anything compiled to machine code). Interfacing with object oriented native code requires a java class which forwards method calls from java to an instance of the native class using jni. This class is the java peer of the native class.

An example:
We have the print_hello class in c++ which we need to use in a java program, to do this we need to define its peer in java.

The native class

  class print_hello{
  public:
      void do_stuff(){std::cout<<"hello"<<std::endl;}
  } 

The peer class in java

  class PrintHello{
    //Address of the native instance (the native object)
    long pointer;

    //ctor. calls native method to create
    //instance of print_hello
    PrintHello(){pointer = newNative();}

    ////////////////////////////
    //This whole class is for the following method
    //which provides access to the functionality 
    //of the native class
    public void doStuff(){do_stuff(pointer);}

    //Calls a jni wrapper for print_hello.do_stuff()
    //has to pass the address of the instance.
    //the native keyword keeps the compiler from 
    //complaining about the missing method body
    private native void do_stuff(long p);

    //
    //Methods for management of native resources.
    //

    //Native instance creation/destruction
    private native long newNative();
    private native deleteNative(long p);

    //Method for manual disposal of native resources
    public void dispose(){deleteNative(pointer);pointer = 0;}
  }

JNI code (incomplete)

All methods declared native require a native jni implementation. The following implements only one of the native methods declared above.

//the method name is generated by the javah tool
//and is required for jni to identify it.
void JNIEXPORT Java_PrintHello_do_stuff(JNIEnv* e,jobject i, jlong pointer){
    print_hello* printer = (print_hello*)pointer;
    printer->do_stuff();
} 
或十年 2024-10-16 16:35:53

如果 Java 对象具有一些用 C 编写的本机方法,则该对象具有对等/本机对象。

A Java object has a peer/native object if it has some native methods written in C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文