Skip to content

Commit 0189b72

Browse files
Remove Serializable from internal Type implementation classes. (#3011)
* Remove `Serializable` from internal `Type` implementation classes. The nested classes `ParameterizedTypeImpl`, `GenericArrayTypeImpl`, and `WildcardTypeImpl` in `GsonTypes` are implementations of the corresponding types (without `Impl`) in `java.lang.reflect`. For some reason, they have always implemented `Serializable`. They are even documented that way, though the documentation is not published since `GsonTypes` is an internal implementation class. The implementations of these interfaces that are returned by `java.lang.reflect` methods such as `Method.getGenericReturnType()` do *not* implement `Serializable` so it seems gratuitous for Gson's implementations to do so. Additionally, `Serializable` classes can potentially participate in exploits. I think it is highly unlikely that there is any practical exploit using these classes, but if we can avoid even having to ask the question then we should. * Remove unnecessary serialization stuff.
1 parent f4d371d commit 0189b72

1 file changed

Lines changed: 6 additions & 23 deletions

File tree

gson/src/main/java/com/google/gson/internal/GsonTypes.java

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static java.util.Objects.requireNonNull;
2020

21-
import java.io.Serializable;
2221
import java.lang.reflect.Array;
2322
import java.lang.reflect.GenericArrayType;
2423
import java.lang.reflect.GenericDeclaration;
@@ -52,7 +51,7 @@ private GsonTypes() {
5251
* Returns a new parameterized type, applying {@code typeArguments} to {@code rawType} and
5352
* enclosed by {@code ownerType}.
5453
*
55-
* @return a {@link java.io.Serializable serializable} parameterized type.
54+
* @return a parameterized type.
5655
*/
5756
public static ParameterizedType newParameterizedTypeWithOwner(
5857
Type ownerType, Class<?> rawType, Type... typeArguments) {
@@ -62,7 +61,7 @@ public static ParameterizedType newParameterizedTypeWithOwner(
6261
/**
6362
* Returns an array type whose elements are all instances of {@code componentType}.
6463
*
65-
* @return a {@link java.io.Serializable serializable} generic array type.
64+
* @return a generic array type.
6665
*/
6766
public static GenericArrayType arrayOf(Type componentType) {
6867
return new GenericArrayTypeImpl(componentType);
@@ -100,7 +99,7 @@ public static WildcardType supertypeOf(Type bound) {
10099

101100
/**
102101
* Returns a type that is functionally equal but not necessarily equal according to {@link
103-
* Object#equals(Object) Object.equals()}. The returned type is {@link java.io.Serializable}.
102+
* Object#equals(Object) Object.equals()}.
104103
*/
105104
public static Type canonicalize(Type type) {
106105
if (type instanceof Class) {
@@ -506,18 +505,11 @@ public static boolean requiresOwnerType(Type rawType) {
506505
return false;
507506
}
508507

509-
// Here and below we put @SuppressWarnings("serial") on fields of type `Type`. Recent Java
510-
// compilers complain that the declared type is not Serializable. But in this context we go out of
511-
// our way to ensure that the Type in question is either Class (which is serializable) or one of
512-
// the nested Type implementations here (which are also serializable).
513-
private static final class ParameterizedTypeImpl implements ParameterizedType, Serializable {
514-
@SuppressWarnings("serial")
508+
private static final class ParameterizedTypeImpl implements ParameterizedType {
515509
private final Type ownerType;
516510

517-
@SuppressWarnings("serial")
518511
private final Type rawType;
519512

520-
@SuppressWarnings("serial")
521513
private final Type[] typeArguments;
522514

523515
ParameterizedTypeImpl(Type ownerType, Class<?> rawType, Type... typeArguments) {
@@ -584,12 +576,9 @@ public String toString() {
584576
}
585577
return stringBuilder.append(">").toString();
586578
}
587-
588-
private static final long serialVersionUID = 0;
589579
}
590580

591-
private static final class GenericArrayTypeImpl implements GenericArrayType, Serializable {
592-
@SuppressWarnings("serial")
581+
private static final class GenericArrayTypeImpl implements GenericArrayType {
593582
private final Type componentType;
594583

595584
GenericArrayTypeImpl(Type componentType) {
@@ -616,8 +605,6 @@ public int hashCode() {
616605
public String toString() {
617606
return typeToString(componentType) + "[]";
618607
}
619-
620-
private static final long serialVersionUID = 0;
621608
}
622609

623610
/**
@@ -626,11 +613,9 @@ public String toString() {
626613
* https://bugs.openjdk.java.net/browse/JDK-8250660. If a lower bound is set, the upper bound must
627614
* be Object.class.
628615
*/
629-
private static final class WildcardTypeImpl implements WildcardType, Serializable {
630-
@SuppressWarnings("serial")
616+
private static final class WildcardTypeImpl implements WildcardType {
631617
private final Type upperBound;
632618

633-
@SuppressWarnings("serial")
634619
private final Type lowerBound;
635620

636621
WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) {
@@ -690,7 +675,5 @@ public String toString() {
690675
return "? extends " + typeToString(upperBound);
691676
}
692677
}
693-
694-
private static final long serialVersionUID = 0;
695678
}
696679
}

0 commit comments

Comments
 (0)