Skip to content

Commit

Permalink
Import jsr305 and use it to mark @nullable stuff. (square#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
swankjesse authored May 6, 2017
1 parent 6c32d42 commit 8d4171c
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 21 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
<artifactId>okio</artifactId>
<version>1.11.0</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
6 changes: 5 additions & 1 deletion wire-gson-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import javax.annotation.Nullable;
import okio.ByteString;

/**
Expand All @@ -29,15 +30,15 @@
*/
class ByteStringTypeAdapter extends TypeAdapter<ByteString> {

@Override public void write(JsonWriter out, ByteString value) throws IOException {
@Override public void write(JsonWriter out, @Nullable ByteString value) throws IOException {
if (value == null) {
out.nullValue();
} else {
out.value(value.base64());
}
}

@Override public ByteString read(JsonReader in) throws IOException {
@Override public @Nullable ByteString 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 @@ -31,6 +31,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;

import static com.squareup.wire.WireField.Label;
import static java.util.Collections.unmodifiableMap;
Expand Down Expand Up @@ -58,7 +59,7 @@ class MessageTypeAdapter<M extends Message<M, B>, B extends Message.Builder<M, B
}

@SuppressWarnings("unchecked")
@Override public void write(JsonWriter out, M message) throws IOException {
@Override public void write(JsonWriter out, @Nullable M message) throws IOException {
if (message == null) {
out.nullValue();
return;
Expand Down Expand Up @@ -105,7 +106,7 @@ private void emitUint64(Long value, JsonWriter out) throws IOException {
}

@SuppressWarnings("unchecked")
@Override public M read(JsonReader in) throws IOException {
@Override public @Nullable M 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 @@ -19,6 +19,7 @@
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import javax.annotation.Nullable;
import okio.ByteString;

/**
Expand All @@ -42,7 +43,7 @@
*/
public final class WireTypeAdapterFactory implements TypeAdapterFactory {
@SuppressWarnings("unchecked")
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
@Override public @Nullable <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (type.getRawType().equals(ByteString.class)) {
return (TypeAdapter<T>) new ByteStringTypeAdapter();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Wire JSON encoding via Gson.
*/
@javax.annotation.ParametersAreNonnullByDefault
package com.squareup.wire;
7 changes: 5 additions & 2 deletions wire-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
</dependency>

<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.squareup.wire;

import java.io.IOException;
import javax.annotation.Nullable;

/**
* An abstract {@link ProtoAdapter} that converts values of an enum to and from integers.
Expand Down Expand Up @@ -46,5 +47,5 @@ protected EnumAdapter(Class<E> type) {
* Converts an integer to an enum.
* Returns null if there is no corresponding enum.
*/
protected abstract E fromValue(int value);
protected abstract @Nullable E fromValue(int value);
}
6 changes: 4 additions & 2 deletions wire-runtime/src/main/java/com/squareup/wire/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.ObjectStreamException;
import java.io.OutputStream;
import java.io.Serializable;
import javax.annotation.Nullable;
import okio.Buffer;
import okio.BufferedSink;
import okio.ByteString;
Expand Down Expand Up @@ -114,7 +115,7 @@ public abstract static class Builder<M extends Message<M, B>, B extends Builder<
* {@link #buildUnknownFields()}. It's automatically cleared in {@link #buildUnknownFields()},
* and can also be manually cleared by calling {@link #clearUnknownFields()}.
*/
transient Buffer unknownFieldsBuffer;
transient @Nullable Buffer unknownFieldsBuffer;
transient ProtoWriter unknownFieldsWriter;

protected Builder() {
Expand All @@ -132,7 +133,8 @@ public final Builder<M, B> addUnknownFields(ByteString unknownFields) {
return this;
}

public final Builder<M, B> addUnknownField(int tag, FieldEncoding fieldEncoding, Object value) {
public final Builder<M, B> addUnknownField(
int tag, FieldEncoding fieldEncoding, @Nullable Object value) {
prepareForNewUnknownFields();
try {
ProtoAdapter<Object> protoAdapter = (ProtoAdapter<Object>) fieldEncoding.rawProtoAdapter();
Expand Down
17 changes: 9 additions & 8 deletions wire-runtime/src/main/java/com/squareup/wire/ProtoAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
Expand All @@ -48,10 +49,10 @@ public abstract class ProtoAdapter<E> {
private final FieldEncoding fieldEncoding;
final Class<?> javaType;

ProtoAdapter<List<E>> packedAdapter;
ProtoAdapter<List<E>> repeatedAdapter;
@Nullable ProtoAdapter<List<E>> packedAdapter;
@Nullable ProtoAdapter<List<E>> repeatedAdapter;

public ProtoAdapter(FieldEncoding fieldEncoding, Class<?> javaType) {
public ProtoAdapter(FieldEncoding fieldEncoding, @Nullable Class<?> javaType) {
this.fieldEncoding = fieldEncoding;
this.javaType = javaType;
}
Expand Down Expand Up @@ -111,7 +112,7 @@ public static ProtoAdapter<?> get(String adapterString) {
}

/** Returns the redacted form of {@code value}. */
public E redact(E value) {
public @Nullable E redact(E value) {
return null;
}

Expand All @@ -126,7 +127,7 @@ public E redact(E value) {
* length-delimited prefix (should the type require one), and value. Returns 0 if {@code value} is
* null.
*/
public int encodedSizeWithTag(int tag, E value) {
public int encodedSizeWithTag(int tag, @Nullable E value) {
if (value == null) return 0;
int size = encodedSize(value);
if (fieldEncoding == FieldEncoding.LENGTH_DELIMITED) {
Expand All @@ -139,7 +140,7 @@ public int encodedSizeWithTag(int tag, E value) {
public abstract void encode(ProtoWriter writer, E value) throws IOException;

/** Write {@code tag} and {@code value} to {@code writer}. If value is null this does nothing. */
public void encodeWithTag(ProtoWriter writer, int tag, E value) throws IOException {
public void encodeWithTag(ProtoWriter writer, int tag, @Nullable E value) throws IOException {
if (value == null) return;
writer.writeTag(tag, fieldEncoding);
if (fieldEncoding == FieldEncoding.LENGTH_DELIMITED) {
Expand Down Expand Up @@ -517,7 +518,7 @@ private static final class MapProtoAdapter<K, V> extends ProtoAdapter<Map<K, V>>
private final MapEntryProtoAdapter<K, V> entryAdapter;

MapProtoAdapter(ProtoAdapter<K> keyAdapter, ProtoAdapter<V> valueAdapter) {
super(FieldEncoding.LENGTH_DELIMITED, null);
super(FieldEncoding.LENGTH_DELIMITED, Map.class);
entryAdapter = new MapEntryProtoAdapter<>(keyAdapter, valueAdapter);
}

Expand Down Expand Up @@ -573,7 +574,7 @@ private static final class MapEntryProtoAdapter<K, V> extends ProtoAdapter<Map.E
final ProtoAdapter<V> valueAdapter;

MapEntryProtoAdapter(ProtoAdapter<K> keyAdapter, ProtoAdapter<V> valueAdapter) {
super(FieldEncoding.LENGTH_DELIMITED, null);
super(FieldEncoding.LENGTH_DELIMITED, Map.Entry.class);
this.keyAdapter = keyAdapter;
this.valueAdapter = valueAdapter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.annotation.Nullable;

/**
* Converts values of an enum to and from integers using reflection.
*/
final class RuntimeEnumAdapter<E extends WireEnum> extends EnumAdapter<E> {
private final Class<E> type;
private Method fromValueMethod; // Loaded lazily to avoid reflection during class loading.
private @Nullable Method fromValueMethod; // Lazy to avoid reflection during class loading.

RuntimeEnumAdapter(Class<E> type) {
super(type);
Expand Down
4 changes: 3 additions & 1 deletion wire-runtime/src/main/java/com/squareup/wire/Wire.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.squareup.wire;

import javax.annotation.Nullable;

public final class Wire {
private Wire() {
}
Expand All @@ -34,7 +36,7 @@ private Wire() {
* second argument, which in this case is the default value for the field
* 'f'.
*/
public static <T> T get(T value, T defaultValue) {
public static <T> T get(@Nullable T value, T defaultValue) {
return value != null ? value : defaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
/**
* Wire runtime support classes.
*/
@javax.annotation.ParametersAreNonnullByDefault
package com.squareup.wire;

0 comments on commit 8d4171c

Please sign in to comment.