Kaynağa Gözat

feat:本地方法对象数据类型(引用类型)的传递和返回
测试JNI的Java代码

yangyi 6 gün önce
ebeveyn
işleme
94c35e0772
1 değiştirilmiş dosya ile 98 ekleme ve 5 silme
  1. 98 5
      src/main/java/space/anyi/jni/parameter/Main.java

+ 98 - 5
src/main/java/space/anyi/jni/parameter/Main.java

@@ -1,15 +1,27 @@
 package space.anyi.jni.parameter;
 
+import java.util.Arrays;
+
 public class Main {
     static {
         System.load("/home/yangyi/JNI-learn/c/parameter/JNI_parameter.so");
+        System.load("/home/yangyi/JNI-learn/c/parameter/JNI_object_parameter.so");
     }
     public static void main(String[] args) {
-        BaseTypeTest baseTypeTest = new BaseTypeTest();
-        int sum = baseTypeTest.sum(1, 2);
-        System.out.println(sum);
-        boolean flag = baseTypeTest.booleanTypeTest(false);
-        System.out.println(flag);
+        //JNI基本数据类型的传递
+//        BaseTypeTest baseTypeTest = new BaseTypeTest();
+//        int sum = baseTypeTest.sum(1, 2);
+//        System.out.println(sum);
+//        boolean flag = baseTypeTest.booleanTypeTest(false);
+//        System.out.println(flag);
+
+        //JNI对象数据类型的传递
+        ObjectTest objectTest = new ObjectTest();
+        Student student = objectTest.getStudent();
+        System.out.println(student);
+        objectTest.printStudentInfo(student);
+        System.out.println(student);
+//        objectTest.free(student);
     }
 }
 class BaseTypeTest {
@@ -18,3 +30,84 @@ class BaseTypeTest {
     //boolean类型的测试
     native boolean booleanTypeTest(boolean flag);
 }
+class ObjectTest{
+    /**
+     * 通过JNI获取对象,返回的是全局的JNI引用,需要手动管理,有内存泄露的风险
+     * @return
+     */
+    native Student getStudent();
+    native void printStudentInfo(Student student);
+
+    /**
+     * 释放JNI创建的全局引用
+     * @param student
+     */
+    native void free(Student student);
+}
+class Student{
+    private String name;
+    private int age;
+    private float height;
+    private int[] source;
+    private String[] hobby;
+
+    public Student(String name) {
+        this.name = name;
+    }
+
+    public int getAge() {
+        return age;
+    }
+
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    public float getHeight() {
+        return height;
+    }
+
+    public void setHeight(float height) {
+        this.height = height;
+    }
+
+    public int[] getSource() {
+        return source;
+    }
+
+    public void setSource(int[] source) {
+        this.source = source;
+    }
+
+    public String[] getHobby() {
+        return hobby;
+    }
+
+    public void setHobby(String[] hobby) {
+        this.hobby = hobby;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public void info(){
+        System.out.println("My name is %s,i am %d year old,height:%f".formatted(this.name,this.age,this.height));
+    }
+
+    @Override
+    public String toString() {
+        return "Student{" +
+                "name='" + name + '\'' +
+                ", age=" + age +
+                ", height=" + height +
+                ", source=" + Arrays.toString(source) +
+                ", hobby=" + Arrays.toString(hobby) +
+                '}';
+    }
+}
+