Skip to content

Commit

Permalink
Remove explicit type arguments from tests where they can be inferred
Browse files Browse the repository at this point in the history
This works because unlike the main sources the tests are compiled with Java 11.
  • Loading branch information
Marcono1234 committed Jun 7, 2024
1 parent 9f8620d commit 5ef8a22
Show file tree
Hide file tree
Showing 21 changed files with 55 additions and 64 deletions.
2 changes: 1 addition & 1 deletion gson/src/test/java/com/google/gson/GsonBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
public class GsonBuilderTest {
private static final TypeAdapter<Object> NULL_TYPE_ADAPTER =
new TypeAdapter<Object>() {
new TypeAdapter<>() {
@Override
public void write(JsonWriter out, Object value) {
throw new AssertionError();
Expand Down
20 changes: 10 additions & 10 deletions gson/src/test/java/com/google/gson/GsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void testOverridesDefaultExcluder() {
new Gson(
CUSTOM_EXCLUDER,
CUSTOM_FIELD_NAMING_STRATEGY,
new HashMap<Type, InstanceCreator<?>>(),
new HashMap<>(),
true,
false,
true,
Expand All @@ -85,12 +85,12 @@ public void testOverridesDefaultExcluder() {
null,
DateFormat.DEFAULT,
DateFormat.DEFAULT,
new ArrayList<TypeAdapterFactory>(),
new ArrayList<TypeAdapterFactory>(),
new ArrayList<TypeAdapterFactory>(),
new ArrayList<>(),
new ArrayList<>(),
new ArrayList<>(),
CUSTOM_OBJECT_TO_NUMBER_STRATEGY,
CUSTOM_NUMBER_TO_NUMBER_STRATEGY,
Collections.<ReflectionAccessFilter>emptyList());
Collections.emptyList());

assertThat(gson.excluder).isEqualTo(CUSTOM_EXCLUDER);
assertThat(gson.fieldNamingStrategy()).isEqualTo(CUSTOM_FIELD_NAMING_STRATEGY);
Expand All @@ -104,7 +104,7 @@ public void testClonedTypeAdapterFactoryListsAreIndependent() {
new Gson(
CUSTOM_EXCLUDER,
CUSTOM_FIELD_NAMING_STRATEGY,
new HashMap<Type, InstanceCreator<?>>(),
new HashMap<>(),
true,
false,
true,
Expand All @@ -117,12 +117,12 @@ public void testClonedTypeAdapterFactoryListsAreIndependent() {
null,
DateFormat.DEFAULT,
DateFormat.DEFAULT,
new ArrayList<TypeAdapterFactory>(),
new ArrayList<TypeAdapterFactory>(),
new ArrayList<TypeAdapterFactory>(),
new ArrayList<>(),
new ArrayList<>(),
new ArrayList<>(),
CUSTOM_OBJECT_TO_NUMBER_STRATEGY,
CUSTOM_NUMBER_TO_NUMBER_STRATEGY,
Collections.<ReflectionAccessFilter>emptyList());
Collections.emptyList());

Gson clone =
original.newBuilder().registerTypeAdapter(int.class, new TestTypeAdapter()).create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private static void assertSerialized(
boolean registerAbstractHierarchyDeserializer,
Object instance) {
JsonDeserializer<Abstract> deserializer =
new JsonDeserializer<Abstract>() {
new JsonDeserializer<>() {
@Override
public Abstract deserialize(
JsonElement json, Type typeOfT, JsonDeserializationContext context)
Expand Down
11 changes: 4 additions & 7 deletions gson/src/test/java/com/google/gson/JsonArrayAsListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void testAdd() {
assertThat(list.add(JsonNull.INSTANCE)).isTrue();

List<JsonElement> expectedList =
Arrays.<JsonElement>asList(
Arrays.asList(
new JsonPrimitive(2),
new JsonPrimitive(3),
new JsonPrimitive(1),
Expand Down Expand Up @@ -111,21 +111,18 @@ public void testAddAll() {
list.addAll(Arrays.asList(new JsonPrimitive(2), new JsonPrimitive(3)));

List<JsonElement> expectedList =
Arrays.<JsonElement>asList(
new JsonPrimitive(1), new JsonPrimitive(2), new JsonPrimitive(3));
Arrays.asList(new JsonPrimitive(1), new JsonPrimitive(2), new JsonPrimitive(3));
assertThat(list).isEqualTo(expectedList);
assertThat(list).isEqualTo(expectedList);

NullPointerException e =
assertThrows(
NullPointerException.class,
() -> list.addAll(0, Collections.<JsonElement>singletonList(null)));
NullPointerException.class, () -> list.addAll(0, Collections.singletonList(null)));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");

e =
assertThrows(
NullPointerException.class,
() -> list.addAll(Collections.<JsonElement>singletonList(null)));
NullPointerException.class, () -> list.addAll(Collections.singletonList(null)));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}

Expand Down
9 changes: 3 additions & 6 deletions gson/src/test/java/com/google/gson/JsonObjectAsMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,12 @@ public void testPutAll() {
var e =
assertThrows(
NullPointerException.class,
() ->
map.putAll(
Collections.<String, JsonElement>singletonMap(null, new JsonPrimitive(1))));
() -> map.putAll(Collections.singletonMap(null, new JsonPrimitive(1))));
assertThat(e).hasMessageThat().isEqualTo("key == null");

e =
assertThrows(
NullPointerException.class,
() -> map.putAll(Collections.<String, JsonElement>singletonMap("a", null)));
NullPointerException.class, () -> map.putAll(Collections.singletonMap("a", null)));
assertThat(e).hasMessageThat().isEqualTo("value == null");
}

Expand Down Expand Up @@ -221,7 +218,7 @@ public void testEntrySet() {
Set<Entry<String, JsonElement>> entrySet = map.entrySet();

List<Entry<?, ?>> expectedEntrySet =
Arrays.<Entry<?, ?>>asList(
Arrays.asList(
new SimpleEntry<>("b", new JsonPrimitive(2)),
new SimpleEntry<>("a", new JsonPrimitive(1)));
// Should contain entries in same order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
public class OverrideCoreTypeAdaptersTest {
private static final TypeAdapter<Boolean> booleanAsIntAdapter =
new TypeAdapter<Boolean>() {
new TypeAdapter<>() {
@Override
public void write(JsonWriter out, Boolean value) throws IOException {
out.value(value ? 1 : 0);
Expand All @@ -45,7 +45,7 @@ public Boolean read(JsonReader in) throws IOException {
};

private static final TypeAdapter<String> swapCaseStringAdapter =
new TypeAdapter<String>() {
new TypeAdapter<>() {
@Override
public void write(JsonWriter out, String value) throws IOException {
out.value(value.toUpperCase(Locale.US));
Expand Down
4 changes: 2 additions & 2 deletions gson/src/test/java/com/google/gson/TypeAdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String read(JsonReader in) {
public void testToJson_ThrowingIOException() {
final IOException exception = new IOException("test");
TypeAdapter<Integer> adapter =
new TypeAdapter<Integer>() {
new TypeAdapter<>() {
@Override
public void write(JsonWriter out, Integer value) throws IOException {
throw exception;
Expand All @@ -73,7 +73,7 @@ public Integer read(JsonReader in) {
}

private static final TypeAdapter<String> adapter =
new TypeAdapter<String>() {
new TypeAdapter<>() {
@Override
public void write(JsonWriter out, String value) throws IOException {
out.value(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static class StatsTypeAdapterFactory implements TypeAdapterFactory {
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new TypeAdapter<T>() {
return new TypeAdapter<>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
++numWrites;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public void testDefault() {
public void testVariousCombinationsParse() {
// Mixing various indent and newline styles in the same string, to be parsed.
String jsonStringMix = "{\r\t'a':\r\n[ 1,2\t]\n}";
TypeToken<Map<String, List<Integer>>> inputType =
new TypeToken<Map<String, List<Integer>>>() {};
TypeToken<Map<String, List<Integer>>> inputType = new TypeToken<>() {};

Map<String, List<Integer>> actualParsed;
// Test all that all combinations of newline can be parsed and generate the same INPUT.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void testInstanceCreatorForCollectionType() {
@SuppressWarnings("serial")
class SubArrayList<T> extends ArrayList<T> {}
InstanceCreator<List<String>> listCreator =
new InstanceCreator<List<String>>() {
new InstanceCreator<>() {
@Override
public List<String> createInstance(Type type) {
return new SubArrayList<>();
Expand All @@ -124,7 +124,7 @@ public void testInstanceCreatorForParametrizedType() {
@SuppressWarnings("serial")
class SubTreeSet<T> extends TreeSet<T> {}
InstanceCreator<SortedSet<?>> sortedSetCreator =
new InstanceCreator<SortedSet<?>>() {
new InstanceCreator<>() {
@Override
public SortedSet<?> createInstance(Type type) {
return new SubTreeSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void testJsonAdapterFactoryInvoked() {
@Test
public void testRegisteredAdapterOverridesJsonAdapter() {
TypeAdapter<A> typeAdapter =
new TypeAdapter<A>() {
new TypeAdapter<>() {
@Override
public void write(JsonWriter out, A value) throws IOException {
out.value("registeredAdapter");
Expand All @@ -98,7 +98,7 @@ public A read(JsonReader in) throws IOException {
@Test
public void testRegisteredSerializerOverridesJsonAdapter() {
JsonSerializer<A> serializer =
new JsonSerializer<A>() {
new JsonSerializer<>() {
@Override
public JsonElement serialize(A src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive("registeredSerializer");
Expand All @@ -115,7 +115,7 @@ public JsonElement serialize(A src, Type typeOfSrc, JsonSerializationContext con
@Test
public void testRegisteredDeserializerOverridesJsonAdapter() {
JsonDeserializer<A> deserializer =
new JsonDeserializer<A>() {
new JsonDeserializer<>() {
@Override
public A deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
Expand Down Expand Up @@ -170,8 +170,7 @@ public void testFactoryReturningNull() {
assertThat(gson.fromJson("null", WithNullReturningFactory.class)).isNull();
assertThat(gson.toJson(null, WithNullReturningFactory.class)).isEqualTo("null");

TypeToken<WithNullReturningFactory<String>> stringTypeArg =
new TypeToken<WithNullReturningFactory<String>>() {};
TypeToken<WithNullReturningFactory<String>> stringTypeArg = new TypeToken<>() {};
WithNullReturningFactory<?> deserialized = gson.fromJson("\"a\"", stringTypeArg);
assertThat(deserialized.t).isEqualTo("custom-read:a");
assertThat(gson.fromJson("null", stringTypeArg)).isNull();
Expand All @@ -181,8 +180,7 @@ public void testFactoryReturningNull() {

// Factory should return `null` for this type and Gson should fall back to reflection-based
// adapter
TypeToken<WithNullReturningFactory<Integer>> numberTypeArg =
new TypeToken<WithNullReturningFactory<Integer>>() {};
TypeToken<WithNullReturningFactory<Integer>> numberTypeArg = new TypeToken<>() {};
deserialized = gson.fromJson("{\"t\":1}", numberTypeArg);
assertThat(deserialized.t).isEqualTo(1);
assertThat(gson.toJson(new WithNullReturningFactory<>(2), numberTypeArg.getType()))
Expand Down Expand Up @@ -266,7 +264,7 @@ private static class C {
static final class JsonAdapterFactory implements TypeAdapterFactory {
@Override
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
return new TypeAdapter<T>() {
return new TypeAdapter<>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
out.value("jsonAdapterFactory");
Expand Down Expand Up @@ -428,7 +426,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {

TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

return new TypeAdapter<T>() {
return new TypeAdapter<>() {
@Override
public T read(JsonReader in) throws IOException {
// Perform custom deserialization
Expand Down Expand Up @@ -478,7 +476,7 @@ private static class WithDelayedDelegatingFactory {
static class Factory implements TypeAdapterFactory {
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return new TypeAdapter<T>() {
return new TypeAdapter<>() {
// suppress Error Prone warning; should be clear that `Factory` refers to enclosing class
@SuppressWarnings("SameNameButDifferent")
private TypeAdapter<T> delegate() {
Expand Down Expand Up @@ -670,7 +668,7 @@ static class Factory implements TypeAdapterFactory {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

return new TypeAdapter<T>() {
return new TypeAdapter<>() {
@Override
public T read(JsonReader in) throws IOException {
// Perform custom deserialization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public Part read(JsonReader in) throws IOException {
private static class GizmoPartTypeAdapterFactory implements TypeAdapterFactory {
@Override
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
return new TypeAdapter<T>() {
return new TypeAdapter<>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
out.value("GizmoPartTypeAdapterFactory");
Expand Down Expand Up @@ -303,7 +303,7 @@ private GadgetWithPrimitivePart(long part) {

private static final class LongToStringTypeAdapterFactory implements TypeAdapterFactory {
static final TypeAdapter<Long> ADAPTER =
new TypeAdapter<Long>() {
new TypeAdapter<>() {
@Override
public void write(JsonWriter out, Long value) throws IOException {
out.value(value.toString());
Expand Down Expand Up @@ -352,7 +352,7 @@ private static final class Gizmo2 {
private static class Gizmo2PartTypeAdapterFactory implements TypeAdapterFactory {
@Override
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
return new TypeAdapter<T>() {
return new TypeAdapter<>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
out.value("GizmoPartTypeAdapterFactory");
Expand Down
2 changes: 1 addition & 1 deletion gson/src/test/java/com/google/gson/functional/MapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ public final void testInterfaceTypeMapWithSerializer() {
"{\"bases\":{\"Test\":" + baseTypeJson + "},\"subs\":{\"Test\":" + subTypeJson + "}}";

JsonSerializer<TestTypes.Base> baseTypeAdapter =
new JsonSerializer<TestTypes.Base>() {
new JsonSerializer<>() {
@Override
public JsonElement serialize(
TestTypes.Base src, Type typeOfSrc, JsonSerializationContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ public void testParameterizedTypeWithCustomSerializer() {
.create();
MyParameterizedType<Integer> intTarget = new MyParameterizedType<>(10);
String json = gson.toJson(intTarget, ptIntegerType);
assertThat(json).isEqualTo(MyParameterizedTypeAdapter.<Integer>getExpectedJson(intTarget));
assertThat(json).isEqualTo(MyParameterizedTypeAdapter.getExpectedJson(intTarget));

MyParameterizedType<String> stringTarget = new MyParameterizedType<>("abc");
json = gson.toJson(stringTarget, ptStringType);
assertThat(json).isEqualTo(MyParameterizedTypeAdapter.<String>getExpectedJson(stringTarget));
assertThat(json).isEqualTo(MyParameterizedTypeAdapter.getExpectedJson(stringTarget));
}

@Test
Expand All @@ -140,12 +140,12 @@ public void testParameterizedTypesWithCustomDeserializer() {
.create();

MyParameterizedType<Integer> src = new MyParameterizedType<>(10);
String json = MyParameterizedTypeAdapter.<Integer>getExpectedJson(src);
String json = MyParameterizedTypeAdapter.getExpectedJson(src);
MyParameterizedType<Integer> intTarget = gson.fromJson(json, ptIntegerType);
assertThat(intTarget.value).isEqualTo(10);

MyParameterizedType<String> srcStr = new MyParameterizedType<>("abc");
json = MyParameterizedTypeAdapter.<String>getExpectedJson(srcStr);
json = MyParameterizedTypeAdapter.getExpectedJson(srcStr);
MyParameterizedType<String> stringTarget = gson.fromJson(json, ptStringType);
assertThat(stringTarget.value).isEqualTo("abc");
}
Expand Down Expand Up @@ -515,7 +515,7 @@ private static void assertCorrectlyDeserialized(Object object) {

@Test
public void testGsonFromJsonTypeToken() {
TypeToken<List<Quantity>> typeToken = new TypeToken<List<Quantity>>() {};
TypeToken<List<Quantity>> typeToken = new TypeToken<>() {};
Type type = typeToken.getType();

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
subtypeToDelegate.put(entry.getValue(), delegate);
}

return new TypeAdapter<R>() {
return new TypeAdapter<>() {
@Override
public R read(JsonReader in) {
JsonElement jsonElement = Streams.parse(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void testDeserializeWithCustomTypeAdapter() throws IOException {

private void usePersonNameAdapter() {
TypeAdapter<Person> personNameAdapter =
new TypeAdapter<Person>() {
new TypeAdapter<>() {
@Override
public Person read(JsonReader in) throws IOException {
String name = in.nextString();
Expand Down Expand Up @@ -181,7 +181,7 @@ public void testDeserialize2dArray() throws IOException {
@Test
public void testNullSafe() {
TypeAdapter<Person> typeAdapter =
new TypeAdapter<Person>() {
new TypeAdapter<>() {
@Override
public Person read(JsonReader in) throws IOException {
List<String> values = Splitter.on(',').splitToList(in.nextString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static class Course<T> {
private final Assignment<T> assignment;

private Course() {
this(null, 0, null, new ArrayList<Student>());
this(null, 0, null, new ArrayList<>());
}

public Course(
Expand Down
Loading

0 comments on commit 5ef8a22

Please sign in to comment.