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

Commit

Permalink
Added exceptions, package refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkJeongHwan committed Sep 18, 2019
1 parent bfc6a81 commit 864a7b3
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 40 deletions.
87 changes: 54 additions & 33 deletions src/main/java/com/realtimetech/kson/KsonContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import com.realtimetech.kson.element.KsonArray;
import com.realtimetech.kson.element.KsonObject;
import com.realtimetech.kson.element.KsonValue;
import com.realtimetech.kson.stack.FastStack;
import com.realtimetech.kson.string.StringMaker;
import com.realtimetech.kson.exception.DeserializeException;
import com.realtimetech.kson.exception.SerializeException;
import com.realtimetech.kson.util.stack.FastStack;
import com.realtimetech.kson.util.string.StringMaker;
import com.realtimetech.kson.transform.Transformer;
import com.realtimetech.reflection.access.ArrayAccessor;
import com.realtimetech.reflection.allocate.UnsafeAllocator;
Expand Down Expand Up @@ -66,7 +68,6 @@ public KsonContext(int stackSize, int stringBufferSize) {
this.primaryObjects = new HashMap<Class<?>, HashMap<Object, Object>>();

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


this.registeredTransformers.put(Date.class, new Transformer<Date>() {
@Override
Expand All @@ -88,7 +89,7 @@ public Object serialize(KsonContext ksonContext, ArrayList<?> value) {
for (Object object : value) {
try {
ksonArray.add(ksonContext.addFromObjectStack(object));
} catch (IllegalArgumentException | IllegalAccessException | IOException e) {
} catch (SerializeException e) {
e.printStackTrace();
}
}
Expand Down Expand Up @@ -121,10 +122,10 @@ public Object serialize(KsonContext ksonContext, HashMap<?, ?> value) {
Object valueObject = value.get(keyObject);

try {
Object tryFromObject = ksonContext.addFromObjectStack(keyObject);
Object tryFromObject2 = ksonContext.addFromObjectStack(valueObject);
ksonObject.put(tryFromObject, tryFromObject2);
} catch (IllegalArgumentException | IllegalAccessException | IOException e) {
Object keyKson = ksonContext.addFromObjectStack(keyObject);
Object valueKson = ksonContext.addFromObjectStack(valueObject);
ksonObject.put(keyKson, valueKson);
} catch (SerializeException e) {
e.printStackTrace();
}
}
Expand Down Expand Up @@ -196,14 +197,6 @@ private boolean isNeedSerialize(Class<?> clazz) {
return true;
}

public <T> T toObject(Class<T> clazz, Object object) throws Exception {
if (object instanceof KsonValue) {
return (T) this.addToObjectStack(clazz, (KsonValue) object);
}

return (T) object;
}

public Transformer<?> getTransformer(Class<?> type) {
if (!this.transformers.containsKey(type)) {
boolean matched = false;
Expand Down Expand Up @@ -246,11 +239,19 @@ public Field getPrimaryKeyField(Class<?> type) {
return this.primaryKeys.get(type);
}

public Object addToObjectStack(Object object) throws Exception {
public <T> T toObject(Class<T> clazz, Object object) throws DeserializeException {
if (object instanceof KsonValue) {
return (T) this.addToObjectStack(clazz, (KsonValue) object);
}

return (T) object;
}

public Object addToObjectStack(Object object) throws DeserializeException {
return this.addToObjectStack(Object.class, object);
}

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

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

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

private Object createAtToObject(boolean first, Class<?> type, Object originalValue) throws Exception {
private Object createAtToObject(boolean first, Class<?> type, Object originalValue) throws DeserializeException {
Object primaryId = null;

if (originalValue instanceof KsonObject) {
KsonObject wrappingObject = (KsonObject) originalValue;

if (wrappingObject.containsKey("#class")) {
type = Class.forName(wrappingObject.get("#class").toString());
try {
type = Class.forName(wrappingObject.get("#class").toString());
} catch (ClassNotFoundException e) {
throw new DeserializeException("Deserialize failed because can't find target class.");
}
originalValue = wrappingObject.get("#data");
} else if (wrappingObject.containsKey("@id")) {
primaryId = wrappingObject.get("@id");
Expand Down Expand Up @@ -336,7 +345,11 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal
convertedValue = Array.newInstance(componentType, ksonArray.size());
} else if (convertedValue instanceof KsonObject) {
if (primaryId == null) {
convertedValue = UnsafeAllocator.newInstance(type);
try {
convertedValue = UnsafeAllocator.newInstance(type);
} catch (Exception e) {
throw new DeserializeException("Deserialize failed because can't allocation object.");
}
} else {
if (!this.primaryObjects.containsKey(type)) {
this.primaryObjects.put(type, new HashMap<Object, Object>());
Expand All @@ -345,7 +358,11 @@ private Object createAtToObject(boolean first, Class<?> type, Object originalVal
HashMap<Object, Object> hashMap = this.primaryObjects.get(type);

if (!hashMap.containsKey(primaryId)) {
hashMap.put(primaryId, UnsafeAllocator.newInstance(type));
try {
hashMap.put(primaryId, UnsafeAllocator.newInstance(type));
} catch (Exception e) {
throw new DeserializeException("Deserialize failed because can't allocation primary object.");
}
}

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

public KsonValue fromObject(Object object) throws IOException, IllegalArgumentException, IllegalAccessException {
public KsonValue fromObject(Object object) throws SerializeException {
if (this.objectStack.isEmpty()) {
return (KsonValue) addFromObjectStack(object);
} else {
throw new IllegalAccessException("This context already running parse!");
throw new SerializeException("This context already running serialize!");
}
}

public Object addFromObjectStack(Object object) throws IOException, IllegalArgumentException, IllegalAccessException {
public Object addFromObjectStack(Object object) throws SerializeException {
boolean needLoop = this.objectStack.isEmpty();

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

for (Field field : this.getAccessibleFields(targetObject.getClass())) {
ksonValue.put(field.getName(), this.createAtFromObject(false, field.getType(), field.get(targetObject)));
try {
ksonValue.put(field.getName(), this.createAtFromObject(false, field.getType(), field.get(targetObject)));
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new SerializeException("Serialize failed because object could can't get from field.");
}
}
} else {
KsonArray ksonValue = (KsonArray) targetKson;
Expand All @@ -413,7 +434,7 @@ public Object addFromObjectStack(Object object) throws IOException, IllegalArgum
return result;
}

private Object createAtFromObject(boolean first, Class<?> type, Object originalValue) {
private Object createAtFromObject(boolean first, Class<?> type, Object originalValue) throws SerializeException {
if (originalValue == null)
return null;

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

if (primaryKeyField != null) {
try {
KsonObject wrappingObject = new KsonObject();
KsonObject wrappingObject = new KsonObject();

try {
wrappingObject.put("@id", primaryKeyField.get(originalValue));

originalValue = wrappingObject;
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
throw new SerializeException("Serialize failed because primary key can't get from field.");
}

originalValue = wrappingObject;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.realtimetech.kson.exception;

public class DeserializeException extends Exception {
private static final long serialVersionUID = -2041738655817004217L;

private String message;

public DeserializeException(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

@Override
public String getLocalizedMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.realtimetech.kson.exception;

public class SerializeException extends Exception{
private static final long serialVersionUID = -8508412598488843333L;

private String message;

public SerializeException(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

@Override
public String getLocalizedMessage() {
return message;
}
}
7 changes: 3 additions & 4 deletions src/main/java/com/realtimetech/kson/test/MainTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.realtimetech.kson.test;

import java.io.IOException;

import com.realtimetech.kson.KsonContext;
import com.realtimetech.kson.builder.KsonBuilder;
import com.realtimetech.kson.element.KsonObject;
import com.realtimetech.kson.element.KsonValue;
import com.realtimetech.kson.pool.KsonPool;
import com.realtimetech.kson.exception.SerializeException;
import com.realtimetech.kson.util.pool.KsonPool;

public class MainTest {
@SuppressWarnings("unused")
Expand All @@ -27,7 +26,7 @@ public void run() {
for (int i = 0; i < 100000; i++) {
try {
KsonValue fromObject = ksonContext.fromObject(testObject);
} catch (IllegalArgumentException | IllegalAccessException | IOException e) {
} catch (SerializeException e) {
e.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.realtimetech.kson.pool;
package com.realtimetech.kson.util.pool;

import java.util.HashMap;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.realtimetech.kson.stack;
package com.realtimetech.kson.util.stack;

import java.util.EmptyStackException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.realtimetech.kson.string;
package com.realtimetech.kson.util.string;

public class StringMaker {
private int raiseSize;
Expand Down

0 comments on commit 864a7b3

Please sign in to comment.