Skip to content

Commit b5831f4

Browse files
authored
Make EncoderException a RuntimeException (#1180)
An error encoding a message is more likely to be unrecoverable than the other way around. Making EncoderException unchecked would reflect this case. This change maintains binary compatibility per https://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html
1 parent 58ce4c0 commit b5831f4

File tree

27 files changed

+104
-148
lines changed

27 files changed

+104
-148
lines changed

encoders/firebase-encoders-json/api.txt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
package com.google.firebase.encoders {
33

44
public interface DataEncoder {
5-
method public void encode(@NonNull Object, @NonNull java.io.Writer) throws com.google.firebase.encoders.EncodingException, java.io.IOException;
6-
method @NonNull public String encode(@NonNull Object) throws com.google.firebase.encoders.EncodingException;
5+
method public void encode(@NonNull Object, @NonNull java.io.Writer) throws java.io.IOException;
6+
method @NonNull public String encode(@NonNull Object);
77
}
88

9-
public final class EncodingException extends java.lang.Exception {
9+
public final class EncodingException extends java.lang.RuntimeException {
1010
ctor public EncodingException(@NonNull String);
1111
ctor public EncodingException(@NonNull String, @NonNull Exception);
1212
}
@@ -15,25 +15,25 @@ package com.google.firebase.encoders {
1515
}
1616

1717
public interface ObjectEncoderContext {
18-
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, @Nullable Object) throws com.google.firebase.encoders.EncodingException, java.io.IOException;
19-
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, double) throws com.google.firebase.encoders.EncodingException, java.io.IOException;
20-
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, int) throws com.google.firebase.encoders.EncodingException, java.io.IOException;
21-
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, long) throws com.google.firebase.encoders.EncodingException, java.io.IOException;
22-
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, boolean) throws com.google.firebase.encoders.EncodingException, java.io.IOException;
23-
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext inline(@Nullable Object) throws com.google.firebase.encoders.EncodingException, java.io.IOException;
18+
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, @Nullable Object) throws java.io.IOException;
19+
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, double) throws java.io.IOException;
20+
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, int) throws java.io.IOException;
21+
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, long) throws java.io.IOException;
22+
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext add(@NonNull String, boolean) throws java.io.IOException;
23+
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext inline(@Nullable Object) throws java.io.IOException;
2424
method @NonNull public com.google.firebase.encoders.ObjectEncoderContext nested(@NonNull String) throws java.io.IOException;
2525
}
2626

2727
public interface ValueEncoder<T> {
2828
}
2929

3030
public interface ValueEncoderContext {
31-
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(@Nullable String) throws com.google.firebase.encoders.EncodingException, java.io.IOException;
32-
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(double) throws com.google.firebase.encoders.EncodingException, java.io.IOException;
33-
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(int) throws com.google.firebase.encoders.EncodingException, java.io.IOException;
34-
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(long) throws com.google.firebase.encoders.EncodingException, java.io.IOException;
35-
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(boolean) throws com.google.firebase.encoders.EncodingException, java.io.IOException;
36-
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(@NonNull byte[]) throws com.google.firebase.encoders.EncodingException, java.io.IOException;
31+
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(@Nullable String) throws java.io.IOException;
32+
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(double) throws java.io.IOException;
33+
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(int) throws java.io.IOException;
34+
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(long) throws java.io.IOException;
35+
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(boolean) throws java.io.IOException;
36+
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(@NonNull byte[]) throws java.io.IOException;
3737
}
3838

3939
}

encoders/firebase-encoders-json/src/json/java/com/google/firebase/encoders/json/JsonDataEncoderBuilder.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ private static final class TimestampEncoder implements ValueEncoder<Date> {
5555
}
5656

