Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a check for use of type variable to TypeToken constructor. #1222

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions gson/src/main/java/com/google/gson/reflect/TypeToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@

package com.google.gson.reflect;

import com.google.gson.internal.$Gson$Types;
import com.google.gson.internal.$Gson$Preconditions;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.internal.$Gson$Preconditions;
import com.google.gson.internal.$Gson$Types;

/**
* Represents a generic type {@code T}. Java doesn't yet provide a way to
* represent generic types, so this class does. Forces clients to create a
Expand Down Expand Up @@ -60,6 +61,10 @@ public class TypeToken<T> {
@SuppressWarnings("unchecked")
protected TypeToken() {
this.type = getSuperclassTypeParameter(getClass());
if(usesTypeVariable(type)) {
throw new RuntimeException("Can not use type variables.");
}

this.rawType = (Class<? super T>) $Gson$Types.getRawType(type);
this.hashCode = type.hashCode();
}
Expand All @@ -85,6 +90,22 @@ static Type getSuperclassTypeParameter(Class<?> subclass) {
}
ParameterizedType parameterized = (ParameterizedType) superclass;
return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
}

static boolean usesTypeVariable(Type type) {
if(type instanceof TypeVariable) {
return true;
} else if(type instanceof ParameterizedType) {
for(Type parameterType : ((ParameterizedType) type).getActualTypeArguments()) {
if(usesTypeVariable(parameterType)) {
return true;
}
}
} else if(type instanceof GenericArrayType) {
return usesTypeVariable(((GenericArrayType) type).getGenericComponentType());
}

return false;
}

/**
Expand Down
30 changes: 30 additions & 0 deletions gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,35 @@ public void testParameterizedFactory() {
Type listOfString = TypeToken.getParameterized(List.class, String.class).getType();
Type listOfListOfString = TypeToken.getParameterized(List.class, listOfString).getType();
assertEquals(expectedListOfListOfListOfString, TypeToken.getParameterized(List.class, listOfListOfString));
}

public <T> void testRejectsTypeVariable() {
try {
new TypeToken<T>() {};
} catch(RuntimeException e) {
return;
}

fail("Expected exception");
}

public <T> void testRejectsNestedTypeVariable() {
try {
new TypeToken<List<T>>() {};
} catch(RuntimeException e) {
return;
}

fail("Expected exception");
}

public <T> void testRejectsGenericArrayWithTypeVariable() {
try {
new TypeToken<List<T>[]>() {};
} catch(RuntimeException e) {
return;
}

fail("Expected exception");
}
}