Skip to content

Commit

Permalink
Merge pull request #34 from devjeonghwan/main
Browse files Browse the repository at this point in the history
Add class transformer
  • Loading branch information
devjeonghwan authored Sep 13, 2023
2 parents f203393 + 390187f commit a9a31b5
Show file tree
Hide file tree
Showing 49 changed files with 424 additions and 247 deletions.
30 changes: 23 additions & 7 deletions src/main/java/com/realtimetech/opack/Opacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.realtimetech.opack.transformer.impl.map.MapTransformer;
import com.realtimetech.opack.transformer.impl.map.WrapMapTransformer;
import com.realtimetech.opack.transformer.impl.path.PathTransformer;
import com.realtimetech.opack.transformer.impl.reflection.ClassTransformer;
import com.realtimetech.opack.transformer.impl.time.CalendarTransformer;
import com.realtimetech.opack.transformer.impl.time.DateTransformer;
import com.realtimetech.opack.transformer.impl.time.java8.LocalDateTimeTransformer;
Expand Down Expand Up @@ -68,6 +69,8 @@ public static class Builder {
private boolean enableConvertEnumToOrdinal;
private boolean enableConvertRecursiveDependencyToNull;

private @Nullable ClassLoader classTransformerClassLoader;

public Builder() {
this.valueStackInitialSize = 512;
this.contextStackInitialSize = 128;
Expand All @@ -76,6 +79,8 @@ public Builder() {
this.enableWrapMapElementType = false;
this.enableConvertEnumToOrdinal = false;
this.enableConvertRecursiveDependencyToNull = false;

this.classTransformerClassLoader = this.getClass().getClassLoader();
}

public Builder setValueStackInitialSize(int valueStackInitialSize) {
Expand Down Expand Up @@ -108,8 +113,13 @@ public Builder setEnableConvertRecursiveDependencyToNull(boolean enableConvertRe
return this;
}

public Builder setClassTransformerClassLoader(@Nullable ClassLoader classTransformerClassLoader) {
this.classTransformerClassLoader = classTransformerClassLoader;
return this;
}

/**
* Create the {@link Opacker Opacker} through this builder.
* Create the {@link Opacker Opacker} through this builder
*
* @return created opacker
*/
Expand Down Expand Up @@ -171,6 +181,12 @@ private Opacker(@NotNull Builder builder) {
this.typeBaker.registerPredefinedTransformer(LocalDate.class, LocalDateTransformer.class, true);
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);
}
} catch (InstantiationException exception) {
throw new IllegalStateException(exception);
}
Expand All @@ -184,7 +200,7 @@ private Opacker(@NotNull Builder builder) {
}

/**
* Serializes the object to {@link OpackValue OpackValue}.
* Serializes the object to {@link OpackValue OpackValue}
*
* @param object the object to be serialized
* @return opack value
Expand Down Expand Up @@ -213,7 +229,7 @@ private Opacker(@NotNull Builder builder) {
}

/**
* Store information needed for serialization in stacks.
* Store information needed for serialization in stacks
*
* @param baseType the class of object to be serialized
* @param object the object to be serialized
Expand Down Expand Up @@ -306,7 +322,7 @@ private Opacker(@NotNull Builder builder) {
}

/**
* Serialize the elements of each opack value in the stack. (OpackObject: fields, OpackArray element : array elements)
* 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
*/
Expand Down Expand Up @@ -360,7 +376,7 @@ private void executeSerializeStack(int endOfStack) throws SerializeException {
}

/**
* Deserializes the opack value to object of the target class.
* Deserializes the opack value to object of the target class
*
* @param type the target class
* @param opackValue the opack value to be deserialized
Expand Down Expand Up @@ -397,7 +413,7 @@ private void executeSerializeStack(int endOfStack) throws SerializeException {
}

/**
* Store information needed for deserialization in stacks.
* Store information needed for deserialization in stacks
*
* @param goalType the class of object to be deserialized
* @param object the object to be deserialized
Expand Down Expand Up @@ -520,7 +536,7 @@ private void executeSerializeStack(int endOfStack) throws SerializeException {
}

/**
* Deserialize the elements of each opack value in the stack. (OpackObject element : fields, OpackArray element : array elements)
* 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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.lang.annotation.Target;

/**
* Fields annotated with @Ignore will not be serialized and deserialized.
* Fields annotated with @Ignore will not be serialized and deserialized
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/realtimetech/opack/annotation/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.lang.annotation.Target;

/**
* Fields annotated with @Name("new name") will be serialized and deserialized to name.
* Fields annotated with @Name("new name") will be serialized and deserialized to name
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.lang.annotation.Target;

/**
* Types/Fields annotated with @Transform(transformer=xxxTransformer.class, inheritable= true | false) will serialize and deserialize as transformed by transformer.
* Types/Fields annotated with @Transform(transformer=xxxTransformer.class, inheritable= true | false) will serialize and deserialize as transformed by transformer
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({
Expand All @@ -40,15 +40,13 @@
})
public @interface Transform {
/**
* Return the transformer for annotated class or field.
* Return the transformer for annotated class or field
*
* @return the transformer class
*/
@NotNull Class<? extends Transformer> transformer();

/**
* Returns whether transformer is inheritable.
*
* @return true if transformer is inheritable
*/
boolean inheritable() default false;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/realtimetech/opack/annotation/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.lang.annotation.Target;

/**
* Fields annotated with @Type(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({
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/realtimetech/opack/bake/BakedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public boolean isWithType() {
}

/**
* Sets the field of the object to a specified value.
* Sets the field of the object to a specified value
*
* @param object the object whose field should be modified
* @param value the new value for the field of object being modified
Expand All @@ -82,7 +82,7 @@ public void set(@NotNull Object object, @Nullable Object value) throws IllegalAc
}

/**
* Returns the field value extracted from the object.
* Returns the field value extracted from the object
*
* @param object the object to extract the field value
* @return field value
Expand Down
51 changes: 39 additions & 12 deletions src/main/java/com/realtimetech/opack/bake/TypeBaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
import java.util.List;

public final class TypeBaker {
static final class PredefinedTransformer {
public static final class PredefinedTransformer {
private final @NotNull Transformer transformer;
private final boolean inheritable;

/**
* Constructs the PredefinedTransformer.
* Constructs the PredefinedTransformer
*
* @param transformer the transformer to be registered
* @param inheritable true if the transformer is inheritable
Expand All @@ -70,7 +70,7 @@ public boolean isInheritable() {
private final @NotNull HashMap<@NotNull Class<?>, @NotNull List<@NotNull PredefinedTransformer>> predefinedTransformerMap;

/**
* Constructs an TypeBaker with the opacker.
* Constructs an TypeBaker with the opacker
*
* @param opacker the opacker
*/
Expand All @@ -84,7 +84,7 @@ public TypeBaker(@NotNull Opacker opacker) {
}

/**
* Returns predefined transformers for a specific class.
* Returns predefined transformers for a specific class
*
* @param type the class to be the target
* @return found predefined transformers
Expand All @@ -99,6 +99,33 @@ public TypeBaker(@NotNull Opacker opacker) {
return predefinedTransformers.toArray(new PredefinedTransformer[0]);
}

/**
* Register a predefined transformer for the specific class with transformer instance
*
* @param type the class to be the target
* @param transformer the transformer to register
* @param inheritable whether transformer is inheritable
* @return true if the predefined transformer registration is successful
*/
public synchronized boolean registerPredefinedTransformer(@NotNull Class<?> type, @NotNull Transformer transformer, boolean inheritable) {
if (!this.predefinedTransformerMap.containsKey(type)) {
this.predefinedTransformerMap.put(type, new LinkedList<>());
}

Class<? extends Transformer> transformerType = transformer.getClass();
List<PredefinedTransformer> predefinedTransformers = this.predefinedTransformerMap.get(type);

for (PredefinedTransformer predefinedTransformer : predefinedTransformers) {
if (predefinedTransformer.getTransformer().getClass() == transformerType) {
return false;
}
}

predefinedTransformers.add(new PredefinedTransformer(transformer, inheritable));

return true;
}

/**
* Calls {@code registerPredefinedTransformer(type, transformerClass, false);}
*
Expand All @@ -112,7 +139,7 @@ public boolean registerPredefinedTransformer(@NotNull Class<?> type, @NotNull Cl
}

/**
* Register a predefined transformer for the specific class.
* Register a predefined transformer for the specific class
*
* @param type the class to be the target
* @param transformerType the predefined transformer to register
Expand Down Expand Up @@ -140,7 +167,7 @@ public synchronized boolean registerPredefinedTransformer(@NotNull Class<?> type
}

/**
* Unregister a predefined transformer for the specific class.
* Unregister a predefined transformer for the specific class
*
* @param type the targeted type class
* @param transformerType the predefined transformer to unregister
Expand Down Expand Up @@ -171,7 +198,7 @@ public synchronized boolean unregisterPredefinedTransformer(@NotNull Class<?> ty
}

/**
* Add transformers of the element to the transformer list.
* Add transformers of the element to the transformer list
*
* @param transformers the transformer list for add
* @param annotatedElement the element to be targeted
Expand Down Expand Up @@ -224,7 +251,7 @@ private void addTransformer(@NotNull List<@NotNull Transformer> transformers, @N
}

/**
* Returns transformers registered through {@link Transform Transform} annotation.
* Returns transformers registered through {@link Transform Transform} annotation
*
* @param annotatedElement the element that annotated {@link Transform Transform}
* @return transformers
Expand All @@ -239,7 +266,7 @@ private void addTransformer(@NotNull List<@NotNull Transformer> transformers, @N
}

/**
* Returns the explicit type of specific element registered through {@link Type ExplicitType}.
* Returns the explicit type of specific element registered through {@link Type ExplicitType}
*
* @param annotatedElement the element that annotated {@link Type ExplicitType}
* @return returns annotated type
Expand All @@ -254,7 +281,7 @@ private void addTransformer(@NotNull List<@NotNull Transformer> transformers, @N
}

/**
* Returns the serialized type of specific element registered through {@link Name SerializedName}.
* Returns the serialized type of specific element registered through {@link Name SerializedName}
*
* @param annotatedElement the element that annotated {@link Type ExplicitType}
* @return returns annotated type
Expand All @@ -269,7 +296,7 @@ private void addTransformer(@NotNull List<@NotNull Transformer> transformers, @N
}

/**
* Bake the class into {@link BakedType BakedType}.
* Bake the class into {@link BakedType BakedType}
*
* @param bakeType the type to bake
* @return baked type info
Expand Down Expand Up @@ -305,7 +332,7 @@ private void addTransformer(@NotNull List<@NotNull Transformer> transformers, @N
}

/**
* Returns BakedType for target class.
* Returns BakedType for target class
*
* @param bakeType the class to be baked
* @return class info
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/realtimetech/opack/codec/OpackCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

public abstract class OpackCodec<I, O> {
/**
* Writes a code that encodes opack value by overriding this.
* Writes a code that encodes opack value by overriding this
*
* @param output the output to encode
* @param opackValue the opack value to encode
Expand All @@ -40,7 +40,7 @@ public abstract class OpackCodec<I, O> {
protected abstract void doEncode(@NotNull O output, @NotNull OpackValue opackValue) throws IOException;

/**
* Writes a code that decodes the value encoded by overriding this.
* Writes a code that decodes the value encoded by overriding this
*
* @param input the input to decode
* @return decoded value
Expand All @@ -49,7 +49,7 @@ public abstract class OpackCodec<I, O> {
protected abstract @NotNull OpackValue doDecode(@NotNull I input) throws IOException;

/**
* Encodes the opack value through a specific codec.
* Encodes the opack value through a specific codec
*
* @param output the output to encode
* @param opackValue the opack value to encode
Expand All @@ -64,7 +64,7 @@ public final synchronized void encode(@NotNull O output, @NotNull OpackValue opa
}

/**
* Decodes the value encoded through a specific codec.
* Decodes the value encoded through a specific codec
*
* @param input the input to decode
* @return decoded value
Expand Down
Loading

0 comments on commit a9a31b5

Please sign in to comment.