From c7c645db2f73e5e392d117c391437ff15815f057 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Wed, 15 Nov 2023 00:09:54 +0100 Subject: [PATCH] Formatting follow-up (#2540) * Formatting follow-up - Adds formatting commits to .git-blame-ignore-revs so that they don't distract during Git blame - Restores hard line breaks in Troubleshooting.md using `\` instead of trailing spaces - Changes formatting of some string literals and comments - Fixes accidental Javadoc and comment issues introduced by manual changes of formatting commit - Fixes license header in $Gson$Types.java erroneously being a Javadoc comment and being reformatted - Slightly changes `JsonReader` `getPath` and `getPreviousPath` documentation to help Javadoc detect first sentence as summary * Remove `spotless:off` markers * Add empty line before comment * Check format for .github YAML files --- .git-blame-ignore-revs | 5 +++ .github/pull_request_template.md | 4 ++- Troubleshooting.md | 10 +++--- .../PostConstructAdapterFactoryTest.java | 4 +-- .../gson/native_test/ReflectionTest.java | 4 +-- gson/pom.xml | 12 +++---- .../main/java/com/google/gson/JsonArray.java | 3 +- .../com/google/gson/internal/$Gson$Types.java | 20 ++++++----- .../gson/internal/ConstructorConstructor.java | 6 ++-- .../com/google/gson/internal/JavaVersion.java | 2 +- .../google/gson/internal/UnsafeAllocator.java | 4 +-- ...onAdapterAnnotationTypeAdapterFactory.java | 2 +- .../bind/ReflectiveTypeAdapterFactory.java | 24 +++++-------- .../bind/TypeAdapterRuntimeTypeWrapper.java | 4 +-- .../gson/internal/bind/TypeAdapters.java | 7 ++-- .../gson/internal/bind/util/ISO8601Utils.java | 6 ++-- .../internal/reflect/ReflectionHelper.java | 5 ++- .../com/google/gson/reflect/TypeToken.java | 10 +++--- .../com/google/gson/stream/JsonReader.java | 8 ++--- .../com/google/gson/stream/JsonWriter.java | 6 ++-- .../functional/DefaultTypeAdaptersTest.java | 4 +-- .../gson/functional/FormattingStyleTest.java | 9 ++++- .../JsonAdapterAnnotationOnClassesTest.java | 5 ++- .../JsonAdapterAnnotationOnFieldsTest.java | 3 +- .../google/gson/functional/LeniencyTest.java | 4 ++- .../com/google/gson/functional/MapTest.java | 4 +-- .../google/gson/functional/PrimitiveTest.java | 8 ++--- .../ReflectionAccessFilterTest.java | 4 +-- .../google/gson/internal/JavaVersionTest.java | 17 ++++----- .../bind/RecursiveTypesResolveTest.java | 6 ++-- .../google/gson/reflect/TypeTokenTest.java | 3 +- .../google/gson/stream/JsonReaderTest.java | 36 +++++++++++-------- .../google/gson/stream/JsonWriterTest.java | 15 ++++++-- pom.xml | 30 +++++++++++----- 34 files changed, 167 insertions(+), 127 deletions(-) create mode 100644 .git-blame-ignore-revs diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 00000000..6d87ad4b --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,5 @@ +# Ignore commit which reformatted code +2c94c757a6a9426cc2fe47bc1c63f69e7c73b7b4 + +# Ignore commit which changed line endings consistently to LF +c2a0e4634a2100494159add78db2ee06f5eb9be6 diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 7041029d..ed9ab5ab 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -19,7 +19,9 @@ ### Checklist -- [ ] New code follows the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html) +- [ ] New code follows the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html)\ + This is automatically checked by `mvn verify`, but can also be checked on its own using `mvn spotless:check`.\ + Style violations can be fixed using `mvn spotless:apply`; this can be done in a separate commit to verify that it did not cause undesired changes. - [ ] If necessary, new public API validates arguments, for example rejects `null` - [ ] New public API has Javadoc - [ ] Javadoc uses `@since $next-version$` diff --git a/Troubleshooting.md b/Troubleshooting.md index be4000f4..d79457c4 100644 --- a/Troubleshooting.md +++ b/Troubleshooting.md @@ -127,8 +127,8 @@ For example, let's assume you want to deserialize the following JSON data: } ``` -This will fail with an exception similar to this one: `MalformedJsonException: Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON at line 5 column 4 path $.languages[2]` -The problem here is the trailing comma (`,`) after `"French"`, trailing commas are not allowed by the JSON specification. The location information "line 5 column 4" points to the `]` in the JSON data (with some slight inaccuracies) because Gson expected another value after `,` instead of the closing `]`. The JSONPath `$.languages[2]` in the exception message also points there: `$.` refers to the root object, `languages` refers to its member of that name and `[2]` refers to the (missing) third value in the JSON array value of that member (numbering starts at 0, so it is `[2]` instead of `[3]`). +This will fail with an exception similar to this one: `MalformedJsonException: Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON at line 5 column 4 path $.languages[2]`\ +The problem here is the trailing comma (`,`) after `"French"`, trailing commas are not allowed by the JSON specification. The location information "line 5 column 4" points to the `]` in the JSON data (with some slight inaccuracies) because Gson expected another value after `,` instead of the closing `]`. The JSONPath `$.languages[2]` in the exception message also points there: `$.` refers to the root object, `languages` refers to its member of that name and `[2]` refers to the (missing) third value in the JSON array value of that member (numbering starts at 0, so it is `[2]` instead of `[3]`).\ The proper solution here is to fix the malformed JSON data. To spot syntax errors in the JSON data easily you can open it in an editor with support for JSON, for example Visual Studio Code. It will highlight within the JSON data the error location and show why the JSON data is considered invalid. @@ -178,8 +178,8 @@ And you want to deserialize the following JSON data: } ``` -This will fail with an exception similar to this one: `IllegalStateException: Expected a string but was BEGIN_ARRAY at line 2 column 17 path $.languages` -This means Gson expected a JSON string value but found the beginning of a JSON array (`[`). The location information "line 2 column 17" points to the `[` in the JSON data (with some slight inaccuracies), so does the JSONPath `$.languages` in the exception message. It refers to the `languages` member of the root object (`$.`). +This will fail with an exception similar to this one: `IllegalStateException: Expected a string but was BEGIN_ARRAY at line 2 column 17 path $.languages`\ +This means Gson expected a JSON string value but found the beginning of a JSON array (`[`). The location information "line 2 column 17" points to the `[` in the JSON data (with some slight inaccuracies), so does the JSONPath `$.languages` in the exception message. It refers to the `languages` member of the root object (`$.`).\ The solution here is to change in the `WebPage` class the field `String languages` to `List languages`. ## `IllegalStateException`: "Expected ... but was NULL" @@ -287,7 +287,7 @@ This will not initialize arbitrary classes, and it will throw a `ClassCastExcept ## `IllegalStateException`: 'TypeToken must be created with a type argument'
`RuntimeException`: 'Missing type parameter' -**Symptom:** An `IllegalStateException` with the message 'TypeToken must be created with a type argument' is thrown. +**Symptom:** An `IllegalStateException` with the message 'TypeToken must be created with a type argument' is thrown.\ For older Gson versions a `RuntimeException` with message 'Missing type parameter' is thrown. **Reason:** diff --git a/extras/src/test/java/com/google/gson/typeadapters/PostConstructAdapterFactoryTest.java b/extras/src/test/java/com/google/gson/typeadapters/PostConstructAdapterFactoryTest.java index c8ccb6bf..1ce937f7 100644 --- a/extras/src/test/java/com/google/gson/typeadapters/PostConstructAdapterFactoryTest.java +++ b/extras/src/test/java/com/google/gson/typeadapters/PostConstructAdapterFactoryTest.java @@ -52,8 +52,8 @@ public class PostConstructAdapterFactoryTest { // Throws NullPointerException without the fix in https://github.com/google/gson/pull/1103 String json = gson.toJson(sandwiches); assertEquals( - "{\"sandwiches\":[{\"bread\":\"white\",\"cheese\":\"cheddar\"},{\"bread\":\"whole" - + " wheat\",\"cheese\":\"swiss\"}]}", + "{\"sandwiches\":[{\"bread\":\"white\",\"cheese\":\"cheddar\"}," + + "{\"bread\":\"whole wheat\",\"cheese\":\"swiss\"}]}", json); MultipleSandwiches sandwichesFromJson = gson.fromJson(json, MultipleSandwiches.class); diff --git a/graal-native-image-test/src/test/java/com/google/gson/native_test/ReflectionTest.java b/graal-native-image-test/src/test/java/com/google/gson/native_test/ReflectionTest.java index 055f452e..6caa10eb 100644 --- a/graal-native-image-test/src/test/java/com/google/gson/native_test/ReflectionTest.java +++ b/graal-native-image-test/src/test/java/com/google/gson/native_test/ReflectionTest.java @@ -90,8 +90,8 @@ class ReflectionTest { assertThat(c.i).isEqualTo(1); c = gson.fromJson("{}", ClassWithoutDefaultConstructor.class); - // Class is instantiated with JDK Unsafe, so field keeps its default value instead of assigned - // -1 + // Class is instantiated with JDK Unsafe, therefore field keeps its default value instead of + // assigned -1 assertThat(c.i).isEqualTo(0); } diff --git a/gson/pom.xml b/gson/pom.xml index 172d3b3c..36c12cfa 100644 --- a/gson/pom.xml +++ b/gson/pom.xml @@ -139,11 +139,11 @@ maven-surefire-plugin - --illegal-access=deny @@ -239,7 +239,7 @@ - diff --git a/gson/src/main/java/com/google/gson/JsonArray.java b/gson/src/main/java/com/google/gson/JsonArray.java index d260780d..0d1cbd2f 100644 --- a/gson/src/main/java/com/google/gson/JsonArray.java +++ b/gson/src/main/java/com/google/gson/JsonArray.java @@ -163,7 +163,8 @@ public final class JsonArray extends JsonElement implements IterableLicensed under the Apache License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - *

http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - *

Unless required by applicable law or agreed to in writing, software distributed under the - * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing permissions and + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and * limitations under the License. */ + package com.google.gson.internal; import static com.google.gson.internal.$Gson$Preconditions.checkArgument; @@ -156,8 +159,7 @@ public final class $Gson$Types { } else { String className = type == null ? "null" : type.getClass().getName(); throw new IllegalArgumentException( - "Expected a Class, ParameterizedType, or " - + "GenericArrayType, but <" + "Expected a Class, ParameterizedType, or GenericArrayType, but <" + type + "> is of type " + className); diff --git a/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java b/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java index 65ac857f..b4deb747 100644 --- a/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java +++ b/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java @@ -303,8 +303,7 @@ public final class ConstructorConstructor { throw new RuntimeException( "Failed to invoke constructor '" + ReflectionHelper.constructorToString(constructor) - + "'" - + " with no args", + + "' with no args", e); } catch (InvocationTargetException e) { // TODO: don't wrap if cause is unchecked? @@ -312,8 +311,7 @@ public final class ConstructorConstructor { throw new RuntimeException( "Failed to invoke constructor '" + ReflectionHelper.constructorToString(constructor) - + "'" - + " with no args", + + "' with no args", e.getCause()); } catch (IllegalAccessException e) { throw ReflectionHelper.createExceptionForUnexpectedIllegalAccess(e); diff --git a/gson/src/main/java/com/google/gson/internal/JavaVersion.java b/gson/src/main/java/com/google/gson/internal/JavaVersion.java index d14ccee5..af981d56 100644 --- a/gson/src/main/java/com/google/gson/internal/JavaVersion.java +++ b/gson/src/main/java/com/google/gson/internal/JavaVersion.java @@ -37,7 +37,7 @@ public final class JavaVersion { version = extractBeginningInt(javaVersion); } if (version == -1) { - return 6; // Choose a minimum supported JDK version as default + return 6; // Choose minimum supported JDK version as default } return version; } diff --git a/gson/src/main/java/com/google/gson/internal/UnsafeAllocator.java b/gson/src/main/java/com/google/gson/internal/UnsafeAllocator.java index 8e18e92f..4a107360 100644 --- a/gson/src/main/java/com/google/gson/internal/UnsafeAllocator.java +++ b/gson/src/main/java/com/google/gson/internal/UnsafeAllocator.java @@ -121,8 +121,8 @@ public abstract class UnsafeAllocator { throw new UnsupportedOperationException( "Cannot allocate " + c - + ". Usage of JDK sun.misc.Unsafe is enabled, " - + "but it could not be used. Make sure your runtime is configured correctly."); + + ". Usage of JDK sun.misc.Unsafe is enabled, but it could not be used." + + " Make sure your runtime is configured correctly."); } }; } diff --git a/gson/src/main/java/com/google/gson/internal/bind/JsonAdapterAnnotationTypeAdapterFactory.java b/gson/src/main/java/com/google/gson/internal/bind/JsonAdapterAnnotationTypeAdapterFactory.java index 2cb9773c..0ce1b031 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/JsonAdapterAnnotationTypeAdapterFactory.java +++ b/gson/src/main/java/com/google/gson/internal/bind/JsonAdapterAnnotationTypeAdapterFactory.java @@ -71,7 +71,7 @@ public final class JsonAdapterAnnotationTypeAdapterFactory implements TypeAdapte return rawType.getAnnotation(JsonAdapter.class); } - // this is not safe; requires that user has specified correct adapter class for @JsonAdapter + // this is not safe; requires that user has specified correct adapter class for @JsonAdapter @SuppressWarnings("unchecked") @Override public TypeAdapter create(Gson gson, TypeToken targetType) { diff --git a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java index 663d3a66..06925d9e 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java @@ -228,8 +228,7 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory { throw new JsonParseException( "null is not allowed as value for record component '" + fieldName - + "'" - + " of primitive type; at path " + + "' of primitive type; at path " + reader.getPath()); } target[index] = fieldValue; @@ -244,8 +243,8 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory { checkAccessible(target, field); } else if (isStaticFinalField) { // Reflection does not permit setting value of `static final` field, even after calling - // `setAccessible` Handle this here to avoid causing IllegalAccessException when calling - // `Field.set` + // `setAccessible` + // Handle this here to avoid causing IllegalAccessException when calling `Field.set` String fieldDescription = ReflectionHelper.getAccessibleObjectDescription(field, false); throw new JsonIOException("Cannot set value of 'static final' " + fieldDescription); } @@ -279,8 +278,7 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory { + declaringType.getName() + " declares multiple JSON fields named '" + duplicateName - + "'; conflict is caused" - + " by fields " + + "'; conflict is caused by fields " + ReflectionHelper.fieldToString(field1) + " and " + ReflectionHelper.fieldToString(field2) @@ -313,8 +311,7 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory { + raw + " (supertype of " + originalRaw - + "). Register a TypeAdapter for this type" - + " or adjust the access filter."); + + "). Register a TypeAdapter for this type or adjust the access filter."); } blockInaccessible = filterResult == FilterResult.BLOCK_INACCESSIBLE; } @@ -603,8 +600,7 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory { throw new IllegalStateException( "Could not find the index in the constructor '" + ReflectionHelper.constructorToString(constructor) - + "'" - + " for field with name '" + + "' for field with name '" + field.fieldName + "', unable to determine which argument in the constructor the field corresponds" + " to. This is unexpected behavior, as we expect the RecordComponents to have the" @@ -624,13 +620,12 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory { } // Note: InstantiationException should be impossible because record class is not abstract; // IllegalArgumentException should not be possible unless a bad adapter returns objects of - // the wrong type + // the wrong type catch (InstantiationException | IllegalArgumentException e) { throw new RuntimeException( "Failed to invoke constructor '" + ReflectionHelper.constructorToString(constructor) - + "'" - + " with args " + + "' with args " + Arrays.toString(accumulator), e); } catch (InvocationTargetException e) { @@ -638,8 +633,7 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory { throw new RuntimeException( "Failed to invoke constructor '" + ReflectionHelper.constructorToString(constructor) - + "'" - + " with args " + + "' with args " + Arrays.toString(accumulator), e.getCause()); } diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapterRuntimeTypeWrapper.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapterRuntimeTypeWrapper.java index 20698e42..f64dbc65 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapterRuntimeTypeWrapper.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapterRuntimeTypeWrapper.java @@ -45,8 +45,8 @@ final class TypeAdapterRuntimeTypeWrapper extends TypeAdapter { // Order of preference for choosing type adapters // First preference: a type adapter registered for the runtime type // Second preference: a type adapter registered for the declared type - // Third preference: reflective type adapter for the runtime type (if it is a subclass of the - // declared type) + // Third preference: reflective type adapter for the runtime type + // (if it is a subclass of the declared type) // Fourth preference: reflective type adapter for the declared type TypeAdapter chosen = delegate; diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java index 75f64dc7..5d4bd1b5 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java @@ -401,8 +401,8 @@ public final class TypeAdapters { out.nullValue(); } else { // For backward compatibility don't call `JsonWriter.value(float)` because that method - // has - // been newly added and not all custom JsonWriter implementations might override it yet + // has been newly added and not all custom JsonWriter implementations might override + // it yet Number floatNumber = value instanceof Float ? value : value.floatValue(); out.value(floatNumber); } @@ -952,7 +952,8 @@ public final class TypeAdapters { public EnumTypeAdapter(final Class classOfT) { try { // Uses reflection to find enum constants to work around name mismatches for obfuscated - // classes Reflection access might throw SecurityException, therefore run this in privileged + // classes + // Reflection access might throw SecurityException, therefore run this in privileged // context; should be acceptable because this only retrieves enum constants, but does not // expose anything else Field[] constantFields = diff --git a/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java b/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java index 4f08b039..b9a0f996 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java +++ b/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java @@ -160,12 +160,14 @@ public class ISO8601Utils { // extract day int day = parseInt(date, offset, offset += 2); + // default time value int hour = 0; int minutes = 0; int seconds = 0; - int milliseconds = - 0; // always use 0 otherwise returned date will include millis of current time + + // always use 0 otherwise returned date will include millis of current time + int milliseconds = 0; // if the value has no time component (and no time zone), we are done boolean hasT = checkOffset(date, offset, 'T'); diff --git a/gson/src/main/java/com/google/gson/internal/reflect/ReflectionHelper.java b/gson/src/main/java/com/google/gson/internal/reflect/ReflectionHelper.java index 1bfdb83b..ded1e689 100644 --- a/gson/src/main/java/com/google/gson/internal/reflect/ReflectionHelper.java +++ b/gson/src/main/java/com/google/gson/internal/reflect/ReflectionHelper.java @@ -159,10 +159,9 @@ public class ReflectionHelper { } catch (Exception exception) { return "Failed making constructor '" + constructorToString(constructor) - + "' accessible;" - + " either increase its visibility or write a custom InstanceCreator or TypeAdapter for" + + "' accessible; either increase its visibility or write a custom InstanceCreator or" + + " TypeAdapter for its declaring type: " // Include the message since it might contain more detailed information - + " its declaring type: " + exception.getMessage() + getInaccessibleTroubleshootingSuffix(exception); } diff --git a/gson/src/main/java/com/google/gson/reflect/TypeToken.java b/gson/src/main/java/com/google/gson/reflect/TypeToken.java index cd633ebc..ceb35dc9 100644 --- a/gson/src/main/java/com/google/gson/reflect/TypeToken.java +++ b/gson/src/main/java/com/google/gson/reflect/TypeToken.java @@ -109,8 +109,8 @@ public class TypeToken { else if (superclass == TypeToken.class) { throw new IllegalStateException( "TypeToken must be created with a type argument: new TypeToken<...>() {}; When using code" - + " shrinkers (ProGuard, R8, ...) make sure that generic signatures are preserved.\n" - + "See " + + " shrinkers (ProGuard, R8, ...) make sure that generic signatures are preserved." + + "\nSee " + TroubleshootingGuide.createUrl("type-token-raw")); } @@ -416,8 +416,7 @@ public class TypeToken { throw new IllegalArgumentException( "Raw type " + rawClass.getName() - + " is not supported because" - + " it requires specifying an owner type"); + + " is not supported because it requires specifying an owner type"); } for (int i = 0; i < expectedArgsCount; i++) { @@ -433,8 +432,7 @@ public class TypeToken { throw new IllegalArgumentException( "Type argument " + typeArgument - + " does not satisfy bounds" - + " for type variable " + + " does not satisfy bounds for type variable " + typeVariable + " declared by " + rawType); diff --git a/gson/src/main/java/com/google/gson/stream/JsonReader.java b/gson/src/main/java/com/google/gson/stream/JsonReader.java index ee7f49cc..79795605 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonReader.java +++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java @@ -1339,8 +1339,8 @@ public class JsonReader implements Closeable { // Only update when object end is explicitly skipped, otherwise stack is not updated // anyways if (count == 0) { - pathNames[stackSize - 1] = - null; // Free the last path name so that it can be garbage collected + // Free the last path name so that it can be garbage collected + pathNames[stackSize - 1] = null; } stackSize--; count--; @@ -1616,7 +1616,7 @@ public class JsonReader implements Closeable { /** * Returns a JSONPath in dot-notation - * to the previous (or current) location in the JSON document: + * to the previous (or current) location in the JSON document. That means: * *

    *
  • For JSON arrays the path points to the index of the previous element.
    @@ -1634,7 +1634,7 @@ public class JsonReader implements Closeable { /** * Returns a JSONPath in dot-notation - * to the next (or current) location in the JSON document: + * to the next (or current) location in the JSON document. That means: * *
      *
    • For JSON arrays the path points to the index of the next element (even if there are no diff --git a/gson/src/main/java/com/google/gson/stream/JsonWriter.java b/gson/src/main/java/com/google/gson/stream/JsonWriter.java index af673058..23786520 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonWriter.java +++ b/gson/src/main/java/com/google/gson/stream/JsonWriter.java @@ -306,9 +306,9 @@ public class JsonWriter implements Closeable, Flushable { * @see #setStrictness(Strictness) */ @Deprecated - @SuppressWarnings( - "InlineMeSuggester") // Don't specify @InlineMe, so caller with `setLenient(false)` becomes - // aware of new Strictness.STRICT + // Don't specify @InlineMe, so caller with `setLenient(false)` becomes aware of new + // Strictness.STRICT + @SuppressWarnings("InlineMeSuggester") public final void setLenient(boolean lenient) { setStrictness(lenient ? Strictness.LENIENT : Strictness.LEGACY_STRICT); } 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 770996f9..a0c493cf 100644 --- a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java @@ -648,8 +648,8 @@ public class DefaultTypeAdaptersTest { assertThat(expected) .hasMessageThat() .isEqualTo( - "Expected a com.google.gson.JsonObject but was com.google.gson.JsonPrimitive; at path" - + " $"); + "Expected a com.google.gson.JsonObject but was com.google.gson.JsonPrimitive;" + + " at path $"); } } diff --git a/gson/src/test/java/com/google/gson/functional/FormattingStyleTest.java b/gson/src/test/java/com/google/gson/functional/FormattingStyleTest.java index 50937de7..2c4d5519 100644 --- a/gson/src/test/java/com/google/gson/functional/FormattingStyleTest.java +++ b/gson/src/test/java/com/google/gson/functional/FormattingStyleTest.java @@ -113,7 +113,14 @@ public class FormattingStyleTest { String expectedJson = buildExpected("\n", " ", true); assertThat(json).isEqualTo(expectedJson); // Sanity check to verify that `buildExpected` works correctly - assertThat(json).isEqualTo("{\n" + " \"a\": [\n" + " 1,\n" + " 2\n" + " ]\n" + "}"); + assertThat(json) + .isEqualTo( + "{\n" // + + " \"a\": [\n" // + + " 1,\n" // + + " 2\n" // + + " ]\n" // + + "}"); } @Test 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 9b40ddb4..7ca84d7a 100644 --- a/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnClassesTest.java +++ b/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnClassesTest.java @@ -474,9 +474,8 @@ public final class JsonAdapterAnnotationOnClassesTest { @Override public TypeAdapter create(Gson gson, TypeToken type) { return new TypeAdapter() { - @SuppressWarnings( - "SameNameButDifferent") // suppress Error Prone warning; should be clear that - // `Factory` refers to enclosing class + // suppress Error Prone warning; should be clear that `Factory` refers to enclosing class + @SuppressWarnings("SameNameButDifferent") private TypeAdapter delegate() { return gson.getDelegateAdapter(Factory.this, type); } diff --git a/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnFieldsTest.java b/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnFieldsTest.java index 4f835560..a5d355f0 100644 --- a/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnFieldsTest.java +++ b/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnFieldsTest.java @@ -604,8 +604,7 @@ public final class JsonAdapterAnnotationOnFieldsTest { assertThat(new Gson().toJson(serialized)).isEqualTo("{\"f\":\"value-custom\"}"); } - // suppress Error Prone warning; should be clear that `Factory` refers to nested class to nested - // class + // suppress Error Prone warning; should be clear that `Factory` refers to nested class @SuppressWarnings("SameNameButDifferent") private static class WithDelayedDelegatingFactory { @JsonAdapter(Factory.class) diff --git a/gson/src/test/java/com/google/gson/functional/LeniencyTest.java b/gson/src/test/java/com/google/gson/functional/LeniencyTest.java index aac42843..40197ae7 100644 --- a/gson/src/test/java/com/google/gson/functional/LeniencyTest.java +++ b/gson/src/test/java/com/google/gson/functional/LeniencyTest.java @@ -40,7 +40,9 @@ public class LeniencyTest { public void testLenientFromJson() { List json = gson.fromJson( - "" + "[ # One!\n" + " 'Hi' #Element!\n" + "] # Array!", + "[ # One!\n" // + + " 'Hi' #Element!\n" // + + "] # Array!", new TypeToken>() {}.getType()); assertThat(json).isEqualTo(singletonList("Hi")); } 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 766d6a58..b7a5f312 100644 --- a/gson/src/test/java/com/google/gson/functional/MapTest.java +++ b/gson/src/test/java/com/google/gson/functional/MapTest.java @@ -500,7 +500,7 @@ public class MapTest { String subTypeJson = new Gson().toJson(subType); String expected = - "{\"bases\":{\"Test\":" + subTypeJson + "}," + "\"subs\":{\"Test\":" + subTypeJson + "}}"; + "{\"bases\":{\"Test\":" + subTypeJson + "},\"subs\":{\"Test\":" + subTypeJson + "}}"; Gson gsonWithComplexKeys = new GsonBuilder().enableComplexMapKeySerialization().create(); String json = gsonWithComplexKeys.toJson(element); @@ -523,7 +523,7 @@ public class MapTest { final JsonElement baseTypeJsonElement = tempGson.toJsonTree(subType, TestTypes.Base.class); String baseTypeJson = tempGson.toJson(baseTypeJsonElement); String expected = - "{\"bases\":{\"Test\":" + baseTypeJson + "}," + "\"subs\":{\"Test\":" + subTypeJson + "}}"; + "{\"bases\":{\"Test\":" + baseTypeJson + "},\"subs\":{\"Test\":" + subTypeJson + "}}"; JsonSerializer baseTypeAdapter = new JsonSerializer() { diff --git a/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java b/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java index 2e5a0c65..69492ed8 100644 --- a/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java +++ b/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java @@ -100,8 +100,8 @@ public class PrimitiveTest { assertThat(e) .hasMessageThat() .isEqualTo( - "java.lang.NumberFormatException: Expected an int but was 2147483648 at line 1 column" - + " 11 path $"); + "java.lang.NumberFormatException: Expected an int but was 2147483648" + + " at line 1 column 11 path $"); } @Test @@ -143,8 +143,8 @@ public class PrimitiveTest { assertThat(e) .hasMessageThat() .isEqualTo( - "java.lang.NumberFormatException: Expected an int but was 2147483648 at line 1 column" - + " 11 path $"); + "java.lang.NumberFormatException: Expected an int but was 2147483648" + + " at line 1 column 11 path $"); } @Test diff --git a/gson/src/test/java/com/google/gson/functional/ReflectionAccessFilterTest.java b/gson/src/test/java/com/google/gson/functional/ReflectionAccessFilterTest.java index 026de086..44679143 100644 --- a/gson/src/test/java/com/google/gson/functional/ReflectionAccessFilterTest.java +++ b/gson/src/test/java/com/google/gson/functional/ReflectionAccessFilterTest.java @@ -447,7 +447,7 @@ public class ReflectionAccessFilterTest { } /** - * Should not fail when deserializing collection interface (Even though this goes through {@link + * Should not fail when deserializing collection interface (even though this goes through {@link * ConstructorConstructor} as well) */ @Test @@ -467,7 +467,7 @@ public class ReflectionAccessFilterTest { } /** - * Should not fail when deserializing specific collection implementation (Even though this goes + * Should not fail when deserializing specific collection implementation (even though this goes * through {@link ConstructorConstructor} as well) */ @Test diff --git a/gson/src/test/java/com/google/gson/internal/JavaVersionTest.java b/gson/src/test/java/com/google/gson/internal/JavaVersionTest.java index b3d49d14..1c52dbb9 100644 --- a/gson/src/test/java/com/google/gson/internal/JavaVersionTest.java +++ b/gson/src/test/java/com/google/gson/internal/JavaVersionTest.java @@ -30,20 +30,20 @@ public class JavaVersionTest { @Test public void testGetMajorJavaVersion() { - assertThat(JavaVersion.getMajorJavaVersion() >= 7) - .isTrue(); // Gson currently requires at least Java 7 + // Gson currently requires at least Java 7 + assertThat(JavaVersion.getMajorJavaVersion() >= 7).isTrue(); } @Test public void testJava6() { - assertThat(JavaVersion.getMajorJavaVersion("1.6.0")) - .isEqualTo(6); // http://www.oracle.com/technetwork/java/javase/version-6-141920.html + // http://www.oracle.com/technetwork/java/javase/version-6-141920.html + assertThat(JavaVersion.getMajorJavaVersion("1.6.0")).isEqualTo(6); } @Test public void testJava7() { - assertThat(JavaVersion.getMajorJavaVersion("1.7.0")) - .isEqualTo(7); // http://www.oracle.com/technetwork/java/javase/jdk7-naming-418744.html + // http://www.oracle.com/technetwork/java/javase/jdk7-naming-418744.html + assertThat(JavaVersion.getMajorJavaVersion("1.7.0")).isEqualTo(7); } @Test @@ -63,8 +63,9 @@ public class JavaVersionTest { public void testJava9() { // Legacy style assertThat(JavaVersion.getMajorJavaVersion("9.0.4")).isEqualTo(9); // Oracle JDK 9 - assertThat(JavaVersion.getMajorJavaVersion("9-Debian")) - .isEqualTo(9); // Debian as reported in https://github.com/google/gson/issues/1310 + // Debian as reported in https://github.com/google/gson/issues/1310 + assertThat(JavaVersion.getMajorJavaVersion("9-Debian")).isEqualTo(9); + // New style assertThat(JavaVersion.getMajorJavaVersion("9-ea+19")).isEqualTo(9); assertThat(JavaVersion.getMajorJavaVersion("9+100")).isEqualTo(9); diff --git a/gson/src/test/java/com/google/gson/internal/bind/RecursiveTypesResolveTest.java b/gson/src/test/java/com/google/gson/internal/bind/RecursiveTypesResolveTest.java index 5f902db5..78d253df 100644 --- a/gson/src/test/java/com/google/gson/internal/bind/RecursiveTypesResolveTest.java +++ b/gson/src/test/java/com/google/gson/internal/bind/RecursiveTypesResolveTest.java @@ -29,8 +29,8 @@ import org.junit.Test; * #440 and similar issues. * *

      These tests originally caused {@link StackOverflowError} because of infinite recursion on - * attempts to resolve generics on types, with an intermediate types like 'Foo2<? extends ? super - * ? extends ... ? extends A>' + * attempts to resolve generics on types, with intermediate types like {@code Foo2} */ public class RecursiveTypesResolveTest { @@ -52,7 +52,7 @@ public class RecursiveTypesResolveTest { assertThat(adapter).isNotNull(); } - /** Tests belows check the behaviour of the methods changed for the fix. */ + /** Tests below check the behavior of the methods changed for the fix. */ @Test public void testDoubleSupertype() { assertThat($Gson$Types.supertypeOf($Gson$Types.supertypeOf(Number.class))) diff --git a/gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java b/gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java index f6cb0f1e..c71c1907 100644 --- a/gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java +++ b/gson/src/test/java/com/google/gson/reflect/TypeTokenTest.java @@ -207,8 +207,7 @@ public final class TypeTokenTest { assertThat(e) .hasMessageThat() .isEqualTo( - "Type argument int does not satisfy bounds" - + " for type variable E declared by " + "Type argument int does not satisfy bounds for type variable E declared by " + List.class); e = diff --git a/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java b/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java index 9ccf5ef4..32611c57 100644 --- a/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java +++ b/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java @@ -148,8 +148,8 @@ public final class JsonReaderTest { assertThat(expected) .hasMessageThat() .startsWith( - "Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" - + " JSON at line 1 column 1 path $"); + "Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON" + + " at line 1 column 1 path $"); reader = new JsonReader(reader("True")); reader.setStrictness(Strictness.STRICT); @@ -158,8 +158,8 @@ public final class JsonReaderTest { assertThat(expected) .hasMessageThat() .startsWith( - "Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" - + " JSON at line 1 column 1 path $"); + "Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON" + + " at line 1 column 1 path $"); } @Test @@ -171,8 +171,8 @@ public final class JsonReaderTest { assertThat(expected) .hasMessageThat() .startsWith( - "Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" - + " JSON at line 1 column 1 path $"); + "Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON" + + " at line 1 column 1 path $"); reader = new JsonReader(reader("FaLse")); reader.setStrictness(Strictness.STRICT); @@ -181,8 +181,8 @@ public final class JsonReaderTest { assertThat(expected) .hasMessageThat() .startsWith( - "Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" - + " JSON at line 1 column 1 path $"); + "Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON" + + " at line 1 column 1 path $"); } @Test @@ -194,8 +194,8 @@ public final class JsonReaderTest { assertThat(expected) .hasMessageThat() .startsWith( - "Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" - + " JSON at line 1 column 1 path $"); + "Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON" + + " at line 1 column 1 path $"); reader = new JsonReader(reader("nulL")); reader.setStrictness(Strictness.STRICT); @@ -204,8 +204,8 @@ public final class JsonReaderTest { assertThat(expected) .hasMessageThat() .startsWith( - "Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" - + " JSON at line 1 column 1 path $"); + "Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON" + + " at line 1 column 1 path $"); } @Test @@ -416,7 +416,11 @@ public final class JsonReaderTest { @Test public void testHelloWorld() throws IOException { - String json = "{\n" + " \"hello\": true,\n" + " \"foo\": [\"world\"]\n" + "}"; + String json = + "{\n" // + + " \"hello\": true,\n" // + + " \"foo\": [\"world\"]\n" // + + "}"; JsonReader reader = new JsonReader(reader(json)); reader.beginObject(); assertThat(reader.nextName()).isEqualTo("hello"); @@ -431,7 +435,11 @@ public final class JsonReaderTest { @Test public void testInvalidJsonInput() throws IOException { - String json = "{\n" + " \"h\\ello\": true,\n" + " \"foo\": [\"world\"]\n" + "}"; + String json = + "{\n" // + + " \"h\\ello\": true,\n" // + + " \"foo\": [\"world\"]\n" // + + "}"; JsonReader reader = new JsonReader(reader(json)); reader.beginObject(); diff --git a/gson/src/test/java/com/google/gson/stream/JsonWriterTest.java b/gson/src/test/java/com/google/gson/stream/JsonWriterTest.java index b467478b..ba36e48e 100644 --- a/gson/src/test/java/com/google/gson/stream/JsonWriterTest.java +++ b/gson/src/test/java/com/google/gson/stream/JsonWriterTest.java @@ -1045,7 +1045,12 @@ public final class JsonWriterTest { jsonWriter.endArray(); String expected = - "[\r\n" + " \t true,\r\n" + " \t \"text\",\r\n" + " \t 5.0,\r\n" + " \t null\r\n" + "]"; + "[\r\n" // + + " \t true,\r\n" // + + " \t \"text\",\r\n" // + + " \t 5.0,\r\n" // + + " \t null\r\n" // + + "]"; assertThat(stringWriter.toString()).isEqualTo(expected); assertThat(jsonWriter.getFormattingStyle().getNewline()).isEqualTo(lineSeparator); @@ -1067,7 +1072,13 @@ public final class JsonWriterTest { jsonWriter.endArray(); jsonWriter.endObject(); - String expected = "{\n" + " \"a\": [\n" + " 1,\n" + " 2\n" + " ]\n" + "}"; + String expected = + "{\n" // + + " \"a\": [\n" // + + " 1,\n" // + + " 2\n" // + + " ]\n" // + + "}"; assertThat(stringWriter.toString()).isEqualTo(expected); } } diff --git a/pom.xml b/pom.xml index 124b95ee..7169230b 100644 --- a/pom.xml +++ b/pom.xml @@ -124,6 +124,7 @@ + com.diffplug.spotless @@ -136,22 +137,34 @@ + + *.md + *.xml + .github/**/*.yml .gitignore + true - 4 + + 2 + + src/test/java/com/google/gson/functional/Java17RecordTest.java src/test/java/com/google/gson/native_test/Java17RecordReflectionTest.java @@ -159,11 +172,10 @@ true + true true - - - . + @@ -209,17 +221,17 @@ 11 all,-missing - false https://docs.oracle.com/en/java/javase/11/docs/api/ https://errorprone.info/api/latest/ - false