Skip to content

Commit

Permalink
Enforce rawType to be a Class in ParameterizedTypeImpl
Browse files Browse the repository at this point in the history
Enforce rawType to be a Class in ParameterizedTypeImpl

Enforce rawType to be a Class in ParameterizedTypeImpl

Enforce rawType to be a Class in ParameterizedTypeImpl

Enforce rawType to be a Class in ParameterizedTypeImpl

Enforce rawType to be a Class in ParameterizedTypeImpl

Enforce rawType to be a Class in ParameterizedTypeImpl
  • Loading branch information
panic08 committed Oct 13, 2024
1 parent 7e98ebd commit c83c6fd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
12 changes: 6 additions & 6 deletions gson/src/main/java/com/google/gson/internal/$Gson$Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class $Gson$Types {
* @return a {@link java.io.Serializable serializable} parameterized type.
*/
public static ParameterizedType newParameterizedTypeWithOwner(
Type ownerType, Type rawType, Type... typeArguments) {
Type ownerType, Class<?> rawType, Type... typeArguments) {
return new ParameterizedTypeImpl(ownerType, rawType, typeArguments);
}

Expand Down Expand Up @@ -112,7 +112,7 @@ public static Type canonicalize(Type type) {
} else if (type instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType) type;
return new ParameterizedTypeImpl(
p.getOwnerType(), p.getRawType(), p.getActualTypeArguments());
p.getOwnerType(), (Class<?>) p.getRawType(), p.getActualTypeArguments());

} else if (type instanceof GenericArrayType) {
GenericArrayType g = (GenericArrayType) type;
Expand Down Expand Up @@ -411,7 +411,8 @@ private static Type resolve(

toResolve =
ownerChanged || argsChanged
? newParameterizedTypeWithOwner(newOwnerType, original.getRawType(), args)
? newParameterizedTypeWithOwner(
newOwnerType, (Class<?>) original.getRawType(), args)
: original;
break;

Expand Down Expand Up @@ -519,10 +520,9 @@ private static final class ParameterizedTypeImpl implements ParameterizedType, S
@SuppressWarnings("serial")
private final Type[] typeArguments;

public ParameterizedTypeImpl(Type ownerType, Type rawType, Type... typeArguments) {
// TODO: Should this enforce that rawType is a Class? See JDK implementation of
// the ParameterizedType interface and https://bugs.openjdk.org/browse/JDK-8250659
public ParameterizedTypeImpl(Type ownerType, Class<?> rawType, Type... typeArguments) {
requireNonNull(rawType);

if (ownerType == null && requiresOwnerType(rawType)) {
throw new IllegalArgumentException("Must specify owner type for " + rawType);
}
Expand Down
12 changes: 7 additions & 5 deletions gson/src/main/java/com/google/gson/reflect/TypeToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,7 @@ public static <T> TypeToken<T> get(Class<T> type) {
* <p>If {@code rawType} is a non-generic class and no type arguments are provided, this method
* simply delegates to {@link #get(Class)} and creates a {@code TypeToken(Class)}.
*
* @throws IllegalArgumentException If {@code rawType} is not of type {@code Class}, or if the
* type arguments are invalid for the raw type
* @throws IllegalArgumentException If the type arguments are invalid for the raw type
*/
public static TypeToken<?> getParameterized(Type rawType, Type... typeArguments) {
Objects.requireNonNull(rawType);
Expand All @@ -392,7 +391,9 @@ public static TypeToken<?> getParameterized(Type rawType, Type... typeArguments)
// See also https://bugs.openjdk.org/browse/JDK-8250659
throw new IllegalArgumentException("rawType must be of type Class, but was " + rawType);
}

Class<?> rawClass = (Class<?>) rawType;

TypeVariable<?>[] typeVariables = rawClass.getTypeParameters();

int expectedArgsCount = typeVariables.length;
Expand All @@ -408,14 +409,14 @@ public static TypeToken<?> getParameterized(Type rawType, Type... typeArguments)

// For legacy reasons create a TypeToken(Class) if the type is not generic
if (typeArguments.length == 0) {
return get(rawClass);
return get((Class<?>) rawType);
}

// Check for this here to avoid misleading exception thrown by ParameterizedTypeImpl
if ($Gson$Types.requiresOwnerType(rawType)) {
throw new IllegalArgumentException(
"Raw type "
+ rawClass.getName()
+ ((Class<?>) rawType).getName()
+ " is not supported because it requires specifying an owner type");
}

Expand All @@ -440,7 +441,8 @@ public static TypeToken<?> getParameterized(Type rawType, Type... typeArguments)
}
}

return new TypeToken<>($Gson$Types.newParameterizedTypeWithOwner(null, rawType, typeArguments));
return new TypeToken<>(
$Gson$Types.newParameterizedTypeWithOwner(null, rawClass, typeArguments));
}

/**
Expand Down
8 changes: 2 additions & 6 deletions gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -187,6 +182,7 @@ public void testParameterizedFactory_Invalid() {
() -> TypeToken.getParameterized(List.class, new Type[] {null}));

GenericArrayType arrayType = (GenericArrayType) TypeToken.getArray(String.class).getType();

IllegalArgumentException e =
assertThrows(
IllegalArgumentException.class,
Expand Down

0 comments on commit c83c6fd

Please sign in to comment.