"Go" JNI object processing

Source: Internet
Author: User

Original URL: http://leidiqiu.iteye.com/blog/720307

The basic problem of JNI is to solve the communication problem that the Java and C + + code call each other, the biggest problem in the writing process of C + + code is to adapt to the code writing rules, C + + call or return content must obey the rules of communication between JVM and C + + code.

The general procedure for calling Java in C + + is as follows:

    • Get class:
      • Jclass cls = Env->findclass ("com/ldq/student");
      • The CLS can be considered a handle to a class
      • "Com/ldq/student" is a class file, note that "com.ldq.Student" cannot be used
    • How to obtain:
      • Jmethodid mid = Env->getmethodid (CLS, "<init>", "() V");
      • The above is a constructor, the parameter is "<init>" "() V"
      • Jmethodid mid = Env->getmethodid (CLS, "Getage", "() I");
      • The above is the method of the class, the first argument is the class handle, the second argument is the method name, the third parameter is the signature identity
td>z 

java type

symbol
boolean

Byte

Char

C&NBSP;&N BSP;
short
int
long l  
float f  

Double

d  

Void

Objects Object lfully-qualified-class-name;        L class name;
Arrays array [array-type [Array type  
Methods Method     &nb sp;             (Argument-types) Return-type (parameter type) return type  
    • Get the object:
      • Jobject Obj=env->newobject (Cls,mid);
      • The above gets the handle of an object
    • To get an object member variable:
      • Jfieldid Fid=env->getfieldid (CLS, "Age", "I");
      • The above and the obtained class method are almost
    • Action member Variable:
      • Jint A=env->getintfield (Obj,mid);
      • age=age+10;
      • Env->setintfield (Obj,fid,a);
    • Return:
      • return obj;

Here is an example of my practice

    • Exlist.java

Java code
  1. Package com.ldq.list;
  2. Import java.util.List;
  3. public class Exlist {
  4. /**
  5. * @param args
  6. */
  7. public static void Main (string[] args) {
  8. TODO auto-generated Method Stub
  9. System.out.println ("-------wifimanager.test ()");
  10. System.out.println (Wifimanager.test ());
  11. System.out.println ("-------Wifimanager.testarray ()");
  12. string[] S1 = Wifimanager.testarray ();
  13. for (int i = 0; i < s1.length; i++) {
  14. System.out.println (S1[i]);
  15. }
  16. System.out.println ("-------Wifimanager.testobject ()");
  17. System.out.println (Wifimanager.testobject (). SSID);
  18. System.out.println (Wifimanager.testobject (). Mac);
  19. System.out.println (Wifimanager.testobject (). level);
  20. System.out.println ("-------Wifimanager.getscanresultsa ()");
  21. scanresult[] s2 = Wifimanager.getscanresultsa ();
  22. for (int i = 0; i < s2.length; i++) {
  23. System.out.println (S2[I].SSID);
  24. System.out.println (S2[I].MAC);
  25. System.out.println (S2[i].level);
  26. }
  27. System.out.println ("-------wifimanager.getscanresults ()");
  28. list<scanresult> list = Wifimanager.getscanresults ();
  29. System.out.println (List.get (0). SSID);
  30. System.out.println (List.get (0). Mac);
  31. System.out.println (List.get (0). level);
  32. }
  33. }

    • Scanresult.java

Java code
  1. Package com.ldq.list;
  2. public class Scanresult {
  3. String SSID;
  4. String mac;
  5. int level;
  6. Public Scanresult () {
  7. }
  8. Public Scanresult (String SSID, String Mac, int. level) {
  9. This.ssid = SSID;
  10. This.mac = Mac;
  11. This.level = level;
  12. }
  13. }

    • Wifimanager.java

Java code
  1. Package com.ldq.list;
  2. Import java.util.List;
  3. public class Wifimanager {
  4. static {
  5. System.loadlibrary ("WiFi");
  6. }
  7. Public native static String test ();
  8. Public native static string[] Testarray ();
  9. Public native static Scanresult Testobject ();
  10. Public native static scanresult[] Getscanresultsa ();
  11. Public native static list<scanresult> getscanresults ();
  12. }

    • Wifi.cpp
CPP Code
  1. #include <jni.h>
  2. #include "Com_ldq_list_wifimanager.h"
  3. /*
  4. * Class:com_ldq_list_wifimanager
  5. * Method:test
  6. * Signature: () ljava/lang/string;
  7. */
  8. Jniexport jstring jnicall java_com_ldq_list_wifimanager_test (jnienv *env, Jclass CLS)
  9. {
  10. return Env->newstringutf ("Hello");
  11. }
  12. /*
  13. * Class:com_ldq_list_wifimanager
  14. * Method:testarray
  15. * Signature: () [ljava/lang/string;
  16. */
  17. Jniexport jobjectarray jnicall Java_com_ldq_list_wifimanager_testarray (jnienv *env, Jclass CLS)
  18. {
  19. Jobjectarray ret;
  20. int i;
  21. Char *message[5]= {"First",
  22. "Second",
  23. "Third",
  24. "Fourth",
  25. "Fifth"};
  26. ret= (Jobjectarray) Env->newobjectarray (5,
  27. Env->findclass ("java/lang/string"),
  28. Env->newstringutf (""));
  29. for (i=0;i<5;i++) {
  30. Env->setobjectarrayelement (
  31. Ret,i,env->newstringutf (Message[i]));
  32. }
  33. return (ret);
  34. }
  35. /*
  36. * Class:com_ldq_list_wifimanager
  37. * Method:testobject
  38. * Signature: () Lcom/ldq/list/scanresult;
  39. */
  40. Jniexport jobject jnicall java_com_ldq_list_wifimanager_testobject (jnienv *env, Jclass CLS)
  41. {
  42. Jclass m_cls = Env->findclass ("Com/ldq/list/scanresult");
  43. Jmethodid mid = Env->getmethodid (M_cls, "<init>", "() V");
  44. Jobject obj = Env->newobject (m_cls,mid);
  45. Jfieldid Fid_ssid = Env->getfieldid (M_cls, "SSID", "ljava/lang/string;");
  46. Jfieldid Fid_mac = Env->getfieldid (M_cls, "Mac", "ljava/lang/string;");
  47. Jfieldid fid_level = Env->getfieldid (M_cls, "level", "I");
  48. Env->setobjectfield (Obj,fid_ssid,env->newstringutf ("AP1"));
  49. Env->setobjectfield (Obj,fid_mac,env->newstringutf ("00-11-22-33-44-55"));
  50. Env->setintfield (obj,fid_level,-66);
  51. return obj;
  52. }
  53. /*
  54. * Class:com_ldq_list_wifimanager
  55. * Method:getscanresultsa
  56. * Signature: () [Lcom/ldq/list/scanresult;
  57. */
  58. Jniexport jobjectarray jnicall Java_com_ldq_list_wifimanager_getscanresultsa (jnienv *env, Jclass CLS)
  59. {
  60. Jclass Cls_array=env->findclass ("Java/lang/object");
  61. Jobjectarray Obj_array=env->newobjectarray (2,cls_array,0);
  62. Jclass cls_obj = Env->findclass ("Com/ldq/list/scanresult");
  63. Jmethodid m = Env->getmethodid (Cls_obj, "<init>", "() V");
  64. Jfieldid Fid_ssid = Env->getfieldid (Cls_obj, "SSID", "ljava/lang/string;");
  65. Jfieldid Fid_mac = Env->getfieldid (Cls_obj, "Mac", "ljava/lang/string;");
  66. Jfieldid fid_level = Env->getfieldid (Cls_obj, "level", "I");
  67. for (int i=0;i<2;i++)
  68. {
  69. Jobject Obj=env->newobject (cls_obj,m);
  70. Jobject O1=env->newstringutf ("AP2");
  71. Env->setobjectfield (OBJ,FID_SSID,O1);
  72. Jobject O2=env->newstringutf ("22-22-22-22-22-22");
  73. Env->setobjectfield (OBJ,FID_MAC,O2);
  74. Env->setintfield (obj,fid_level,-66);
  75. Env->setobjectarrayelement (Obj_array,i,obj);
  76. }
  77. return obj_array;
  78. }
  79. /*
  80. * Class:com_ldq_list_wifimanager
  81. * Method:getscanresults
  82. * Signature: () ljava/util/list;
  83. */
  84. Jniexport jobject jnicall java_com_ldq_list_wifimanager_getscanresults (jnienv *env, Jclass CLS)
  85. {
  86. Jclass m_cls_list = Env->findclass ("Java/util/arraylist");
  87. Jmethodid m_mid_list = Env->getmethodid (m_cls_list, "<init>", "() V");
  88. Jobject m_obj_list = Env->newobject (m_cls_list,m_mid_list);
  89. Jmethodid M_mid_add = Env->getmethodid (m_cls_list, "Add", "(Ljava/lang/object;) Z");
  90. Jclass M_cls_result = Env->findclass ("Com/ldq/list/scanresult");
  91. Jmethodid M_mid_result = Env->getmethodid (M_cls_result, "<init>", "() V");
  92. Jobject M_obj_result = Env->newobject (M_cls_result,m_mid_result);
  93. Jfieldid m_fid_1 = Env->getfieldid (M_cls_result, "SSID", "ljava/lang/string;");
  94. Jfieldid m_fid_2 = Env->getfieldid (M_cls_result, "Mac", "ljava/lang/string;");
  95. Jfieldid m_fid_3 = Env->getfieldid (M_cls_result, "level", "I");
  96. Env->setobjectfield (M_obj_result,m_fid_1,env->newstringutf ("AP6"));
  97. Env->setobjectfield (M_obj_result,m_fid_2,env->newstringutf ("66-66-66-66-66-66"));
  98. Env->setintfield (m_obj_result,m_fid_3,-66);
  99. Env->callbooleanmethod (M_obj_list,m_mid_add,m_obj_result);
  100. return m_obj_list;
  101. }

"Go" JNI object processing

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.