-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxLib_GenericData.pde
More file actions
220 lines (191 loc) · 6.24 KB
/
Copy pathxLib_GenericData.pde
File metadata and controls
220 lines (191 loc) · 6.24 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import java.lang.reflect.Field;
// classe de base des données sauvegardable
class GenericData
{
GenericData(String chapter_name)
{
this.chapter_name = chapter_name;
}
String chapter_name = "";
boolean changed = true;
ArrayList<GenericData> chapters = new ArrayList<GenericData>();
void addChapter(GenericData data_chapter)
{
chapters.add(data_chapter);
}
public void setInt(String name, int value) {
try {
Field field = findFieldInHierarchy(this.getClass(), name);
if (field == null)
throw new NoSuchFieldException(name);
field.setAccessible(true); // Allow access to private fields if necessary
if (field.getType() == int.class) { // Check if the field is of type int
field.setInt(this, value); // Set the field value
changed = true; // Mark the object as changed
} else {
println("Field '" + name + "' is not of type int.");
}
}
catch (NoSuchFieldException e) {
String class_name = this.getClass().getSimpleName();
println(class_name + ".'" + name + "' does not exist.");
// println("Field '" + name + "' does not exist.");
}
catch (IllegalAccessException e) {
e.printStackTrace(); // Handle exceptions gracefully
}
}
// Method to load attributes from a JSONObject using reflection
public void LoadJson(JSONObject json) {
if (json == null) return;
// load primitive fields using reflection
Field[] fields = getAllInstanceFields(this.getClass());
for (Field field : fields) {
try {
field.setAccessible(true); // Allow access to private fields if necessary
String name = field.getName();
if (name == "changed" || name =="this$0" ||
name == "chapter_name" || name == "chapters" ||
java.lang.reflect.Modifier.isStatic(field.getModifiers()))
{
continue;
}
if (field.getType() == boolean.class) {
field.set(this, json.getBoolean(name, field.getBoolean(this)));
} else if (field.getType() == int.class) {
field.set(this, json.getInt(name, field.getInt(this)));
} else if (field.getType() == float.class) {
field.set(this, json.getFloat(name, field.getFloat(this)));
} else if (field.getType() == String.class) {
field.set(this, json.getString(name, (String) field.get(this)));
}
}
catch (IllegalAccessException e) {
e.printStackTrace(); // Handle exceptions gracefully
}
}
// Load chapters
for (GenericData chapter : chapters) {
chapter.LoadJson(json.getJSONObject(chapter.chapter_name));
}
changed = true;
}
// Method to convert all attributes to a JSONObject using reflection
public JSONObject SaveJson() {
JSONObject json = new JSONObject();
// save primitive fields using reflection
Field[] fields = getAllInstanceFields(this.getClass());
for (Field field : fields) {
try {
field.setAccessible(true); // Allow access to private fields if necessary
String name = field.getName();
if (name == "changed" || name =="this$0" ||
name == "chapter_name" || name == "chapters" ||
java.lang.reflect.Modifier.isStatic(field.getModifiers()))
{
continue;
}
Object value = field.get(this);
if (value instanceof Boolean) {
json.setBoolean(name, (Boolean) value);
} else if (value instanceof Integer) {
json.setInt(name, (Integer) value);
} else if (value instanceof Float) {
json.setFloat(name, (Float) value);
} else if (value instanceof String) {
json.setString(name, (String) value);
}
// else if (value != null) {
// json.setString(name, value.toString());
// }
}
catch (IllegalAccessException e) {
e.printStackTrace(); // Handle exceptions gracefully
}
}
// Save chapters
for (GenericData chapter : chapters) {
json.setJSONObject(chapter.chapter_name, chapter.SaveJson());
}
return json;
}
void CopyFrom(GenericData src)
{
Field[] fields = getAllInstanceFields(this.getClass());
for (Field field : fields) {
try {
field.setAccessible(true); // Allow access to private fields if necessary
String name = field.getName();
if (name == "changed" || name =="this$0" ||
name == "chapter_name" || name == "chapters" ||
java.lang.reflect.Modifier.isStatic(field.getModifiers()))
{
continue;
}
Object value = field.get(this);
// Copy only primitive types and String
if (value instanceof Boolean) {
field.set(this, field.getBoolean(src));
} else if (value instanceof Integer)
{
field.set(this, field.getInt(src));
} else if (value instanceof Float)
{
field.set(this, field.getFloat(src));
} else if (value instanceof String)
{
field.set(this, (String) field.get(src));
}
// field.set(this, field.get(src));
}
catch (IllegalAccessException e) {
e.printStackTrace(); // Handle exceptions gracefully
}
}
for (GenericData chapter : chapters) {
GenericData src_chapter = null;
for (GenericData c : src.chapters) {
if (c.chapter_name.equals(chapter.chapter_name)) {
src_chapter = c;
break;
}
}
if (src_chapter != null) {
chapter.CopyFrom(src_chapter);
}
}
}
private Field[] getAllInstanceFields(Class<?> cls)
{
ArrayList<Field> all = new ArrayList<Field>();
Class<?> cur = cls;
while (cur != null)
{
Field[] own = cur.getDeclaredFields();
for (Field f : own)
{
all.add(f);
}
cur = cur.getSuperclass();
}
Field[] result = new Field[all.size()];
for (int i = 0; i < all.size(); i++)
result[i] = all.get(i);
return result;
}
private Field findFieldInHierarchy(Class<?> cls, String name)
{
Class<?> cur = cls;
while (cur != null)
{
try {
return cur.getDeclaredField(name);
}
catch (NoSuchFieldException e)
{
cur = cur.getSuperclass();
}
}
return null;
}
}