Skip to content

Commit

Permalink
Formats .java files
Browse files Browse the repository at this point in the history
  • Loading branch information
MaicolAntali committed Nov 6, 2023
1 parent 61b9806 commit 495cf27
Show file tree
Hide file tree
Showing 226 changed files with 11,250 additions and 9,071 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,29 @@
*/
package com.google.gson.extras.examples.rawcollections;

import java.util.ArrayList;
import java.util.Collection;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import java.util.ArrayList;
import java.util.Collection;

public class RawCollectionsExample {
static class Event {
private String name;
private String source;

private Event(String name, String source) {
this.name = name;
this.source = source;
}

@Override
public String toString() {
return String.format("(name=%s, source=%s)", name, source);
}
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({"unchecked", "rawtypes"})
public static void main(String[] args) {
Gson gson = new Gson();
Collection collection = new ArrayList();
Expand Down
82 changes: 34 additions & 48 deletions extras/src/main/java/com/google/gson/graph/GraphAdapterBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,28 @@
import java.util.Map;
import java.util.Queue;

/**
* Writes a graph of objects as a list of named nodes.
*/
/** Writes a graph of objects as a list of named nodes. */
// TODO: proper documentation
public final class GraphAdapterBuilder {
private final Map<Type, InstanceCreator<?>> instanceCreators;
private final ConstructorConstructor constructorConstructor;

public GraphAdapterBuilder() {
this.instanceCreators = new HashMap<>();
this.constructorConstructor = new ConstructorConstructor(instanceCreators, true, Collections.<ReflectionAccessFilter>emptyList());
this.instanceCreators = new HashMap<>();
this.constructorConstructor =
new ConstructorConstructor(
instanceCreators, true, Collections.<ReflectionAccessFilter>emptyList());
}

public GraphAdapterBuilder addType(Type type) {
final ObjectConstructor<?> objectConstructor = constructorConstructor.get(TypeToken.get(type));
InstanceCreator<Object> instanceCreator = new InstanceCreator<Object>() {
@Override
public Object createInstance(Type type) {
return objectConstructor.construct();
}
};
InstanceCreator<Object> instanceCreator =
new InstanceCreator<Object>() {
@Override
public Object createInstance(Type type) {
return objectConstructor.construct();
}
};
return addType(type, instanceCreator);
}

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

static class Factory implements TypeAdapterFactory, InstanceCreator<Object> {
private final Map<Type, InstanceCreator<?>> instanceCreators;

@SuppressWarnings("ThreadLocalUsage")
private final ThreadLocal<Graph> graphThreadLocal = new ThreadLocal<>();

Expand All @@ -95,7 +98,8 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> typeAdapter = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
@Override public void write(JsonWriter out, T value) throws IOException {
@Override
public void write(JsonWriter out, T value) throws IOException {
if (value == null) {
out.nullValue();
return;
Expand Down Expand Up @@ -144,7 +148,8 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
}
}

@Override public T read(JsonReader in) throws IOException {
@Override
public T read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
Expand Down Expand Up @@ -207,13 +212,12 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
}

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

static class Graph {
/**
* The graph elements. On serialization keys are objects (using an identity
* hash map) and on deserialization keys are the string names (using a
* standard hash map).
* The graph elements. On serialization keys are objects (using an identity hash map) and on
* deserialization keys are the string names (using a standard hash map).
*/
private final Map<Object, Element<?>> map;

/**
* The queue of elements to write during serialization. Unused during
* deserialization.
*/
/** The queue of elements to write during serialization. Unused during deserialization. */
private final Queue<Element<?>> queue = new ArrayDeque<>();

/**
* The instance currently being deserialized. Used as a backdoor between
* the graph traversal (which needs to know instances) and instance creators
* which create them.
* The instance currently being deserialized. Used as a backdoor between the graph traversal
* (which needs to know instances) and instance creators which create them.
*/
private Element<Object> nextCreate;

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

/**
* Returns a unique name for an element to be inserted into the graph.
*/
/** Returns a unique name for an element to be inserted into the graph. */
public String nextName() {
return "0x" + Integer.toHexString(map.size() + 1);
}
}

/**
* An element of the graph during serialization or deserialization.
*/
/** An element of the graph during serialization or deserialization. */
static class Element<T> {
/**
* This element's name in the top level graph object.
*/
/** This element's name in the top level graph object. */
private final String id;

/**
* The value if known. During deserialization this is lazily populated.
*/
/** The value if known. During deserialization this is lazily populated. */
private T value;

/**
* This element's type adapter if known. During deserialization this is
* lazily populated.
*/
/** This element's type adapter if known. During deserialization this is lazily populated. */
private TypeAdapter<T> typeAdapter;

/**
* The element to deserialize. Unused in serialization.
*/
/** The element to deserialize. Unused in serialization. */
private final JsonElement element;

Element(T value, String id, TypeAdapter<T> typeAdapter, JsonElement element) {
Expand Down
14 changes: 7 additions & 7 deletions extras/src/main/java/com/google/gson/interceptors/Intercept.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/**
* Use this annotation to indicate various interceptors for class instances after
* they have been processed by Gson. For example, you can use it to validate an instance
* after it has been deserialized from Json.
* Here is an example of how this annotation is used:
* Use this annotation to indicate various interceptors for class instances after they have been
* processed by Gson. For example, you can use it to validate an instance after it has been
* deserialized from Json. Here is an example of how this annotation is used:
*
* <p>Here is an example of how this annotation is used:
*
* <pre>
* &#64;Intercept(postDeserialize=UserValidator.class)
* public class User {
Expand Down Expand Up @@ -56,8 +56,8 @@
public @interface Intercept {

/**
* Specify the class that provides the methods that should be invoked after an instance
* has been deserialized.
* Specify the class that provides the methods that should be invoked after an instance has been
* deserialized.
*/
@SuppressWarnings("rawtypes")
public Class<? extends JsonPostDeserializer> postDeserialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
import com.google.gson.stream.JsonWriter;
import java.io.IOException;

/**
* A type adapter factory that implements {@code @Intercept}.
*/
/** A type adapter factory that implements {@code @Intercept}. */
public final class InterceptorFactory implements TypeAdapterFactory {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Intercept intercept = type.getRawType().getAnnotation(Intercept.class);
if (intercept == null) {
return null;
Expand All @@ -52,11 +51,13 @@ public InterceptorAdapter(TypeAdapter<T> delegate, Intercept intercept) {
}
}

@Override public void write(JsonWriter out, T value) throws IOException {
@Override
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}

@Override public T read(JsonReader in) throws IOException {
@Override
public T read(JsonReader in) throws IOException {
T result = delegate.read(in);
postDeserializer.postDeserialize(result);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
import com.google.gson.InstanceCreator;

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

/**
* This method is called by Gson after the object has been deserialized from Json.
*/
/** This method is called by Gson after the object has been deserialized from Json. */
public void postDeserialize(T object);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,63 @@

package com.google.gson.typeadapters;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.annotation.PostConstruct;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.annotation.PostConstruct;

public class PostConstructAdapterFactory implements TypeAdapterFactory {
// copied from https://gist.github.com/swankjesse/20df26adaf639ed7fd160f145a0b661a
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
for (Class<?> t = type.getRawType(); (t != Object.class) && (t.getSuperclass() != null); t = t.getSuperclass()) {
for (Method m : t.getDeclaredMethods()) {
if (m.isAnnotationPresent(PostConstruct.class)) {
m.setAccessible(true);
TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new PostConstructAdapter<>(delegate, m);
}
}
// copied from https://gist.github.com/swankjesse/20df26adaf639ed7fd160f145a0b661a
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
for (Class<?> t = type.getRawType();
(t != Object.class) && (t.getSuperclass() != null);
t = t.getSuperclass()) {
for (Method m : t.getDeclaredMethods()) {
if (m.isAnnotationPresent(PostConstruct.class)) {
m.setAccessible(true);
TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new PostConstructAdapter<>(delegate, m);
}
return null;
}
}
return null;
}

final static class PostConstructAdapter<T> extends TypeAdapter<T> {
private final TypeAdapter<T> delegate;
private final Method method;
static final class PostConstructAdapter<T> extends TypeAdapter<T> {
private final TypeAdapter<T> delegate;
private final Method method;

public PostConstructAdapter(TypeAdapter<T> delegate, Method method) {
this.delegate = delegate;
this.method = method;
}
public PostConstructAdapter(TypeAdapter<T> delegate, Method method) {
this.delegate = delegate;
this.method = method;
}

@Override public T read(JsonReader in) throws IOException {
T result = delegate.read(in);
if (result != null) {
try {
method.invoke(result);
} catch (IllegalAccessException e) {
throw new AssertionError();
} catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException) throw (RuntimeException) e.getCause();
throw new RuntimeException(e.getCause());
}
}
return result;
@Override
public T read(JsonReader in) throws IOException {
T result = delegate.read(in);
if (result != null) {
try {
method.invoke(result);
} catch (IllegalAccessException e) {
throw new AssertionError();
} catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException) throw (RuntimeException) e.getCause();
throw new RuntimeException(e.getCause());
}
}
return result;
}

@Override public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
@Override
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
}
}
Loading

0 comments on commit 495cf27

Please sign in to comment.