Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is a native object?

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

like image 653
satheesh.droid Avatar asked Dec 31 '10 08:12

satheesh.droid


People also ask

What is host object and native object?

Native objects are objects that adhere to the specs, i.e. "standard objects". Host objects are objects that the browser (or other runtime environment like Node) provides.

What is not a native object in JavaScript?

Any object which is not native is a host object, basically Host Objects are provided by the browser environment. Ex: window, document, location, history, XMLHttpRequest, setTimeout, getElementsByTagName, querySelectorAll, DOM nodes etc.

Is integer a native object in JavaScript?

JavaScript has several built-in or native objects. These objects are accessible anywhere in your program and will work the same way in any browser running in any operating system. JavaScript Number Object − The Number object represents numerical date, either integers or floating-point numbers.

What is JavaScript native?

The native prototype is a JavaScript property that all built-in constructor functions in JavaScript use to inherit methods and properties from one another.


1 Answers

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();
} 
like image 165
josefx Avatar answered Nov 15 '22 12:11

josefx