Bump Gson to 2.13.2 (#4490)

This commit is contained in:
Glavo 2025-09-16 14:55:59 +08:00 committed by GitHub
parent 831a8a9bb8
commit e4bc8f48c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 69 additions and 92 deletions

View File

@ -17,9 +17,9 @@
*/ */
package org.jackhuang.hmcl.util; package org.jackhuang.hmcl.util;
import static com.google.gson.internal.GsonPreconditions.checkArgument; import org.jetbrains.annotations.NotNull;
import static java.util.Objects.requireNonNull;
import java.io.Serial;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType; import java.lang.reflect.GenericArrayType;
@ -51,6 +51,12 @@ public final class TypeUtils {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
private static void checkArgument(boolean condition) {
if (!condition) {
throw new IllegalArgumentException();
}
}
/** /**
* Returns a new parameterized type, applying {@code typeArguments} to {@code rawType} and * Returns a new parameterized type, applying {@code typeArguments} to {@code rawType} and
* enclosed by {@code ownerType}. * enclosed by {@code ownerType}.
@ -79,8 +85,8 @@ public final class TypeUtils {
*/ */
public static WildcardType subtypeOf(Type bound) { public static WildcardType subtypeOf(Type bound) {
Type[] upperBounds; Type[] upperBounds;
if (bound instanceof WildcardType) { if (bound instanceof WildcardType w) {
upperBounds = ((WildcardType) bound).getUpperBounds(); upperBounds = w.getUpperBounds();
} else { } else {
upperBounds = new Type[]{bound}; upperBounds = new Type[]{bound};
} }
@ -93,8 +99,8 @@ public final class TypeUtils {
*/ */
public static WildcardType supertypeOf(Type bound) { public static WildcardType supertypeOf(Type bound) {
Type[] lowerBounds; Type[] lowerBounds;
if (bound instanceof WildcardType) { if (bound instanceof WildcardType w) {
lowerBounds = ((WildcardType) bound).getLowerBounds(); lowerBounds = w.getLowerBounds();
} else { } else {
lowerBounds = new Type[]{bound}; lowerBounds = new Type[]{bound};
} }
@ -106,21 +112,17 @@ public final class TypeUtils {
* Object#equals(Object) Object.equals()}. The returned type is {@link java.io.Serializable}. * Object#equals(Object) Object.equals()}. The returned type is {@link java.io.Serializable}.
*/ */
public static Type canonicalize(Type type) { public static Type canonicalize(Type type) {
if (type instanceof Class) { if (type instanceof Class<?> c) {
Class<?> c = (Class<?>) type;
return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c; return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c;
} else if (type instanceof ParameterizedType) { } else if (type instanceof ParameterizedType p) {
ParameterizedType p = (ParameterizedType) type;
return new ParameterizedTypeImpl( return new ParameterizedTypeImpl(
p.getOwnerType(), (Class<?>) p.getRawType(), p.getActualTypeArguments()); p.getOwnerType(), (Class<?>) p.getRawType(), p.getActualTypeArguments());
} else if (type instanceof GenericArrayType) { } else if (type instanceof GenericArrayType g) {
GenericArrayType g = (GenericArrayType) type;
return new GenericArrayTypeImpl(g.getGenericComponentType()); return new GenericArrayTypeImpl(g.getGenericComponentType());
} else if (type instanceof WildcardType) { } else if (type instanceof WildcardType w) {
WildcardType w = (WildcardType) type;
return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds()); return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds());
} else { } else {
@ -130,12 +132,10 @@ public final class TypeUtils {
} }
public static Class<?> getRawType(Type type) { public static Class<?> getRawType(Type type) {
if (type instanceof Class<?>) { if (type instanceof Class<?> clazz) {
// type is a normal class. // type is a normal class.
return (Class<?>) type; return clazz;
} else if (type instanceof ParameterizedType parameterizedType) {
} else if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
// getRawType() returns Type instead of Class; that seems to be an API mistake, // getRawType() returns Type instead of Class; that seems to be an API mistake,
// see https://bugs.openjdk.org/browse/JDK-8250659 // see https://bugs.openjdk.org/browse/JDK-8250659
@ -143,8 +143,8 @@ public final class TypeUtils {
checkArgument(rawType instanceof Class); checkArgument(rawType instanceof Class);
return (Class<?>) rawType; return (Class<?>) rawType;
} else if (type instanceof GenericArrayType) { } else if (type instanceof GenericArrayType g) {
Type componentType = ((GenericArrayType) type).getGenericComponentType(); Type componentType = g.getGenericComponentType();
return Array.newInstance(getRawType(componentType), 0).getClass(); return Array.newInstance(getRawType(componentType), 0).getClass();
} else if (type instanceof TypeVariable) { } else if (type instanceof TypeVariable) {
@ -152,8 +152,8 @@ public final class TypeUtils {
// having a raw type that's more general than necessary is okay // having a raw type that's more general than necessary is okay
return Object.class; return Object.class;
} else if (type instanceof WildcardType) { } else if (type instanceof WildcardType w) {
Type[] bounds = ((WildcardType) type).getUpperBounds(); Type[] bounds = w.getUpperBounds();
// Currently the JLS only permits one bound for wildcards so using first bound is safe // Currently the JLS only permits one bound for wildcards so using first bound is safe
assert bounds.length == 1; assert bounds.length == 1;
return getRawType(bounds[0]); return getRawType(bounds[0]);
@ -184,43 +184,35 @@ public final class TypeUtils {
// Class already specifies equals(). // Class already specifies equals().
return a.equals(b); return a.equals(b);
} else if (a instanceof ParameterizedType) { } else if (a instanceof ParameterizedType pa) {
if (!(b instanceof ParameterizedType)) { if (!(b instanceof ParameterizedType pb)) {
return false; return false;
} }
// TODO: save a .clone() call // TODO: save a .clone() call
ParameterizedType pa = (ParameterizedType) a;
ParameterizedType pb = (ParameterizedType) b;
return equal(pa.getOwnerType(), pb.getOwnerType()) return equal(pa.getOwnerType(), pb.getOwnerType())
&& pa.getRawType().equals(pb.getRawType()) && pa.getRawType().equals(pb.getRawType())
&& Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments()); && Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments());
} else if (a instanceof GenericArrayType) { } else if (a instanceof GenericArrayType ga) {
if (!(b instanceof GenericArrayType)) { if (!(b instanceof GenericArrayType gb)) {
return false; return false;
} }
GenericArrayType ga = (GenericArrayType) a;
GenericArrayType gb = (GenericArrayType) b;
return equals(ga.getGenericComponentType(), gb.getGenericComponentType()); return equals(ga.getGenericComponentType(), gb.getGenericComponentType());
} else if (a instanceof WildcardType) { } else if (a instanceof WildcardType wa) {
if (!(b instanceof WildcardType)) { if (!(b instanceof WildcardType wb)) {
return false; return false;
} }
WildcardType wa = (WildcardType) a;
WildcardType wb = (WildcardType) b;
return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds()) return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds())
&& Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds()); && Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());
} else if (a instanceof TypeVariable) { } else if (a instanceof TypeVariable<?> va) {
if (!(b instanceof TypeVariable)) { if (!(b instanceof TypeVariable<?> vb)) {
return false; return false;
} }
TypeVariable<?> va = (TypeVariable<?>) a;
TypeVariable<?> vb = (TypeVariable<?>) b;
return Objects.equals(va.getGenericDeclaration(), vb.getGenericDeclaration()) return Objects.equals(va.getGenericDeclaration(), vb.getGenericDeclaration())
&& va.getName().equals(vb.getName()); && va.getName().equals(vb.getName());
@ -231,7 +223,7 @@ public final class TypeUtils {
} }
public static String typeToString(Type type) { public static String typeToString(Type type) {
return type instanceof Class ? ((Class<?>) type).getName() : type.toString(); return type instanceof Class<?> clazz ? clazz.getName() : type.toString();
} }
/** /**
@ -281,10 +273,10 @@ public final class TypeUtils {
* @param supertype a superclass of, or interface implemented by, this. * @param supertype a superclass of, or interface implemented by, this.
*/ */
public static Type getSupertype(Type context, Class<?> contextRawType, Class<?> supertype) { public static Type getSupertype(Type context, Class<?> contextRawType, Class<?> supertype) {
if (context instanceof WildcardType) { if (context instanceof WildcardType w) {
// Wildcards are useless for resolving supertypes. As the upper bound has the same raw type, // Wildcards are useless for resolving supertypes. As the upper bound has the same raw type,
// use it instead // use it instead
Type[] bounds = ((WildcardType) context).getUpperBounds(); Type[] bounds = w.getUpperBounds();
// Currently the JLS only permits one bound for wildcards so using first bound is safe // Currently the JLS only permits one bound for wildcards so using first bound is safe
assert bounds.length == 1; assert bounds.length == 1;
context = bounds[0]; context = bounds[0];
@ -300,8 +292,8 @@ public final class TypeUtils {
* @throws ClassCastException if this type is not an array. * @throws ClassCastException if this type is not an array.
*/ */
public static Type getArrayComponentType(Type array) { public static Type getArrayComponentType(Type array) {
return array instanceof GenericArrayType return array instanceof GenericArrayType g
? ((GenericArrayType) array).getGenericComponentType() ? g.getGenericComponentType()
: ((Class<?>) array).getComponentType(); : ((Class<?>) array).getComponentType();
} }
@ -313,8 +305,8 @@ public final class TypeUtils {
public static Type getCollectionElementType(Type context, Class<?> contextRawType) { public static Type getCollectionElementType(Type context, Class<?> contextRawType) {
Type collectionType = getSupertype(context, contextRawType, Collection.class); Type collectionType = getSupertype(context, contextRawType, Collection.class);
if (collectionType instanceof ParameterizedType) { if (collectionType instanceof ParameterizedType p) {
return ((ParameterizedType) collectionType).getActualTypeArguments()[0]; return p.getActualTypeArguments()[0];
} }
return Object.class; return Object.class;
} }
@ -335,16 +327,14 @@ public final class TypeUtils {
Type mapType = getSupertype(context, contextRawType, Map.class); Type mapType = getSupertype(context, contextRawType, Map.class);
// TODO: strip wildcards? // TODO: strip wildcards?
if (mapType instanceof ParameterizedType) { if (mapType instanceof ParameterizedType mapParameterizedType) {
ParameterizedType mapParameterizedType = (ParameterizedType) mapType;
return mapParameterizedType.getActualTypeArguments(); return mapParameterizedType.getActualTypeArguments();
} }
return new Type[]{Object.class, Object.class}; return new Type[]{Object.class, Object.class};
} }
public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) { public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
return resolve(context, contextRawType, toResolve, new HashMap<>());
return resolve(context, contextRawType, toResolve, new HashMap<TypeVariable<?>, Type>());
} }
private static Type resolve( private static Type resolve(
@ -355,8 +345,7 @@ public final class TypeUtils {
// this implementation is made a little more complicated in an attempt to avoid object-creation // this implementation is made a little more complicated in an attempt to avoid object-creation
TypeVariable<?> resolving = null; TypeVariable<?> resolving = null;
while (true) { while (true) {
if (toResolve instanceof TypeVariable) { if (toResolve instanceof TypeVariable<?> typeVariable) {
TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
Type previouslyResolved = visitedTypeVariables.get(typeVariable); Type previouslyResolved = visitedTypeVariables.get(typeVariable);
if (previouslyResolved != null) { if (previouslyResolved != null) {
// cannot reduce due to infinite recursion // cannot reduce due to infinite recursion
@ -374,24 +363,21 @@ public final class TypeUtils {
break; break;
} }
} else if (toResolve instanceof Class && ((Class<?>) toResolve).isArray()) { } else if (toResolve instanceof Class<?> original && original.isArray()) {
Class<?> original = (Class<?>) toResolve;
Type componentType = original.getComponentType(); Type componentType = original.getComponentType();
Type newComponentType = Type newComponentType =
resolve(context, contextRawType, componentType, visitedTypeVariables); resolve(context, contextRawType, componentType, visitedTypeVariables);
toResolve = equal(componentType, newComponentType) ? original : arrayOf(newComponentType); toResolve = equal(componentType, newComponentType) ? original : arrayOf(newComponentType);
break; break;
} else if (toResolve instanceof GenericArrayType) { } else if (toResolve instanceof GenericArrayType original) {
GenericArrayType original = (GenericArrayType) toResolve;
Type componentType = original.getGenericComponentType(); Type componentType = original.getGenericComponentType();
Type newComponentType = Type newComponentType =
resolve(context, contextRawType, componentType, visitedTypeVariables); resolve(context, contextRawType, componentType, visitedTypeVariables);
toResolve = equal(componentType, newComponentType) ? original : arrayOf(newComponentType); toResolve = equal(componentType, newComponentType) ? original : arrayOf(newComponentType);
break; break;
} else if (toResolve instanceof ParameterizedType) { } else if (toResolve instanceof ParameterizedType original) {
ParameterizedType original = (ParameterizedType) toResolve;
Type ownerType = original.getOwnerType(); Type ownerType = original.getOwnerType();
Type newOwnerType = resolve(context, contextRawType, ownerType, visitedTypeVariables); Type newOwnerType = resolve(context, contextRawType, ownerType, visitedTypeVariables);
boolean ownerChanged = !equal(newOwnerType, ownerType); boolean ownerChanged = !equal(newOwnerType, ownerType);
@ -417,8 +403,7 @@ public final class TypeUtils {
: original; : original;
break; break;
} else if (toResolve instanceof WildcardType) { } else if (toResolve instanceof WildcardType original) {
WildcardType original = (WildcardType) toResolve;
Type[] originalLowerBound = original.getLowerBounds(); Type[] originalLowerBound = original.getLowerBounds();
Type[] originalUpperBound = original.getUpperBounds(); Type[] originalUpperBound = original.getUpperBounds();
@ -461,9 +446,9 @@ public final class TypeUtils {
} }
Type declaredBy = getGenericSupertype(context, contextRawType, declaredByRaw); Type declaredBy = getGenericSupertype(context, contextRawType, declaredByRaw);
if (declaredBy instanceof ParameterizedType) { if (declaredBy instanceof ParameterizedType p) {
int index = indexOf(declaredByRaw.getTypeParameters(), unknown); int index = indexOf(declaredByRaw.getTypeParameters(), unknown);
return ((ParameterizedType) declaredBy).getActualTypeArguments()[index]; return p.getActualTypeArguments()[index];
} }
return unknown; return unknown;
@ -484,11 +469,11 @@ public final class TypeUtils {
*/ */
private static Class<?> declaringClassOf(TypeVariable<?> typeVariable) { private static Class<?> declaringClassOf(TypeVariable<?> typeVariable) {
GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration(); GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
return genericDeclaration instanceof Class ? (Class<?>) genericDeclaration : null; return genericDeclaration instanceof Class<?> clazz ? clazz : null;
} }
static void checkNotPrimitive(Type type) { static void checkNotPrimitive(Type type) {
checkArgument(!(type instanceof Class<?>) || !((Class<?>) type).isPrimitive()); checkArgument(!(type instanceof Class<?> clazz) || !clazz.isPrimitive());
} }
/** /**
@ -499,8 +484,7 @@ public final class TypeUtils {
* would create parameterized types with owner type. * would create parameterized types with owner type.
*/ */
public static boolean requiresOwnerType(Type rawType) { public static boolean requiresOwnerType(Type rawType) {
if (rawType instanceof Class<?>) { if (rawType instanceof Class<?> rawTypeAsClass) {
Class<?> rawTypeAsClass = (Class<?>) rawType;
return !Modifier.isStatic(rawTypeAsClass.getModifiers()) return !Modifier.isStatic(rawTypeAsClass.getModifiers())
&& rawTypeAsClass.getDeclaringClass() != null; && rawTypeAsClass.getDeclaringClass() != null;
} }
@ -512,17 +496,12 @@ public final class TypeUtils {
// our way to ensure that the Type in question is either Class (which is serializable) or one 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). // the nested Type implementations here (which are also serializable).
private static final class ParameterizedTypeImpl implements ParameterizedType, Serializable { private static final class ParameterizedTypeImpl implements ParameterizedType, Serializable {
@SuppressWarnings("serial")
private final Type ownerType; private final Type ownerType;
@SuppressWarnings("serial")
private final Type rawType; private final Type rawType;
@SuppressWarnings("serial")
private final Type[] typeArguments; private final Type[] typeArguments;
public ParameterizedTypeImpl(Type ownerType, Class<?> rawType, Type... typeArguments) { public ParameterizedTypeImpl(Type ownerType, Class<?> rawType, Type... typeArguments) {
requireNonNull(rawType); Objects.requireNonNull(rawType);
if (ownerType == null && requiresOwnerType(rawType)) { if (ownerType == null && requiresOwnerType(rawType)) {
throw new IllegalArgumentException("Must specify owner type for " + rawType); throw new IllegalArgumentException("Must specify owner type for " + rawType);
@ -532,19 +511,19 @@ public final class TypeUtils {
this.rawType = canonicalize(rawType); this.rawType = canonicalize(rawType);
this.typeArguments = typeArguments.clone(); this.typeArguments = typeArguments.clone();
for (int t = 0, length = this.typeArguments.length; t < length; t++) { for (int t = 0, length = this.typeArguments.length; t < length; t++) {
requireNonNull(this.typeArguments[t]); Objects.requireNonNull(this.typeArguments[t]);
checkNotPrimitive(this.typeArguments[t]); checkNotPrimitive(this.typeArguments[t]);
this.typeArguments[t] = canonicalize(this.typeArguments[t]); this.typeArguments[t] = canonicalize(this.typeArguments[t]);
} }
} }
@Override @Override
public Type[] getActualTypeArguments() { public Type @NotNull [] getActualTypeArguments() {
return typeArguments.clone(); return typeArguments.clone();
} }
@Override @Override
public Type getRawType() { public @NotNull Type getRawType() {
return rawType; return rawType;
} }
@ -555,8 +534,7 @@ public final class TypeUtils {
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
return other instanceof ParameterizedType return other instanceof ParameterizedType p && TypeUtils.equals(this, p);
&& TypeUtils.equals(this, (ParameterizedType) other);
} }
private static int hashCodeOrZero(Object o) { private static int hashCodeOrZero(Object o) {
@ -586,26 +564,26 @@ public final class TypeUtils {
return stringBuilder.append(">").toString(); return stringBuilder.append(">").toString();
} }
@Serial
private static final long serialVersionUID = 0; private static final long serialVersionUID = 0;
} }
private static final class GenericArrayTypeImpl implements GenericArrayType, Serializable { private static final class GenericArrayTypeImpl implements GenericArrayType, Serializable {
@SuppressWarnings("serial")
private final Type componentType; private final Type componentType;
public GenericArrayTypeImpl(Type componentType) { public GenericArrayTypeImpl(Type componentType) {
requireNonNull(componentType); Objects.requireNonNull(componentType);
this.componentType = canonicalize(componentType); this.componentType = canonicalize(componentType);
} }
@Override @Override
public Type getGenericComponentType() { public @NotNull Type getGenericComponentType() {
return componentType; return componentType;
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
return o instanceof GenericArrayType && TypeUtils.equals(this, (GenericArrayType) o); return o instanceof GenericArrayType g && TypeUtils.equals(this, g);
} }
@Override @Override
@ -618,20 +596,18 @@ public final class TypeUtils {
return typeToString(componentType) + "[]"; return typeToString(componentType) + "[]";
} }
@Serial
private static final long serialVersionUID = 0; private static final long serialVersionUID = 0;
} }
/** /**
* The WildcardType interface supports multiple upper bounds and multiple lower bounds. We only * The WildcardType interface supports multiple upper bounds and multiple lower bounds. We only
* support what the target Java version supports - at most one bound, see also * support what the target Java version supports - at most one bound, see also
* https://bugs.openjdk.java.net/browse/JDK-8250660. If a lower bound is set, the upper bound must * <a href="https://bugs.openjdk.org/browse/JDK-8250660">JDK-8250660</a>.
* be Object.class. * If a lower bound is set, the upper bound must be Object.class.
*/ */
private static final class WildcardTypeImpl implements WildcardType, Serializable { private static final class WildcardTypeImpl implements WildcardType, Serializable {
@SuppressWarnings("serial")
private final Type upperBound; private final Type upperBound;
@SuppressWarnings("serial")
private final Type lowerBound; private final Type lowerBound;
public WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) { public WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) {
@ -639,14 +615,14 @@ public final class TypeUtils {
checkArgument(upperBounds.length == 1); checkArgument(upperBounds.length == 1);
if (lowerBounds.length == 1) { if (lowerBounds.length == 1) {
requireNonNull(lowerBounds[0]); Objects.requireNonNull(lowerBounds[0]);
checkNotPrimitive(lowerBounds[0]); checkNotPrimitive(lowerBounds[0]);
checkArgument(upperBounds[0] == Object.class); checkArgument(upperBounds[0] == Object.class);
this.lowerBound = canonicalize(lowerBounds[0]); this.lowerBound = canonicalize(lowerBounds[0]);
this.upperBound = Object.class; this.upperBound = Object.class;
} else { } else {
requireNonNull(upperBounds[0]); Objects.requireNonNull(upperBounds[0]);
checkNotPrimitive(upperBounds[0]); checkNotPrimitive(upperBounds[0]);
this.lowerBound = null; this.lowerBound = null;
this.upperBound = canonicalize(upperBounds[0]); this.upperBound = canonicalize(upperBounds[0]);
@ -654,18 +630,18 @@ public final class TypeUtils {
} }
@Override @Override
public Type[] getUpperBounds() { public Type @NotNull [] getUpperBounds() {
return new Type[]{upperBound}; return new Type[]{upperBound};
} }
@Override @Override
public Type[] getLowerBounds() { public Type @NotNull [] getLowerBounds() {
return lowerBound != null ? new Type[]{lowerBound} : EMPTY_TYPE_ARRAY; return lowerBound != null ? new Type[]{lowerBound} : EMPTY_TYPE_ARRAY;
} }
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
return other instanceof WildcardType && TypeUtils.equals(this, (WildcardType) other); return other instanceof WildcardType w && TypeUtils.equals(this, w);
} }
@Override @Override
@ -685,6 +661,7 @@ public final class TypeUtils {
} }
} }
@Serial
private static final long serialVersionUID = 0; private static final long serialVersionUID = 0;
} }
} }

View File

@ -3,7 +3,7 @@ hmclauncher = "3.7.0.0"
jetbrains-annotations = "26.0.1" jetbrains-annotations = "26.0.1"
kala-compress = "1.27.1-1" kala-compress = "1.27.1-1"
simple-png-javafx = "0.3.0" simple-png-javafx = "0.3.0"
gson = "2.13.0" gson = "2.13.2"
toml4j = "0.7.2" toml4j = "0.7.2"
xz = "1.10" xz = "1.10"
fx-gson = "5.0.0" fx-gson = "5.0.0"