-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.java
More file actions
176 lines (162 loc) · 6.14 KB
/
Copy pathTable.java
File metadata and controls
176 lines (162 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package model;
import util.ArrayUtils;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public abstract class Table extends Unsigned {
/**
* 实现字节数组bytes的new动作
* @return
*/
protected void newBytes() {
List<byte[]> list_bytes = getListByteOfDeclaredFields();
this.bytes = ArrayUtils.newarray(list_bytes);
}
/**
* 枚举this对象的全部字段(不包括父类的 且 字段的类继承Unsigned类),将bytes数组合成一个list返回
* @return
*/
protected List<byte[]> getListByteOfDeclaredFields() {
List<byte[]> list_bytes = new ArrayList<>();
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
Object obj = field.get(this);
list_bytes.addAll(getListByteOfDeclaredObject(obj));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return list_bytes;
}
/**
* 对象的类需要继承Unsigned类<br>
* 若对象非数组类型,直接返回bytes数组转化的list<br>
* 若对象是数组类型,递归枚举每个元素,合成所有bytes数组转化的list
* @param obj
* @return
*/
protected List<byte[]> getListByteOfDeclaredObject(Object obj) {
List<byte[]> list_bytes = new ArrayList<>();
if (obj != null) {
if (obj.getClass().isArray()) {
for (int i = 0; i < Array.getLength(obj); i ++) {
list_bytes.addAll(getListByteOfDeclaredObject(Array.get(obj, i)));
}
} else if (Unsigned.class.isInstance(obj))
list_bytes.add(((Unsigned) obj).bytes);
}
return list_bytes;
}
/**
* 对其他数据进行填充,派生类一般会重写这个方法
* @param constant_infos
* @return
* @throws Exception
*/
public Table fill(Constant_Info[] constant_infos) {
if (constant_infos == null)
throw new NullPointerException("infos is null");
return this;
}
/**
* 对其他数据进行填充之前,先进行异常判断,派生类一般会调用这个方法
* @param index
* @param constant_infos
* @param clazz
* @throws Exception
*/
protected static void fillForException(U2 index, Constant_Info[] constant_infos, Class clazz) {
if (index == null)
throw new NullPointerException("index is null");
if (index.getValue() < 1 || index.getValue() >= constant_infos.length)
throw new RuntimeException("index " + index.getValue() + " not in range [1, " + constant_infos.length + "]");
if (!clazz.isInstance(constant_infos[index.getValue()]))
throw new RuntimeException("not instanceof " + clazz.getName());
}
private static String names(Class[] clazzs) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < clazzs.length; i ++) {
if (i != 0) sb.append(", ");
sb.append(clazzs[i].getName());
}
sb.append("]");
return sb.toString();
}
protected static void fillForException(U2 index, Constant_Info[] constant_infos, Class[] clazzs) {
if (index == null)
throw new NullPointerException("index is null");
if (index.getValue() < 1 || index.getValue() >= constant_infos.length)
throw new RuntimeException("index " + index.getValue() + " not in range [1, " + constant_infos.length + "]");
Class clazz0 = null;
for (Class clazz : clazzs) {
if (clazz.isInstance(constant_infos[index.getValue()])) {
clazz0 = clazz;
break;
}
}
if (clazz0 == null)
throw new RuntimeException("not instanceof " + names(clazzs));
}
/**
* 按照特有的格式返回<br>
* 注意:派生类调用toString()方法时,会自动调用该方法,此时this指向的是派生类的对象,非本类的对象
* @return
*/
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(toStringClass());
sb.append(toStringDeclaredFields());
return sb.toString();
}
/**
* 返回类名 及 bytes数组的十六进制字符串
* @return
*/
protected String toStringClass() {
StringBuilder sb = new StringBuilder();
sb.append(this.getClass().getSimpleName());
sb.append("(").append(this.parseBytesToHexString()).append(")");
return sb.toString();
}
/**
* 枚举this对象的全部字段(不包括父类的),以特殊格式返回
* @return
*/
protected String toStringDeclaredFields() {
StringBuilder sb = new StringBuilder();
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
sb.append("\n").append(field.getName()).append(": [");
sb.append(toStringDeclaredObject(field.get(this))).append("]");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/**
* 若对象非数组类型,以name: [value1, value2]的格式返回<br>
* 若对象是数组类型,递归枚举每个元素,直到不是数组类型时,以name: [[value1], [value2]]的格式返回
* @param obj
* @return
*/
private String toStringDeclaredObject(Object obj) {
StringBuilder sb = new StringBuilder();
if (obj != null) {
if (obj.getClass().isArray()) {
for (int i = 0; i < Array.getLength(obj); i ++) {
if (i != 0) sb.append(", ");
sb.append("[").append(toStringDeclaredObject(Array.get(obj, i))).append("]");
}
} else
sb.append(obj.toString().replaceAll("\n", ", "));
}
return sb.toString();
}
}