Skip to content

Commit

Permalink
Suppress serialization warnings. (#2502)
Browse files Browse the repository at this point in the history
Recent versions of javac emit a warning if a serializable class has
a non-transient instance field with a declared type that does not
inherit `Serializable`. In this context, we know that the actual
values will always be serializable.
  • Loading branch information
eamonnmcmanus authored Sep 30, 2023
1 parent 9807705 commit 45acc4d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gson/src/main/java/com/google/gson/internal/$Gson$Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,16 @@ public static boolean requiresOwnerType(Type rawType) {
return false;
}

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

public ParameterizedTypeImpl(Type ownerType, Type rawType, Type... typeArguments) {
Expand Down Expand Up @@ -564,6 +571,7 @@ private static int hashCodeOrZero(Object o) {
}

private static final class GenericArrayTypeImpl implements GenericArrayType, Serializable {
@SuppressWarnings("serial")
private final Type componentType;

public GenericArrayTypeImpl(Type componentType) {
Expand Down Expand Up @@ -598,7 +606,9 @@ public GenericArrayTypeImpl(Type componentType) {
* is set, the upper bound must be Object.class.
*/
private static final class WildcardTypeImpl implements WildcardType, Serializable {
@SuppressWarnings("serial")
private final Type upperBound;
@SuppressWarnings("serial")
private final Type lowerBound;

public WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) {
Expand Down

0 comments on commit 45acc4d

Please sign in to comment.