Skip to content

Commit

Permalink
Rename annotations(ExplicitType, SerializedName)
Browse files Browse the repository at this point in the history
  • Loading branch information
devjeonghwan committed Apr 19, 2022
1 parent 531c624 commit fce1d67
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 55 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> listField;

/*
This field is serialized/deserialized to explicit type `ArrayList` instead of ambiguous field type `List`
*/
@Name("newFieldName")
private String oldFieldName;
}
```
#### 2. Field Transformer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
12 changes: 3 additions & 9 deletions src/main/java/com/realtimetech/opack/bake/BakedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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.
*
Expand Down
45 changes: 22 additions & 23 deletions src/main/java/com/realtimetech/opack/bake/TypeBaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Transformer> transformers, AnnotatedElement annotatedElement, boolean root) throws BakeException {
private void addTransformer(List<Transformer> transformers, AnnotatedElement annotatedElement, boolean root) throws BakeException {
if (annotatedElement instanceof Class) {
Class<?> elementType = (Class<?>) annotatedElement;
Class<?> superType = elementType.getSuperclass();
Expand Down Expand Up @@ -232,37 +232,37 @@ void addTransformer(List<Transformer> 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<Transformer> 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;
Expand All @@ -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<BakedType.Property> properties = new LinkedList<>();
Transformer[] transformers = new Transformer[0];

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,7 +38,7 @@ public class ListTest {
public static class ListClass {
private LinkedList<String> linkedListValue;

@ExplicitType(type = ArrayList.class)
@Type(ArrayList.class)
private List<String> arrayListValue;

public ListClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,7 +36,7 @@ public class MapTest {
public static class MapClass {
private HashMap<String, String> hashMapValue;

@ExplicitType(type = HashMap.class)
@Type(HashMap.class)
private Map<String, String> mapValue;

public MapClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +32,7 @@

public class SerializedNameFieldTest {
public static class SerializedNameFieldTestClass {
@SerializedName("newName")
@Name("newName")
private String oldName;

public SerializedNameFieldTestClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fce1d67

Please sign in to comment.