Skip to content

Commit

Permalink
Approximate raw type with bound of type variable
Browse files Browse the repository at this point in the history
  • Loading branch information
msevcenko authored Dec 7, 2023
1 parent b5bc7b9 commit 1b9c565
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions gson/src/main/java/com/google/gson/internal/$Gson$Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,14 @@ public static Class<?> getRawType(Type type) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
return Array.newInstance(getRawType(componentType), 0).getClass();

} else if (type instanceof TypeVariable) {
// we could use the variable's bounds, but that won't work if there are multiple.
// having a raw type that's more general than necessary is okay
return Object.class;

} else if (type instanceof TypeVariable<?>) {
TypeVariable<?> typeVariable = (TypeVariable<?>) type;
// approximate the raw type with the bound of type type variable, if there are
// multiple bounds, all we can do is picking the first one
Type[] bounds = typeVariable.getBounds();
// Javadoc specifies some bound is always returned, Object if not specified
assert bounds.length > 0;
return getRawType(bounds[0]);
} else if (type instanceof WildcardType) {
Type[] bounds = ((WildcardType) type).getUpperBounds();
// Currently the JLS only permits one bound for wildcards so using first bound is safe
Expand Down

0 comments on commit 1b9c565

Please sign in to comment.