From 8f0eeb974379431cf8a55811dd9355349a60ba65 Mon Sep 17 00:00:00 2001 From: devjeonghwan Date: Thu, 12 Oct 2023 16:16:31 +0900 Subject: [PATCH] Add `de/serializeObject` method in `Opacker.java` --- ...e__com_esotericsoftware_kryo_5_0_0_RC1.xml | 12 +++ ...dle__com_esotericsoftware_minlog_1_3_0.xml | 12 +++ ...com_esotericsoftware_reflectasm_1_11_7.xml | 12 +++ ...ackson_core_jackson_annotations_2_13_2.xml | 12 +++ ...erxml_jackson_core_jackson_core_2_13_2.xml | 15 ++++ ...jackson_core_jackson_databind_2_13_2_2.xml | 15 ++++ ...radle__com_google_code_gson_gson_2_8_9.xml | 12 +++ ..._org_apiguardian_apiguardian_api_1_1_2.xml | 12 +++ ...adle__org_jetbrains_annotations_22_0_0.xml | 12 +++ ..._junit_jupiter_junit_jupiter_api_5_8_2.xml | 12 +++ ...nit_jupiter_junit_jupiter_engine_5_8_2.xml | 12 +++ ..._platform_junit_platform_commons_1_8_2.xml | 12 +++ ...t_platform_junit_platform_engine_1_8_2.xml | 12 +++ .../Gradle__org_objenesis_objenesis_2_6.xml | 12 +++ ...radle__org_opentest4j_opentest4j_1_2_0.xml | 12 +++ .../java/com/realtimetech/opack/Opacker.java | 87 ++++++++++++------- .../realtimetech/opack/codec/OpackCodec.java | 4 +- .../opack/codec/dense/DenseCodec.java | 4 +- .../codec/dense/writer/ByteArrayWriter.java | 16 ++-- .../dense/writer/OutputStreamWriter.java | 16 ++-- .../opack/codec/dense/writer/Writer.java | 16 ++-- .../opack/codec/json/JsonCodec.java | 4 +- .../opack/transformer/impl/TypeWrapper.java | 10 ++- .../impl/list/ListTransformer.java | 2 +- .../transformer/impl/map/MapTransformer.java | 2 +- .../impl/reflection/ClassTransformer.java | 10 +-- .../realtimetech/opack/value/OpackArray.java | 4 +- .../annotation/AnnotationWithTypeTest.java | 1 - .../reflection/ClassTransformTest.java | 3 - .../test/performance/GsonPerformanceTest.java | 7 ++ .../performance/JacksonPerformanceTest.java | 7 ++ .../test/performance/KryoPerformanceTest.java | 7 ++ 32 files changed, 305 insertions(+), 81 deletions(-) create mode 100644 .idea/libraries/Gradle__com_esotericsoftware_kryo_5_0_0_RC1.xml create mode 100644 .idea/libraries/Gradle__com_esotericsoftware_minlog_1_3_0.xml create mode 100644 .idea/libraries/Gradle__com_esotericsoftware_reflectasm_1_11_7.xml create mode 100644 .idea/libraries/Gradle__com_fasterxml_jackson_core_jackson_annotations_2_13_2.xml create mode 100644 .idea/libraries/Gradle__com_fasterxml_jackson_core_jackson_core_2_13_2.xml create mode 100644 .idea/libraries/Gradle__com_fasterxml_jackson_core_jackson_databind_2_13_2_2.xml create mode 100644 .idea/libraries/Gradle__com_google_code_gson_gson_2_8_9.xml create mode 100644 .idea/libraries/Gradle__org_apiguardian_apiguardian_api_1_1_2.xml create mode 100644 .idea/libraries/Gradle__org_jetbrains_annotations_22_0_0.xml create mode 100644 .idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_api_5_8_2.xml create mode 100644 .idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_engine_5_8_2.xml create mode 100644 .idea/libraries/Gradle__org_junit_platform_junit_platform_commons_1_8_2.xml create mode 100644 .idea/libraries/Gradle__org_junit_platform_junit_platform_engine_1_8_2.xml create mode 100644 .idea/libraries/Gradle__org_objenesis_objenesis_2_6.xml create mode 100644 .idea/libraries/Gradle__org_opentest4j_opentest4j_1_2_0.xml diff --git a/.idea/libraries/Gradle__com_esotericsoftware_kryo_5_0_0_RC1.xml b/.idea/libraries/Gradle__com_esotericsoftware_kryo_5_0_0_RC1.xml new file mode 100644 index 0000000..c4c5374 --- /dev/null +++ b/.idea/libraries/Gradle__com_esotericsoftware_kryo_5_0_0_RC1.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__com_esotericsoftware_minlog_1_3_0.xml b/.idea/libraries/Gradle__com_esotericsoftware_minlog_1_3_0.xml new file mode 100644 index 0000000..21b1790 --- /dev/null +++ b/.idea/libraries/Gradle__com_esotericsoftware_minlog_1_3_0.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__com_esotericsoftware_reflectasm_1_11_7.xml b/.idea/libraries/Gradle__com_esotericsoftware_reflectasm_1_11_7.xml new file mode 100644 index 0000000..5b8849a --- /dev/null +++ b/.idea/libraries/Gradle__com_esotericsoftware_reflectasm_1_11_7.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__com_fasterxml_jackson_core_jackson_annotations_2_13_2.xml b/.idea/libraries/Gradle__com_fasterxml_jackson_core_jackson_annotations_2_13_2.xml new file mode 100644 index 0000000..0748cf1 --- /dev/null +++ b/.idea/libraries/Gradle__com_fasterxml_jackson_core_jackson_annotations_2_13_2.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__com_fasterxml_jackson_core_jackson_core_2_13_2.xml b/.idea/libraries/Gradle__com_fasterxml_jackson_core_jackson_core_2_13_2.xml new file mode 100644 index 0000000..f43d7cf --- /dev/null +++ b/.idea/libraries/Gradle__com_fasterxml_jackson_core_jackson_core_2_13_2.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__com_fasterxml_jackson_core_jackson_databind_2_13_2_2.xml b/.idea/libraries/Gradle__com_fasterxml_jackson_core_jackson_databind_2_13_2_2.xml new file mode 100644 index 0000000..f01ae03 --- /dev/null +++ b/.idea/libraries/Gradle__com_fasterxml_jackson_core_jackson_databind_2_13_2_2.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__com_google_code_gson_gson_2_8_9.xml b/.idea/libraries/Gradle__com_google_code_gson_gson_2_8_9.xml new file mode 100644 index 0000000..4c0173f --- /dev/null +++ b/.idea/libraries/Gradle__com_google_code_gson_gson_2_8_9.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_apiguardian_apiguardian_api_1_1_2.xml b/.idea/libraries/Gradle__org_apiguardian_apiguardian_api_1_1_2.xml new file mode 100644 index 0000000..723874b --- /dev/null +++ b/.idea/libraries/Gradle__org_apiguardian_apiguardian_api_1_1_2.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_jetbrains_annotations_22_0_0.xml b/.idea/libraries/Gradle__org_jetbrains_annotations_22_0_0.xml new file mode 100644 index 0000000..892ffec --- /dev/null +++ b/.idea/libraries/Gradle__org_jetbrains_annotations_22_0_0.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_api_5_8_2.xml b/.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_api_5_8_2.xml new file mode 100644 index 0000000..1357007 --- /dev/null +++ b/.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_api_5_8_2.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_engine_5_8_2.xml b/.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_engine_5_8_2.xml new file mode 100644 index 0000000..575d95d --- /dev/null +++ b/.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_engine_5_8_2.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_junit_platform_junit_platform_commons_1_8_2.xml b/.idea/libraries/Gradle__org_junit_platform_junit_platform_commons_1_8_2.xml new file mode 100644 index 0000000..7a28900 --- /dev/null +++ b/.idea/libraries/Gradle__org_junit_platform_junit_platform_commons_1_8_2.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_junit_platform_junit_platform_engine_1_8_2.xml b/.idea/libraries/Gradle__org_junit_platform_junit_platform_engine_1_8_2.xml new file mode 100644 index 0000000..8e9823c --- /dev/null +++ b/.idea/libraries/Gradle__org_junit_platform_junit_platform_engine_1_8_2.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_objenesis_objenesis_2_6.xml b/.idea/libraries/Gradle__org_objenesis_objenesis_2_6.xml new file mode 100644 index 0000000..6a32f4f --- /dev/null +++ b/.idea/libraries/Gradle__org_objenesis_objenesis_2_6.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_opentest4j_opentest4j_1_2_0.xml b/.idea/libraries/Gradle__org_opentest4j_opentest4j_1_2_0.xml new file mode 100644 index 0000000..6cdf140 --- /dev/null +++ b/.idea/libraries/Gradle__org_opentest4j_opentest4j_1_2_0.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/realtimetech/opack/Opacker.java b/src/main/java/com/realtimetech/opack/Opacker.java index 637dfd4..73a8ee9 100644 --- a/src/main/java/com/realtimetech/opack/Opacker.java +++ b/src/main/java/com/realtimetech/opack/Opacker.java @@ -69,7 +69,7 @@ public static class Builder { private boolean enableConvertEnumToOrdinal; private boolean enableConvertRecursiveDependencyToNull; - private @Nullable ClassLoader classTransformerClassLoader; + private @NotNull ClassLoader classLoader; public Builder() { this.valueStackInitialSize = 512; @@ -80,41 +80,41 @@ public Builder() { this.enableConvertEnumToOrdinal = false; this.enableConvertRecursiveDependencyToNull = false; - this.classTransformerClassLoader = this.getClass().getClassLoader(); + this.classLoader = this.getClass().getClassLoader(); } - public Builder setValueStackInitialSize(int valueStackInitialSize) { + public @NotNull Builder setValueStackInitialSize(int valueStackInitialSize) { this.valueStackInitialSize = valueStackInitialSize; return this; } - public Builder setContextStackInitialSize(int contextStackInitialSize) { + public @NotNull Builder setContextStackInitialSize(int contextStackInitialSize) { this.contextStackInitialSize = contextStackInitialSize; return this; } - public Builder setEnableWrapListElementType(boolean enableWrapListElementType) { + public @NotNull Builder setEnableWrapListElementType(boolean enableWrapListElementType) { this.enableWrapListElementType = enableWrapListElementType; return this; } - public Builder setEnableWrapMapElementType(boolean enableWrapMapElementType) { + public @NotNull Builder setEnableWrapMapElementType(boolean enableWrapMapElementType) { this.enableWrapMapElementType = enableWrapMapElementType; return this; } - public Builder setEnableConvertEnumToOrdinal(boolean enableConvertEnumToOrdinal) { + public @NotNull Builder setEnableConvertEnumToOrdinal(boolean enableConvertEnumToOrdinal) { this.enableConvertEnumToOrdinal = enableConvertEnumToOrdinal; return this; } - public Builder setEnableConvertRecursiveDependencyToNull(boolean enableConvertRecursiveDependencyToNull) { + public @NotNull Builder setEnableConvertRecursiveDependencyToNull(boolean enableConvertRecursiveDependencyToNull) { this.enableConvertRecursiveDependencyToNull = enableConvertRecursiveDependencyToNull; return this; } - public Builder setClassTransformerClassLoader(@Nullable ClassLoader classTransformerClassLoader) { - this.classTransformerClassLoader = classTransformerClassLoader; + public @NotNull Builder setClassLoader(@NotNull ClassLoader classLoader) { + this.classLoader = classLoader; return this; } @@ -123,7 +123,7 @@ public Builder setClassTransformerClassLoader(@Nullable ClassLoader classTransfo * * @return created opacker */ - public Opacker create() { + public @NotNull Opacker create() { return new Opacker(this); } } @@ -132,6 +132,7 @@ public enum State { NONE, SERIALIZE, DESERIALIZE } + private final @NotNull ClassLoader classLoader; private final @NotNull TypeBaker typeBaker; private final @NotNull FastStack<@NotNull Object> objectStack; @@ -151,6 +152,7 @@ public enum State { * @throws IllegalStateException if the predefined transformer cannot be instanced */ private Opacker(@NotNull Builder builder) { + this.classLoader = builder.classLoader; this.typeBaker = new TypeBaker(this); this.objectStack = new FastStack<>(builder.contextStackInitialSize); @@ -182,11 +184,7 @@ private Opacker(@NotNull Builder builder) { this.typeBaker.registerPredefinedTransformer(LocalTime.class, LocalTimeTransformer.class, true); this.typeBaker.registerPredefinedTransformer(LocalDateTime.class, LocalDateTimeTransformer.class, true); - if (builder.classTransformerClassLoader != null) { - ClassTransformer classTransformer = new ClassTransformer(builder.classTransformerClassLoader); - - this.typeBaker.registerPredefinedTransformer(Class.class, classTransformer, true); - } + this.typeBaker.registerPredefinedTransformer(Class.class, ClassTransformer.class, true); } catch (InstantiationException exception) { throw new IllegalStateException(exception); } @@ -195,23 +193,39 @@ private Opacker(@NotNull Builder builder) { this.enableConvertRecursiveDependencyToNull = builder.enableConvertRecursiveDependencyToNull; } + public @NotNull ClassLoader getClassLoader() { + return classLoader; + } + public @NotNull TypeBaker getTypeBaker() { return this.typeBaker; } + /** * Serializes the object to {@link OpackValue OpackValue} * * @param object the object to be serialized - * @return opack value - * @throws SerializeException if a problem occurs during serializing; if this opacker is deserializing + * @return the serialized opack value + * @throws SerializeException if a problem occurs during serializing, if this opacker is deserializing */ public synchronized @Nullable OpackValue serialize(@NotNull Object object) throws SerializeException { + return (OpackValue) this.serializeObject(object); + } + + /** + * Serializes the object to {@link OpackValue OpackValue} + * + * @param object the object to be serialized + * @return the serialized object + * @throws SerializeException if a problem occurs during serializing, if this opacker is deserializing + */ + public synchronized @Nullable Object serializeObject(@NotNull Object object) throws SerializeException { if (this.state == State.DESERIALIZE) throw new SerializeException("Opacker is deserializing."); int separatorStack = this.objectStack.getSize(); - OpackValue value = (OpackValue) this.prepareObjectSerialize(object.getClass(), object); + Object serializedObject = this.prepareObjectSerialize(object.getClass(), object); State lastState = this.state; try { @@ -225,7 +239,7 @@ private Opacker(@NotNull Builder builder) { } } - return value; + return serializedObject; } /** @@ -234,7 +248,7 @@ private Opacker(@NotNull Builder builder) { * @param baseType the class of object to be serialized * @param object the object to be serialized * @return prepared opack value - * @throws SerializeException if a problem occurs during serializing; if the baseType cannot be baked into {@link BakedType BakedType} + * @throws SerializeException if a problem occurs during serializing, if the baseType cannot be baked into {@link BakedType BakedType} */ private @Nullable Object prepareObjectSerialize(@NotNull Class baseType, @NotNull Object object) throws SerializeException { try { @@ -284,7 +298,7 @@ private Opacker(@NotNull Builder builder) { } /* - Optimize algorithm for big array + Optimize algorithm for a big array */ if (OpackArray.isAllowArray(objectType)) { int dimensions = ReflectionUtil.getArrayDimension(objectType); @@ -324,7 +338,7 @@ private Opacker(@NotNull Builder builder) { /** * Serialize the elements of each opack value in the stack * - * @throws SerializeException if a problem occurs during serializing; if the field in the class of instance to be serialized is not accessible + * @throws SerializeException if a problem occurs during serializing, if the field in the class of instance to be serialized is not accessible */ private void executeSerializeStack(int endOfStack) throws SerializeException { while (this.objectStack.getSize() > endOfStack) { @@ -375,27 +389,40 @@ private void executeSerializeStack(int endOfStack) throws SerializeException { } } + /** - * Deserializes the opack value to object of the target class + * Deserializes the object to object of the target class * * @param type the target class * @param opackValue the opack value to be deserialized - * @return deserialized object - * @throws DeserializeException if a problem occurs during deserializing; if this opacker is serializing + * @return the deserialized object + * @throws DeserializeException if a problem occurs during deserializing, if this opacker is serializing */ public synchronized @Nullable T deserialize(@NotNull Class type, @NotNull OpackValue opackValue) throws DeserializeException { + return this.deserializeObject(type, opackValue); + } + + /** + * Deserializes the object to object of the target class + * + * @param type the target class + * @param object the object to be deserialized + * @return the deserialized object + * @throws DeserializeException if a problem occurs during deserializing, if this opacker is serializing + */ + public synchronized @Nullable T deserializeObject(@NotNull Class type, @NotNull Object object) throws DeserializeException { if (this.state == State.SERIALIZE) throw new DeserializeException("Opacker is serializing."); int separatorStack = this.objectStack.getSize(); - Object object = this.prepareObjectDeserialize(type, opackValue, false, null); + Object deserializedObject = this.prepareObjectDeserialize(type, object, false, null); - if (object == null) { + if (deserializedObject == null) { return null; } - T value = type.cast(object); + T value = type.cast(deserializedObject); State lastState = this.state; try { @@ -538,7 +565,7 @@ private void executeSerializeStack(int endOfStack) throws SerializeException { /** * Deserialize the elements of each opack value in the stack * - * @throws DeserializeException if a problem occurs during deserializing; if the field in the class of instance to be deserialized is not accessible + * @throws DeserializeException if a problem occurs during deserializing, if the field in the class of instance to be deserialized is not accessible */ private void executeDeserializeStack(int endOfStack) throws DeserializeException { while (this.objectStack.getSize() > endOfStack) { diff --git a/src/main/java/com/realtimetech/opack/codec/OpackCodec.java b/src/main/java/com/realtimetech/opack/codec/OpackCodec.java index 0d615e3..6f3f48a 100644 --- a/src/main/java/com/realtimetech/opack/codec/OpackCodec.java +++ b/src/main/java/com/realtimetech/opack/codec/OpackCodec.java @@ -53,7 +53,7 @@ public abstract class OpackCodec { * * @param output the output to encode * @param opackValue the opack value to encode - * @throws EncodeException if a problem occurs during encoding; if the type of data to be encoded is not allowed in specific codec + * @throws EncodeException if a problem occurs during encoding, if the type of data to be encoded is not allowed in specific codec */ public final synchronized void encode(@NotNull O output, @NotNull OpackValue opackValue) throws EncodeException { try { @@ -68,7 +68,7 @@ public final synchronized void encode(@NotNull O output, @NotNull OpackValue opa * * @param input the input to decode * @return decoded value - * @throws DecodeException if a problem occurs during decoding; if the type of data to be decoded is not allowed in specific codec + * @throws DecodeException if a problem occurs during decoding, if the type of data to be decoded is not allowed in specific codec */ public final synchronized OpackValue decode(@NotNull I input) throws DecodeException { try { diff --git a/src/main/java/com/realtimetech/opack/codec/dense/DenseCodec.java b/src/main/java/com/realtimetech/opack/codec/dense/DenseCodec.java index 86d3696..527e55d 100644 --- a/src/main/java/com/realtimetech/opack/codec/dense/DenseCodec.java +++ b/src/main/java/com/realtimetech/opack/codec/dense/DenseCodec.java @@ -466,7 +466,7 @@ protected void doEncode(@NotNull Writer writer, @NotNull OpackValue opackValue) * * @param opackValue the OpackValue to encode * @return returns encoded bytes - * @throws EncodeException if a problem occurs during encoding; if the type of data to be encoded is not allowed in specific codec + * @throws EncodeException if a problem occurs during encoding, if the type of data to be encoded is not allowed in specific codec */ public byte @NotNull [] encode(@NotNull OpackValue opackValue) throws EncodeException { ByteArrayWriter byteArrayWriter = new ByteArrayWriter(); @@ -771,7 +771,7 @@ protected void doEncode(@NotNull Writer writer, @NotNull OpackValue opackValue) * * @param bytes the bytes to decode * @return the decoded opack value - * @throws DecodeException if a problem occurs during decoding; if the type of data to be decoded is not allowed in specific codec + * @throws DecodeException if a problem occurs during decoding, if the type of data to be decoded is not allowed in specific codec */ public @NotNull OpackValue decode(byte @NotNull [] bytes) throws DecodeException { ByteArrayReader byteArrayReader = new ByteArrayReader(bytes); diff --git a/src/main/java/com/realtimetech/opack/codec/dense/writer/ByteArrayWriter.java b/src/main/java/com/realtimetech/opack/codec/dense/writer/ByteArrayWriter.java index 621dc96..6436830 100644 --- a/src/main/java/com/realtimetech/opack/codec/dense/writer/ByteArrayWriter.java +++ b/src/main/java/com/realtimetech/opack/codec/dense/writer/ByteArrayWriter.java @@ -95,7 +95,7 @@ private void increaseArray(int requireSize) { * Writes the specified byte to this output stream * * @param value the byte - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeByte(int value) throws IOException { this.increaseArray(1); @@ -107,7 +107,7 @@ public void writeByte(int value) throws IOException { * Writes the specified character to this output stream * * @param value the character - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeChar(char value) throws IOException { this.increaseArray(2); @@ -120,7 +120,7 @@ public void writeChar(char value) throws IOException { * Writes the specified short to this output stream * * @param value the short - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeShort(short value) throws IOException { this.increaseArray(2); @@ -133,7 +133,7 @@ public void writeShort(short value) throws IOException { * Writes the specified int to this output stream * * @param value the int - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeInt(int value) throws IOException { this.increaseArray(4); @@ -148,7 +148,7 @@ public void writeInt(int value) throws IOException { * Writes the specified float to this output stream * * @param value the float - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeFloat(float value) throws IOException { this.writeInt(Float.floatToRawIntBits(value)); @@ -158,7 +158,7 @@ public void writeFloat(float value) throws IOException { * Writes the specified long to this output stream * * @param value the long - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeLong(long value) throws IOException { this.increaseArray(8); @@ -177,7 +177,7 @@ public void writeLong(long value) throws IOException { * Writes the specified double to this output stream * * @param value the double - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeDouble(double value) throws IOException { this.writeLong(Double.doubleToRawLongBits(value)); @@ -187,7 +187,7 @@ public void writeDouble(double value) throws IOException { * Writes the specified bytes to this output stream * * @param bytes the byte array to write - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeBytes(byte @NotNull [] bytes) throws IOException { this.increaseArray(bytes.length); diff --git a/src/main/java/com/realtimetech/opack/codec/dense/writer/OutputStreamWriter.java b/src/main/java/com/realtimetech/opack/codec/dense/writer/OutputStreamWriter.java index 4972a9d..d46e754 100644 --- a/src/main/java/com/realtimetech/opack/codec/dense/writer/OutputStreamWriter.java +++ b/src/main/java/com/realtimetech/opack/codec/dense/writer/OutputStreamWriter.java @@ -53,7 +53,7 @@ public class OutputStreamWriter implements Writer { * Writes the specified byte to this output stream * * @param value the byte - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeByte(int value) throws IOException { this.outputStream.write(value); @@ -63,7 +63,7 @@ public void writeByte(int value) throws IOException { * Writes the specified character to this output stream * * @param value the character - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeChar(char value) throws IOException { this.outputStream.write((byte) ((value >> 8) & 0xff)); @@ -74,7 +74,7 @@ public void writeChar(char value) throws IOException { * Writes the specified short to this output stream * * @param value the short - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeShort(short value) throws IOException { this.outputStream.write((byte) ((value >> 8) & 0xff)); @@ -85,7 +85,7 @@ public void writeShort(short value) throws IOException { * Writes the specified int to this output stream * * @param value the int - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeInt(int value) throws IOException { this.outputStream.write((byte) ((value >> 24) & 0xff)); @@ -98,7 +98,7 @@ public void writeInt(int value) throws IOException { * Writes the specified float to this output stream * * @param value the float - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeFloat(float value) throws IOException { this.writeInt(Float.floatToRawIntBits(value)); @@ -108,7 +108,7 @@ public void writeFloat(float value) throws IOException { * Writes the specified long to this output stream * * @param value the long - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeLong(long value) throws IOException { this.outputStream.write((byte) ((value >> 56) & 0xff)); @@ -125,7 +125,7 @@ public void writeLong(long value) throws IOException { * Writes the specified double to this output stream * * @param value the double - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeDouble(double value) throws IOException { this.writeLong(Double.doubleToRawLongBits(value)); @@ -135,7 +135,7 @@ public void writeDouble(double value) throws IOException { * Writes the specified bytes to this output stream * * @param bytes the byte array to write - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ public void writeBytes(byte @NotNull [] bytes) throws IOException { this.outputStream.write(bytes); diff --git a/src/main/java/com/realtimetech/opack/codec/dense/writer/Writer.java b/src/main/java/com/realtimetech/opack/codec/dense/writer/Writer.java index d5264e0..addaab2 100644 --- a/src/main/java/com/realtimetech/opack/codec/dense/writer/Writer.java +++ b/src/main/java/com/realtimetech/opack/codec/dense/writer/Writer.java @@ -31,7 +31,7 @@ public interface Writer { * Writes the specified byte to this output stream * * @param value the byte - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ void writeByte(int value) throws IOException; @@ -39,7 +39,7 @@ public interface Writer { * Writes the specified character to this output stream * * @param value the character - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ void writeChar(char value) throws IOException; @@ -47,7 +47,7 @@ public interface Writer { * Writes the specified short to this output stream * * @param value the short - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ void writeShort(short value) throws IOException; @@ -55,7 +55,7 @@ public interface Writer { * Writes the specified int to this output stream * * @param value the int - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ void writeInt(int value) throws IOException; @@ -63,7 +63,7 @@ public interface Writer { * Writes the specified float to this output stream * * @param value the float - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ void writeFloat(float value) throws IOException; @@ -71,7 +71,7 @@ public interface Writer { * Writes the specified long to this output stream * * @param value the long - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ void writeLong(long value) throws IOException; @@ -79,7 +79,7 @@ public interface Writer { * Writes the specified double to this output stream * * @param value the double - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ void writeDouble(double value) throws IOException; @@ -87,7 +87,7 @@ public interface Writer { * Writes the specified bytes to this output stream * * @param bytes the byte array to write - * @throws IOException if an I/O error occurs; if the output stream has been closed. + * @throws IOException if an I/O error occurs, if the output stream has been closed. */ void writeBytes(byte @NotNull [] bytes) throws IOException; } diff --git a/src/main/java/com/realtimetech/opack/codec/json/JsonCodec.java b/src/main/java/com/realtimetech/opack/codec/json/JsonCodec.java index 119faa9..f94095f 100644 --- a/src/main/java/com/realtimetech/opack/codec/json/JsonCodec.java +++ b/src/main/java/com/realtimetech/opack/codec/json/JsonCodec.java @@ -681,7 +681,7 @@ protected void doEncode(@NotNull Writer writer, @NotNull OpackValue opackValue) * * @param opackValue the opack value to encode * @return returns encoded string - * @throws EncodeException if a problem occurs during encoding; if the type of data to be encoded is not allowed in specific codec + * @throws EncodeException if a problem occurs during encoding, if the type of data to be encoded is not allowed in specific codec */ public synchronized @NotNull String encode(@NotNull OpackValue opackValue) throws EncodeException { this.encodeStringWriter.reset(); @@ -695,7 +695,7 @@ protected void doEncode(@NotNull Writer writer, @NotNull OpackValue opackValue) * * @param data the json string to decode * @return OpackValue - * @throws IOException if there is a syntax problem with the json string; if the json string has a unicode whose unknown pattern + * @throws IOException if there is a syntax problem with the json string, if the json string has a unicode whose unknown pattern */ @Override protected @NotNull OpackValue doDecode(@NotNull String data) throws IOException { diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapper.java b/src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapper.java index 1c4b95d..e20a281 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapper.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapper.java @@ -67,9 +67,9 @@ public class TypeWrapper { opackObject.put("value", opackArray); } else { - OpackValue opackValue = opacker.serialize(object); + Object serializedObject = opacker.serializeObject(object); - opackObject.put("value", opackValue); + opackObject.put("value", serializedObject); } opackObject.put("type", object.getClass().getName()); @@ -108,7 +108,7 @@ public class TypeWrapper { } try { - Class objectType = Class.forName((String) type); + Class objectType = Class.forName((String) type, true, opacker.getClassLoader()); if (objectType.isArray()) { Class componentType = objectType.getComponentType(); @@ -132,11 +132,13 @@ public class TypeWrapper { return arrayObject; } else { + /* if (!(value instanceof OpackValue)) { throw new DeserializeException("Expected opack value as a `value` in wrapped object but " + value.getClass().getName() + "."); } + */ - return opacker.deserialize(objectType, (OpackValue) value); + return opacker.deserializeObject(objectType, value); } } catch (ClassNotFoundException classNotFoundException) { throw new DeserializeException(classNotFoundException); diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/list/ListTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/list/ListTransformer.java index e8a6226..47c6821 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/list/ListTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/list/ListTransformer.java @@ -107,7 +107,7 @@ public class ListTransformer extends DataStructureTransformer { @Override protected @Nullable Object serializeObject(@NotNull Opacker opacker, @Nullable Object element) throws SerializeException { if (element != null && !OpackValue.isAllowType(element.getClass())) { - return opacker.serialize(element); + return opacker.serializeObject(element); } return element; diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/map/MapTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/map/MapTransformer.java index f436c39..afd16e9 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/map/MapTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/map/MapTransformer.java @@ -116,7 +116,7 @@ public class MapTransformer extends DataStructureTransformer { @Override protected @Nullable Object serializeObject(@NotNull Opacker opacker, @Nullable Object element) throws SerializeException { if (element != null && !OpackValue.isAllowType(element.getClass())) { - return opacker.serialize(element); + return opacker.serializeObject(element); } return element; diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/reflection/ClassTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/reflection/ClassTransformer.java index 67213e7..50d36bc 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/reflection/ClassTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/reflection/ClassTransformer.java @@ -29,15 +29,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Date; - public class ClassTransformer implements Transformer { - private final @NotNull ClassLoader classLoader; - - public ClassTransformer(@NotNull ClassLoader classLoader) { - this.classLoader = classLoader; - } - /** * Serialize specific value to opack value * @@ -69,7 +61,7 @@ public ClassTransformer(@NotNull ClassLoader classLoader) { public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object object) throws DeserializeException { if (object instanceof String) { try { - return this.classLoader.loadClass((String) object); + return Class.forName((String) object, true, opacker.getClassLoader()); } catch (ClassNotFoundException classNotFoundException) { throw new DeserializeException("Failed to load class with set class loader.", classNotFoundException); } diff --git a/src/main/java/com/realtimetech/opack/value/OpackArray.java b/src/main/java/com/realtimetech/opack/value/OpackArray.java index 69c9637..74ad977 100644 --- a/src/main/java/com/realtimetech/opack/value/OpackArray.java +++ b/src/main/java/com/realtimetech/opack/value/OpackArray.java @@ -53,7 +53,7 @@ public static boolean isAllowArray(@NotNull Class arrayType) { * * @param arrayObject the array object for create * @return created opack array - * @throws IllegalArgumentException if the component type for array object is not primitive type; if the array object is not 1 dimension + * @throws IllegalArgumentException if the component type for array object is not primitive type, if the array object is not 1 dimension */ public static OpackArray createWithArrayObject(@NotNull Object arrayObject) { return new OpackArray<>(arrayObject); @@ -65,7 +65,7 @@ public static OpackArray createWithArrayObject(@NotNull Object arrayObject) { * Constructs an opack array with the specified array object of which component type is the primitive type * * @param arrayObject the array object for create - * @throws IllegalArgumentException if the component type for array object is not primitive type; if the array object is not 1 dimension + * @throws IllegalArgumentException if the component type for array object is not primitive type, if the array object is not 1 dimension */ private OpackArray(@NotNull Object arrayObject) { if (!arrayObject.getClass().isArray()) { diff --git a/src/test/java/com/realtimetech/opack/test/opacker/annotation/AnnotationWithTypeTest.java b/src/test/java/com/realtimetech/opack/test/opacker/annotation/AnnotationWithTypeTest.java index bd107ab..e907832 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/annotation/AnnotationWithTypeTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/annotation/AnnotationWithTypeTest.java @@ -27,7 +27,6 @@ import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; -import com.realtimetech.opack.test.opacker.single.ObjectTest; import com.realtimetech.opack.value.OpackValue; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/reflection/ClassTransformTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/reflection/ClassTransformTest.java index cf78a0c..a3c3b6d 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/transform/reflection/ClassTransformTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/reflection/ClassTransformTest.java @@ -26,12 +26,9 @@ import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; -import com.realtimetech.opack.test.opacker.transform.path.PathTransformTest; import com.realtimetech.opack.value.OpackValue; import org.junit.jupiter.api.Test; -import java.nio.file.Path; - public class ClassTransformTest { public static class ClassTransformClass { private Class targetClass; diff --git a/src/test/java/com/realtimetech/opack/test/performance/GsonPerformanceTest.java b/src/test/java/com/realtimetech/opack/test/performance/GsonPerformanceTest.java index 46d4603..4a6c805 100644 --- a/src/test/java/com/realtimetech/opack/test/performance/GsonPerformanceTest.java +++ b/src/test/java/com/realtimetech/opack/test/performance/GsonPerformanceTest.java @@ -56,20 +56,27 @@ public void gson_json() { JsonElement decode = gson.fromJson(encode, JsonElement.class); PerformanceClass deserialize = gson.fromJson(decode, PerformanceClass.class); + assert deserialize != null; + deserialize.hashCode(); }; PerformanceClass.ExceptionRunnable gsonDirectRunnable = () -> { String encode = gson.toJson(performanceClass); PerformanceClass decode = gson.fromJson(encode, PerformanceClass.class); + assert decode != null; + decode.hashCode(); }; PerformanceClass.ExceptionRunnable opackRunnable = () -> { OpackValue serialize = opacker.serialize(performanceClass); + assert serialize != null; String encode = jsonCodec.encode(serialize); OpackValue decode = jsonCodec.decode(encode); PerformanceClass deserialize = opacker.deserialize(PerformanceClass.class, decode); + assert deserialize != null; + deserialize.hashCode(); }; diff --git a/src/test/java/com/realtimetech/opack/test/performance/JacksonPerformanceTest.java b/src/test/java/com/realtimetech/opack/test/performance/JacksonPerformanceTest.java index 3306566..50e522d 100644 --- a/src/test/java/com/realtimetech/opack/test/performance/JacksonPerformanceTest.java +++ b/src/test/java/com/realtimetech/opack/test/performance/JacksonPerformanceTest.java @@ -58,15 +58,22 @@ public void jackson_bytes() throws SerializeException, EncodeException, DecodeEx String value = objectMapper.writeValueAsString(performanceClass); PerformanceClass deserialize = objectMapper.readValue(value, PerformanceClass.class); + assert deserialize != null; + deserialize.hashCode(); }; PerformanceClass.ExceptionRunnable opackRunnable = () -> { OpackValue serialize = opacker.serialize(performanceClass); + + assert serialize != null; + String encode = jsonCodec.encode(serialize); OpackValue decode = jsonCodec.decode(encode); PerformanceClass deserialize = opacker.deserialize(PerformanceClass.class, decode); + assert deserialize != null; + deserialize.hashCode(); }; diff --git a/src/test/java/com/realtimetech/opack/test/performance/KryoPerformanceTest.java b/src/test/java/com/realtimetech/opack/test/performance/KryoPerformanceTest.java index 137adff..44e3cbc 100644 --- a/src/test/java/com/realtimetech/opack/test/performance/KryoPerformanceTest.java +++ b/src/test/java/com/realtimetech/opack/test/performance/KryoPerformanceTest.java @@ -105,17 +105,24 @@ public void kryo_bytes() throws Exception { byte[] encode = byteBufferOutput.toBytes(); PerformanceClass deserialize = kryo.readObject(new ByteBufferInput(encode), PerformanceClass.class); + assert deserialize != null; + deserialize.hashCode(); }; PerformanceClass.ExceptionRunnable opackRunnable = () -> { byteArrayWriter.reset(); OpackValue serialize = opacker.serialize(performanceClass); + + assert serialize != null; + denseCodec.encode(byteArrayWriter, serialize); byte[] encode = byteArrayWriter.toByteArray(); OpackValue decode = denseCodec.decode(encode); PerformanceClass deserialize = opacker.deserialize(PerformanceClass.class, decode); + assert deserialize != null; + deserialize.hashCode(); };