5757
@Override
58-
public void encode(@NonNull Date o, @NonNull ValueEncoderContext ctx)
59-
throws EncodingException, IOException {
58+
public void encode(@NonNull Date o, @NonNull ValueEncoderContext ctx) throws IOException {
6059
ctx.add(rfc339.format(o));
6160
}
6261
}
@@ -115,8 +114,7 @@ public JsonDataEncoderBuilder ignoreNullValues(boolean ignore) {
115114
public DataEncoder build() {
116115
return new DataEncoder() {
117116
@Override
118-
public void encode(@NonNull Object o, @NonNull Writer writer)
119-
throws IOException, EncodingException {
117+
public void encode(@NonNull Object o, @NonNull Writer writer) throws IOException {
120118
JsonValueObjectEncoderContext encoderContext =
121119
new JsonValueObjectEncoderContext(
122120
writer, objectEncoders, valueEncoders, fallbackEncoder, ignoreNullValues);
@@ -125,7 +123,7 @@ public void encode(@NonNull Object o, @NonNull Writer writer)
125123
}
126124

127125
@Override
128-
public String encode(@NonNull Object o) throws EncodingException {
126+
public String encode(@NonNull Object o) {
129127
StringWriter stringWriter = new StringWriter();
130128
try {
131129
encode(o, stringWriter);

encoders/firebase-encoders-json/src/json/java/com/google/firebase/encoders/json/JsonValueObjectEncoderContext.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private JsonValueObjectEncoderContext(JsonValueObjectEncoderContext anotherConte
6363
@NonNull
6464
@Override
6565
public JsonValueObjectEncoderContext add(@NonNull String name, @Nullable Object o)
66-
throws IOException, EncodingException {
66+
throws IOException {
6767
if (ignoreNullValues) {
6868
return internalAddIgnoreNullValues(name, o);
6969
}
@@ -72,43 +72,39 @@ public JsonValueObjectEncoderContext add(@NonNull String name, @Nullable Object
7272

7373
@NonNull
7474
@Override
75-
public JsonValueObjectEncoderContext add(@NonNull String name, double value)
76-
throws IOException, EncodingException {
75+
public JsonValueObjectEncoderContext add(@NonNull String name, double value) throws IOException {
7776
maybeUnNest();
7877
jsonWriter.name(name);
7978
return add(value);
8079
}
8180

8281
@NonNull
8382
@Override
84-
public JsonValueObjectEncoderContext add(@NonNull String name, int value)
85-
throws IOException, EncodingException {
83+
public JsonValueObjectEncoderContext add(@NonNull String name, int value) throws IOException {
8684
maybeUnNest();
8785
jsonWriter.name(name);
8886
return add(value);
8987
}
9088

9189
@NonNull
9290
@Override
93-
public JsonValueObjectEncoderContext add(@NonNull String name, long value)
94-
throws IOException, EncodingException {
91+
public JsonValueObjectEncoderContext add(@NonNull String name, long value) throws IOException {
9592
maybeUnNest();
9693
jsonWriter.name(name);
9794
return add(value);
9895
}
9996

10097
@NonNull
10198
@Override
102-
public JsonValueObjectEncoderContext add(@NonNull String name, boolean value)
103-
throws IOException, EncodingException {
99+
public JsonValueObjectEncoderContext add(@NonNull String name, boolean value) throws IOException {
104100
maybeUnNest();
105101
jsonWriter.name(name);
106102
return add(value);
107103
}
108104

109105
@NonNull
110106
@Override
111-
public ObjectEncoderContext inline(@Nullable Object value) throws IOException, EncodingException {
107+
public ObjectEncoderContext inline(@Nullable Object value) throws IOException {
112108
return add(value, true);
113109
}
114110

@@ -124,49 +120,47 @@ public ObjectEncoderContext nested(@NonNull String name) throws IOException {
124120

125121
@NonNull
126122
@Override
127-
public JsonValueObjectEncoderContext add(@Nullable String value)
128-
throws IOException, EncodingException {
123+
public JsonValueObjectEncoderContext add(@Nullable String value) throws IOException {
129124
maybeUnNest();
130125
jsonWriter.value(value);
131126
return this;
132127
}
133128

134129
@NonNull
135130
@Override
136-
public JsonValueObjectEncoderContext add(double value) throws IOException, EncodingException {
131+
public JsonValueObjectEncoderContext add(double value) throws IOException {
137132
maybeUnNest();
138133
jsonWriter.value(value);
139134
return this;
140135
}
141136

142137
@NonNull
143138
@Override
144-
public JsonValueObjectEncoderContext add(int value) throws IOException, EncodingException {
139+
public JsonValueObjectEncoderContext add(int value) throws IOException {
145140
maybeUnNest();
146141
jsonWriter.value(value);
147142
return this;
148143
}
149144

150145
@NonNull
151146
@Override
152-
public JsonValueObjectEncoderContext add(long value) throws IOException, EncodingException {
147+
public JsonValueObjectEncoderContext add(long value) throws IOException {
153148
maybeUnNest();
154149
jsonWriter.value(value);
155150
return this;
156151
}
157152

158153
@NonNull
159154
@Override
160-
public JsonValueObjectEncoderContext add(boolean value) throws IOException, EncodingException {
155+
public JsonValueObjectEncoderContext add(boolean value) throws IOException {
161156
maybeUnNest();
162157
jsonWriter.value(value);
163158
return this;
164159
}
165160

166161
@NonNull
167162
@Override
168-
public JsonValueObjectEncoderContext add(@Nullable byte[] bytes)
169-
throws IOException, EncodingException {
163+
public JsonValueObjectEncoderContext add(@Nullable byte[] bytes) throws IOException {
170164
maybeUnNest();
171165
if (bytes == null) {
172166
jsonWriter.nullValue();
@@ -177,8 +171,7 @@ public JsonValueObjectEncoderContext add(@Nullable byte[] bytes)
177171
}
178172

179173
@NonNull
180-
JsonValueObjectEncoderContext add(@Nullable Object o, boolean inline)
181-
throws IOException, EncodingException {
174+
JsonValueObjectEncoderContext add(@Nullable Object o, boolean inline) throws IOException {
182175
if (inline && cannotBeInline(o)) {
183176
throw new EncodingException(
184177
String.format("%s cannot be encoded inline", o == null ? null : o.getClass()));
@@ -279,7 +272,7 @@ JsonValueObjectEncoderContext add(@Nullable Object o, boolean inline)
279272
}
280273

281274
JsonValueObjectEncoderContext doEncode(ObjectEncoder<Object> encoder, Object o, boolean inline)
282-
throws IOException, EncodingException {
275+
throws IOException {
283276
if (!inline) jsonWriter.beginObject();
284277
encoder.encode(o, this);
285278
if (!inline) jsonWriter.endObject();

encoders/firebase-encoders-json/src/main/java/com/google/firebase/encoders/DataEncoder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
public interface DataEncoder {
2929

3030
/** Encodes {@code obj} into {@code writer}. */
31-
void encode(@NonNull Object obj, @NonNull Writer writer) throws IOException, EncodingException;
31+
void encode(@NonNull Object obj, @NonNull Writer writer) throws IOException;
3232

3333
/** Returns the string-encoded representation of {@code obj}. */
3434
@NonNull
35-
String encode(@NonNull Object obj) throws EncodingException;
35+
String encode(@NonNull Object obj);
3636
}

encoders/firebase-encoders-json/src/main/java/com/google/firebase/encoders/Encoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
interface Encoder<TValue, TContext> {
2626

2727
/** Encode {@code obj} using {@code TContext}. */
28-
void encode(@NonNull TValue obj, @NonNull TContext context) throws EncodingException, IOException;
28+
void encode(@NonNull TValue obj, @NonNull TContext context) throws IOException;
2929
}

encoders/firebase-encoders-json/src/main/java/com/google/firebase/encoders/EncodingException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* An exception thrown when an {@code Encoder} ends up in an invalid state, or if the provided
2121
* object is in an state that cannot be encoded.
2222
*/
23-
public final class EncodingException extends Exception {
23+
public final class EncodingException extends RuntimeException {
2424

2525
/** Creates a {@code EncodingException} with the given message. */
2626
public EncodingException(@NonNull String message) {

encoders/firebase-encoders-json/src/main/java/com/google/firebase/encoders/ObjectEncoderContext.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,23 @@ public interface ObjectEncoderContext {
4848
* by the encoders will be propagated.
4949
*/
5050
@NonNull
51-
ObjectEncoderContext add(@NonNull String name, @Nullable Object obj)
52-
throws IOException, EncodingException;
51+
ObjectEncoderContext add(@NonNull String name, @Nullable Object obj) throws IOException;
5352

5453
/** Add an entry with {@code name} mapped to the encoded primitive type of {@code value}. */
5554
@NonNull
56-
ObjectEncoderContext add(@NonNull String name, double value)
57-
throws IOException, EncodingException;
55+
ObjectEncoderContext add(@NonNull String name, double value) throws IOException;
5856

5957
/** Add an entry with {@code name} mapped to the encoded primitive type of {@code value}. */
6058
@NonNull
61-
ObjectEncoderContext add(@NonNull String name, int value) throws IOException, EncodingException;
59+
ObjectEncoderContext add(@NonNull String name, int value) throws IOException;
6260

6361
/** Add an entry with {@code name} mapped to the encoded primitive type of {@code value}. */
6462
@NonNull
65-
ObjectEncoderContext add(@NonNull String name, long value) throws IOException, EncodingException;
63+
ObjectEncoderContext add(@NonNull String name, long value) throws IOException;
6664

6765
/** Add an entry with {@code name} mapped to the encoded primitive type of {@code value}. */
6866
@NonNull
69-
ObjectEncoderContext add(@NonNull String name, boolean value)
70-
throws IOException, EncodingException;
67+
ObjectEncoderContext add(@NonNull String name, boolean value) throws IOException;
7168

7269
/**
7370
* Encodes a given object inline in current context.
@@ -87,7 +84,7 @@ ObjectEncoderContext add(@NonNull String name, boolean value)
8784
* }</pre>
8885
*/
8986
@NonNull
90-
ObjectEncoderContext inline(@Nullable Object value) throws IOException, EncodingException;
87+
ObjectEncoderContext inline(@Nullable Object value) throws IOException;
9188

9289
/**
9390
* Begin a nested JSON object.

encoders/firebase-encoders-json/src/main/java/com/google/firebase/encoders/ValueEncoderContext.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,25 @@ public interface ValueEncoderContext {
2828

2929
/** Adds {@code value} as a primitive encoded value. */
3030
@NonNull
31-
ValueEncoderContext add(@Nullable String value) throws IOException, EncodingException;
31+
ValueEncoderContext add(@Nullable String value) throws IOException;
3232

3333
/** Adds {@code value} as a primitive encoded value. */
3434
@NonNull
35-
ValueEncoderContext add(double value) throws IOException, EncodingException;
35+
ValueEncoderContext add(double value) throws IOException;
3636

3737
/** Adds {@code value} as a primitive encoded value. */
3838
@NonNull
39-
ValueEncoderContext add(int value) throws IOException, EncodingException;
39+
ValueEncoderContext add(int value) throws IOException;
4040

4141
/** Adds {@code value} as a primitive encoded value. */
4242
@NonNull
43-
ValueEncoderContext add(long value) throws IOException, EncodingException;
43+
ValueEncoderContext add(long value) throws IOException;
4444

4545
/** Adds {@code value} as a primitive encoded value. */
4646
@NonNull
47-
ValueEncoderContext add(boolean value) throws IOException, EncodingException;
47+
ValueEncoderContext add(boolean value) throws IOException;
4848

4949
/** Adds {@code value} as a encoded array of bytes. */
5050
@NonNull
51-
ValueEncoderContext add(@NonNull byte[] bytes) throws IOException, EncodingException;
51+
ValueEncoderContext add(@NonNull byte[] bytes) throws IOException;
5252
}

encoders/firebase-encoders-json/src/test/java/com/google/firebase/encoders/json/JsonDataEncoderBuilderTests.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import androidx.test.ext.junit.runners.AndroidJUnit4;
2020
import com.google.firebase.encoders.DataEncoder;
21-
import com.google.firebase.encoders.EncodingException;
2221
import com.google.firebase.encoders.ObjectEncoderContext;
2322
import com.google.firebase.encoders.ValueEncoderContext;
2423
import java.util.Collections;
@@ -31,7 +30,7 @@ public class JsonDataEncoderBuilderTests {
3130
static class Foo {}
3231

3332
@Test
34-
public void configureWith_shouldCorrectlyRegisterObjectEncoder() throws EncodingException {
33+
public void configureWith_shouldCorrectlyRegisterObjectEncoder() {
3534
DataEncoder encoder =
3635
new JsonDataEncoderBuilder()
3736
.configureWith(
@@ -47,7 +46,7 @@ public void configureWith_shouldCorrectlyRegisterObjectEncoder() throws Encoding
4746
}
4847

4948
@Test
50-
public void configureWith_shouldCorrectlyRegisterValueEncoder() throws EncodingException {
49+
public void configureWith_shouldCorrectlyRegisterValueEncoder() {
5150
DataEncoder encoder =
5251
new JsonDataEncoderBuilder()
5352
.configureWith(
@@ -64,8 +63,7 @@ public void configureWith_shouldCorrectlyRegisterValueEncoder() throws EncodingE
6463
}
6564

6665
@Test
67-
public void ignoreNullValues_shouldCorrectlyEncodeObjectIgnoringNullObjects()
68-
throws EncodingException {
66+
public void ignoreNullValues_shouldCorrectlyEncodeObjectIgnoringNullObjects() {
6967
DataEncoder encoder =
7068
new JsonDataEncoderBuilder()
7169
.configureWith(
@@ -83,8 +81,7 @@ public void ignoreNullValues_shouldCorrectlyEncodeObjectIgnoringNullObjects()
8381
}
8482

8583
@Test
86-
public void ignoreNullValues_shouldCorrectlyEncodeValueIgnoringNullObjects()
87-
throws EncodingException {
84+
public void ignoreNullValues_shouldCorrectlyEncodeValueIgnoringNullObjects() {
8885
DataEncoder encoder =
8986
new JsonDataEncoderBuilder()
9087
.configureWith(

0 commit comments

Comments
 (0)