From 9414b9b3b61d59474a274aab21193391e5b97e52 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Fri, 12 Aug 2016 12:11:57 -0400 Subject: [PATCH] Add static factories for array and parameterized type tokens. These are useful when creating TypeAdapterFactories that delegate to others with more complex types. They also are useful when writing dynamic code that deals with types that cannot be fully reified using the normal subclass technique. --- .../com/google/gson/reflect/TypeToken.java | 15 ++++++++++++ .../google/gson/reflect/TypeTokenTest.java | 23 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/gson/src/main/java/com/google/gson/reflect/TypeToken.java b/gson/src/main/java/com/google/gson/reflect/TypeToken.java index e16e8e6d..3fb8af2b 100644 --- a/gson/src/main/java/com/google/gson/reflect/TypeToken.java +++ b/gson/src/main/java/com/google/gson/reflect/TypeToken.java @@ -302,4 +302,19 @@ public class TypeToken { public static TypeToken get(Class type) { return new TypeToken(type); } + + /** + * Gets type literal for the parameterized type represented by applying {@code typeArguments} to + * {@code rawType}. + */ + public static TypeToken getParameterized(Type rawType, Type... typeArguments) { + return new TypeToken($Gson$Types.newParameterizedTypeWithOwner(null, rawType, typeArguments)); + } + + /** + * Gets type literal for the array type whose elements are all instances of {@code componentType}. + */ + public static TypeToken getArray(Type componentType) { + return new TypeToken($Gson$Types.arrayOf(componentType)); + } } diff --git a/gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java b/gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java index 7dda9d47..40572716 100644 --- a/gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java +++ b/gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java @@ -19,6 +19,7 @@ package com.google.gson.reflect; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.RandomAccess; import java.util.Set; import junit.framework.TestCase; @@ -79,4 +80,26 @@ public final class TypeTokenTest extends TestCase { // listOfSetOfUnknown = listOfSetOfString; // doesn't compile; must be false assertFalse(TypeToken.get(b).isAssignableFrom(a)); } + + public void testArrayFactory() { + TypeToken expectedStringArray = new TypeToken() {}; + assertEquals(expectedStringArray, TypeToken.getArray(String.class)); + + TypeToken expectedListOfStringArray = new TypeToken[]>() {}; + Type listOfString = new TypeToken>() {}.getType(); + assertEquals(expectedListOfStringArray, TypeToken.getArray(listOfString)); + } + + public void testParameterizedFactory() { + TypeToken expectedListOfString = new TypeToken>() {}; + assertEquals(expectedListOfString, TypeToken.getParameterized(List.class, String.class)); + + TypeToken expectedMapOfStringToString = new TypeToken>() {}; + assertEquals(expectedMapOfStringToString, TypeToken.getParameterized(Map.class, String.class, String.class)); + + TypeToken expectedListOfListOfListOfString = new TypeToken>>>() {}; + Type listOfString = TypeToken.getParameterized(List.class, String.class).getType(); + Type listOfListOfString = TypeToken.getParameterized(List.class, listOfString).getType(); + assertEquals(expectedListOfListOfListOfString, TypeToken.getParameterized(List.class, listOfListOfString)); + } }