diff --git a/pom.xml b/pom.xml index 048bc44678..b48684c8f6 100644 --- a/pom.xml +++ b/pom.xml @@ -76,6 +76,12 @@ okio 1.11.0 + + com.google.code.findbugs + jsr305 + 3.0.2 + provided + com.google.code.gson gson diff --git a/wire-gson-support/pom.xml b/wire-gson-support/pom.xml index bb737aa591..7f58385896 100644 --- a/wire-gson-support/pom.xml +++ b/wire-gson-support/pom.xml @@ -21,7 +21,11 @@ com.google.code.gson gson - + + com.google.code.findbugs + jsr305 + provided + junit junit diff --git a/wire-gson-support/src/main/java/com/squareup/wire/ByteStringTypeAdapter.java b/wire-gson-support/src/main/java/com/squareup/wire/ByteStringTypeAdapter.java index 7b51eaff1f..1fcc0f46ea 100644 --- a/wire-gson-support/src/main/java/com/squareup/wire/ByteStringTypeAdapter.java +++ b/wire-gson-support/src/main/java/com/squareup/wire/ByteStringTypeAdapter.java @@ -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; /** @@ -29,7 +30,7 @@ */ class ByteStringTypeAdapter extends TypeAdapter { - @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 { @@ -37,7 +38,7 @@ class ByteStringTypeAdapter extends TypeAdapter { } } - @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; diff --git a/wire-gson-support/src/main/java/com/squareup/wire/MessageTypeAdapter.java b/wire-gson-support/src/main/java/com/squareup/wire/MessageTypeAdapter.java index 741026e76b..27069769f2 100644 --- a/wire-gson-support/src/main/java/com/squareup/wire/MessageTypeAdapter.java +++ b/wire-gson-support/src/main/java/com/squareup/wire/MessageTypeAdapter.java @@ -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; @@ -58,7 +59,7 @@ class MessageTypeAdapter, B extends Message.Builder TypeAdapter create(Gson gson, TypeToken type) { + @Override public @Nullable TypeAdapter create(Gson gson, TypeToken type) { if (type.getRawType().equals(ByteString.class)) { return (TypeAdapter) new ByteStringTypeAdapter(); } diff --git a/wire-gson-support/src/main/java/com/squareup/wire/package-info.java b/wire-gson-support/src/main/java/com/squareup/wire/package-info.java new file mode 100644 index 0000000000..f4c17a1a39 --- /dev/null +++ b/wire-gson-support/src/main/java/com/squareup/wire/package-info.java @@ -0,0 +1,5 @@ +/** + * Wire JSON encoding via Gson. + */ +@javax.annotation.ParametersAreNonnullByDefault +package com.squareup.wire; diff --git a/wire-runtime/pom.xml b/wire-runtime/pom.xml index 8c65195c4c..ecde3d2256 100644 --- a/wire-runtime/pom.xml +++ b/wire-runtime/pom.xml @@ -16,13 +16,16 @@ com.squareup.okio okio - com.google.android android provided - + + com.google.code.findbugs + jsr305 + provided + junit junit diff --git a/wire-runtime/src/main/java/com/squareup/wire/EnumAdapter.java b/wire-runtime/src/main/java/com/squareup/wire/EnumAdapter.java index 92d7826a5a..3ae33a4dff 100644 --- a/wire-runtime/src/main/java/com/squareup/wire/EnumAdapter.java +++ b/wire-runtime/src/main/java/com/squareup/wire/EnumAdapter.java @@ -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. @@ -46,5 +47,5 @@ protected EnumAdapter(Class 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); } diff --git a/wire-runtime/src/main/java/com/squareup/wire/Message.java b/wire-runtime/src/main/java/com/squareup/wire/Message.java index 147db1a161..7635016a86 100644 --- a/wire-runtime/src/main/java/com/squareup/wire/Message.java +++ b/wire-runtime/src/main/java/com/squareup/wire/Message.java @@ -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; @@ -114,7 +115,7 @@ public abstract static class Builder, 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() { @@ -132,7 +133,8 @@ public final Builder addUnknownFields(ByteString unknownFields) { return this; } - public final Builder addUnknownField(int tag, FieldEncoding fieldEncoding, Object value) { + public final Builder addUnknownField( + int tag, FieldEncoding fieldEncoding, @Nullable Object value) { prepareForNewUnknownFields(); try { ProtoAdapter protoAdapter = (ProtoAdapter) fieldEncoding.rawProtoAdapter(); diff --git a/wire-runtime/src/main/java/com/squareup/wire/ProtoAdapter.java b/wire-runtime/src/main/java/com/squareup/wire/ProtoAdapter.java index bb2be00f94..389992c63b 100644 --- a/wire-runtime/src/main/java/com/squareup/wire/ProtoAdapter.java +++ b/wire-runtime/src/main/java/com/squareup/wire/ProtoAdapter.java @@ -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; @@ -48,10 +49,10 @@ public abstract class ProtoAdapter { private final FieldEncoding fieldEncoding; final Class javaType; - ProtoAdapter> packedAdapter; - ProtoAdapter> repeatedAdapter; + @Nullable ProtoAdapter> packedAdapter; + @Nullable ProtoAdapter> repeatedAdapter; - public ProtoAdapter(FieldEncoding fieldEncoding, Class javaType) { + public ProtoAdapter(FieldEncoding fieldEncoding, @Nullable Class javaType) { this.fieldEncoding = fieldEncoding; this.javaType = javaType; } @@ -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; } @@ -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) { @@ -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) { @@ -517,7 +518,7 @@ private static final class MapProtoAdapter extends ProtoAdapter> private final MapEntryProtoAdapter entryAdapter; MapProtoAdapter(ProtoAdapter keyAdapter, ProtoAdapter valueAdapter) { - super(FieldEncoding.LENGTH_DELIMITED, null); + super(FieldEncoding.LENGTH_DELIMITED, Map.class); entryAdapter = new MapEntryProtoAdapter<>(keyAdapter, valueAdapter); } @@ -573,7 +574,7 @@ private static final class MapEntryProtoAdapter extends ProtoAdapter valueAdapter; MapEntryProtoAdapter(ProtoAdapter keyAdapter, ProtoAdapter valueAdapter) { - super(FieldEncoding.LENGTH_DELIMITED, null); + super(FieldEncoding.LENGTH_DELIMITED, Map.Entry.class); this.keyAdapter = keyAdapter; this.valueAdapter = valueAdapter; } diff --git a/wire-runtime/src/main/java/com/squareup/wire/RuntimeEnumAdapter.java b/wire-runtime/src/main/java/com/squareup/wire/RuntimeEnumAdapter.java index 1cf005339a..4bf5546765 100644 --- a/wire-runtime/src/main/java/com/squareup/wire/RuntimeEnumAdapter.java +++ b/wire-runtime/src/main/java/com/squareup/wire/RuntimeEnumAdapter.java @@ -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 extends EnumAdapter { private final Class 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 type) { super(type); diff --git a/wire-runtime/src/main/java/com/squareup/wire/Wire.java b/wire-runtime/src/main/java/com/squareup/wire/Wire.java index 925d54d558..ad156c0b25 100644 --- a/wire-runtime/src/main/java/com/squareup/wire/Wire.java +++ b/wire-runtime/src/main/java/com/squareup/wire/Wire.java @@ -15,6 +15,8 @@ */ package com.squareup.wire; +import javax.annotation.Nullable; + public final class Wire { private Wire() { } @@ -34,7 +36,7 @@ private Wire() { * second argument, which in this case is the default value for the field * 'f'. */ - public static T get(T value, T defaultValue) { + public static T get(@Nullable T value, T defaultValue) { return value != null ? value : defaultValue; } } diff --git a/wire-runtime/src/main/java/com/squareup/wire/package-info.java b/wire-runtime/src/main/java/com/squareup/wire/package-info.java index 274dddbe86..50d54d6f9b 100644 --- a/wire-runtime/src/main/java/com/squareup/wire/package-info.java +++ b/wire-runtime/src/main/java/com/squareup/wire/package-info.java @@ -17,4 +17,5 @@ /** * Wire runtime support classes. */ +@javax.annotation.ParametersAreNonnullByDefault package com.squareup.wire;