Hide Gson.getNextAdapter() for the current release.

This commit is contained in:
Jesse Wilson 2011-12-31 05:30:40 +00:00
parent 4057b98bab
commit ecdf9150f6
7 changed files with 68 additions and 27 deletions

View File

@ -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<T> typeAdapter = gson.getNextAdapter(this, type);
final TypeAdapter<T> typeAdapter = GsonInternalAccess.INSTANCE.getNextAdapter(gson, this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
@Override public void write(JsonWriter out, T value) throws IOException {

View File

@ -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<T> implements TypeAdapterFactory {
final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate
= new LinkedHashMap<Class<?>, TypeAdapter<?>>();
for (Map.Entry<String, Class<?>> 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);
}

View File

@ -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 <T> TypeAdapter<T> getNextAdapter(TypeAdapterFactory skipPast, TypeToken<T> type) {
boolean skipPastFound = false;
static {
GsonInternalAccess.INSTANCE = new GsonInternalAccess() {
@Override public <T> TypeAdapter<T> getNextAdapter(
Gson gson, TypeAdapterFactory skipPast, TypeToken<T> 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<T> candidate = factory.create(gson, type);
if (candidate != null) {
return candidate;
}
}
continue;
}
TypeAdapter<T> candidate = factory.create(this, type);
if (candidate != null) {
return candidate;
throw new IllegalArgumentException("GSON cannot serialize " + type);
}
}
throw new IllegalArgumentException("GSON cannot serialize " + type);
};
}
/**

View File

@ -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<T> extends TypeAdapter<T> {
TypeAdapter<T> d = delegate;
return d != null
? d
: (delegate = gson.getNextAdapter(skipPast, typeToken));
: (delegate = GsonInternalAccess.INSTANCE.getNextAdapter(gson, skipPast, typeToken));
}
/**

View File

@ -68,7 +68,7 @@ import java.io.Writer;
* nextLong()}, {@code nextString()} or {@code nextNull()}. Writers should make
* exactly one call to one of <code>value()</code> or <code>nullValue()</code>.
* 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.

View File

@ -141,7 +141,7 @@ public final class Excluder implements TypeAdapterFactory, Cloneable {
TypeAdapter<T> d = delegate;
return d != null
? d
: (delegate = gson.getNextAdapter(Excluder.this, type));
: (delegate = GsonInternalAccess.INSTANCE.getNextAdapter(gson, Excluder.this, type));
}
};
}

View File

@ -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 <T> TypeAdapter<T> getNextAdapter(
Gson gson, TypeAdapterFactory skipPast, TypeToken<T> type);
}