2016-03-05 00:42:26 +01:00
|
|
|
package zutil.parser.binary;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
2016-04-11 17:04:22 +02:00
|
|
|
import java.util.*;
|
2016-03-05 00:42:26 +01:00
|
|
|
|
|
|
|
|
import zutil.converter.Converter;
|
2016-04-11 17:04:22 +02:00
|
|
|
import zutil.parser.binary.BinaryStruct.*;
|
2016-03-05 00:42:26 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A class representing each field in a BinaryStruct.
|
|
|
|
|
*/
|
2016-04-11 17:04:22 +02:00
|
|
|
public class BinaryFieldData {
|
2016-03-05 00:42:26 +01:00
|
|
|
private static final HashMap<Class, List<BinaryFieldData>> cache = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
private int index;
|
|
|
|
|
private int length;
|
2016-04-11 17:04:22 +02:00
|
|
|
private BinaryFieldSerializer serializer;
|
2016-03-05 00:42:26 +01:00
|
|
|
private Field field;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected static List<BinaryFieldData> getStructFieldList(Class<? extends BinaryStruct> clazz){
|
|
|
|
|
if (!cache.containsKey(clazz)) {
|
2016-04-11 17:04:22 +02:00
|
|
|
try {
|
|
|
|
|
ArrayList<BinaryFieldData> list = new ArrayList<>();
|
|
|
|
|
for (Field field : clazz.getDeclaredFields()) {
|
|
|
|
|
if (field.isAnnotationPresent(BinaryField.class) ||
|
|
|
|
|
field.isAnnotationPresent(CustomBinaryField.class))
|
|
|
|
|
|
|
|
|
|
list.add(new BinaryFieldData(field));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
Collections.sort(list, new Comparator<BinaryFieldData>(){
|
|
|
|
|
@Override
|
|
|
|
|
public int compare(BinaryFieldData o1, BinaryFieldData o2) {
|
|
|
|
|
return o1.index - o2.index;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
cache.put(clazz, list);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e);
|
2016-03-05 00:42:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return cache.get(clazz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-04-11 17:04:22 +02:00
|
|
|
private BinaryFieldData(Field f) throws IllegalAccessException, InstantiationException {
|
2016-03-05 00:42:26 +01:00
|
|
|
field = f;
|
2016-04-11 17:04:22 +02:00
|
|
|
if (field.isAnnotationPresent(CustomBinaryField.class)){
|
|
|
|
|
CustomBinaryField fieldData = field.getAnnotation(CustomBinaryField.class);
|
|
|
|
|
index = fieldData.index();
|
|
|
|
|
serializer = (BinaryFieldSerializer) fieldData.serializer().newInstance();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
BinaryField fieldData = field.getAnnotation(BinaryField.class);
|
|
|
|
|
index = fieldData.index();
|
|
|
|
|
length = fieldData.length();
|
|
|
|
|
}
|
2016-03-05 00:42:26 +01:00
|
|
|
}
|
|
|
|
|
|
2016-04-14 13:15:49 +02:00
|
|
|
protected void setByteValue(Object obj, byte[] data){
|
2016-03-05 00:42:26 +01:00
|
|
|
try {
|
|
|
|
|
field.setAccessible(true);
|
|
|
|
|
if (field.getType() == Boolean.class || field.getType() == boolean.class)
|
|
|
|
|
field.set(obj, data[0] != 0);
|
|
|
|
|
else if (field.getType() == Integer.class || field.getType() == int.class)
|
|
|
|
|
field.set(obj, Converter.toInt(data));
|
|
|
|
|
else if (field.getType() == String.class)
|
|
|
|
|
field.set(obj, new String(data));
|
2016-04-11 17:04:22 +02:00
|
|
|
else
|
|
|
|
|
throw new UnsupportedOperationException("Unsupported BinaryStruct field class: "+ field.getClass());
|
2016-03-05 00:42:26 +01:00
|
|
|
} catch (IllegalAccessException e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-14 13:15:49 +02:00
|
|
|
protected void setValue(Object obj, Object value){
|
|
|
|
|
try {
|
|
|
|
|
field.setAccessible(true);
|
|
|
|
|
field.set(obj, value);
|
|
|
|
|
} catch (IllegalAccessException e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-05 00:42:26 +01:00
|
|
|
|
2016-04-14 13:15:49 +02:00
|
|
|
protected byte[] getByteValue(Object obj){
|
2016-03-05 00:42:26 +01:00
|
|
|
try {
|
2016-03-15 13:55:20 +01:00
|
|
|
field.setAccessible(true);
|
2016-03-05 00:42:26 +01:00
|
|
|
if (field.getType() == Boolean.class || field.getType() == boolean.class)
|
|
|
|
|
return new byte[]{ (byte)(field.getBoolean(obj) ? 0x01 : 0x00) };
|
|
|
|
|
else if (field.getType() == Integer.class || field.getType() == int.class)
|
|
|
|
|
return Converter.toBytes(field.getInt(obj));
|
|
|
|
|
else if (field.getType() == String.class)
|
|
|
|
|
return ((String)(field.get(obj))).getBytes();
|
2016-03-15 13:55:20 +01:00
|
|
|
else
|
|
|
|
|
throw new UnsupportedOperationException("Unsupported BinaryStruct field class: "+ field.getClass());
|
2016-03-05 00:42:26 +01:00
|
|
|
} catch (IllegalAccessException e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-04-14 13:15:49 +02:00
|
|
|
protected Object getValue(Object obj){
|
|
|
|
|
try {
|
|
|
|
|
field.setAccessible(true);
|
|
|
|
|
return field.get(obj);
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-03-05 00:42:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public int getBitLength(){
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 13:15:49 +02:00
|
|
|
public BinaryFieldSerializer getSerializer(){
|
|
|
|
|
return serializer;
|
|
|
|
|
}
|
2016-03-05 00:42:26 +01:00
|
|
|
}
|