您的位置:首页 > 编程语言 > Java开发

使用JNI将C结构体转为JAVA实体类

2016-07-29 23:31 525 查看
最近项目中有关于这部分的功能需求,所以在空闲时参考网上的资料,并对其部分功能做了简单封装,整理了一份demo。可实现将C语言中的结构体转换为JAVA类型的实体类。

目前支持基础数据类型int、long、foloat、double、const char *、bool的转换,也支持将数组装换为JAVA中的ArrayList。目前有个难点是将枚举类型直接转换为JAVA中的枚举,这个目前还搞不定,如果有朋友知道怎么搞,欢迎来讨论指导。

demo中JAVA部分的开发工具使用的是eclipse,C部分使用的是VS2013。

这里贴出部分代码,完整的demo点击下面的链接下载,谢谢。

一、JAVA部分:

public class TestJni {

public TestJni() {
System.load("E:\\VS_Workspace\\Win32TestJni\\x64\\Debug\\Win32TestJni.dll");
}

public native void testJni(String string);

/**
* 注册回调方法
*
* @param methodName
*/
public native void registerCallback(String methodName);

/**
* 接收C++传来的信息
*
* @param messageType
* @param object
*/
public void recvMessage(int messageType, Object object) {
if (messageType == 0) {
System.out.println("messageType=" + messageType + ",object=" + (CallItem) object);
}else if (messageType == 1) {
System.out.println("messageType=" + messageType + ",object=" + (CallItemBig) object);
}

}

public static void main(String[] args) {
new TestJni().registerCallback("recvMessage");
}
}


package com.test.jni;

import java.util.List;

public class CallItem {
private int callID;
private String remoteNumber;
private long longValue;
private float floatValue;
private double doubleValue;
private boolean boolValue;
private List stringList;
//private MessageType messageType;// 暂时未想到直接回传枚举的办法,需以int来匹配
// private UnitType unitType;

public CallItem() {
}

public int getCallID() {
return callID;
}

public void setCallID(int callID) {
this.callID = callID;
}

public String getRemoteNumber() {
return remoteNumber;
}

public void setRemoteNumber(String remoteNumber) {
this.remoteNumber = remoteNumber;
}

public long getLongValue() {
return longValue;
}

public float getFloatValue() {
return floatValue;
}

public double getDoubleValue() {
return doubleValue;
}

public boolean isBoolValue() {
return boolValue;
}

public void setBoolValue(boolean boolValue) {
this.boolValue = boolValue;
}

public List getStringList() {
return stringList;
}

public void setLongValue(long longValue) {
this.longValue = longValue;
}

public void setFloatValue(float floatValue) {
this.floatValue = floatValue;
}

public void setDoubleValue(double doubleValue) {
this.doubleValue = doubleValue;
}

public void setStringList(List stringList) {
this.stringList = stringList;
}

@Override
public String toString() {
return "CallItem [callID=" + callID + ", remoteNumber=" + remoteNumber + ", longValue=" + longValue
+ ", floatValue=" + floatValue + ", doubleValue=" + doubleValue + ", boolValue=" + boolValue
+ ", stringList=" + stringList + "]";
}

}


package com.test.jni;

import java.util.ArrayList;

public class CallItemBig {
private ArrayList callItemList;
private CallItem callItem;

public CallItemBig() {
}

public ArrayList getCallItemList() {
return callItemList;
}

public void setCallItemList(ArrayList callItemList) {
this.callItemList = callItemList;
}

public CallItem getCallItem() {
return callItem;
}

public void setCallItem(CallItem callItem) {
this.callItem = callItem;
}

@Override
public String toString() {
return "CallItemBig [callItemList=" + callItemList + ", callItem=" + callItem + "]";
}

}


messageType=1,object=CallItemBig [callItemList=[CallItem [callID=1, remoteNumber=13800005001, longValue=1234123456421312342, floatValue=0.789, doubleValue=23.234234, boolValue=true, stringList=null]], callItem=CallItem [callID=1, remoteNumber=13800005001, longValue=1234123456421312342, floatValue=0.789, doubleValue=23.234234, boolValue=true, stringList=null]]
classname=com/test/jni/TestJni
FindClass:clazz=186214056
GetMethodID:clazz=186214056,methodName=recvMessage,sig=(ILjava/lang/Object;)V
GetMethodID:mid=185008400
classname=com/test/jni/CallItem
FindClass:clazz=186214064
GetMethodID:clazz=186214064,methodName=,sig=()V
GetMethodID:mid=185008408
GetMethodID:clazz=186214064,methodName=setCallID,sig=(I)V
GetMethodID:mid=185008416
GetMethodID:clazz=186214064,methodName=setRemoteNumber,sig=(Ljava/lang/String;)V
GetMethodID:mid=185008424
GetMethodID:clazz=186214064,methodName=setBoolValue,sig=(Z)V
GetMethodID:mid=185008432
GetMethodID:clazz=186214064,methodName=setLongValue,sig=(J)V
GetMethodID:mid=185008440
GetMethodID:clazz=186214064,methodName=setDoubleValue,sig=(D)V
GetMethodID:mid=185008448
GetMethodID:clazz=186214064,methodName=setFloatValue,sig=(F)V
GetMethodID:mid=185008456
classname=java/util/ArrayList
FindClass:clazz=186214088
GetMethodID:clazz=186214088,methodName=,sig=()V
GetMethodID:mid=185008464
classname=java/util/ArrayList
FindClass:clazz=186214104
classname=com/test/jni/CallItemBig
FindClass:clazz=186214112
GetMethodID:clazz=186214112,methodName=,sig=()V
GetMethodID:mid=185008480
GetMethodID:clazz=186214112,methodName=setCallItemList,sig=(Ljava/util/ArrayList;)V
GetMethodID:mid=185008488
GetMethodID:clazz=186214112,methodName=setCallItem,sig=(Lcom/test/jni/CallItem;)V
GetMethodID:mid=185008496


demo下载点:http://download.csdn.net/detail/dongping8887/9590189

转载请注明出处,谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: