Skip to content

Commit 1b9c565

Browse files
authored
Approximate raw type with bound of type variable
Fixes google#2563
1 parent b5bc7b9 commit 1b9c565

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

gson/src/main/java/com/google/gson/internal/$Gson$Types.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,14 @@ public static Class<?> getRawType(Type type) {
145145
Type componentType = ((GenericArrayType) type).getGenericComponentType();
146146
return Array.newInstance(getRawType(componentType), 0).getClass();
147147

148-
} else if (type instanceof TypeVariable) {
149-
// we could use the variable's bounds, but that won't work if there are multiple.
150-
// having a raw type that's more general than necessary is okay
151-
return Object.class;
152-
148+
} else if (type instanceof TypeVariable<?>) {
149+
TypeVariable<?> typeVariable = (TypeVariable<?>) type;
150+
// approximate the raw type with the bound of type type variable, if there are
151+
// multiple bounds, all we can do is picking the first one
152+
Type[] bounds = typeVariable.getBounds();
153+
// Javadoc specifies some bound is always returned, Object if not specified
154+
assert bounds.length > 0;
155+
return getRawType(bounds[0]);
153156
} else if (type instanceof WildcardType) {
154157
Type[] bounds = ((WildcardType) type).getUpperBounds();
155158
// Currently the JLS only permits one bound for wildcards so using first bound is safe

0 commit comments

Comments
 (0)