解决JNA 高版本使用结构体时出现 java.lang.Error: Structure.getFieldOrder()

方法一:

在结构体的类上@Structure.FieldOrder注解,将结构体的每个字段都填入注解中

 @Structure.FieldOrder({"wMainEventType","wSubEventType","byRes"})
 public class NET_DVR_EVENT_LINKAGE_INFO extends Structure {
        public short wMainEventType;                   
        public short wSubEventType;                     
        public byte[] byRes = new byte[28];
 }

方法二:

构建一个结构体公共类,继承jna里面的Structure类,并实现getFieldOrder该方法,自定义结构体的字段获取方法

public abstract class FieldStructure extends Structure implements Serializable {

    @Override
    protected List getFieldOrder() {
        List fieldOrderList = new ArrayList<>();
        for (Class cls = getClass();
             !cls.equals(FieldStructure.class);
             cls = cls.getSuperclass()) {
            Field[] fields = cls.getDeclaredFields();
            int modifiers;
            for (Field field : fields) {
                modifiers = field.getModifiers();
                if (Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) {
                    continue;
                }
                fieldOrderList.add(field.getName());
            }
        }
        return fieldOrderList;
    }
}

你可能感兴趣的:(JNA,java)