From 1f947d3931f22b7e88534c137313fb4851b9a1ae Mon Sep 17 00:00:00 2001 From: devjeonghwan Date: Mon, 21 Aug 2023 16:40:48 +0900 Subject: [PATCH] Add `WithType` annotation --- .../java/com/realtimetech/opack/Opacker.java | 25 ++- .../opack/annotation/WithType.java | 40 +++++ .../realtimetech/opack/bake/BakedType.java | 8 +- .../realtimetech/opack/bake/TypeBaker.java | 8 +- .../opack/transformer/Transformer.java | 8 +- .../impl/DataStructureTransformer.java | 2 +- .../transformer/impl/TypeWrapTransformer.java | 91 ----------- .../opack/transformer/impl/TypeWrapper.java | 145 ++++++++++++++++++ .../impl/file/FileTransformer.java | 20 +-- .../impl/list/ListTransformer.java | 27 ++-- .../impl/list/WrapListTransformer.java | 32 +--- .../transformer/impl/map/MapTransformer.java | 28 ++-- .../impl/map/WrapMapTransformer.java | 32 +--- .../impl/path/PathTransformer.java | 20 +-- .../impl/time/CalendarTransformer.java | 20 +-- .../impl/time/DateTransformer.java | 20 +-- .../time/java8/LocalDateTimeTransformer.java | 20 +-- .../impl/time/java8/LocalDateTransformer.java | 20 +-- .../impl/time/java8/LocalTimeTransformer.java | 20 +-- .../AnnotationWithTypeTest.java} | 38 +++-- .../opacker/list/WrapListElementTest.java | 28 +++- .../test/opacker/map/WrapMapElementTest.java | 27 +++- 22 files changed, 402 insertions(+), 277 deletions(-) create mode 100644 src/main/java/com/realtimetech/opack/annotation/WithType.java delete mode 100644 src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapTransformer.java create mode 100644 src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapper.java rename src/test/java/com/realtimetech/opack/test/opacker/{transform/TypeWrapTransformTest.java => annotation/AnnotationWithTypeTest.java} (65%) diff --git a/src/main/java/com/realtimetech/opack/Opacker.java b/src/main/java/com/realtimetech/opack/Opacker.java index 44feda1..e965926 100644 --- a/src/main/java/com/realtimetech/opack/Opacker.java +++ b/src/main/java/com/realtimetech/opack/Opacker.java @@ -28,6 +28,7 @@ import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.transformer.Transformer; +import com.realtimetech.opack.transformer.impl.TypeWrapper; import com.realtimetech.opack.transformer.impl.file.FileTransformer; import com.realtimetech.opack.transformer.impl.list.ListTransformer; import com.realtimetech.opack.transformer.impl.list.WrapListTransformer; @@ -189,7 +190,7 @@ private Opacker(@NotNull Builder builder) { * @return opack value * @throws SerializeException if a problem occurs during serializing; if this opacker is deserializing */ - public synchronized OpackValue serialize(@NotNull Object object) throws SerializeException { + public synchronized @Nullable OpackValue serialize(@NotNull Object object) throws SerializeException { if (this.state == State.DESERIALIZE) throw new SerializeException("Opacker is deserializing."); @@ -336,6 +337,10 @@ private void executeSerializeStack(int endOfStack) throws SerializeException { Object element = property.get(object); Class fieldType = property.getType(); + if (property.isWithType()) { + element = TypeWrapper.wrapObject(this, element); + } + if (property.getTransformer() != null) { element = property.getTransformer().serialize(this, fieldType, element); } @@ -362,13 +367,13 @@ private void executeSerializeStack(int endOfStack) throws SerializeException { * @return deserialized object * @throws DeserializeException if a problem occurs during deserializing; if this opacker is serializing */ - public synchronized T deserialize(@NotNull Class type, @NotNull OpackValue opackValue) throws DeserializeException { + public synchronized @Nullable T deserialize(@NotNull Class type, @NotNull OpackValue opackValue) throws DeserializeException { if (this.state == State.SERIALIZE) throw new DeserializeException("Opacker is serializing."); int separatorStack = this.objectStack.getSize(); - Object object = this.prepareObjectDeserialize(type, opackValue, null); + Object object = this.prepareObjectDeserialize(type, opackValue, false, null); if (object == null) { return null; @@ -399,7 +404,7 @@ public synchronized T deserialize(@NotNull Class type, @NotNull OpackValu * @return prepared object * @throws DeserializeException if a problem occurs during deserializing */ - private synchronized @Nullable Object prepareObjectDeserialize(@NotNull Class goalType, @NotNull Object object, @Nullable Transformer fieldTransformer) throws DeserializeException { + private synchronized @Nullable Object prepareObjectDeserialize(@NotNull Class goalType, @NotNull Object object, boolean withType, @Nullable Transformer fieldTransformer) throws DeserializeException { try { BakedType bakedType = this.typeBaker.get(goalType); @@ -421,6 +426,14 @@ public synchronized T deserialize(@NotNull Class type, @NotNull OpackValu } } + if (withType) { + object = TypeWrapper.unwrapObject(this, object); + + if (object == null) { + return null; + } + } + /* Early stopping */ @@ -526,7 +539,7 @@ private void executeDeserializeStack(int endOfStack) throws DeserializeException Object element = opackArray.get(index); if (element != null) { - Object deserializedValue = this.prepareObjectDeserialize(componentType, element, null); + Object deserializedValue = this.prepareObjectDeserialize(componentType, element, false, null); if (deserializedValue != null) { ReflectionUtil.setArrayItem(object, index, ReflectionUtil.cast(componentType, deserializedValue)); @@ -548,7 +561,7 @@ private void executeDeserializeStack(int endOfStack) throws DeserializeException Object propertyValue = null; if (element != null) { - Object deserializedValue = this.prepareObjectDeserialize(fieldType, element, property.getTransformer()); + Object deserializedValue = this.prepareObjectDeserialize(fieldType, element, property.isWithType(), property.getTransformer()); if (deserializedValue != null) { propertyValue = ReflectionUtil.cast(actualFieldType, deserializedValue); diff --git a/src/main/java/com/realtimetech/opack/annotation/WithType.java b/src/main/java/com/realtimetech/opack/annotation/WithType.java new file mode 100644 index 0000000..051fe56 --- /dev/null +++ b/src/main/java/com/realtimetech/opack/annotation/WithType.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2021 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.annotation; + +import org.jetbrains.annotations.NotNull; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Fields annotated with @Type(type=xxxx.class) will serialize and deserialize to explicit type instead of fields type. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ + ElementType.FIELD, +}) +public @interface WithType { +} \ No newline at end of file diff --git a/src/main/java/com/realtimetech/opack/bake/BakedType.java b/src/main/java/com/realtimetech/opack/bake/BakedType.java index 30c7a27..931520f 100644 --- a/src/main/java/com/realtimetech/opack/bake/BakedType.java +++ b/src/main/java/com/realtimetech/opack/bake/BakedType.java @@ -33,13 +33,15 @@ public final static class Property { private final @NotNull Field field; private final @NotNull String name; private final @NotNull Class type; + private final boolean withType; private final @Nullable Transformer transformer; - public Property(@NotNull Field field, @Nullable String name, @Nullable Transformer transformer, @Nullable Class type) { + public Property(@NotNull Field field, @Nullable String name, @Nullable Class type, boolean withType, @Nullable Transformer transformer) { this.field = field; this.name = name == null ? this.field.getName() : name; this.type = type == null ? this.field.getType() : type; + this.withType = withType; this.transformer = transformer; } @@ -56,6 +58,10 @@ public Property(@NotNull Field field, @Nullable String name, @Nullable Transform return type; } + public boolean isWithType() { + return withType; + } + public @Nullable Transformer getTransformer() { return transformer; } diff --git a/src/main/java/com/realtimetech/opack/bake/TypeBaker.java b/src/main/java/com/realtimetech/opack/bake/TypeBaker.java index f739538..9b60ff6 100644 --- a/src/main/java/com/realtimetech/opack/bake/TypeBaker.java +++ b/src/main/java/com/realtimetech/opack/bake/TypeBaker.java @@ -23,10 +23,7 @@ package com.realtimetech.opack.bake; import com.realtimetech.opack.Opacker; -import com.realtimetech.opack.annotation.Ignore; -import com.realtimetech.opack.annotation.Name; -import com.realtimetech.opack.annotation.Transform; -import com.realtimetech.opack.annotation.Type; +import com.realtimetech.opack.annotation.*; import com.realtimetech.opack.exception.BakeException; import com.realtimetech.opack.transformer.Transformer; import com.realtimetech.opack.transformer.TransformerFactory; @@ -296,8 +293,9 @@ private void addTransformer(@NotNull List<@NotNull Transformer> transformers, @N Transformer[] fieldTransformers = this.getTransformer(field); Class type = this.getAnnotatedType(field); String name = this.getAnnotatedName(field); + boolean withType = field.isAnnotationPresent(WithType.class); - properties.add(new BakedType.Property(field, name, fieldTransformers.length > 0 ? fieldTransformers[0] : null, type)); + properties.add(new BakedType.Property(field, name, type, withType, fieldTransformers.length > 0 ? fieldTransformers[0] : null)); } transformers = this.getTransformer(bakeType); diff --git a/src/main/java/com/realtimetech/opack/transformer/Transformer.java b/src/main/java/com/realtimetech/opack/transformer/Transformer.java index 3ead26a..97c0577 100644 --- a/src/main/java/com/realtimetech/opack/transformer/Transformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/Transformer.java @@ -34,20 +34,20 @@ public interface Transformer { * * @param opacker the opacker * @param originalType the original type - * @param value the value to be serialized + * @param object the object to be serialized * @return opack value * @throws SerializeException if a problem occurs during serializing */ - @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException; + @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object object) throws SerializeException; /** * Deserialize opack value. * * @param opacker the opacker - * @param value the opack value to be deserialized + * @param object the object to be deserialized * @param goalType the goal type to deserialize * @return deserialized value * @throws DeserializeException if a problem occurs during deserializing */ - @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException; + @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object object) throws DeserializeException; } \ No newline at end of file diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/DataStructureTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/DataStructureTransformer.java index 5d77376..25c7684 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/DataStructureTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/DataStructureTransformer.java @@ -45,7 +45,7 @@ public abstract class DataStructureTransformer implements Transformer { * Deserializes the {@link OpackValue OpackValue}. * * @param opacker the opacker - * @param element the opack value to be deserialized + * @param element the element to be deserialized * @return deserialized element * @throws ClassNotFoundException if the class cannot be located * @throws DeserializeException if a problem occurs during deserializing diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapTransformer.java deleted file mode 100644 index fc3cdc1..0000000 --- a/src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapTransformer.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (C) 2021 REALTIMETECH All Rights Reserved - * - * Licensed either under the Apache License, Version 2.0, or (at your option) - * under the terms of the GNU General Public License as published by - * the Free Software Foundation (subject to the "Classpath" exception), - * either version 2, or any later version (collectively, the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * http://www.gnu.org/licenses/ - * http://www.gnu.org/software/classpath/license.html - * - * or as provided in the LICENSE file that accompanied this code. - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.realtimetech.opack.transformer.impl; - -import com.realtimetech.opack.Opacker; -import com.realtimetech.opack.exception.DeserializeException; -import com.realtimetech.opack.exception.SerializeException; -import com.realtimetech.opack.transformer.Transformer; -import com.realtimetech.opack.value.OpackObject; -import com.realtimetech.opack.value.OpackValue; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -public class TypeWrapTransformer implements Transformer { - /** - * Serialize specific value to opack value. - * - * @param opacker the opacker - * @param originalType the original type - * @param value the value to be serialized - * @return opack value - * @throws SerializeException if a problem occurs during serializing - */ - @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { - if (value != null) { - OpackValue opackValue = opacker.serialize(value); - OpackObject opackObject = new OpackObject<>(); - - opackObject.put("type", value.getClass().getName()); - opackObject.put("value", opackValue); - - return opackObject; - } - - return value; - } - - /** - * Deserialize opack value. - * - * @param opacker the opacker - * @param goalType the goal type to deserialize - * @param value the opack value to be deserialized - * @return deserialized value - * @throws DeserializeException if a problem occurs during deserializing - */ - @Override - public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { - if (value instanceof OpackObject) { - OpackObject opackObject = (OpackObject) value; - - if (opackObject.containsKey("type") && opackObject.containsKey("value")) { - String type = (String) opackObject.get("type"); - Object opackValue = opackObject.get("value"); - - try { - Class objectClass = Class.forName(type); - - if (opackValue instanceof OpackValue) { - return opacker.deserialize(objectClass, (OpackValue) value); - } - } catch (ClassNotFoundException classNotFoundException) { - throw new DeserializeException(classNotFoundException); - } - } - } - - return value; - } -} diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapper.java b/src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapper.java new file mode 100644 index 0000000..1c4b95d --- /dev/null +++ b/src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapper.java @@ -0,0 +1,145 @@ +/* + * Copyright (C) 2023 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.transformer.impl; + +import com.realtimetech.opack.Opacker; +import com.realtimetech.opack.exception.DeserializeException; +import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.util.ReflectionUtil; +import com.realtimetech.opack.value.OpackArray; +import com.realtimetech.opack.value.OpackObject; +import com.realtimetech.opack.value.OpackValue; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.Array; + +public class TypeWrapper { + /** + * Wrap object into opack object with object type + * + * @param opacker the opacker + * @param object the object to be serialized + * @return wrapped object + * @throws SerializeException if a problem occurs during serializing + */ + public static @Nullable Object wrapObject(@NotNull Opacker opacker, @Nullable Object object) throws SerializeException { + if (object == null) { + return null; + } + + if (OpackValue.isAllowType(object.getClass())) { + return object; + } + + OpackObject opackObject = new OpackObject<>(); + + if (object.getClass().isArray()) { + OpackArray opackArray = new OpackArray<>(); + int length = Array.getLength(object); + + for (int index = 0; index < length; index++) { + Object element = ReflectionUtil.getArrayItem(object, index); + Object wrappedObject = TypeWrapper.wrapObject(opacker, element); + + opackArray.add(wrappedObject); + } + + opackObject.put("value", opackArray); + } else { + OpackValue opackValue = opacker.serialize(object); + + opackObject.put("value", opackValue); + } + + opackObject.put("type", object.getClass().getName()); + + return opackObject; + } + + /** + * Unwrap opack object into object using proper object type + * + * @param opacker the opacker + * @param object the object to be unwrapped + * @return unwrapped object + * @throws DeserializeException if a problem occurs during deserializing + */ + public static @Nullable Object unwrapObject(@NotNull Opacker opacker, @Nullable Object object) throws DeserializeException { + if (object == null) { + return null; + } + + if (!(object instanceof OpackObject)) { + return object; + } + + OpackObject opackObject = (OpackObject) object; + + if (!opackObject.containsKey("type") || !opackObject.containsKey("value")) { + throw new DeserializeException("Not exists properties in wrapped opack object."); + } + + Object type = opackObject.get("type"); + Object value = opackObject.get("value"); + + if (!(type instanceof String)) { + throw new DeserializeException("Expected string as a `type` in wrapped object but " + type.getClass().getName() + "."); + } + + try { + Class objectType = Class.forName((String) type); + + if (objectType.isArray()) { + Class componentType = objectType.getComponentType(); + + if (!(value instanceof OpackArray)) { + throw new DeserializeException("Expected opack array as a `value` in wrapped object but " + value.getClass().getName() + "."); + } + + OpackArray opackArray = (OpackArray) value; + Object arrayObject = Array.newInstance(componentType, opackArray.length()); + + for (int index = 0; index < opackArray.length(); index++) { + Object element = opackArray.get(index); + + if (element != null) { + element = TypeWrapper.unwrapObject(opacker, element); + } + + ReflectionUtil.setArrayItem(arrayObject, index, element); + } + + 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); + } + } catch (ClassNotFoundException classNotFoundException) { + throw new DeserializeException(classNotFoundException); + } + } +} diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/file/FileTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/file/FileTransformer.java index c0b28c2..d135369 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/file/FileTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/file/FileTransformer.java @@ -37,17 +37,17 @@ public class FileTransformer implements Transformer { * * @param opacker the opacker * @param originalType the original type - * @param value the value to be serialized + * @param object the object to be serialized * @return opack value * @throws SerializeException if a problem occurs during serializing */ @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { - if (value instanceof File) { - return value.toString(); + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object object) throws SerializeException { + if (object instanceof File) { + return object.toString(); } - return value; + return object; } /** @@ -55,16 +55,16 @@ public class FileTransformer implements Transformer { * * @param opacker the opacker * @param goalType the goal type to deserialize - * @param value the opack value to be deserialized + * @param object the object to be deserialized * @return deserialized value * @throws DeserializeException if a problem occurs during deserializing */ @Override - public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { - if (value instanceof String) { - return new File(String.valueOf(value)); + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object object) throws DeserializeException { + if (object instanceof String) { + return new File(String.valueOf(object)); } - return value; + return object; } } 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 10574a0..e3d57d6 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 @@ -41,24 +41,24 @@ public class ListTransformer extends DataStructureTransformer { * * @param opacker the opacker * @param originalType the original type - * @param value the value to be serialized + * @param object the object to be serialized * @return opack value * @throws SerializeException if a problem occurs during serializing */ @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { - if (value instanceof List) { - List list = (List) value; + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object object) throws SerializeException { + if (object instanceof List) { + List list = (List) object; OpackArray opackArray = new OpackArray<>(list.size()); - for (Object object : list) { - opackArray.add(this.serializeObject(opacker, object)); + for (Object element : list) { + opackArray.add(this.serializeObject(opacker, element)); } return opackArray; } - return value; + return object; } /** @@ -66,14 +66,15 @@ public class ListTransformer extends DataStructureTransformer { * * @param opacker the opacker * @param goalType the goal type to deserialize - * @param value the opack value to be deserialized + * @param object the object to be deserialized * @return deserialized value * @throws DeserializeException if a problem occurs during deserializing */ @Override - public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { - if (value instanceof OpackArray) { - OpackArray opackArray = (OpackArray) value; + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object object) throws DeserializeException { + if (object instanceof OpackArray) { + OpackArray opackArray = (OpackArray) object; + if (List.class.isAssignableFrom(goalType)) { try { List list = (List) ReflectionUtil.createInstance(goalType); @@ -92,7 +93,7 @@ public class ListTransformer extends DataStructureTransformer { } } - return value; + return object; } /** @@ -116,7 +117,7 @@ public class ListTransformer extends DataStructureTransformer { * Deserializes the {@link OpackValue OpackValue}. * * @param opacker the opacker - * @param element the opack value to be deserialized + * @param element the element to be deserialized * @return deserialized element * @throws ClassNotFoundException if the class cannot be located * @throws DeserializeException if a problem occurs during deserializing diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/list/WrapListTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/list/WrapListTransformer.java index 150868a..bbaa30a 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/list/WrapListTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/list/WrapListTransformer.java @@ -25,6 +25,7 @@ import com.realtimetech.opack.Opacker; import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.transformer.impl.TypeWrapper; import com.realtimetech.opack.value.OpackObject; import com.realtimetech.opack.value.OpackValue; import org.jetbrains.annotations.NotNull; @@ -41,45 +42,20 @@ public class WrapListTransformer extends ListTransformer { */ @Override protected @Nullable Object serializeObject(@NotNull Opacker opacker, @Nullable Object element) throws SerializeException { - if (element != null && !OpackValue.isAllowType(element.getClass())) { - OpackValue opackValue = opacker.serialize(element); - OpackObject opackObject = new OpackObject<>(); - - opackObject.put("type", element.getClass().getName()); - opackObject.put("value", opackValue); - - return opackObject; - } - - return element; + return TypeWrapper.wrapObject(opacker, element); } /** * Deserializes the {@link OpackValue OpackValue}. * * @param opacker the opacker - * @param element the opack value to be deserialized + * @param element the element to be deserialized * @return deserialized element * @throws ClassNotFoundException if the class cannot be located * @throws DeserializeException if a problem occurs during deserializing */ @Override protected @Nullable Object deserializeObject(@NotNull Opacker opacker, @Nullable Object element) throws ClassNotFoundException, DeserializeException { - if (element instanceof OpackObject) { - OpackObject opackObject = (OpackObject) element; - - if (opackObject.containsKey("type") && opackObject.containsKey("value")) { - String type = (String) opackObject.get("type"); - Object value = opackObject.get("value"); - - Class objectClass = Class.forName(type); - - if (value instanceof OpackValue) { - return opacker.deserialize(objectClass, (OpackValue) value); - } - } - } - - return element; + return TypeWrapper.unwrapObject(opacker, 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 f439f0e..7cd99ae 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 @@ -41,19 +41,19 @@ public class MapTransformer extends DataStructureTransformer { * * @param opacker the opacker * @param originalType the original type - * @param value the value to be serialized + * @param object the object to be serialized * @return opack value * @throws SerializeException if a problem occurs during serializing */ @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { - if (value instanceof Map) { - Map map = (Map) value; + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object object) throws SerializeException { + if (object instanceof Map) { + Map map = (Map) object; OpackObject opackObject = new OpackObject<>(map.size()); - for (Object object : map.keySet()) { - Object keyObject = object; - Object valueObject = map.get(object); + for (Object element : map.keySet()) { + Object keyObject = element; + Object valueObject = map.get(element); keyObject = serializeObject(opacker, keyObject); valueObject = serializeObject(opacker, valueObject); @@ -64,7 +64,7 @@ public class MapTransformer extends DataStructureTransformer { return opackObject; } - return value; + return object; } /** @@ -72,14 +72,14 @@ public class MapTransformer extends DataStructureTransformer { * * @param opacker the opacker * @param goalType the goal type to deserialize - * @param value the opack value to be deserialized + * @param object the object to be deserialized * @return deserialized value * @throws DeserializeException if a problem occurs during deserializing */ @Override - public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { - if (value instanceof OpackObject) { - OpackObject opackObject = (OpackObject) value; + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object object) throws DeserializeException { + if (object instanceof OpackObject) { + OpackObject opackObject = (OpackObject) object; if (Map.class.isAssignableFrom(goalType)) { try { Map map = (Map) ReflectionUtil.createInstance(goalType); @@ -102,7 +102,7 @@ public class MapTransformer extends DataStructureTransformer { } } - return value; + return object; } /** @@ -126,7 +126,7 @@ public class MapTransformer extends DataStructureTransformer { * Deserializes the {@link OpackValue OpackValue}. * * @param opacker the opacker - * @param element the opack value to be deserialized + * @param element the element to be deserialized * @return deserialized element * @throws ClassNotFoundException if the class cannot be located * @throws DeserializeException if a problem occurs during deserializing diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/map/WrapMapTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/map/WrapMapTransformer.java index 50af33b..eae125d 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/map/WrapMapTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/map/WrapMapTransformer.java @@ -25,6 +25,7 @@ import com.realtimetech.opack.Opacker; import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.transformer.impl.TypeWrapper; import com.realtimetech.opack.value.OpackObject; import com.realtimetech.opack.value.OpackValue; import org.jetbrains.annotations.NotNull; @@ -41,45 +42,20 @@ public class WrapMapTransformer extends MapTransformer { */ @Override protected @Nullable Object serializeObject(@NotNull Opacker opacker, @Nullable Object element) throws SerializeException { - if (element != null && !OpackValue.isAllowType(element.getClass())) { - OpackValue opackValue = opacker.serialize(element); - OpackObject opackObject = new OpackObject<>(); - - opackObject.put("type", element.getClass().getName()); - opackObject.put("value", opackValue); - - return opackObject; - } - - return element; + return TypeWrapper.wrapObject(opacker,element); } /** * Deserializes the {@link OpackValue OpackValue}. * * @param opacker the opacker - * @param element the opack value to be deserialized + * @param element the element to be deserialized * @return deserialized element * @throws ClassNotFoundException if the class cannot be located * @throws DeserializeException if a problem occurs during deserializing */ @Override protected @Nullable Object deserializeObject(@NotNull Opacker opacker, @Nullable Object element) throws ClassNotFoundException, DeserializeException { - if (element instanceof OpackObject) { - OpackObject opackObject = (OpackObject) element; - - if (opackObject.containsKey("type") && opackObject.containsKey("value")) { - String type = (String) opackObject.get("type"); - Object value = opackObject.get("value"); - - Class objectClass = Class.forName(type); - - if (value instanceof OpackValue) { - return opacker.deserialize(objectClass, (OpackValue) value); - } - } - } - - return element; + return TypeWrapper.unwrapObject(opacker, element); } } diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/path/PathTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/path/PathTransformer.java index 2cad36a..5415609 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/path/PathTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/path/PathTransformer.java @@ -37,17 +37,17 @@ public class PathTransformer implements Transformer { * * @param opacker the opacker * @param originalType the original type - * @param value the value to be serialized + * @param object the object to be serialized * @return opack value * @throws SerializeException if a problem occurs during serializing */ @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { - if (value instanceof Path) { - return ((Path) value).toString(); + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object object) throws SerializeException { + if (object instanceof Path) { + return ((Path) object).toString(); } - return value; + return object; } /** @@ -55,16 +55,16 @@ public class PathTransformer implements Transformer { * * @param opacker the opacker * @param goalType the goal type to deserialize - * @param value the opack value to be deserialized + * @param object the object to be deserialized * @return deserialized value * @throws DeserializeException if a problem occurs during deserializing */ @Override - public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { - if (value instanceof String) { - return Path.of(String.valueOf(value)); + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object object) throws DeserializeException { + if (object instanceof String) { + return Path.of(String.valueOf(object)); } - return value; + return object; } } diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/time/CalendarTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/time/CalendarTransformer.java index 53307d7..bd77b84 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/time/CalendarTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/time/CalendarTransformer.java @@ -38,17 +38,17 @@ public class CalendarTransformer implements Transformer { * * @param opacker the opacker * @param originalType the original type - * @param value the value to be serialized + * @param object the object to be serialized * @return opack value * @throws SerializeException if a problem occurs during serializing */ @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { - if (value instanceof Calendar) { - return ((Calendar) value).getTime().getTime(); + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object object) throws SerializeException { + if (object instanceof Calendar) { + return ((Calendar) object).getTime().getTime(); } - return value; + return object; } /** @@ -56,20 +56,20 @@ public class CalendarTransformer implements Transformer { * * @param opacker the opacker * @param goalType the goal type to deserialize - * @param value the opack value to be deserialized + * @param object the object to be deserialized * @return deserialized value * @throws DeserializeException if a problem occurs during deserializing */ @Override - public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { - if (value instanceof Long) { + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object object) throws DeserializeException { + if (object instanceof Long) { Calendar calendar = Calendar.getInstance(); - calendar.setTime(new Date((Long) value)); + calendar.setTime(new Date((Long) object)); return calendar; } - return value; + return object; } } diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/time/DateTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/time/DateTransformer.java index 2c9f262..f2dde59 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/time/DateTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/time/DateTransformer.java @@ -37,17 +37,17 @@ public class DateTransformer implements Transformer { * * @param opacker the opacker * @param originalType the original type - * @param value the value to be serialized + * @param object the object to be serialized * @return opack value * @throws SerializeException if a problem occurs during serializing */ @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { - if (value instanceof Date) { - return ((Date) value).getTime(); + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object object) throws SerializeException { + if (object instanceof Date) { + return ((Date) object).getTime(); } - return value; + return object; } /** @@ -55,16 +55,16 @@ public class DateTransformer implements Transformer { * * @param opacker the opacker * @param goalType the goal type to deserialize - * @param value the opack value to be deserialized + * @param object the object to be deserialized * @return deserialized value * @throws DeserializeException if a problem occurs during deserializing */ @Override - public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { - if (value instanceof Long) { - return new Date((Long) value); + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object object) throws DeserializeException { + if (object instanceof Long) { + return new Date((Long) object); } - return value; + return object; } } diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTimeTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTimeTransformer.java index 0f5266f..dac9a06 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTimeTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTimeTransformer.java @@ -38,14 +38,14 @@ public class LocalDateTimeTransformer implements Transformer { * * @param opacker the opacker * @param originalType the original type - * @param value the value to be serialized + * @param object the object to be serialized * @return opack value * @throws SerializeException if a problem occurs during serializing */ @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { - if (value instanceof LocalDateTime) { - LocalDateTime localDateTime = (LocalDateTime) value; + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object object) throws SerializeException { + if (object instanceof LocalDateTime) { + LocalDateTime localDateTime = (LocalDateTime) object; return OpackArray.createWithArrayObject( new int[]{ @@ -60,7 +60,7 @@ public class LocalDateTimeTransformer implements Transformer { ); } - return value; + return object; } /** @@ -68,14 +68,14 @@ public class LocalDateTimeTransformer implements Transformer { * * @param opacker the opacker * @param goalType the goal type to deserialize - * @param value the opack value to be deserialized + * @param object the object to be deserialized * @return deserialized value * @throws DeserializeException if a problem occurs during deserializing */ @Override - public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { - if (value instanceof OpackArray) { - OpackArray opackArray = (OpackArray) value; + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object object) throws DeserializeException { + if (object instanceof OpackArray) { + OpackArray opackArray = (OpackArray) object; if (opackArray.length() == 7) { return LocalDateTime.of( @@ -90,6 +90,6 @@ public class LocalDateTimeTransformer implements Transformer { } } - return value; + return object; } } diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTransformer.java index 735124a..5b4042e 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTransformer.java @@ -38,14 +38,14 @@ public class LocalDateTransformer implements Transformer { * * @param opacker the opacker * @param originalType the original type - * @param value the value to be serialized + * @param object the object to be serialized * @return opack value * @throws SerializeException if a problem occurs during serializing */ @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { - if (value instanceof LocalDate) { - LocalDate localDate = (LocalDate) value; + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object object) throws SerializeException { + if (object instanceof LocalDate) { + LocalDate localDate = (LocalDate) object; return OpackArray.createWithArrayObject( new int[]{ @@ -56,7 +56,7 @@ public class LocalDateTransformer implements Transformer { ); } - return value; + return object; } /** @@ -64,14 +64,14 @@ public class LocalDateTransformer implements Transformer { * * @param opacker the opacker * @param goalType the goal type to deserialize - * @param value the opack value to be deserialized + * @param object the object to be deserialized * @return deserialized value * @throws DeserializeException if a problem occurs during deserializing */ @Override - public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { - if (value instanceof OpackArray) { - OpackArray opackArray = (OpackArray) value; + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object object) throws DeserializeException { + if (object instanceof OpackArray) { + OpackArray opackArray = (OpackArray) object; if (opackArray.length() == 3) { return LocalDate.of( @@ -82,6 +82,6 @@ public class LocalDateTransformer implements Transformer { } } - return value; + return object; } } diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalTimeTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalTimeTransformer.java index 7bbd127..580ffd5 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalTimeTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalTimeTransformer.java @@ -38,14 +38,14 @@ public class LocalTimeTransformer implements Transformer { * * @param opacker the opacker * @param originalType the original type - * @param value the value to be serialized + * @param object the object to be serialized * @return opack value * @throws SerializeException if a problem occurs during serializing */ @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { - if (value instanceof LocalTime) { - LocalTime localTime = (LocalTime) value; + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object object) throws SerializeException { + if (object instanceof LocalTime) { + LocalTime localTime = (LocalTime) object; return OpackArray.createWithArrayObject( new int[]{ @@ -57,7 +57,7 @@ public class LocalTimeTransformer implements Transformer { ); } - return value; + return object; } /** @@ -65,14 +65,14 @@ public class LocalTimeTransformer implements Transformer { * * @param opacker the opacker * @param goalType the goal type to deserialize - * @param value the opack value to be deserialized + * @param object the object to be deserialized * @return deserialized value * @throws DeserializeException if a problem occurs during deserializing */ @Override - public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { - if (value instanceof OpackArray) { - OpackArray opackArray = (OpackArray) value; + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object object) throws DeserializeException { + if (object instanceof OpackArray) { + OpackArray opackArray = (OpackArray) object; if (opackArray.length() == 4) { return LocalTime.of( @@ -84,6 +84,6 @@ public class LocalTimeTransformer implements Transformer { } } - return value; + return object; } } diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/TypeWrapTransformTest.java b/src/test/java/com/realtimetech/opack/test/opacker/annotation/AnnotationWithTypeTest.java similarity index 65% rename from src/test/java/com/realtimetech/opack/test/opacker/transform/TypeWrapTransformTest.java rename to src/test/java/com/realtimetech/opack/test/opacker/annotation/AnnotationWithTypeTest.java index 0395746..4ff5598 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/transform/TypeWrapTransformTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/annotation/AnnotationWithTypeTest.java @@ -20,33 +20,49 @@ * limitations under the License. */ -package com.realtimetech.opack.test.opacker.transform; +package com.realtimetech.opack.test.opacker.annotation; import com.realtimetech.opack.Opacker; -import com.realtimetech.opack.annotation.Transform; -import com.realtimetech.opack.annotation.Type; +import com.realtimetech.opack.annotation.WithType; import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; -import com.realtimetech.opack.transformer.impl.TypeWrapTransformer; import com.realtimetech.opack.value.OpackValue; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; -public class TypeWrapTransformTest { - public static class TypeWrapTransformClass { - @Transform(transformer = TypeWrapTransformer.class) +public class AnnotationWithTypeTest { + public static class WithTypeClass { + @WithType private @NotNull List listWithTransformer; - @Type(LinkedList.class) + @WithType + private @NotNull List @NotNull [] listArrayWithTransformer; + + @WithType private @NotNull List listWithAnnotation; - public TypeWrapTransformClass() { + public WithTypeClass() { this.listWithTransformer = new LinkedList<>(); + this.listArrayWithTransformer = new List[8]; this.listWithAnnotation = new LinkedList<>(); + + this.addItemsRandomly(this.listWithTransformer); + this.addItemsRandomly(this.listWithAnnotation); + + for(int index = 0; index < this.listArrayWithTransformer.length; index++) { + if(index % 2 == 0) { + this.listArrayWithTransformer[index] = new ArrayList<>(); + } else { + this.listArrayWithTransformer[index] = new LinkedList<>(); + } + + this.addItemsRandomly(this.listArrayWithTransformer[index]); + } } private void addItemsRandomly(@NotNull List list) { @@ -59,10 +75,10 @@ private void addItemsRandomly(@NotNull List list) { @Test public void test() throws SerializeException, DeserializeException, OpackAssert.AssertException { Opacker opacker = new Opacker.Builder().create(); - TypeWrapTransformClass originalObject = new TypeWrapTransformClass(); + WithTypeClass originalObject = new WithTypeClass(); OpackValue serialized = opacker.serialize(originalObject); - TypeWrapTransformClass deserialized = opacker.deserialize(TypeWrapTransformClass.class, serialized); + WithTypeClass deserialized = opacker.deserialize(WithTypeClass.class, serialized); OpackAssert.assertEquals(originalObject, deserialized); } diff --git a/src/test/java/com/realtimetech/opack/test/opacker/list/WrapListElementTest.java b/src/test/java/com/realtimetech/opack/test/opacker/list/WrapListElementTest.java index a1e5c35..4e67488 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/list/WrapListElementTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/list/WrapListElementTest.java @@ -30,9 +30,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.util.LinkedList; -import java.util.Objects; -import java.util.Random; +import java.util.*; public class WrapListElementTest { static final Random RANDOM = new Random(); @@ -45,6 +43,30 @@ public WrapListClass() { this.wrappedTypeList.add(null); this.wrappedTypeList.add("test 1"); this.wrappedTypeList.add(new TestElement()); + + { + LinkedList linkedList = new LinkedList(); + + linkedList.add(new WrapListElementTest.TestElement()); + + this.wrappedTypeList.add(linkedList); + } + + { + List[] linkedListArray = new List[4]; + + for (int index = 0; index < linkedListArray.length; index++) { + if (index % 2 == 0) { + linkedListArray[index] = new ArrayList<>(); + } else { + linkedListArray[index] = new LinkedList<>(); + } + + linkedListArray[index].add(new WrapListElementTest.TestElement()); + } + + this.wrappedTypeList.add(linkedListArray); + } } } diff --git a/src/test/java/com/realtimetech/opack/test/opacker/map/WrapMapElementTest.java b/src/test/java/com/realtimetech/opack/test/opacker/map/WrapMapElementTest.java index e7065a5..14c9590 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/map/WrapMapElementTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/map/WrapMapElementTest.java @@ -31,8 +31,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.util.HashMap; -import java.util.Random; +import java.util.*; public class WrapMapElementTest { static final Random RANDOM = new Random(); @@ -48,6 +47,30 @@ public WrapMapClass() { this.wrappedTypeMap.put("object_value", new WrapListElementTest.TestElement()); this.wrappedTypeMap.put(new WrapListElementTest.TestElement(), "object_key"); + + { + HashMap hashMap = new HashMap(); + + hashMap.put("inner_object_value", new WrapListElementTest.TestElement()); + + this.wrappedTypeMap.put("inner_map", hashMap); + } + + { + Map[] hashMapArray = new Map[4]; + + for (int index = 0; index < hashMapArray.length; index++) { + if (index % 2 == 0) { + hashMapArray[index] = new HashMap<>(); + } else { + hashMapArray[index] = new TreeMap<>(); + } + + hashMapArray[index].put("inner_object_value", new WrapListElementTest.TestElement()); + } + + this.wrappedTypeMap.put("inner_map_array", hashMapArray); + } } }