Skip to content

Commit 0a5b2da

Browse files
Merge branch 'main' into marcono1234/codeql-github-actions
2 parents d93e995 + 676b005 commit 0a5b2da

File tree

6 files changed

+82
-62
lines changed

6 files changed

+82
-62
lines changed

gson/src/main/java/com/google/gson/GsonBuilder.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.google.gson.annotations.Since;
3434
import com.google.gson.annotations.Until;
3535
import com.google.gson.internal.Excluder;
36-
import com.google.gson.internal.GsonPreconditions;
3736
import com.google.gson.internal.bind.DefaultDateTypeAdapter;
3837
import com.google.gson.internal.bind.TreeTypeAdapter;
3938
import com.google.gson.internal.bind.TypeAdapters;
@@ -704,11 +703,16 @@ private static int checkDateFormatStyle(int style) {
704703
@CanIgnoreReturnValue
705704
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
706705
Objects.requireNonNull(type);
707-
GsonPreconditions.checkArgument(
708-
typeAdapter instanceof JsonSerializer<?>
709-
|| typeAdapter instanceof JsonDeserializer<?>
710-
|| typeAdapter instanceof InstanceCreator<?>
711-
|| typeAdapter instanceof TypeAdapter<?>);
706+
Objects.requireNonNull(typeAdapter);
707+
if (!(typeAdapter instanceof JsonSerializer<?>
708+
|| typeAdapter instanceof JsonDeserializer<?>
709+
|| typeAdapter instanceof InstanceCreator<?>
710+
|| typeAdapter instanceof TypeAdapter<?>)) {
711+
throw new IllegalArgumentException(
712+
"Class "
713+
+ typeAdapter.getClass().getName()
714+
+ " does not implement any supported type adapter class or interface");
715+
}
712716

713717
if (hasNonOverridableAdapter(type)) {
714718
throw new IllegalArgumentException("Cannot override built-in adapter for " + type);
@@ -778,10 +782,15 @@ public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
778782
@CanIgnoreReturnValue
779783
public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) {
780784
Objects.requireNonNull(baseType);
781-
GsonPreconditions.checkArgument(
782-
typeAdapter instanceof JsonSerializer<?>
783-
|| typeAdapter instanceof JsonDeserializer<?>
784-
|| typeAdapter instanceof TypeAdapter<?>);
785+
Objects.requireNonNull(typeAdapter);
786+
if (!(typeAdapter instanceof JsonSerializer<?>
787+
|| typeAdapter instanceof JsonDeserializer<?>
788+
|| typeAdapter instanceof TypeAdapter<?>)) {
789+
throw new IllegalArgumentException(
790+
"Class "
791+
+ typeAdapter.getClass().getName()
792+
+ " does not implement any supported type adapter class or interface");
793+
}
785794

786795
if (typeAdapter instanceof JsonDeserializer || typeAdapter instanceof JsonSerializer) {
787796
hierarchyFactories.add(TreeTypeAdapter.newTypeHierarchyFactory(baseType, typeAdapter));

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.google.gson.internal;
1818

19-
import static com.google.gson.internal.GsonPreconditions.checkArgument;
2019
import static java.util.Objects.requireNonNull;
2120

2221
import java.io.Serializable;
@@ -139,7 +138,6 @@ public static Class<?> getRawType(Type type) {
139138
// getRawType() returns Type instead of Class; that seems to be an API mistake,
140139
// see https://bugs.openjdk.org/browse/JDK-8250659
141140
Type rawType = parameterizedType.getRawType();
142-
checkArgument(rawType instanceof Class);
143141
return (Class<?>) rawType;
144142

145143
} else if (type instanceof GenericArrayType) {
@@ -286,7 +284,10 @@ private static Type getSupertype(Type context, Class<?> contextRawType, Class<?>
286284
assert bounds.length == 1;
287285
context = bounds[0];
288286
}
289-
checkArgument(supertype.isAssignableFrom(contextRawType));
287+
if (!supertype.isAssignableFrom(contextRawType)) {
288+
throw new IllegalArgumentException(
289+
contextRawType + " is not the same as or a subtype of " + supertype);
290+
}
290291
return resolve(
291292
context, contextRawType, GsonTypes.getGenericSupertype(context, contextRawType, supertype));
292293
}
@@ -485,7 +486,9 @@ private static Class<?> declaringClassOf(TypeVariable<?> typeVariable) {
485486
}
486487

487488
static void checkNotPrimitive(Type type) {
488-
checkArgument(!(type instanceof Class<?>) || !((Class<?>) type).isPrimitive());
489+
if (type instanceof Class<?> && ((Class<?>) type).isPrimitive()) {
490+
throw new IllegalArgumentException("Primitive type is not allowed");
491+
}
489492
}
490493

491494
/**
@@ -632,13 +635,20 @@ private static final class WildcardTypeImpl implements WildcardType, Serializabl
632635
private final Type lowerBound;
633636

634637
public WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) {
635-
checkArgument(lowerBounds.length <= 1);
636-
checkArgument(upperBounds.length == 1);
638+
if (lowerBounds.length > 1) {
639+
throw new IllegalArgumentException("At most one lower bound is supported");
640+
}
641+
if (upperBounds.length != 1) {
642+
throw new IllegalArgumentException("Exactly one upper bound must be specified");
643+
}
637644

638645
if (lowerBounds.length == 1) {
639646
requireNonNull(lowerBounds[0]);
640647
checkNotPrimitive(lowerBounds[0]);
641-
checkArgument(upperBounds[0] == Object.class);
648+
if (upperBounds[0] != Object.class) {
649+
throw new IllegalArgumentException(
650+
"When lower bound is specified, upper bound must be Object");
651+
}
642652
this.lowerBound = canonicalize(lowerBounds[0]);
643653
this.upperBound = Object.class;
644654

gson/src/main/java/com/google/gson/internal/bind/TreeTypeAdapter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
import com.google.gson.JsonSerializer;
2626
import com.google.gson.TypeAdapter;
2727
import com.google.gson.TypeAdapterFactory;
28-
import com.google.gson.internal.GsonPreconditions;
2928
import com.google.gson.internal.Streams;
3029
import com.google.gson.reflect.TypeToken;
3130
import com.google.gson.stream.JsonReader;
3231
import com.google.gson.stream.JsonWriter;
3332
import java.io.IOException;
3433
import java.lang.reflect.Type;
34+
import java.util.Objects;
3535

3636
/**
3737
* Adapts a Gson 1.x tree-style adapter as a streaming TypeAdapter. Since the tree adapter may be
@@ -162,7 +162,13 @@ private static final class SingleTypeFactory implements TypeAdapterFactory {
162162
serializer = typeAdapter instanceof JsonSerializer ? (JsonSerializer<?>) typeAdapter : null;
163163
deserializer =
164164
typeAdapter instanceof JsonDeserializer ? (JsonDeserializer<?>) typeAdapter : null;
165-
GsonPreconditions.checkArgument(serializer != null || deserializer != null);
165+
if (serializer == null && deserializer == null) {
166+
Objects.requireNonNull(typeAdapter);
167+
throw new IllegalArgumentException(
168+
"Type adapter "
169+
+ typeAdapter.getClass().getName()
170+
+ " must implement JsonSerializer or JsonDeserializer");
171+
}
166172
this.exactType = exactType;
167173
this.matchRawType = matchRawType;
168174
this.hierarchyType = hierarchyType;

gson/src/test/java/com/google/gson/GsonBuilderTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,20 @@ public void testSetStrictness() throws IOException {
248248
assertThat(gson.newJsonWriter(new StringWriter()).getStrictness()).isEqualTo(strictness);
249249
}
250250

251+
@Test
252+
public void testRegisterTypeAdapterNotAdapterClass() {
253+
GsonBuilder gsonBuilder = new GsonBuilder();
254+
IllegalArgumentException e =
255+
assertThrows(
256+
IllegalArgumentException.class,
257+
() -> gsonBuilder.registerTypeAdapter(Integer.class, "test"));
258+
assertThat(e)
259+
.hasMessageThat()
260+
.isEqualTo(
261+
"Class java.lang.String does not implement any supported type adapter class or"
262+
+ " interface");
263+
}
264+
251265
@Test
252266
public void testRegisterTypeAdapterForObjectAndJsonElements() {
253267
String errorMessage = "Cannot override built-in adapter for ";
@@ -280,6 +294,20 @@ public void testRegisterTypeAdapterForJsonElements() {
280294
assertThat(adapter.toJson(new JsonArray())).isEqualTo("[]");
281295
}
282296

297+
@Test
298+
public void testRegisterTypeHierarchyAdapterNotAdapterClass() {
299+
GsonBuilder gsonBuilder = new GsonBuilder();
300+
IllegalArgumentException e =
301+
assertThrows(
302+
IllegalArgumentException.class,
303+
() -> gsonBuilder.registerTypeHierarchyAdapter(Integer.class, "test"));
304+
assertThat(e)
305+
.hasMessageThat()
306+
.isEqualTo(
307+
"Class java.lang.String does not implement any supported type adapter class or"
308+
+ " interface");
309+
}
310+
283311
@Ignore(
284312
"Registering adapter for JsonElement is allowed (for now) for backward compatibility, see"
285313
+ " https://github.com/google/gson/issues/2787")

gson/src/test/java/com/google/gson/internal/GsonTypesTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929

3030
@SuppressWarnings("ClassNamedLikeTypeParameter") // for dummy classes A, B, ...
3131
public final class GsonTypesTest {
32+
@Test
33+
public void testWildcardBoundsInvalid() {
34+
String expectedMessage = "Primitive type is not allowed";
35+
IllegalArgumentException e =
36+
assertThrows(IllegalArgumentException.class, () -> GsonTypes.subtypeOf(int.class));
37+
assertThat(e).hasMessageThat().isEqualTo(expectedMessage);
38+
39+
e = assertThrows(IllegalArgumentException.class, () -> GsonTypes.supertypeOf(int.class));
40+
assertThat(e).hasMessageThat().isEqualTo(expectedMessage);
41+
}
3242

3343
@Test
3444
public void testNewParameterizedTypeWithoutOwner() throws Exception {

0 commit comments

Comments
 (0)