Passing and using Java objects

Source: Internet
Author: User

In the previous precedent, we passed a string to the intrinsic method. In fact, you can also pass the Java object you created to the intrinsic method.
Within our intrinsic approach, you can access the fields and methods of those objects that you have received.
To pass the object, the original Java syntax is used when declaring the intrinsic method. As shown in the following example, Myjavaclass has a public (common) field, as well as a publicly method. The Useobjects class declares an intrinsic method for receiving an object of the Myjavaclass class. To investigate whether an intrinsic method can control its own arguments, we set the public field of the argument, call the intrinsic method, and then print out the value of the public field.

Class Myjavaclass {public
  void Divbytwo () {avalue/= 2;}
  public int avalue;
}

public class Useobjects {public
  static void Main (String [] args) {
    useobjects app = new Useobjects ();
    Myjavaclass anobj = new Myjavaclass ();
    Anobj.avalue = 2;
    App.changeobject (anobj);
    System.out.println ("Java:" + anobj.avalue);
  }
  Private native void 
  changeobject (Myjavaclass obj);
  static {
    system.loadlibrary ("Useobjimpl");
  }
}


After compiling the code and passing the. class file to the Javah, you can implement the intrinsic method. In the following example, once the field and method IDs are obtained, they are accessed through the JNI function.

 jniexport void Jnicall java_useobjects_changeobject (jnienv * env, jobject jthis, Jobject obj) {
  Jclass CLS;
  Jfieldid FID;
  Jmethodid mid;
  int value;
  CLS = Env->getobjectclass (obj);
  FID = Env->getfieldid (CLS, "Avalue", "I");
  MID = Env->getmethodid (CLS, "Divbytwo", "() V");
  Value = Env->getintfield (obj, FID);
  printf ("Native:%d\n", value);
  Env->setintfield (obj, FID, 6);
  Env->callvoidmethod (obj, mid);
  Value = Env->getintfield (obj, FID);
printf ("Native:%d\n", value); }

In addition to the first argument, the C + + function receives a jobject that represents the "inherent" side of the Java object Reference-the reference that we pass from the Java code. We simply read the avalue, print it out, change the value, call the object's Divbytwo () method, and then print the value again.
To access a field or method, you must first obtain its identifier. With the appropriate JNI function, the class object, element name and signature information can be easily obtained. These functions return an identifier that can be used to access the corresponding element. Although this approach is somewhat tortuous, our inherent approach does not know anything about the internal layout of Java objects. Therefore, it must access fields and methods through the index returned by the JVM. As a result, different JVMs can implement different internal object layouts without affecting the intrinsic methods.
If you run a Java program, you'll find that the objects coming from the Java side are handled by our inherent methods. But what is the transmission? Is it a pointer, or a Java reference? And what does the garbage collector do during the intrinsic method invocation?
The garbage collector continues to run during the inherent method execution, but during an intrinsic method invocation, our objects are guaranteed not to be collected as "garbage". To ensure this, a "local reference" is created in advance and cleared immediately after the intrinsic method call. Because their lifetimes are closely related to the invocation process, they can guarantee the validity of an object during an intrinsic method invocation.
Because these references are created and corrupted each time a function call is made, you cannot make local copies of the intrinsic methods (local copies) in the static variable. If you want a reference to remain valid for the duration of the function, you need a global reference. Global references are not created by the JVM, but by invoking a specific JNI function, a programmer can extend a local reference to a global reference. When you create a global reference, you are responsible for the "lifetime" of the referenced object. The global reference (and the object it references) stays in memory until explicitly releasing the reference with a specific JNI function. It is similar to C's malloc () and free ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.