diff --git a/README.md b/README.md index fb46c38..a30bca8 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ OpackValue decodedOpackValue2 = denseCodec.decode(inputStream); ``` ### Advanced Usage -#### 1. Ignore and ExplicitType +#### 1. Ignore and ExplicitType and SerializedName ```java public class SomeObject { private String stringField; @@ -148,8 +148,14 @@ public class SomeObject { /* This field is serialized/deserialized to explicit type `ArrayList` instead of ambiguous field type `List` */ - @ExplicitType(type = ArrayList.class) + @Type(ArrayList.class) private List listField; + + /* + This field is serialized/deserialized to explicit type `ArrayList` instead of ambiguous field type `List` + */ + @Name("newFieldName") + private String oldFieldName; } ``` #### 2. Field Transformer diff --git a/src/main/java/com/realtimetech/opack/annotation/SerializedName.java b/src/main/java/com/realtimetech/opack/annotation/Name.java similarity index 92% rename from src/main/java/com/realtimetech/opack/annotation/SerializedName.java rename to src/main/java/com/realtimetech/opack/annotation/Name.java index 6231c51..cb48e09 100644 --- a/src/main/java/com/realtimetech/opack/annotation/SerializedName.java +++ b/src/main/java/com/realtimetech/opack/annotation/Name.java @@ -28,13 +28,13 @@ import java.lang.annotation.Target; /** - * Fields annotated with @Ignore will not be serialized and deserialized. + * Fields annotated with @Name("new name") will be serialized and deserialized to name. */ @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD, }) -public @interface SerializedName { +public @interface Name { /** * @return returns name to be serialized/deserialized */ diff --git a/src/main/java/com/realtimetech/opack/annotation/ExplicitType.java b/src/main/java/com/realtimetech/opack/annotation/Type.java similarity index 87% rename from src/main/java/com/realtimetech/opack/annotation/ExplicitType.java rename to src/main/java/com/realtimetech/opack/annotation/Type.java index 6b1e620..8f45a08 100644 --- a/src/main/java/com/realtimetech/opack/annotation/ExplicitType.java +++ b/src/main/java/com/realtimetech/opack/annotation/Type.java @@ -30,15 +30,15 @@ import java.lang.annotation.Target; /** - * Fields annotated with @ExplicitType(type=xxxx.class) will serialize and deserialize to explicit type instead of fields type. + * 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 ExplicitType { +public @interface Type { /** * @return the explicit type */ - @NotNull Class type(); + @NotNull Class value(); } \ 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 6f821c6..2c9b43d 100644 --- a/src/main/java/com/realtimetech/opack/bake/BakedType.java +++ b/src/main/java/com/realtimetech/opack/bake/BakedType.java @@ -35,15 +35,13 @@ public static class Property { private final @NotNull Class type; private final @Nullable Transformer transformer; - private final @Nullable Class explicitType; - public Property(@NotNull Field field, @NotNull String name, @Nullable Transformer transformer, @Nullable Class explicitType) { + public Property(@NotNull Field field, @Nullable String name, @Nullable Transformer transformer, @Nullable Class type) { this.field = field; - this.name = name; - this.type = explicitType == null ? this.field.getType() : explicitType; + this.name = name == null ? this.field.getName() : name; + this.type = type == null ? this.field.getType() : type; this.transformer = transformer; - this.explicitType = explicitType; } public @NotNull Field getField() { @@ -62,10 +60,6 @@ public Property(@NotNull Field field, @NotNull String name, @Nullable Transforme return transformer; } - public @Nullable Class getExplicitType() { - return explicitType; - } - /** * Sets the field of the object to a specified value. * diff --git a/src/main/java/com/realtimetech/opack/bake/TypeBaker.java b/src/main/java/com/realtimetech/opack/bake/TypeBaker.java index 30d9dfe..1ae3158 100644 --- a/src/main/java/com/realtimetech/opack/bake/TypeBaker.java +++ b/src/main/java/com/realtimetech/opack/bake/TypeBaker.java @@ -23,9 +23,9 @@ package com.realtimetech.opack.bake; import com.realtimetech.opack.Opacker; -import com.realtimetech.opack.annotation.ExplicitType; +import com.realtimetech.opack.annotation.Type; import com.realtimetech.opack.annotation.Ignore; -import com.realtimetech.opack.annotation.SerializedName; +import com.realtimetech.opack.annotation.Name; import com.realtimetech.opack.annotation.Transform; import com.realtimetech.opack.exception.BakeException; import com.realtimetech.opack.transformer.Transformer; @@ -180,7 +180,7 @@ public synchronized boolean unregisterPredefinedTransformer(@NotNull Class ty * @param root whether the element is not super class (whether the element is the root) * @throws BakeException if transformer class object cannot be instantiated */ - void addTransformer(List transformers, AnnotatedElement annotatedElement, boolean root) throws BakeException { + private void addTransformer(List transformers, AnnotatedElement annotatedElement, boolean root) throws BakeException { if (annotatedElement instanceof Class) { Class elementType = (Class) annotatedElement; Class superType = elementType.getSuperclass(); @@ -232,37 +232,37 @@ void addTransformer(List transformers, AnnotatedElement annotatedEl * @return transformers * @throws BakeException if transformer class object cannot be instantiated */ - Transformer[] getTransformer(AnnotatedElement annotatedElement) throws BakeException { + private Transformer[] getTransformer(AnnotatedElement annotatedElement) throws BakeException { List transformers = new LinkedList<>(); this.addTransformer(transformers, annotatedElement, true); return transformers.toArray(new Transformer[0]); } /** - * Returns the explicit type of specific element registered through {@link ExplicitType ExplicitType}. + * Returns the explicit type of specific element registered through {@link Type ExplicitType}. * - * @param annotatedElement the element that annotated {@link ExplicitType ExplicitType} - * @return explicit type + * @param annotatedElement the element that annotated {@link Type ExplicitType} + * @return returns annotated type */ - Class getExplicitType(AnnotatedElement annotatedElement) { - if (annotatedElement.isAnnotationPresent(ExplicitType.class)) { - ExplicitType explicit = annotatedElement.getAnnotation(ExplicitType.class); - return explicit.type(); + private Class getAnnotatedType(AnnotatedElement annotatedElement) { + if (annotatedElement.isAnnotationPresent(Type.class)) { + Type type = annotatedElement.getAnnotation(Type.class); + return type.value(); } return null; } /** - * Returns the serialized type of specific element registered through {@link SerializedName SerializedName}. + * Returns the serialized type of specific element registered through {@link Name SerializedName}. * - * @param annotatedElement the element that annotated {@link ExplicitType ExplicitType} - * @return explicit type + * @param annotatedElement the element that annotated {@link Type ExplicitType} + * @return returns annotated type */ - String getSerializedName(AnnotatedElement annotatedElement) { - if (annotatedElement.isAnnotationPresent(SerializedName.class)) { - SerializedName explicit = annotatedElement.getAnnotation(SerializedName.class); - return explicit.value(); + private String getAnnotatedName(AnnotatedElement annotatedElement) { + if (annotatedElement.isAnnotationPresent(Name.class)) { + Name name = annotatedElement.getAnnotation(Name.class); + return name.value(); } return null; @@ -275,7 +275,7 @@ String getSerializedName(AnnotatedElement annotatedElement) { * @return baked type info * @throws BakeException if a problem occurs during baking a class into {@link BakedType BakedType} */ - @NotNull BakedType bake(@NotNull Class bakeType) throws BakeException { + private @NotNull BakedType bake(@NotNull Class bakeType) throws BakeException { List properties = new LinkedList<>(); Transformer[] transformers = new Transformer[0]; @@ -291,11 +291,10 @@ String getSerializedName(AnnotatedElement annotatedElement) { } Transformer[] fieldTransformers = this.getTransformer(field); - Class explicitType = this.getExplicitType(field); - String serializedName = this.getSerializedName(field); - String fieldName = serializedName == null ? field.getName() : serializedName; + Class type = this.getAnnotatedType(field); + String name = this.getAnnotatedName(field); - properties.add(new BakedType.Property(field, fieldName, fieldTransformers.length > 0 ? fieldTransformers[0] : null, explicitType)); + properties.add(new BakedType.Property(field, name, fieldTransformers.length > 0 ? fieldTransformers[0] : null, type)); } transformers = this.getTransformer(bakeType); diff --git a/src/test/java/com/realtimetech/opack/test/opacker/ClassTransformTest.java b/src/test/java/com/realtimetech/opack/test/opacker/ClassTransformTest.java index 85920a7..d086cce 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/ClassTransformTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/ClassTransformTest.java @@ -23,7 +23,7 @@ package com.realtimetech.opack.test.opacker; import com.realtimetech.opack.Opacker; -import com.realtimetech.opack.annotation.ExplicitType; +import com.realtimetech.opack.annotation.Type; import com.realtimetech.opack.annotation.Transform; import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; @@ -130,16 +130,16 @@ public ClassTransformNoInheritableChild(byte[] bytes) { public static class ClassTransformClass { private ClassTransformInheritable classTransformInheritableValue; private ClassTransformInheritableChild classTransformInheritableChildValue; - @ExplicitType(type = ClassTransformInheritable.class) + @Type(ClassTransformInheritable.class) private Object explicitClassTransformInheritableValue; - @ExplicitType(type = ClassTransformInheritableChild.class) + @Type(ClassTransformInheritableChild.class) private Object explicitClassTransformInheritableChildValue; private ClassTransformNoInheritable classTransformNoInheritableValue; private ClassTransformNoInheritableChild classTransformNoInheritableChildValue; - @ExplicitType(type = ClassTransformNoInheritable.class) + @Type(ClassTransformNoInheritable.class) private Object explicitClassTransformNoInheritableValue; - @ExplicitType(type = ClassTransformNoInheritableChild.class) + @Type(ClassTransformNoInheritableChild.class) private Object explicitClassTransformNoInheritableChildValue; public ClassTransformClass() { diff --git a/src/test/java/com/realtimetech/opack/test/opacker/ExplicitObjectTest.java b/src/test/java/com/realtimetech/opack/test/opacker/ExplicitObjectTest.java index 3fe810e..6b5f9f3 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/ExplicitObjectTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/ExplicitObjectTest.java @@ -23,7 +23,7 @@ package com.realtimetech.opack.test.opacker; import com.realtimetech.opack.Opacker; -import com.realtimetech.opack.annotation.ExplicitType; +import com.realtimetech.opack.annotation.Type; import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; @@ -39,10 +39,10 @@ public class ExplicitObjectTest { public static class ExplicitObjectClass { private Object nullValue; - @ExplicitType(type = ObjectTest.SubObjectClass[].class) + @Type(ObjectTest.SubObjectClass[].class) private Object subObjectArrayValue; - @ExplicitType(type = ObjectTest.SubObjectClass.class) + @Type(ObjectTest.SubObjectClass.class) private Object subObjectWithExplicitValue; public ExplicitObjectClass() { diff --git a/src/test/java/com/realtimetech/opack/test/opacker/ListTest.java b/src/test/java/com/realtimetech/opack/test/opacker/ListTest.java index 485e57b..b1f09fc 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/ListTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/ListTest.java @@ -23,7 +23,7 @@ package com.realtimetech.opack.test.opacker; import com.realtimetech.opack.Opacker; -import com.realtimetech.opack.annotation.ExplicitType; +import com.realtimetech.opack.annotation.Type; import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; @@ -38,7 +38,7 @@ public class ListTest { public static class ListClass { private LinkedList linkedListValue; - @ExplicitType(type = ArrayList.class) + @Type(ArrayList.class) private List arrayListValue; public ListClass() { diff --git a/src/test/java/com/realtimetech/opack/test/opacker/MapTest.java b/src/test/java/com/realtimetech/opack/test/opacker/MapTest.java index 9574bd4..a2a89bf 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/MapTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/MapTest.java @@ -23,7 +23,7 @@ package com.realtimetech.opack.test.opacker; import com.realtimetech.opack.Opacker; -import com.realtimetech.opack.annotation.ExplicitType; +import com.realtimetech.opack.annotation.Type; import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; @@ -36,7 +36,7 @@ public class MapTest { public static class MapClass { private HashMap hashMapValue; - @ExplicitType(type = HashMap.class) + @Type(HashMap.class) private Map mapValue; public MapClass() { diff --git a/src/test/java/com/realtimetech/opack/test/opacker/SerializedNameFieldTest.java b/src/test/java/com/realtimetech/opack/test/opacker/SerializedNameFieldTest.java index fe0b214..5f009e4 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/SerializedNameFieldTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/SerializedNameFieldTest.java @@ -23,8 +23,7 @@ package com.realtimetech.opack.test.opacker; import com.realtimetech.opack.Opacker; -import com.realtimetech.opack.annotation.Ignore; -import com.realtimetech.opack.annotation.SerializedName; +import com.realtimetech.opack.annotation.Name; import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; @@ -33,7 +32,7 @@ public class SerializedNameFieldTest { public static class SerializedNameFieldTestClass { - @SerializedName("newName") + @Name("newName") private String oldName; public SerializedNameFieldTestClass() { diff --git a/src/test/java/com/realtimetech/opack/test/performance/PerformanceClass.java b/src/test/java/com/realtimetech/opack/test/performance/PerformanceClass.java index 5806829..f96f6ac 100644 --- a/src/test/java/com/realtimetech/opack/test/performance/PerformanceClass.java +++ b/src/test/java/com/realtimetech/opack/test/performance/PerformanceClass.java @@ -22,7 +22,6 @@ package com.realtimetech.opack.test.performance; -import com.realtimetech.opack.annotation.SerializedName; import com.realtimetech.opack.test.opacker.*; import java.util.Random;