Skip to content
This repository was archived by the owner on Feb 16, 2022. It is now read-only.

Commit 864a7b3

Browse files
author
ParkJeongHwan
committed
Added exceptions, package refactoring.
1 parent bfc6a81 commit 864a7b3

File tree

7 files changed

+100
-40
lines changed

7 files changed

+100
-40
lines changed

src/main/java/com/realtimetech/kson/KsonContext.java

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import com.realtimetech.kson.element.KsonArray;
1616
import com.realtimetech.kson.element.KsonObject;
1717
import com.realtimetech.kson.element.KsonValue;
18-
import com.realtimetech.kson.stack.FastStack;
19-
import com.realtimetech.kson.string.StringMaker;
18+
import com.realtimetech.kson.exception.DeserializeException;
19+
import com.realtimetech.kson.exception.SerializeException;
20+
import com.realtimetech.kson.util.stack.FastStack;
21+
import com.realtimetech.kson.util.string.StringMaker;
2022
import com.realtimetech.kson.transform.Transformer;
2123
import com.realtimetech.reflection.access.ArrayAccessor;
2224
import com.realtimetech.reflection.allocate.UnsafeAllocator;
@@ -66,7 +68,6 @@ public KsonContext(int stackSize, int stringBufferSize) {
6668
this.primaryObjects = new HashMap<Class<?>, HashMap<Object, Object>>();
6769

6870
this.cachedFields = new HashMap<Class<?>, Field[]>();
69-
7071

7172
this.registeredTransformers.put(Date.class, new Transformer<Date>() {
7273
@Override
@@ -88,7 +89,7 @@ public Object serialize(KsonContext ksonContext, ArrayList<?> value) {
8889
for (Object object : value) {
8990
try {
9091
ksonArray.add(ksonContext.addFromObjectStack(object));
91-
} catch (IllegalArgumentException | IllegalAccessException | IOException e) {
92+
} catch (SerializeException e) {
9293
e.printStackTrace();
9394
}
9495
}
@@ -121,10 +122,10 @@ public Object serialize(KsonContext ksonContext, HashMap<?, ?> value) {
121122
Object valueObject = value.get(keyObject);
122123

123124
try {
124-
Object tryFromObject = ksonContext.addFromObjectStack(keyObject);
125-
Object tryFromObject2 = ksonContext.addFromObjectStack(valueObject);
126-
ksonObject.put(tryFromObject, tryFromObject2);
127-
} catch (IllegalArgumentException | IllegalAccessException | IOException e) {
125+
Object keyKson = ksonContext.addFromObjectStack(keyObject);
126+
Object valueKson = ksonContext.addFromObjectStack(valueObject);
127+
ksonObject.put(keyKson, valueKson);
128+
} catch (SerializeException e) {
128129
e.printStackTrace();
129130
}
130131
}
@@ -196,14 +197,6 @@ private boolean isNeedSerialize(Class<?> clazz) {
196197
return true;
197198
}
198199

199-
public <T> T toObject(Class<T> clazz, Object object) throws Exception {
200-
if (object instanceof KsonValue) {
201-
return (T) this.addToObjectStack(clazz, (KsonValue) object);
202-
}
203-
204-
return (T) object;
205-
}
206-
207200
public Transformer<?> getTransformer(Class<?> type) {
208201
if (!this.transformers.containsKey(type)) {
209202
boolean matched = false;
@@ -246,11 +239,19 @@ public Field getPrimaryKeyField(Class<?> type) {
246239
return this.primaryKeys.get(type);
247240
}
248241

249-
public Object addToObjectStack(Object object) throws Exception {
242+
public <T> T toObject(Class<T> clazz, Object object) throws DeserializeException {
243+
if (object instanceof KsonValue) {
244+
return (T) this.addToObjectStack(clazz, (KsonValue) object);
245+
}
246+
247+
return (T) object;
248+
}
249+
250+
public Object addToObjectStack(Object object) throws DeserializeException {
250251
return this.addToObjectStack(Object.class, object);
251252
}
252253

253-
public Object addToObjectStack(Class<?> clazz, Object object) throws Exception {
254+
public Object addToObjectStack(Class<?> clazz, Object object) throws DeserializeException {
254255
boolean needLoop = this.objectStack.isEmpty();
255256

256257
Object result = this.createAtToObject(true, clazz, object);
@@ -266,7 +267,11 @@ public Object addToObjectStack(Class<?> clazz, Object object) throws Exception {
266267
KsonObject ksonValue = (KsonObject) targetKson;
267268

268269
for (Field field : this.getAccessibleFields(targetObjectClass)) {
269-
field.set(targetObject, createAtToObject(false, field.getType(), ksonValue.get(field.getName())));
270+
try {
271+
field.set(targetObject, createAtToObject(false, field.getType(), ksonValue.get(field.getName())));
272+
} catch (IllegalArgumentException | IllegalAccessException e) {
273+
throw new DeserializeException("Deserialize failed because can't access the field.");
274+
}
270275
}
271276
} else {
272277
KsonArray ksonValue = (KsonArray) targetKson;
@@ -291,14 +296,18 @@ public Object addToObjectStack(Class<?> clazz, Object object) throws Exception {
291296
return result;
292297
}
293298

294-
private Object createAtToObject(boolean first, Class<?> type, Object originalValue) throws Exception {
299+
private Object createAtToObject(boolean first, Class<?> type, Object originalValue) throws DeserializeException {
295300
Object primaryId = null;
296301

297302
if (originalValue instanceof KsonObject) {
298303
KsonObject wrappingObject = (KsonObject) originalValue;
299304

300305
if (wrappingObject.containsKey("#class")) {
301-
type = Class.forName(wrappingObject.get("#class").toString());
306+
try {
307+
type = Class.forName(wrappingObject.get("#class").toString());
308+
} catch (ClassNotFoundException e) {
309+
throw new DeserializeException("Deserialize failed because can't find target class.");
310+
}
302311
originalValue = wrappingObject.get("#data");
303312
} else if (wrappingObject.containsKey("@id")) {
304313
primaryId = wrappingObject.get("@id");
@@ -336,7 +345,11 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal
336345
convertedValue = Array.newInstance(componentType, ksonArray.size());
337346
} else if (convertedValue instanceof KsonObject) {
338347
if (primaryId == null) {
339-
convertedValue = UnsafeAllocator.newInstance(type);
348+
try {
349+
convertedValue = UnsafeAllocator.newInstance(type);
350+
} catch (Exception e) {
351+
throw new DeserializeException("Deserialize failed because can't allocation object.");
352+
}
340353
} else {
341354
if (!this.primaryObjects.containsKey(type)) {
342355
this.primaryObjects.put(type, new HashMap<Object, Object>());
@@ -345,7 +358,11 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal
345358
HashMap<Object, Object> hashMap = this.primaryObjects.get(type);
346359

347360
if (!hashMap.containsKey(primaryId)) {
348-
hashMap.put(primaryId, UnsafeAllocator.newInstance(type));
361+
try {
362+
hashMap.put(primaryId, UnsafeAllocator.newInstance(type));
363+
} catch (Exception e) {
364+
throw new DeserializeException("Deserialize failed because can't allocation primary object.");
365+
}
349366
}
350367

351368
convertedValue = hashMap.get(primaryId);
@@ -362,15 +379,15 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal
362379
return convertedValue;
363380
}
364381

365-
public KsonValue fromObject(Object object) throws IOException, IllegalArgumentException, IllegalAccessException {
382+
public KsonValue fromObject(Object object) throws SerializeException {
366383
if (this.objectStack.isEmpty()) {
367384
return (KsonValue) addFromObjectStack(object);
368385
} else {
369-
throw new IllegalAccessException("This context already running parse!");
386+
throw new SerializeException("This context already running serialize!");
370387
}
371388
}
372389

373-
public Object addFromObjectStack(Object object) throws IOException, IllegalArgumentException, IllegalAccessException {
390+
public Object addFromObjectStack(Object object) throws SerializeException {
374391
boolean needLoop = this.objectStack.isEmpty();
375392

376393
Object result = null;
@@ -386,7 +403,11 @@ public Object addFromObjectStack(Object object) throws IOException, IllegalArgum
386403
KsonObject ksonValue = (KsonObject) targetKson;
387404

388405
for (Field field : this.getAccessibleFields(targetObject.getClass())) {
389-
ksonValue.put(field.getName(), this.createAtFromObject(false, field.getType(), field.get(targetObject)));
406+
try {
407+
ksonValue.put(field.getName(), this.createAtFromObject(false, field.getType(), field.get(targetObject)));
408+
} catch (IllegalArgumentException | IllegalAccessException e) {
409+
throw new SerializeException("Serialize failed because object could can't get from field.");
410+
}
390411
}
391412
} else {
392413
KsonArray ksonValue = (KsonArray) targetKson;
@@ -413,7 +434,7 @@ public Object addFromObjectStack(Object object) throws IOException, IllegalArgum
413434
return result;
414435
}
415436

416-
private Object createAtFromObject(boolean first, Class<?> type, Object originalValue) {
437+
private Object createAtFromObject(boolean first, Class<?> type, Object originalValue) throws SerializeException {
417438
if (originalValue == null)
418439
return null;
419440

@@ -429,15 +450,15 @@ private Object createAtFromObject(boolean first, Class<?> type, Object originalV
429450
Field primaryKeyField = getPrimaryKeyField(originalValueType);
430451

431452
if (primaryKeyField != null) {
432-
try {
433-
KsonObject wrappingObject = new KsonObject();
453+
KsonObject wrappingObject = new KsonObject();
434454

455+
try {
435456
wrappingObject.put("@id", primaryKeyField.get(originalValue));
436-
437-
originalValue = wrappingObject;
438457
} catch (IllegalArgumentException | IllegalAccessException e) {
439-
e.printStackTrace();
458+
throw new SerializeException("Serialize failed because primary key can't get from field.");
440459
}
460+
461+
originalValue = wrappingObject;
441462
}
442463
}
443464

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.realtimetech.kson.exception;
2+
3+
public class DeserializeException extends Exception {
4+
private static final long serialVersionUID = -2041738655817004217L;
5+
6+
private String message;
7+
8+
public DeserializeException(String message) {
9+
this.message = message;
10+
}
11+
12+
public String getMessage() {
13+
return message;
14+
}
15+
16+
@Override
17+
public String getLocalizedMessage() {
18+
return message;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.realtimetech.kson.exception;
2+
3+
public class SerializeException extends Exception{
4+
private static final long serialVersionUID = -8508412598488843333L;
5+
6+
private String message;
7+
8+
public SerializeException(String message) {
9+
this.message = message;
10+
}
11+
12+
public String getMessage() {
13+
return message;
14+
}
15+
16+
@Override
17+
public String getLocalizedMessage() {
18+
return message;
19+
}
20+
}

src/main/java/com/realtimetech/kson/test/MainTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.realtimetech.kson.test;
22

3-
import java.io.IOException;
4-
53
import com.realtimetech.kson.KsonContext;
64
import com.realtimetech.kson.builder.KsonBuilder;
75
import com.realtimetech.kson.element.KsonObject;
86
import com.realtimetech.kson.element.KsonValue;
9-
import com.realtimetech.kson.pool.KsonPool;
7+
import com.realtimetech.kson.exception.SerializeException;
8+
import com.realtimetech.kson.util.pool.KsonPool;
109

1110
public class MainTest {
1211
@SuppressWarnings("unused")
@@ -27,7 +26,7 @@ public void run() {
2726
for (int i = 0; i < 100000; i++) {
2827
try {
2928
KsonValue fromObject = ksonContext.fromObject(testObject);
30-
} catch (IllegalArgumentException | IllegalAccessException | IOException e) {
29+
} catch (SerializeException e) {
3130
e.printStackTrace();
3231
}
3332
}

src/main/java/com/realtimetech/kson/pool/KsonPool.java renamed to src/main/java/com/realtimetech/kson/util/pool/KsonPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.realtimetech.kson.pool;
1+
package com.realtimetech.kson.util.pool;
22

33
import java.util.HashMap;
44

src/main/java/com/realtimetech/kson/stack/FastStack.java renamed to src/main/java/com/realtimetech/kson/util/stack/FastStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.realtimetech.kson.stack;
1+
package com.realtimetech.kson.util.stack;
22

33
import java.util.EmptyStackException;
44

src/main/java/com/realtimetech/kson/string/StringMaker.java renamed to src/main/java/com/realtimetech/kson/util/string/StringMaker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.realtimetech.kson.string;
1+
package com.realtimetech.kson.util.string;
22

33
public class StringMaker {
44
private int raiseSize;

0 commit comments

Comments
 (0)