Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import org.jspecify.annotations.Nullable;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -151,7 +153,7 @@ static class Factory implements TypeAdapterFactory, InstanceCreator<Object> {
}

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
public @Nullable <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (!instanceCreators.containsKey(type.getType())) {
return null;
}
Expand All @@ -160,7 +162,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
public void write(JsonWriter out, @Nullable T value) throws IOException {
if (value == null) {
out.nullValue();
return;
Expand Down Expand Up @@ -210,7 +212,7 @@ public void write(JsonWriter out, T value) throws IOException {
}

@Override
public T read(JsonReader in) throws IOException {
public @Nullable T read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import org.jspecify.annotations.Nullable;

import java.io.IOException;

/** 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) {
public @Nullable <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Intercept intercept = type.getRawType().getAnnotation(Intercept.class);
if (intercept == null) {
return null;
Expand All @@ -52,12 +54,12 @@ public InterceptorAdapter(TypeAdapter<T> delegate, Intercept intercept) {
}

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

@Override
public T read(JsonReader in) throws IOException {
public @Nullable 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 @@ -22,6 +22,8 @@
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import org.jspecify.annotations.Nullable;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -30,7 +32,7 @@
public class PostConstructAdapterFactory implements TypeAdapterFactory {
// copied from https://gist.github.com/swankjesse/20df26adaf639ed7fd160f145a0b661a
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
public @Nullable <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
for (Class<?> t = type.getRawType();
(t != Object.class) && (t.getSuperclass() != null);
t = t.getSuperclass()) {
Expand All @@ -55,7 +57,7 @@ public PostConstructAdapter(TypeAdapter<T> delegate, Method method) {
}

@Override
public T read(JsonReader in) throws IOException {
public @Nullable T read(JsonReader in) throws IOException {
T result = delegate.read(in);
if (result != null) {
try {
Expand All @@ -73,7 +75,7 @@ public T read(JsonReader in) throws IOException {
}

@Override
public void write(JsonWriter out, T value) throws IOException {
public void write(JsonWriter out, @Nullable T value) throws IOException {
delegate.write(out, value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import org.jspecify.annotations.Nullable;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -241,7 +243,7 @@ public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) {
}

@Override
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
public @Nullable <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
if (type == null) {
return null;
}
Expand All @@ -263,7 +265,7 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {

return new TypeAdapter<R>() {
@Override
public R read(JsonReader in) throws IOException {
public @Nullable R read(JsonReader in) throws IOException {
JsonElement jsonElement = jsonElementAdapter.read(in);
JsonElement labelJsonElement;
if (maintainType) {
Expand Down Expand Up @@ -294,7 +296,7 @@ public R read(JsonReader in) throws IOException {
}

@Override
public void write(JsonWriter out, R value) throws IOException {
public void write(JsonWriter out, @Nullable R value) throws IOException {
Class<?> srcType = value.getClass();
String label = subtypeToLabel.get(srcType);
@SuppressWarnings("unchecked") // registration requires that subtype extends T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import org.jspecify.annotations.Nullable;

import java.io.IOException;
import java.text.ParseException;
import java.text.ParsePosition;
Expand All @@ -34,7 +36,7 @@ public final class UtcDateTypeAdapter extends TypeAdapter<Date> {
private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC");

@Override
public void write(JsonWriter out, Date date) throws IOException {
public void write(JsonWriter out, @Nullable Date date) throws IOException {
if (date == null) {
out.nullValue();
} else {
Expand All @@ -44,7 +46,7 @@ public void write(JsonWriter out, Date date) throws IOException {
}

@Override
public Date read(JsonReader in) throws IOException {
public @Nullable Date read(JsonReader in) throws IOException {
try {
if (in.peek().equals(JsonToken.NULL)) {
in.nextNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jspecify.annotations.Nullable;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -114,12 +116,12 @@ public void testCustomTypeAdapter() {
User.class,
new TypeAdapter<User>() {
@Override
public void write(JsonWriter out, User value) throws IOException {
public void write(JsonWriter out, @Nullable User value) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public User read(JsonReader in) throws IOException {
public @Nullable User read(JsonReader in) throws IOException {
in.beginObject();
assertThat(in.nextName()).isEqualTo("name");
String name = in.nextString();
Expand Down
5 changes: 5 additions & 0 deletions gson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<artifactId>error_prone_annotations</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
Expand Down
Loading
Loading