Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions sdk/src/main/java/io/dapr/utils/TypeRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public TypeRef() {
/**
* Constructor for reflection.
*
* @param clazz Class type to be referenced.
* @param type Type to be referenced.
*/
private TypeRef(Class<T> clazz) {
this.type = clazz;
private TypeRef(Type type) {
this.type = type;
}

/**
Expand Down Expand Up @@ -118,4 +118,19 @@ public static <T> TypeRef<T> get(Class<T> clazz) {

return new TypeRef<T>(clazz) {};
}

/**
* Creates a reference to a given class type.
* @param type Type to be referenced.
* @param <T> Type to be referenced.
* @return Class type reference.
*/
public static <T> TypeRef<T> get(Type type) {
if (type instanceof Class) {
Class clazz = (Class) type;
return get(clazz);
}

return new TypeRef<T>(type) {};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
Expand Down Expand Up @@ -234,6 +235,17 @@ public void serializeObjectTest() {
} catch (IOException exception) {
fail(exception.getMessage());
}

try {
serializedValue = SERIALIZER.serialize(obj);
assertNotNull(serializedValue);
Type t = MyObjectTestToSerialize.class;
TypeRef<MyObjectTestToSerialize> tr = TypeRef.get(t);
MyObjectTestToSerialize deserializedValue = SERIALIZER.deserialize(serializedValue, tr);
assertEquals(obj, deserializedValue);
} catch (IOException exception) {
fail(exception.getMessage());
}
}

@Test
Expand Down Expand Up @@ -425,6 +437,16 @@ public void deserializeArrayObjectTest() {
} catch (IOException exception) {
fail(exception.getMessage());
}

try {
TypeRef<List<MyObjectTestToSerialize>> tr1 = new TypeRef<List<MyObjectTestToSerialize>>(){};
Type t = tr1.getType();
TypeRef<?> tr = TypeRef.get(t);
result = (List<MyObjectTestToSerialize>) SERIALIZER.deserialize(jsonToDeserialize.getBytes(), tr);
assertEquals("The expected value is different than the actual result", expectedResult, result.get(0));
} catch (IOException exception) {
fail(exception.getMessage());
}
}

@Test
Expand Down