From 73768be112357ec7358c15de8b7f72285732571e Mon Sep 17 00:00:00 2001 From: Andrey Litvitski <120543954+panic08@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:32:50 +0300 Subject: [PATCH] Replace anonymous classes with lambdas in tests (#2766) Replace anonymous classes with lambdas in tests --- .../java/com/google/gson/GsonBuilderTest.java | 27 +--- .../test/java/com/google/gson/GsonTest.java | 51 ++---- .../com/google/gson/GsonTypeAdapterTest.java | 9 +- .../gson/functional/ConcurrencyTest.java | 29 ++-- .../functional/CustomDeserializerTest.java | 55 ++----- .../gson/functional/CustomSerializerTest.java | 11 +- .../functional/CustomTypeAdaptersTest.java | 145 ++++++------------ .../functional/DefaultTypeAdaptersTest.java | 10 +- .../gson/functional/InstanceCreatorTest.java | 43 +----- .../JsonAdapterAnnotationOnClassesTest.java | 17 +- .../com/google/gson/functional/MapTest.java | 37 ++--- .../functional/NullObjectAndFieldTest.java | 19 +-- .../google/gson/functional/ObjectTest.java | 41 ++--- .../TypeAdapterRuntimeTypeWrapperTest.java | 37 +---- 14 files changed, 134 insertions(+), 397 deletions(-) diff --git a/gson/src/test/java/com/google/gson/GsonBuilderTest.java b/gson/src/test/java/com/google/gson/GsonBuilderTest.java index f5890e7161..757183fec0 100644 --- a/gson/src/test/java/com/google/gson/GsonBuilderTest.java +++ b/gson/src/test/java/com/google/gson/GsonBuilderTest.java @@ -24,7 +24,6 @@ import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; -import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.text.DateFormat; @@ -57,13 +56,7 @@ public void testCreatingMoreThanOnce() { assertThat(gson).isNotNull(); assertThat(builder.create()).isNotNull(); - builder.setFieldNamingStrategy( - new FieldNamingStrategy() { - @Override - public String translateName(Field f) { - return "test"; - } - }); + builder.setFieldNamingStrategy(f -> "test"); Gson otherGson = builder.create(); assertThat(otherGson).isNotNull(); @@ -94,23 +87,15 @@ public void write(JsonWriter out, CustomClass1 value) throws IOException { out.value("custom-adapter"); } }); + gsonBuilder.registerTypeHierarchyAdapter( CustomClass2.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - CustomClass2 src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive("custom-hierarchy-adapter"); - } - }); + (JsonSerializer) + (src, typeOfSrc, context) -> new JsonPrimitive("custom-hierarchy-adapter")); + gsonBuilder.registerTypeAdapter( CustomClass3.class, - new InstanceCreator() { - @Override - public CustomClass3 createInstance(Type type) { - return new CustomClass3("custom-instance"); - } - }); + (InstanceCreator) type -> new CustomClass3("custom-instance")); assertDefaultGson(gson); // New GsonBuilder created from `gson` should not have been affected by changes diff --git a/gson/src/test/java/com/google/gson/GsonTest.java b/gson/src/test/java/com/google/gson/GsonTest.java index 94c4409a42..45cd1d4479 100644 --- a/gson/src/test/java/com/google/gson/GsonTest.java +++ b/gson/src/test/java/com/google/gson/GsonTest.java @@ -28,7 +28,6 @@ import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; -import java.lang.reflect.Type; import java.text.DateFormat; import java.util.ArrayList; import java.util.Collections; @@ -486,23 +485,15 @@ public void write(JsonWriter out, CustomClass1 value) throws IOException { out.value("custom-adapter"); } }); + gsonBuilder.registerTypeHierarchyAdapter( CustomClass2.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - CustomClass2 src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive("custom-hierarchy-adapter"); - } - }); + (JsonSerializer) + (src, typeOfSrc, context) -> new JsonPrimitive("custom-hierarchy-adapter")); + gsonBuilder.registerTypeAdapter( CustomClass3.class, - new InstanceCreator() { - @Override - public CustomClass3 createInstance(Type type) { - return new CustomClass3("custom-instance"); - } - }); + (InstanceCreator) type -> new CustomClass3("custom-instance")); assertDefaultGson(gson); // New GsonBuilder created from `gson` should not have been affected by changes either @@ -549,21 +540,11 @@ public void write(JsonWriter out, CustomClass1 value) throws IOException { }) .registerTypeHierarchyAdapter( CustomClass2.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - CustomClass2 src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive("custom-hierarchy-adapter"); - } - }) + (JsonSerializer) + (src, typeOfSrc, context) -> new JsonPrimitive("custom-hierarchy-adapter")) .registerTypeAdapter( CustomClass3.class, - new InstanceCreator() { - @Override - public CustomClass3 createInstance(Type type) { - return new CustomClass3("custom-instance"); - } - }) + (InstanceCreator) type -> new CustomClass3("custom-instance")) .create(); assertCustomGson(gson); @@ -585,21 +566,11 @@ public void write(JsonWriter out, CustomClass1 value) throws IOException { }); gsonBuilder.registerTypeHierarchyAdapter( CustomClass2.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - CustomClass2 src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive("overwritten custom-hierarchy-adapter"); - } - }); + (JsonSerializer) + (src, typeOfSrc, context) -> new JsonPrimitive("overwritten custom-hierarchy-adapter")); gsonBuilder.registerTypeAdapter( CustomClass3.class, - new InstanceCreator() { - @Override - public CustomClass3 createInstance(Type type) { - return new CustomClass3("overwritten custom-instance"); - } - }); + (InstanceCreator) type -> new CustomClass3("overwritten custom-instance")); // `gson` object should not have been affected by changes to new GsonBuilder assertCustomGson(gson); diff --git a/gson/src/test/java/com/google/gson/GsonTypeAdapterTest.java b/gson/src/test/java/com/google/gson/GsonTypeAdapterTest.java index 5b4fb49907..969405eb0f 100644 --- a/gson/src/test/java/com/google/gson/GsonTypeAdapterTest.java +++ b/gson/src/test/java/com/google/gson/GsonTypeAdapterTest.java @@ -152,13 +152,8 @@ private static void assertSerialized( boolean registerAbstractHierarchyDeserializer, Object instance) { JsonDeserializer deserializer = - new JsonDeserializer<>() { - @Override - public Abstract deserialize( - JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - throw new AssertionError(); - } + (json, typeOfT, context) -> { + throw new AssertionError(); }; GsonBuilder builder = new GsonBuilder(); if (registerAbstractDeserializer) { diff --git a/gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java b/gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java index fe77cddd69..b4af36128a 100644 --- a/gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java +++ b/gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java @@ -112,23 +112,20 @@ public void testMultiThreadDeserialization() throws InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(10); for (int taskCount = 0; taskCount < 10; taskCount++) { executor.execute( - new Runnable() { - @Override - public void run() { - try { - startLatch.await(); - for (int i = 0; i < 10; i++) { - MyObject deserialized = - gson.fromJson("{'a':'hello','b':'world','i':1}", MyObject.class); - assertThat(deserialized.a).isEqualTo("hello"); - assertThat(deserialized.b).isEqualTo("world"); - assertThat(deserialized.i).isEqualTo(1); - } - } catch (Throwable t) { - error.set(t); - } finally { - finishedLatch.countDown(); + () -> { + try { + startLatch.await(); + for (int i = 0; i < 10; i++) { + MyObject deserialized = + gson.fromJson("{'a':'hello','b':'world','i':1}", MyObject.class); + assertThat(deserialized.a).isEqualTo("hello"); + assertThat(deserialized.b).isEqualTo("world"); + assertThat(deserialized.i).isEqualTo(1); } + } catch (Throwable t) { + error.set(t); + } finally { + finishedLatch.countDown(); } }); } diff --git a/gson/src/test/java/com/google/gson/functional/CustomDeserializerTest.java b/gson/src/test/java/com/google/gson/functional/CustomDeserializerTest.java index 732f1a3cb7..075399a864 100644 --- a/gson/src/test/java/com/google/gson/functional/CustomDeserializerTest.java +++ b/gson/src/test/java/com/google/gson/functional/CustomDeserializerTest.java @@ -123,15 +123,12 @@ public void testJsonTypeFieldBasedDeserialization() { new GsonBuilder() .registerTypeAdapter( MyBase.class, - new JsonDeserializer() { - @Override - public MyBase deserialize( - JsonElement json, Type pojoType, JsonDeserializationContext context) - throws JsonParseException { - String type = json.getAsJsonObject().get(MyBase.TYPE_ACCESS).getAsString(); - return context.deserialize(json, SubTypes.valueOf(type).getSubclass()); - } - }) + (JsonDeserializer) + (json1, pojoType, context) -> { + String type = json1.getAsJsonObject().get(MyBase.TYPE_ACCESS).getAsString(); + + return context.deserialize(json1, SubTypes.valueOf(type).getSubclass()); + }) .create(); SubType1 target = (SubType1) gson.fromJson(json, MyBase.class); assertThat(target.field1).isEqualTo("abc"); @@ -170,15 +167,7 @@ public void testCustomDeserializerReturnsNullForTopLevelObject() { Gson gson = new GsonBuilder() .registerTypeAdapter( - Base.class, - new JsonDeserializer() { - @Override - public Base deserialize( - JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - return null; - } - }) + Base.class, (JsonDeserializer) (json, typeOfT, context) -> null) .create(); String json = "{baseName:'Base',subName:'SubRevised'}"; Base target = gson.fromJson(json, Base.class); @@ -190,15 +179,7 @@ public void testCustomDeserializerReturnsNull() { Gson gson = new GsonBuilder() .registerTypeAdapter( - Base.class, - new JsonDeserializer() { - @Override - public Base deserialize( - JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - return null; - } - }) + Base.class, (JsonDeserializer) (json, typeOfT, context) -> null) .create(); String json = "{base:{baseName:'Base',subName:'SubRevised'}}"; ClassWithBaseField target = gson.fromJson(json, ClassWithBaseField.class); @@ -210,15 +191,7 @@ public void testCustomDeserializerReturnsNullForArrayElements() { Gson gson = new GsonBuilder() .registerTypeAdapter( - Base.class, - new JsonDeserializer() { - @Override - public Base deserialize( - JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - return null; - } - }) + Base.class, (JsonDeserializer) (json, typeOfT, context) -> null) .create(); String json = "[{baseName:'Base'},{baseName:'Base'}]"; Base[] target = gson.fromJson(json, Base[].class); @@ -231,15 +204,7 @@ public void testCustomDeserializerReturnsNullForArrayElementsForArrayField() { Gson gson = new GsonBuilder() .registerTypeAdapter( - Base.class, - new JsonDeserializer() { - @Override - public Base deserialize( - JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - return null; - } - }) + Base.class, (JsonDeserializer) (json, typeOfT, context) -> null) .create(); String json = "{bases:[{baseName:'Base'},{baseName:'Base'}]}"; ClassWithBaseArray target = gson.fromJson(json, ClassWithBaseArray.class); diff --git a/gson/src/test/java/com/google/gson/functional/CustomSerializerTest.java b/gson/src/test/java/com/google/gson/functional/CustomSerializerTest.java index 9b8a9b8865..de6d57f194 100644 --- a/gson/src/test/java/com/google/gson/functional/CustomSerializerTest.java +++ b/gson/src/test/java/com/google/gson/functional/CustomSerializerTest.java @@ -23,7 +23,6 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.common.TestTypes.Base; import com.google.gson.common.TestTypes.BaseSerializer; @@ -31,7 +30,6 @@ import com.google.gson.common.TestTypes.ClassWithBaseField; import com.google.gson.common.TestTypes.Sub; import com.google.gson.common.TestTypes.SubSerializer; -import java.lang.reflect.Type; import org.junit.Test; /** @@ -98,14 +96,7 @@ public void testSerializerReturnsNull() { Gson gson = new GsonBuilder() .registerTypeAdapter( - Base.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - Base src, Type typeOfSrc, JsonSerializationContext context) { - return null; - } - }) + Base.class, (JsonSerializer) (src, typeOfSrc, context) -> null) .create(); JsonElement json = gson.toJsonTree(new Base()); assertThat(json.isJsonNull()).isTrue(); diff --git a/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java index 7499aa8a42..cce0107f0f 100644 --- a/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java @@ -63,18 +63,13 @@ public void testCustomSerializers() { builder .registerTypeAdapter( ClassWithCustomTypeConverter.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - ClassWithCustomTypeConverter src, - Type typeOfSrc, - JsonSerializationContext context) { - JsonObject json = new JsonObject(); - json.addProperty("bag", 5); - json.addProperty("value", 25); - return json; - } - }) + (JsonSerializer) + (src, typeOfSrc, context) -> { + JsonObject json = new JsonObject(); + json.addProperty("bag", 5); + json.addProperty("value", 25); + return json; + }) .create(); ClassWithCustomTypeConverter target = new ClassWithCustomTypeConverter(); assertThat(gson.toJson(target)).isEqualTo("{\"bag\":5,\"value\":25}"); @@ -86,16 +81,13 @@ public void testCustomDeserializers() { new GsonBuilder() .registerTypeAdapter( ClassWithCustomTypeConverter.class, - new JsonDeserializer() { - @Override - public ClassWithCustomTypeConverter deserialize( - JsonElement json, Type typeOfT, JsonDeserializationContext context) { - JsonObject jsonObject = json.getAsJsonObject(); - int value = jsonObject.get("bag").getAsInt(); - return new ClassWithCustomTypeConverter( - new BagOfPrimitives(value, value, false, ""), value); - } - }) + (JsonDeserializer) + (json, typeOfT, context) -> { + JsonObject jsonObject = json.getAsJsonObject(); + int value = jsonObject.get("bag").getAsInt(); + return new ClassWithCustomTypeConverter( + new BagOfPrimitives(value, value, false, ""), value); + }) .create(); String json = "{\"bag\":5,\"value\":25}"; ClassWithCustomTypeConverter target = gson.fromJson(json, ClassWithCustomTypeConverter.class); @@ -133,13 +125,7 @@ public void testCustomNestedSerializers() { new GsonBuilder() .registerTypeAdapter( BagOfPrimitives.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - BagOfPrimitives src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(6); - } - }) + (JsonSerializer) (src, typeOfSrc, context) -> new JsonPrimitive(6)) .create(); ClassWithCustomTypeConverter target = new ClassWithCustomTypeConverter(); assertThat(gson.toJson(target)).isEqualTo("{\"bag\":6,\"value\":10}"); @@ -151,15 +137,11 @@ public void testCustomNestedDeserializers() { new GsonBuilder() .registerTypeAdapter( BagOfPrimitives.class, - new JsonDeserializer() { - @Override - public BagOfPrimitives deserialize( - JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - int value = json.getAsInt(); - return new BagOfPrimitives(value, value, false, ""); - } - }) + (JsonDeserializer) + (json, typeOfT, context) -> { + int value = json.getAsInt(); + return new BagOfPrimitives(value, value, false, ""); + }) .create(); String json = "{\"bag\":7,\"value\":25}"; ClassWithCustomTypeConverter target = gson.fromJson(json, ClassWithCustomTypeConverter.class); @@ -172,15 +154,12 @@ public void testCustomTypeAdapterDoesNotAppliesToSubClasses() { new GsonBuilder() .registerTypeAdapter( Base.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - Base src, Type typeOfSrc, JsonSerializationContext context) { - JsonObject json = new JsonObject(); - json.addProperty("value", src.baseValue); - return json; - } - }) + (JsonSerializer) + (src, typeOfSrc, context) -> { + JsonObject json = new JsonObject(); + json.addProperty("value", src.baseValue); + return json; + }) .create(); Base b = new Base(); String json = gson.toJson(b); @@ -196,15 +175,12 @@ public void testCustomTypeAdapterAppliesToSubClassesSerializedAsBaseClass() { new GsonBuilder() .registerTypeAdapter( Base.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - Base src, Type typeOfSrc, JsonSerializationContext context) { - JsonObject json = new JsonObject(); - json.addProperty("value", src.baseValue); - return json; - } - }) + (JsonSerializer) + (src, typeOfSrc, context) -> { + JsonObject json = new JsonObject(); + json.addProperty("value", src.baseValue); + return json; + }) .create(); Base b = new Base(); String json = gson.toJson(b); @@ -260,13 +236,7 @@ public void testCustomSerializerInvokedForPrimitives() { Gson gson = new GsonBuilder() .registerTypeAdapter( - boolean.class, - new JsonSerializer() { - @Override - public JsonElement serialize(Boolean s, Type t, JsonSerializationContext c) { - return new JsonPrimitive(s ? 1 : 0); - } - }) + boolean.class, (JsonSerializer) (s, t, c) -> new JsonPrimitive(s ? 1 : 0)) .create(); assertThat(gson.toJson(true, boolean.class)).isEqualTo("1"); assertThat(gson.toJson(true, Boolean.class)).isEqualTo("true"); @@ -278,13 +248,7 @@ public void testCustomDeserializerInvokedForPrimitives() { new GsonBuilder() .registerTypeAdapter( boolean.class, - new JsonDeserializer() { - @Override - public Boolean deserialize( - JsonElement json, Type t, JsonDeserializationContext context) { - return json.getAsInt() != 0; - } - }) + (JsonDeserializer) (json, t, context) -> json.getAsInt() != 0) .create(); assertThat(gson.fromJson("1", boolean.class)).isEqualTo(Boolean.TRUE); assertThat(gson.fromJson("true", Boolean.class)).isEqualTo(Boolean.TRUE); @@ -296,17 +260,14 @@ public void testCustomByteArraySerializer() { new GsonBuilder() .registerTypeAdapter( byte[].class, - new JsonSerializer() { - @Override - public JsonElement serialize( - byte[] src, Type typeOfSrc, JsonSerializationContext context) { - StringBuilder sb = new StringBuilder(src.length); - for (byte b : src) { - sb.append(b); - } - return new JsonPrimitive(sb.toString()); - } - }) + (JsonSerializer) + (src, typeOfSrc, context) -> { + StringBuilder sb = new StringBuilder(src.length); + for (byte b : src) { + sb.append(b); + } + return new JsonPrimitive(sb.toString()); + }) .create(); byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; String json = gson.toJson(data); @@ -319,19 +280,15 @@ public void testCustomByteArrayDeserializerAndInstanceCreator() { new GsonBuilder() .registerTypeAdapter( byte[].class, - new JsonDeserializer() { - @Override - public byte[] deserialize( - JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - String str = json.getAsString(); - byte[] data = new byte[str.length()]; - for (int i = 0; i < data.length; ++i) { - data[i] = Byte.parseByte("" + str.charAt(i)); - } - return data; - } - }); + (JsonDeserializer) + (json, typeOfT, context) -> { + String str = json.getAsString(); + byte[] data = new byte[str.length()]; + for (int i = 0; i < data.length; ++i) { + data[i] = Byte.parseByte("" + str.charAt(i)); + } + return data; + }); Gson gson = gsonBuilder.create(); String json = "'0123456789'"; byte[] actual = gson.fromJson(json, byte[].class); diff --git a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java index 9744d47955..c3892188be 100644 --- a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java @@ -22,7 +22,6 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; -import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonNull; @@ -608,14 +607,7 @@ public void testDateSerializationWithPatternNotOverridenByTypeAdapter() { .setDateFormat(pattern) .registerTypeAdapter( Date.class, - new JsonDeserializer() { - @Override - public Date deserialize( - JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - return new Date(1315806903103L); - } - }) + (JsonDeserializer) (json, typeOfT, context) -> new Date(1315806903103L)) .create(); Date now = new Date(1315806903103L); diff --git a/gson/src/test/java/com/google/gson/functional/InstanceCreatorTest.java b/gson/src/test/java/com/google/gson/functional/InstanceCreatorTest.java index 5a76b9f175..91eb54e35e 100644 --- a/gson/src/test/java/com/google/gson/functional/InstanceCreatorTest.java +++ b/gson/src/test/java/com/google/gson/functional/InstanceCreatorTest.java @@ -44,14 +44,7 @@ public class InstanceCreatorTest { public void testInstanceCreatorReturnsBaseType() { Gson gson = new GsonBuilder() - .registerTypeAdapter( - Base.class, - new InstanceCreator() { - @Override - public Base createInstance(Type type) { - return new Base(); - } - }) + .registerTypeAdapter(Base.class, (InstanceCreator) type -> new Base()) .create(); String json = "{baseName:'BaseRevised',subName:'Sub'}"; Base base = gson.fromJson(json, Base.class); @@ -62,14 +55,7 @@ public Base createInstance(Type type) { public void testInstanceCreatorReturnsSubTypeForTopLevelObject() { Gson gson = new GsonBuilder() - .registerTypeAdapter( - Base.class, - new InstanceCreator() { - @Override - public Base createInstance(Type type) { - return new Sub(); - } - }) + .registerTypeAdapter(Base.class, (InstanceCreator) type -> new Sub()) .create(); String json = "{baseName:'Base',subName:'SubRevised'}"; @@ -85,14 +71,7 @@ public Base createInstance(Type type) { public void testInstanceCreatorReturnsSubTypeForField() { Gson gson = new GsonBuilder() - .registerTypeAdapter( - Base.class, - new InstanceCreator() { - @Override - public Base createInstance(Type type) { - return new Sub(); - } - }) + .registerTypeAdapter(Base.class, (InstanceCreator) type -> new Sub()) .create(); String json = "{base:{baseName:'Base',subName:'SubRevised'}}"; ClassWithBaseField target = gson.fromJson(json, ClassWithBaseField.class); @@ -105,13 +84,7 @@ public Base createInstance(Type type) { public void testInstanceCreatorForCollectionType() { @SuppressWarnings("serial") class SubArrayList extends ArrayList {} - InstanceCreator> listCreator = - new InstanceCreator<>() { - @Override - public List createInstance(Type type) { - return new SubArrayList<>(); - } - }; + InstanceCreator> listCreator = type -> new SubArrayList<>(); Type listOfStringType = new TypeToken>() {}.getType(); Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create(); List list = gson.fromJson("[\"a\"]", listOfStringType); @@ -123,13 +96,7 @@ public List createInstance(Type type) { public void testInstanceCreatorForParametrizedType() { @SuppressWarnings("serial") class SubTreeSet extends TreeSet {} - InstanceCreator> sortedSetCreator = - new InstanceCreator<>() { - @Override - public SortedSet createInstance(Type type) { - return new SubTreeSet<>(); - } - }; + InstanceCreator> sortedSetCreator = type -> new SubTreeSet<>(); Gson gson = new GsonBuilder().registerTypeAdapter(SortedSet.class, sortedSetCreator).create(); Type sortedSetType = new TypeToken>() {}.getType(); diff --git a/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnClassesTest.java b/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnClassesTest.java index a92ee2a2e4..a4e694112b 100644 --- a/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnClassesTest.java +++ b/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnClassesTest.java @@ -26,7 +26,6 @@ import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; @@ -98,12 +97,7 @@ public A read(JsonReader in) throws IOException { @Test public void testRegisteredSerializerOverridesJsonAdapter() { JsonSerializer serializer = - new JsonSerializer<>() { - @Override - public JsonElement serialize(A src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive("registeredSerializer"); - } - }; + (src, typeOfSrc, context) -> new JsonPrimitive("registeredSerializer"); Gson gson = new GsonBuilder().registerTypeAdapter(A.class, serializer).create(); String json = gson.toJson(new A("abcd")); assertThat(json).isEqualTo("\"registeredSerializer\""); @@ -114,14 +108,7 @@ public JsonElement serialize(A src, Type typeOfSrc, JsonSerializationContext con /** The deserializer overrides Json adapter, but for serializer the jsonAdapter is used. */ @Test public void testRegisteredDeserializerOverridesJsonAdapter() { - JsonDeserializer deserializer = - new JsonDeserializer<>() { - @Override - public A deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - return new A("registeredDeserializer"); - } - }; + JsonDeserializer deserializer = (json, typeOfT, context) -> new A("registeredDeserializer"); Gson gson = new GsonBuilder().registerTypeAdapter(A.class, deserializer).create(); String json = gson.toJson(new A("abcd")); assertThat(json).isEqualTo("\"jsonAdapter\""); diff --git a/gson/src/test/java/com/google/gson/functional/MapTest.java b/gson/src/test/java/com/google/gson/functional/MapTest.java index 99ac53b1d2..9ab43d325c 100644 --- a/gson/src/test/java/com/google/gson/functional/MapTest.java +++ b/gson/src/test/java/com/google/gson/functional/MapTest.java @@ -27,7 +27,6 @@ import com.google.gson.JsonParseException; import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.JsonSyntaxException; import com.google.gson.common.TestTypes; @@ -311,14 +310,7 @@ public void testMapStandardSubclassDeserialization() { public void testMapSubclassDeserialization() { Gson gson = new GsonBuilder() - .registerTypeAdapter( - MyMap.class, - new InstanceCreator() { - @Override - public MyMap createInstance(Type type) { - return new MyMap(); - } - }) + .registerTypeAdapter(MyMap.class, (InstanceCreator) type -> new MyMap()) .create(); String json = "{\"a\":1,\"b\":2}"; MyMap map = gson.fromJson(json, MyMap.class); @@ -334,17 +326,14 @@ public void testCustomSerializerForSpecificMapType() { new GsonBuilder() .registerTypeAdapter( type, - new JsonSerializer>() { - @Override - public JsonElement serialize( - Map src, Type typeOfSrc, JsonSerializationContext context) { - JsonArray array = new JsonArray(); - for (long value : src.values()) { - array.add(new JsonPrimitive(value)); - } - return array; - } - }) + (JsonSerializer>) + (src, typeOfSrc, context) -> { + JsonArray array = new JsonArray(); + for (long value : src.values()) { + array.add(new JsonPrimitive(value)); + } + return array; + }) .create(); Map src = new LinkedHashMap<>(); @@ -526,13 +515,7 @@ public final void testInterfaceTypeMapWithSerializer() { "{\"bases\":{\"Test\":" + baseTypeJson + "},\"subs\":{\"Test\":" + subTypeJson + "}}"; JsonSerializer baseTypeAdapter = - new JsonSerializer<>() { - @Override - public JsonElement serialize( - TestTypes.Base src, Type typeOfSrc, JsonSerializationContext context) { - return baseTypeJsonElement; - } - }; + (src, typeOfSrc, context) -> baseTypeJsonElement; Gson gson = new GsonBuilder() diff --git a/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java b/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java index d84baa4b62..7c90bf6515 100644 --- a/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java +++ b/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java @@ -20,7 +20,6 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonNull; @@ -229,13 +228,8 @@ public void testCustomTypeAdapterPassesNullSerialization() { new GsonBuilder() .registerTypeAdapter( ObjectWithField.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - ObjectWithField src, Type typeOfSrc, JsonSerializationContext context) { - return context.serialize(null); - } - }) + (JsonSerializer) + (src, typeOfSrc, context) -> context.serialize(null)) .create(); ObjectWithField target = new ObjectWithField(); target.value = "value1"; @@ -249,13 +243,8 @@ public void testCustomTypeAdapterPassesNullDeserialization() { new GsonBuilder() .registerTypeAdapter( ObjectWithField.class, - new JsonDeserializer() { - @Override - public ObjectWithField deserialize( - JsonElement json, Type type, JsonDeserializationContext context) { - return context.deserialize(null, type); - } - }) + (JsonDeserializer) + (json, type, context) -> context.deserialize(null, type)) .create(); String json = "{value:'value1'}"; ObjectWithField target = gson.fromJson(json, ObjectWithField.class); diff --git a/gson/src/test/java/com/google/gson/functional/ObjectTest.java b/gson/src/test/java/com/google/gson/functional/ObjectTest.java index 13582fa218..5812daf8f1 100644 --- a/gson/src/test/java/com/google/gson/functional/ObjectTest.java +++ b/gson/src/test/java/com/google/gson/functional/ObjectTest.java @@ -24,14 +24,11 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.InstanceCreator; -import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; import com.google.gson.JsonIOException; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.JsonSyntaxException; import com.google.gson.common.TestTypes.ArrayOfObjects; @@ -388,13 +385,8 @@ public void testAnonymousLocalClassesCustomSerialization() { new GsonBuilder() .registerTypeHierarchyAdapter( ClassWithNoFields.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - ClassWithNoFields src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive("custom-value"); - } - }) + (JsonSerializer) + (src, typeOfSrc, context) -> new JsonPrimitive("custom-value")) .create(); assertThat( @@ -409,13 +401,8 @@ class Local {} new GsonBuilder() .registerTypeAdapter( Local.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - Local src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive("custom-value"); - } - }) + (JsonSerializer) + (src, typeOfSrc, context) -> new JsonPrimitive("custom-value")) .create(); assertThat(gson.toJson(new Local())).isEqualTo("\"custom-value\""); } @@ -426,13 +413,8 @@ public void testAnonymousLocalClassesCustomDeserialization() { new GsonBuilder() .registerTypeHierarchyAdapter( ClassWithNoFields.class, - new JsonDeserializer() { - @Override - public ClassWithNoFields deserialize( - JsonElement json, Type typeOfT, JsonDeserializationContext context) { - return new ClassWithNoFields(); - } - }) + (JsonDeserializer) + (json, typeOfT, context) -> new ClassWithNoFields()) .create(); assertThat(gson.fromJson("{}", ClassWithNoFields.class)).isNotNull(); @@ -445,13 +427,10 @@ class Local {} new GsonBuilder() .registerTypeAdapter( Local.class, - new JsonDeserializer() { - @Override - public Local deserialize( - JsonElement json, Type typeOfT, JsonDeserializationContext context) { - throw new AssertionError("should not be called"); - } - }) + (JsonDeserializer) + (json, typeOfT, context) -> { + throw new AssertionError("should not be called"); + }) .create(); // Custom deserializer is ignored assertThat(gson.fromJson("{}", Local.class)).isNull(); diff --git a/gson/src/test/java/com/google/gson/functional/TypeAdapterRuntimeTypeWrapperTest.java b/gson/src/test/java/com/google/gson/functional/TypeAdapterRuntimeTypeWrapperTest.java index 174c0f061a..b81907a335 100644 --- a/gson/src/test/java/com/google/gson/functional/TypeAdapterRuntimeTypeWrapperTest.java +++ b/gson/src/test/java/com/google/gson/functional/TypeAdapterRuntimeTypeWrapperTest.java @@ -24,7 +24,6 @@ import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; @@ -63,13 +62,7 @@ public void testJsonSerializer() { new GsonBuilder() .registerTypeAdapter( Base.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - Base src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive("serializer"); - } - }) + (JsonSerializer) (src, typeOfSrc, context) -> new JsonPrimitive("serializer")) .create(); String json = gson.toJson(new Container()); @@ -148,13 +141,8 @@ public void testJsonDeserializer_JsonSerializerDelegate() { // Register JsonSerializer as delegate .registerTypeAdapter( Base.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - Base src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive("custom delegate"); - } - }) + (JsonSerializer) + (src, typeOfSrc, context) -> new JsonPrimitive("custom delegate")) .registerTypeAdapter(Base.class, new Deserializer()) .create(); @@ -175,22 +163,13 @@ public void testJsonDeserializer_SubclassBackwardCompatibility() { new GsonBuilder() .registerTypeAdapter( Subclass.class, - new JsonDeserializer() { - @Override - public Subclass deserialize( - JsonElement json, Type typeOfT, JsonDeserializationContext context) { - throw new AssertionError("not needed for this test"); - } - }) + (JsonDeserializer) + (json, typeOfT, context) -> { + throw new AssertionError("not needed for this test"); + }) .registerTypeAdapter( Base.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - Base src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive("base"); - } - }) + (JsonSerializer) (src, typeOfSrc, context) -> new JsonPrimitive("base")) .create(); String json = gson.toJson(new Container());