Skip to content

Commit

Permalink
Merge pull request #29 from devjeonghwan/add-with-type
Browse files Browse the repository at this point in the history
Add `WithType` annotation
  • Loading branch information
devjeonghwan authored Aug 21, 2023
2 parents 6f2f816 + ab54aef commit b70e7a1
Show file tree
Hide file tree
Showing 22 changed files with 402 additions and 277 deletions.
25 changes: 19 additions & 6 deletions src/main/java/com/realtimetech/opack/Opacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");

Expand Down Expand Up @@ -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);
}
Expand All @@ -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> T deserialize(@NotNull Class<T> type, @NotNull OpackValue opackValue) throws DeserializeException {
public synchronized <T> @Nullable T deserialize(@NotNull Class<T> 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;
Expand Down Expand Up @@ -399,7 +404,7 @@ public synchronized <T> T deserialize(@NotNull Class<T> 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);

Expand All @@ -421,6 +426,14 @@ public synchronized <T> T deserialize(@NotNull Class<T> type, @NotNull OpackValu
}
}

if (withType) {
object = TypeWrapper.unwrapObject(this, object);

if (object == null) {
return null;
}
}

/*
Early stopping
*/
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/realtimetech/opack/annotation/WithType.java
Original file line number Diff line number Diff line change
@@ -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 {
}
8 changes: 7 additions & 1 deletion src/main/java/com/realtimetech/opack/bake/BakedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/realtimetech/opack/bake/TypeBaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

Loading

0 comments on commit b70e7a1

Please sign in to comment.