JNI's internal class problems

Source: Internet
Author: User

I am not familiar with Java when I first came into contact with JNI. I have encountered some problems due to the android project.

The Java code is as follows:

Class {

Static {

System. loadlibrary ("");

}

Int A = 1;

Class B {

Int B = 2;

Public void show (){

System. Out. println ("B =" + B );

}

};

Public native B getb ();

Public void show (){

System. Out. println ("A =" + );

Getb (). Show ();

}

};

As shown in the above Code, a has an internal class B. If you want to return the instance of B in the JNI layer, first use javap-s-p A. B to view the signature.

A $ B ();
Signature: (La;) V

The constructor B has a parameter Class.

The C code is as follows:

Jniexport jobject jnicall java_a_getb (jnienv * ENV, jobject thiz)
{
Jobject B = NULL;
Jclass bcls = (* env)-> findclass (ENV, "A $ B ");
Jmethodid mid = (* env)-> getmethodid (ENV, bcls, "<init>", "(La;) V ");
B = (* env)-> newobject (ENV, bcls, mid, thiz );
Return B;
}

Note the red code above. Because B is the internal class of A, the findclass is a $ B.

Then the construction method of B. Because it is the construction method, the third parameter of getmethodid is fixed as "<init>" and the fourth parameter, which is "() for general classes "() V ",

Because B is an internal class of A, you can see through the javap above that it should be "(La;) V"

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.