mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Merge pull request #333 from square/eric.0719.nonnull_annotations
Fail earlier with null annotation set.
This commit is contained in:
@@ -119,6 +119,8 @@ public final class Moshi {
|
|||||||
@SuppressWarnings("unchecked") // Factories are required to return only matching JsonAdapters.
|
@SuppressWarnings("unchecked") // Factories are required to return only matching JsonAdapters.
|
||||||
public <T> JsonAdapter<T> nextAdapter(JsonAdapter.Factory skipPast, Type type,
|
public <T> JsonAdapter<T> nextAdapter(JsonAdapter.Factory skipPast, Type type,
|
||||||
Set<? extends Annotation> annotations) {
|
Set<? extends Annotation> annotations) {
|
||||||
|
if (annotations == null) throw new NullPointerException("annotations == null");
|
||||||
|
|
||||||
type = Types.canonicalize(type);
|
type = Types.canonicalize(type);
|
||||||
|
|
||||||
int skipPastIndex = factories.indexOf(skipPast);
|
int skipPastIndex = factories.indexOf(skipPast);
|
||||||
|
@@ -32,6 +32,7 @@ import java.util.List;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import javax.crypto.KeyGenerator;
|
import javax.crypto.KeyGenerator;
|
||||||
import okio.Buffer;
|
import okio.Buffer;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -704,6 +705,23 @@ public final class MoshiTest {
|
|||||||
@Uppercase
|
@Uppercase
|
||||||
static String uppercaseString;
|
static String uppercaseString;
|
||||||
|
|
||||||
|
@Test public void nextJsonAdapterDisallowsNullAnnotations() throws Exception {
|
||||||
|
JsonAdapter.Factory badFactory = new JsonAdapter.Factory() {
|
||||||
|
@Nullable @Override
|
||||||
|
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations,
|
||||||
|
Moshi moshi) {
|
||||||
|
return moshi.nextAdapter(this, type, null);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Moshi moshi = new Moshi.Builder().add(badFactory).build();
|
||||||
|
try {
|
||||||
|
moshi.adapter(Object.class);
|
||||||
|
fail();
|
||||||
|
} catch (NullPointerException expected) {
|
||||||
|
assertThat(expected).hasMessage("annotations == null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test public void delegatingJsonAdapterFactory() throws Exception {
|
@Test public void delegatingJsonAdapterFactory() throws Exception {
|
||||||
Moshi moshi = new Moshi.Builder()
|
Moshi moshi = new Moshi.Builder()
|
||||||
.add(new UppercaseAdapterFactory())
|
.add(new UppercaseAdapterFactory())
|
||||||
|
Reference in New Issue
Block a user