diff --git a/extras/src/main/java/com/google/gson/graph/GraphTypeAdapterFactory.java b/extras/src/main/java/com/google/gson/graph/GraphTypeAdapterFactory.java index 5aaa9e25..95a108c9 100644 --- a/extras/src/main/java/com/google/gson/graph/GraphTypeAdapterFactory.java +++ b/extras/src/main/java/com/google/gson/graph/GraphTypeAdapterFactory.java @@ -20,6 +20,7 @@ import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapterFactory; +import com.google.gson.internal.GsonInternalAccess; import com.google.gson.internal.bind.JsonTreeReader; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; @@ -58,7 +59,7 @@ public final class GraphTypeAdapterFactory implements TypeAdapterFactory { return null; } - final TypeAdapter typeAdapter = gson.getNextAdapter(this, type); + final TypeAdapter typeAdapter = GsonInternalAccess.INSTANCE.getNextAdapter(gson, this, type); final TypeAdapter elementAdapter = gson.getAdapter(JsonElement.class); return new TypeAdapter() { @Override public void write(JsonWriter out, T value) throws IOException { diff --git a/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java b/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java index 81b8b00d..03f75041 100644 --- a/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java +++ b/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java @@ -24,6 +24,7 @@ import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapterFactory; +import com.google.gson.internal.GsonInternalAccess; import com.google.gson.internal.Streams; import com.google.gson.internal.bind.JsonTreeWriter; import com.google.gson.internal.bind.JsonTreeReader; @@ -193,7 +194,8 @@ public final class RuntimeTypeAdapterFactory implements TypeAdapterFactory { final Map, TypeAdapter> subtypeToDelegate = new LinkedHashMap, TypeAdapter>(); for (Map.Entry> entry : labelToSubtype.entrySet()) { - TypeAdapter delegate = gson.getNextAdapter(this, TypeToken.get(entry.getValue())); + TypeAdapter delegate = GsonInternalAccess.INSTANCE + .getNextAdapter(gson, this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 09f3365e..93506bbd 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -18,6 +18,7 @@ package com.google.gson; import com.google.gson.internal.ConstructorConstructor; import com.google.gson.internal.Excluder; +import com.google.gson.internal.GsonInternalAccess; import com.google.gson.internal.Primitives; import com.google.gson.internal.Streams; import com.google.gson.internal.bind.ArrayTypeAdapter; @@ -362,32 +363,29 @@ public final class Gson { } } - /** - * Returns a type adapter for {@code} type that isn't {@code skipPast}. This - * can be used for type adapters to compose other, simpler type adapters. - * - * @throws IllegalArgumentException if this GSON cannot serialize and - * deserialize {@code type}. - * @since 2.1 - */ - public TypeAdapter getNextAdapter(TypeAdapterFactory skipPast, TypeToken type) { - boolean skipPastFound = false; + static { + GsonInternalAccess.INSTANCE = new GsonInternalAccess() { + @Override public TypeAdapter getNextAdapter( + Gson gson, TypeAdapterFactory skipPast, TypeToken type) { + boolean skipPastFound = false; - for (TypeAdapterFactory factory : factories) { - if (!skipPastFound) { - if (factory == skipPast) { - skipPastFound = true; + for (TypeAdapterFactory factory : gson.factories) { + if (!skipPastFound) { + if (factory == skipPast) { + skipPastFound = true; + } + continue; + } + + TypeAdapter candidate = factory.create(gson, type); + if (candidate != null) { + return candidate; + } } - continue; - } - TypeAdapter candidate = factory.create(this, type); - if (candidate != null) { - return candidate; + throw new IllegalArgumentException("GSON cannot serialize " + type); } - } - - throw new IllegalArgumentException("GSON cannot serialize " + type); + }; } /** diff --git a/gson/src/main/java/com/google/gson/TreeTypeAdapter.java b/gson/src/main/java/com/google/gson/TreeTypeAdapter.java index 05f6f3ee..642e1c1b 100644 --- a/gson/src/main/java/com/google/gson/TreeTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/TreeTypeAdapter.java @@ -17,6 +17,7 @@ package com.google.gson; import com.google.gson.internal.$Gson$Preconditions; +import com.google.gson.internal.GsonInternalAccess; import com.google.gson.internal.Streams; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; @@ -75,7 +76,7 @@ final class TreeTypeAdapter extends TypeAdapter { TypeAdapter d = delegate; return d != null ? d - : (delegate = gson.getNextAdapter(skipPast, typeToken)); + : (delegate = GsonInternalAccess.INSTANCE.getNextAdapter(gson, skipPast, typeToken)); } /** diff --git a/gson/src/main/java/com/google/gson/TypeAdapter.java b/gson/src/main/java/com/google/gson/TypeAdapter.java index 2f05641d..c4325708 100644 --- a/gson/src/main/java/com/google/gson/TypeAdapter.java +++ b/gson/src/main/java/com/google/gson/TypeAdapter.java @@ -68,7 +68,7 @@ import java.io.Writer; * nextLong()}, {@code nextString()} or {@code nextNull()}. Writers should make * exactly one call to one of value() or nullValue(). * For arrays, type adapters should start with a call to {@code beginArray()}, - * convert all elements, and finish with a call to {@code endArray}. For + * convert all elements, and finish with a call to {@code endArray()}. For * objects, they should start with {@code beginObject()}, convert the object, * and finish with {@code endObject()}. Failing to convert a value or converting * too many values may cause the application to crash. diff --git a/gson/src/main/java/com/google/gson/internal/Excluder.java b/gson/src/main/java/com/google/gson/internal/Excluder.java index dce4f1d7..61c37b77 100644 --- a/gson/src/main/java/com/google/gson/internal/Excluder.java +++ b/gson/src/main/java/com/google/gson/internal/Excluder.java @@ -141,7 +141,7 @@ public final class Excluder implements TypeAdapterFactory, Cloneable { TypeAdapter d = delegate; return d != null ? d - : (delegate = gson.getNextAdapter(Excluder.this, type)); + : (delegate = GsonInternalAccess.INSTANCE.getNextAdapter(gson, Excluder.this, type)); } }; } diff --git a/gson/src/main/java/com/google/gson/internal/GsonInternalAccess.java b/gson/src/main/java/com/google/gson/internal/GsonInternalAccess.java new file mode 100644 index 00000000..2bef899a --- /dev/null +++ b/gson/src/main/java/com/google/gson/internal/GsonInternalAccess.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gson.internal; + +import com.google.gson.Gson; +import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; +import com.google.gson.reflect.TypeToken; + +/** + * Internal-only APIs of Gson available only to other classes in Gson. + */ +public abstract class GsonInternalAccess { + public static GsonInternalAccess INSTANCE; + + /** + * Returns a type adapter for {@code} type that isn't {@code skipPast}. This + * can be used for type adapters to compose other, simpler type adapters. + * + * @throws IllegalArgumentException if this GSON cannot serialize and + * deserialize {@code type}. + */ + public abstract TypeAdapter getNextAdapter( + Gson gson, TypeAdapterFactory skipPast, TypeToken type); +}