Throwing a helpful error in cases where the user forgot to use the TypeToken idiom.

This commit is contained in:
Inderjeet Singh 2009-04-01 22:24:10 +00:00
parent 5c6d5a0d11
commit 84f0ddd6bb

View File

@ -80,6 +80,7 @@ final class TypeInfoFactory {
} }
} }
} else if (typeToEvaluate instanceof TypeVariable) { } else if (typeToEvaluate instanceof TypeVariable) {
if (parentType instanceof ParameterizedType) {
// The class definition has the actual types used for the type variables. // The class definition has the actual types used for the type variables.
// Find the matching actual type for the Type Variable used for the field. // Find the matching actual type for the Type Variable used for the field.
// For example, class Foo<A> { A a; } // For example, class Foo<A> { A a; }
@ -92,6 +93,11 @@ final class TypeInfoFactory {
int indexOfActualTypeArgument = getIndex(classTypeVariables, fieldTypeVariable); int indexOfActualTypeArgument = getIndex(classTypeVariables, fieldTypeVariable);
Type[] actualTypeArguments = objParameterizedType.getActualTypeArguments(); Type[] actualTypeArguments = objParameterizedType.getActualTypeArguments();
return actualTypeArguments[indexOfActualTypeArgument]; return actualTypeArguments[indexOfActualTypeArgument];
} else {
throw new UnsupportedOperationException("Expecting parameterized type, got " + parentType
+ ".\n Are you missing the use of TypeToken idiom?\n See "
+ "http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Gener");
}
} else if (typeToEvaluate instanceof WildcardType) { } else if (typeToEvaluate instanceof WildcardType) {
WildcardType castedType = (WildcardType) typeToEvaluate; WildcardType castedType = (WildcardType) typeToEvaluate;
return getActualType(castedType.getUpperBounds()[0], parentType, rawParentClass); return getActualType(castedType.getUpperBounds()[0], parentType, rawParentClass);