Skip to content

Commit 2c94c75

Browse files
authored
Formats codebase (#2531)
* Formats `.java` files * Formats `.md` files
1 parent cb6643f commit 2c94c75

File tree

229 files changed

+11259
-9080
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

229 files changed

+11259
-9080
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ Despite supporting older Java versions, Gson also provides a JPMS module descrip
4747
These are the optional Java Platform Module System (JPMS) JDK modules which Gson depends on.
4848
This only applies when running Java 9 or newer.
4949

50-
- `java.sql` (optional since Gson 2.8.9)
50+
- `java.sql` (optional since Gson 2.8.9)
5151
When this module is present, Gson provides default adapters for some SQL date and time classes.
5252

53-
- `jdk.unsupported`, respectively class `sun.misc.Unsafe` (optional)
53+
- `jdk.unsupported`, respectively class `sun.misc.Unsafe` (optional)
5454
When this module is present, Gson can use the `Unsafe` class to create instances of classes without no-args constructor.
5555
However, care should be taken when relying on this. `Unsafe` is not available in all environments and its usage has some pitfalls,
5656
see [`GsonBuilder.disableJdkUnsafe()`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#disableJdkUnsafe()).
@@ -87,7 +87,7 @@ JDK 11 or newer is required for building, JDK 17 is recommended.
8787

8888
### Contributing
8989

90-
See the [contributing guide](https://github.com/google/.github/blob/master/CONTRIBUTING.md).
90+
See the [contributing guide](https://github.com/google/.github/blob/master/CONTRIBUTING.md).
9191
Please perform a quick search to check if there are already existing issues or pull requests related to your contribution.
9292

9393
Keep in mind that Gson is in maintenance mode. If you want to add a new feature, please first search for existing GitHub issues, or create a new one to discuss the feature and get feedback.

Troubleshooting.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ For example, let's assume you want to deserialize the following JSON data:
127127
}
128128
```
129129

130-
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]`
131-
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]`).
130+
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]`
131+
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]`).
132132
The proper solution here is to fix the malformed JSON data.
133133

134134
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:
178178
}
179179
```
180180

181-
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`
182-
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 (`$.`).
181+
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`
182+
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 (`$.`).
183183
The solution here is to change in the `WebPage` class the field `String languages` to `List<String> languages`.
184184

185185
## <a id="adapter-not-null-safe"></a> `IllegalStateException`: "Expected ... but was NULL"
@@ -287,7 +287,7 @@ This will not initialize arbitrary classes, and it will throw a `ClassCastExcept
287287

288288
## <a id="type-token-raw"></a> `IllegalStateException`: 'TypeToken must be created with a type argument' <br> `RuntimeException`: 'Missing type parameter'
289289

290-
**Symptom:** An `IllegalStateException` with the message 'TypeToken must be created with a type argument' is thrown.
290+
**Symptom:** An `IllegalStateException` with the message 'TypeToken must be created with a type argument' is thrown.
291291
For older Gson versions a `RuntimeException` with message 'Missing type parameter' is thrown.
292292

293293
**Reason:**

UserGuide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ gson.registerTypeAdapter(MyType.class, new MyDeserializer());
405405
gson.registerTypeAdapter(MyType.class, new MyInstanceCreator());
406406
```
407407

408-
`registerTypeAdapter` call checks
408+
`registerTypeAdapter` call checks
409409
1. if the type adapter implements more than one of these interfaces, in that case it registers the adapter for all of them.
410410
2. if the type adapter is for the Object class or JsonElement or any of its subclasses, in that case it throws IllegalArgumentException because overriding the built-in adapters for these types is not supported.
411411

extras/src/main/java/com/google/gson/extras/examples/rawcollections/RawCollectionsExample.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,29 @@
1515
*/
1616
package com.google.gson.extras.examples.rawcollections;
1717

18-
import java.util.ArrayList;
19-
import java.util.Collection;
20-
2118
import com.google.gson.Gson;
2219
import com.google.gson.JsonArray;
2320
import com.google.gson.JsonParser;
21+
import java.util.ArrayList;
22+
import java.util.Collection;
2423

2524
public class RawCollectionsExample {
2625
static class Event {
2726
private String name;
2827
private String source;
28+
2929
private Event(String name, String source) {
3030
this.name = name;
3131
this.source = source;
3232
}
33+
3334
@Override
3435
public String toString() {
3536
return String.format("(name=%s, source=%s)", name, source);
3637
}
3738
}
3839

39-
@SuppressWarnings({ "unchecked", "rawtypes" })
40+
@SuppressWarnings({"unchecked", "rawtypes"})
4041
public static void main(String[] args) {
4142
Gson gson = new Gson();
4243
Collection collection = new ArrayList();

extras/src/main/java/com/google/gson/graph/GraphAdapterBuilder.java

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,28 @@
3838
import java.util.Map;
3939
import java.util.Queue;
4040

41-
/**
42-
* Writes a graph of objects as a list of named nodes.
43-
*/
41+
/** Writes a graph of objects as a list of named nodes. */
4442
// TODO: proper documentation
4543
public final class GraphAdapterBuilder {
4644
private final Map<Type, InstanceCreator<?>> instanceCreators;
4745
private final ConstructorConstructor constructorConstructor;
4846

4947
public GraphAdapterBuilder() {
50-
this.instanceCreators = new HashMap<>();
51-
this.constructorConstructor = new ConstructorConstructor(instanceCreators, true, Collections.<ReflectionAccessFilter>emptyList());
48+
this.instanceCreators = new HashMap<>();
49+
this.constructorConstructor =
50+
new ConstructorConstructor(
51+
instanceCreators, true, Collections.<ReflectionAccessFilter>emptyList());
5252
}
53+
5354
public GraphAdapterBuilder addType(Type type) {
5455
final ObjectConstructor<?> objectConstructor = constructorConstructor.get(TypeToken.get(type));
55-
InstanceCreator<Object> instanceCreator = new InstanceCreator<Object>() {
56-
@Override
57-
public Object createInstance(Type type) {
58-
return objectConstructor.construct();
59-
}
60-
};
56+
InstanceCreator<Object> instanceCreator =
57+
new InstanceCreator<Object>() {
58+
@Override
59+
public Object createInstance(Type type) {
60+
return objectConstructor.construct();
61+
}
62+
};
6163
return addType(type, instanceCreator);
6264
}
6365

@@ -79,6 +81,7 @@ public void registerOn(GsonBuilder gsonBuilder) {
7981

8082
static class Factory implements TypeAdapterFactory, InstanceCreator<Object> {
8183
private final Map<Type, InstanceCreator<?>> instanceCreators;
84+
8285
@SuppressWarnings("ThreadLocalUsage")
8386
private final ThreadLocal<Graph> graphThreadLocal = new ThreadLocal<>();
8487

@@ -95,7 +98,8 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
9598
final TypeAdapter<T> typeAdapter = gson.getDelegateAdapter(this, type);
9699
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
97100
return new TypeAdapter<T>() {
98-
@Override public void write(JsonWriter out, T value) throws IOException {
101+
@Override
102+
public void write(JsonWriter out, T value) throws IOException {
99103
if (value == null) {
100104
out.nullValue();
101105
return;
@@ -144,7 +148,8 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
144148
}
145149
}
146150

147-
@Override public T read(JsonReader in) throws IOException {
151+
@Override
152+
public T read(JsonReader in) throws IOException {
148153
if (in.peek() == JsonToken.NULL) {
149154
in.nextNull();
150155
return null;
@@ -207,13 +212,12 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
207212
}
208213

209214
/**
210-
* Hook for the graph adapter to get a reference to a deserialized value
211-
* before that value is fully populated. This is useful to deserialize
212-
* values that directly or indirectly reference themselves: we can hand
213-
* out an instance before read() returns.
215+
* Hook for the graph adapter to get a reference to a deserialized value before that value is
216+
* fully populated. This is useful to deserialize values that directly or indirectly reference
217+
* themselves: we can hand out an instance before read() returns.
214218
*
215-
* <p>Gson should only ever call this method when we're expecting it to;
216-
* that is only when we've called back into Gson to deserialize a tree.
219+
* <p>Gson should only ever call this method when we're expecting it to; that is only when we've
220+
* called back into Gson to deserialize a tree.
217221
*/
218222
@Override
219223
public Object createInstance(Type type) {
@@ -231,60 +235,42 @@ public Object createInstance(Type type) {
231235

232236
static class Graph {
233237
/**
234-
* The graph elements. On serialization keys are objects (using an identity
235-
* hash map) and on deserialization keys are the string names (using a
236-
* standard hash map).
238+
* The graph elements. On serialization keys are objects (using an identity hash map) and on
239+
* deserialization keys are the string names (using a standard hash map).
237240
*/
238241
private final Map<Object, Element<?>> map;
239242

240-
/**
241-
* The queue of elements to write during serialization. Unused during
242-
* deserialization.
243-
*/
243+
/** The queue of elements to write during serialization. Unused during deserialization. */
244244
private final Queue<Element<?>> queue = new ArrayDeque<>();
245245

246246
/**
247-
* The instance currently being deserialized. Used as a backdoor between
248-
* the graph traversal (which needs to know instances) and instance creators
249-
* which create them.
247+
* The instance currently being deserialized. Used as a backdoor between the graph traversal
248+
* (which needs to know instances) and instance creators which create them.
250249
*/
251250
private Element<Object> nextCreate;
252251

253252
private Graph(Map<Object, Element<?>> map) {
254253
this.map = map;
255254
}
256255

257-
/**
258-
* Returns a unique name for an element to be inserted into the graph.
259-
*/
256+
/** Returns a unique name for an element to be inserted into the graph. */
260257
public String nextName() {
261258
return "0x" + Integer.toHexString(map.size() + 1);
262259
}
263260
}
264261

265-
/**
266-
* An element of the graph during serialization or deserialization.
267-
*/
262+
/** An element of the graph during serialization or deserialization. */
268263
static class Element<T> {
269-
/**
270-
* This element's name in the top level graph object.
271-
*/
264+
/** This element's name in the top level graph object. */
272265
private final String id;
273266

274-
/**
275-
* The value if known. During deserialization this is lazily populated.
276-
*/
267+
/** The value if known. During deserialization this is lazily populated. */
277268
private T value;
278269

279-
/**
280-
* This element's type adapter if known. During deserialization this is
281-
* lazily populated.
282-
*/
270+
/** This element's type adapter if known. During deserialization this is lazily populated. */
283271
private TypeAdapter<T> typeAdapter;
284272

285-
/**
286-
* The element to deserialize. Unused in serialization.
287-
*/
273+
/** The element to deserialize. Unused in serialization. */
288274
private final JsonElement element;
289275

290276
Element(T value, String id, TypeAdapter<T> typeAdapter, JsonElement element) {

extras/src/main/java/com/google/gson/interceptors/Intercept.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323

24-
2524
/**
26-
* Use this annotation to indicate various interceptors for class instances after
27-
* they have been processed by Gson. For example, you can use it to validate an instance
28-
* after it has been deserialized from Json.
29-
* Here is an example of how this annotation is used:
25+
* Use this annotation to indicate various interceptors for class instances after they have been
26+
* processed by Gson. For example, you can use it to validate an instance after it has been
27+
* deserialized from Json. Here is an example of how this annotation is used:
28+
*
3029
* <p>Here is an example of how this annotation is used:
30+
*
3131
* <pre>
3232
* &#64;Intercept(postDeserialize=UserValidator.class)
3333
* public class User {
@@ -56,8 +56,8 @@
5656
public @interface Intercept {
5757

5858
/**
59-
* Specify the class that provides the methods that should be invoked after an instance
60-
* has been deserialized.
59+
* Specify the class that provides the methods that should be invoked after an instance has been
60+
* deserialized.
6161
*/
6262
@SuppressWarnings("rawtypes")
6363
public Class<? extends JsonPostDeserializer> postDeserialize();

extras/src/main/java/com/google/gson/interceptors/InterceptorFactory.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@
2424
import com.google.gson.stream.JsonWriter;
2525
import java.io.IOException;
2626

27-
/**
28-
* A type adapter factory that implements {@code @Intercept}.
29-
*/
27+
/** A type adapter factory that implements {@code @Intercept}. */
3028
public final class InterceptorFactory implements TypeAdapterFactory {
31-
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
29+
@Override
30+
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
3231
Intercept intercept = type.getRawType().getAnnotation(Intercept.class);
3332
if (intercept == null) {
3433
return null;
@@ -52,11 +51,13 @@ public InterceptorAdapter(TypeAdapter<T> delegate, Intercept intercept) {
5251
}
5352
}
5453

55-
@Override public void write(JsonWriter out, T value) throws IOException {
54+
@Override
55+
public void write(JsonWriter out, T value) throws IOException {
5656
delegate.write(out, value);
5757
}
5858

59-
@Override public T read(JsonReader in) throws IOException {
59+
@Override
60+
public T read(JsonReader in) throws IOException {
6061
T result = delegate.read(in);
6162
postDeserializer.postDeserialize(result);
6263
return result;

extras/src/main/java/com/google/gson/interceptors/JsonPostDeserializer.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818
import com.google.gson.InstanceCreator;
1919

2020
/**
21-
* This interface is implemented by a class that wishes to inspect or modify an object
22-
* after it has been deserialized. You must define a no-args constructor or register an
23-
* {@link InstanceCreator} for such a class.
21+
* This interface is implemented by a class that wishes to inspect or modify an object after it has
22+
* been deserialized. You must define a no-args constructor or register an {@link InstanceCreator}
23+
* for such a class.
2424
*
2525
* @author Inderjeet Singh
2626
*/
2727
public interface JsonPostDeserializer<T> {
2828

29-
/**
30-
* This method is called by Gson after the object has been deserialized from Json.
31-
*/
29+
/** This method is called by Gson after the object has been deserialized from Json. */
3230
public void postDeserialize(T object);
3331
}

0 commit comments

Comments
 (0)