Moved Cache, LruCache, Pair, Primitives and UnsafeAllocator to com.google.gson and made them package private.

This commit is contained in:
Inderjeet Singh 2011-03-30 13:59:06 +00:00
parent 46a8e9b411
commit 5bc80cd693
19 changed files with 62 additions and 79 deletions

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.gson.internal; package com.google.gson;
/** /**
* Defines generic cache interface. * Defines generic cache interface.
@ -22,7 +22,7 @@ package com.google.gson.internal;
* @author Inderjeet Singh * @author Inderjeet Singh
* @author Joel Leitch * @author Joel Leitch
*/ */
public interface $Cache<K, V> { interface Cache<K, V> {
/** /**
* Adds the new value object into the cache for the given key. If the key already * Adds the new value object into the cache for the given key. If the key already

View File

@ -16,8 +16,6 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Cache;
import com.google.gson.internal.$LruCache;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
@ -30,14 +28,14 @@ class DefaultConstructorAllocator {
private static final Constructor<Null> NULL_CONSTRUCTOR = createNullConstructor(); private static final Constructor<Null> NULL_CONSTRUCTOR = createNullConstructor();
// Package private for testing purposes. // Package private for testing purposes.
final $Cache<Class<?>, Constructor<?>> constructorCache; final Cache<Class<?>, Constructor<?>> constructorCache;
public DefaultConstructorAllocator() { public DefaultConstructorAllocator() {
this(200); this(200);
} }
public DefaultConstructorAllocator(int cacheSize) { public DefaultConstructorAllocator(int cacheSize) {
constructorCache = new $LruCache<Class<?>, Constructor<?>>(cacheSize); constructorCache = new LruCache<Class<?>, Constructor<?>>(cacheSize);
} }
private static final Constructor<Null> createNullConstructor() { private static final Constructor<Null> createNullConstructor() {

View File

@ -16,9 +16,6 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Cache;
import com.google.gson.internal.$LruCache;
import com.google.gson.internal.$Pair;
import com.google.gson.internal.$Preconditions; import com.google.gson.internal.$Preconditions;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
@ -42,8 +39,8 @@ public final class FieldAttributes {
private static final String MAX_CACHE_PROPERTY_NAME = private static final String MAX_CACHE_PROPERTY_NAME =
"com.google.gson.annotation_cache_size_hint"; "com.google.gson.annotation_cache_size_hint";
private static final $Cache<$Pair<Class<?>, String>, Collection<Annotation>> ANNOTATION_CACHE = private static final Cache<Pair<Class<?>, String>, Collection<Annotation>> ANNOTATION_CACHE =
new $LruCache<$Pair<Class<?>,String>, Collection<Annotation>>(getMaxCacheSize()); new LruCache<Pair<Class<?>,String>, Collection<Annotation>>(getMaxCacheSize());
private final Class<?> declaringClazz; private final Class<?> declaringClazz;
private final Field field; private final Field field;
@ -157,7 +154,7 @@ public final class FieldAttributes {
*/ */
public Collection<Annotation> getAnnotations() { public Collection<Annotation> getAnnotations() {
if (annotations == null) { if (annotations == null) {
$Pair<Class<?>, String> key = new $Pair<Class<?>, String>(declaringClazz, name); Pair<Class<?>, String> key = new Pair<Class<?>, String>(declaringClazz, name);
annotations = ANNOTATION_CACHE.getElement(key); annotations = ANNOTATION_CACHE.getElement(key);
if (annotations == null) { if (annotations == null) {
annotations = Collections.unmodifiableCollection( annotations = Collections.unmodifiableCollection(

View File

@ -16,7 +16,6 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Primitives;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
@ -388,7 +387,7 @@ public final class Gson {
*/ */
public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException { public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException {
Object object = fromJson(json, (Type) classOfT); Object object = fromJson(json, (Type) classOfT);
return $Primitives.wrap(classOfT).cast(object); return Primitives.wrap(classOfT).cast(object);
} }
/** /**
@ -441,7 +440,7 @@ public final class Gson {
JsonReader jsonReader = new JsonReader(json); JsonReader jsonReader = new JsonReader(json);
Object object = fromJson(jsonReader, classOfT); Object object = fromJson(jsonReader, classOfT);
assertFullConsumption(object, jsonReader); assertFullConsumption(object, jsonReader);
return $Primitives.wrap(classOfT).cast(object); return Primitives.wrap(classOfT).cast(object);
} }
/** /**
@ -520,7 +519,7 @@ public final class Gson {
*/ */
public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException { public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
Object object = fromJson(json, (Type) classOfT); Object object = fromJson(json, (Type) classOfT);
return $Primitives.wrap(classOfT).cast(object); return Primitives.wrap(classOfT).cast(object);
} }
/** /**

View File

@ -16,7 +16,6 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Pair;
import com.google.gson.internal.$Preconditions; import com.google.gson.internal.$Preconditions;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -74,7 +73,7 @@ abstract class JsonDeserializationVisitor<T> implements ObjectNavigator.Visitor
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final boolean visitUsingCustomHandler(ObjectTypePair objTypePair) { public final boolean visitUsingCustomHandler(ObjectTypePair objTypePair) {
$Pair<JsonDeserializer<?>, ObjectTypePair> pair = objTypePair.getMatchingHandler(deserializers); Pair<JsonDeserializer<?>, ObjectTypePair> pair = objTypePair.getMatchingHandler(deserializers);
if (pair == null) { if (pair == null) {
return false; return false;
} }
@ -85,7 +84,7 @@ abstract class JsonDeserializationVisitor<T> implements ObjectNavigator.Visitor
} }
protected Object invokeCustomDeserializer(JsonElement element, protected Object invokeCustomDeserializer(JsonElement element,
$Pair<JsonDeserializer<?>, ObjectTypePair> pair) { Pair<JsonDeserializer<?>, ObjectTypePair> pair) {
if (element == null || element.isJsonNull()) { if (element == null || element.isJsonNull()) {
return null; return null;
} }

View File

@ -16,8 +16,6 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Pair;
import com.google.gson.internal.$Primitives;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -102,7 +100,7 @@ final class JsonObjectDeserializationVisitor<T> extends JsonDeserializationVisit
throw new JsonParseException("Expecting object found: " + json); throw new JsonParseException("Expecting object found: " + json);
} }
JsonElement child = json.getAsJsonObject().get(fName); JsonElement child = json.getAsJsonObject().get(fName);
boolean isPrimitive = $Primitives.isPrimitive(declaredTypeOfField); boolean isPrimitive = Primitives.isPrimitive(declaredTypeOfField);
if (child == null) { // Child will be null if the field wasn't present in Json if (child == null) { // Child will be null if the field wasn't present in Json
return true; return true;
} else if (child.isJsonNull()) { } else if (child.isJsonNull()) {
@ -112,7 +110,7 @@ final class JsonObjectDeserializationVisitor<T> extends JsonDeserializationVisit
return true; return true;
} }
ObjectTypePair objTypePair = new ObjectTypePair(null, declaredTypeOfField, false); ObjectTypePair objTypePair = new ObjectTypePair(null, declaredTypeOfField, false);
$Pair<JsonDeserializer<?>, ObjectTypePair> pair = objTypePair.getMatchingHandler(deserializers); Pair<JsonDeserializer<?>, ObjectTypePair> pair = objTypePair.getMatchingHandler(deserializers);
if (pair == null) { if (pair == null) {
return false; return false;
} }

View File

@ -16,7 +16,6 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Pair;
import com.google.gson.internal.$Types; import com.google.gson.internal.$Types;
import com.google.gson.internal.$Preconditions; import com.google.gson.internal.$Preconditions;
@ -174,7 +173,7 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private JsonElement findAndInvokeCustomSerializer(ObjectTypePair objTypePair) { private JsonElement findAndInvokeCustomSerializer(ObjectTypePair objTypePair) {
$Pair<JsonSerializer<?>,ObjectTypePair> pair = objTypePair.getMatchingHandler(serializers); Pair<JsonSerializer<?>,ObjectTypePair> pair = objTypePair.getMatchingHandler(serializers);
if (pair == null) { if (pair == null) {
return null; return null;
} }

View File

@ -14,26 +14,26 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.gson.internal; package com.google.gson;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
/** /**
* An implementation of the {@link $Cache} interface that evict objects from the cache using an * An implementation of the {@link Cache} interface that evict objects from the cache using an
* LRU (least recently used) algorithm. Object start getting evicted from the cache once the * LRU (least recently used) algorithm. Object start getting evicted from the cache once the
* {@code maxCapacity} is reached. * {@code maxCapacity} is reached.
* *
* @author Inderjeet Singh * @author Inderjeet Singh
* @author Joel Leitch * @author Joel Leitch
*/ */
public final class $LruCache<K, V> extends LinkedHashMap<K, V> implements $Cache<K, V> { final class LruCache<K, V> extends LinkedHashMap<K, V> implements Cache<K, V> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final int maxCapacity; private final int maxCapacity;
public $LruCache(int maxCapacity) { public LruCache(int maxCapacity) {
super(maxCapacity, 0.7F, true); super(maxCapacity, 0.7F, true);
this.maxCapacity = maxCapacity; this.maxCapacity = maxCapacity;
} }

View File

@ -17,7 +17,6 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Types; import com.google.gson.internal.$Types;
import com.google.gson.internal.$UnsafeAllocator;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -33,7 +32,7 @@ import java.lang.reflect.Type;
* @author Joel Leitch * @author Joel Leitch
*/ */
final class MappedObjectConstructor implements ObjectConstructor { final class MappedObjectConstructor implements ObjectConstructor {
private static final $UnsafeAllocator unsafeAllocator = $UnsafeAllocator.create(); private static final UnsafeAllocator unsafeAllocator = UnsafeAllocator.create();
private static final DefaultConstructorAllocator defaultConstructorAllocator = private static final DefaultConstructorAllocator defaultConstructorAllocator =
new DefaultConstructorAllocator(500); new DefaultConstructorAllocator(500);

View File

@ -17,7 +17,6 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Types; import com.google.gson.internal.$Types;
import com.google.gson.internal.$Primitives;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -129,6 +128,6 @@ final class ObjectNavigator {
private static boolean isPrimitiveOrString(Object objectToVisit) { private static boolean isPrimitiveOrString(Object objectToVisit) {
Class<?> realClazz = objectToVisit.getClass(); Class<?> realClazz = objectToVisit.getClass();
return realClazz == Object.class || realClazz == String.class return realClazz == Object.class || realClazz == String.class
|| $Primitives.unwrap(realClazz).isPrimitive(); || Primitives.unwrap(realClazz).isPrimitive();
} }
} }

View File

@ -15,7 +15,6 @@
*/ */
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Pair;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -52,7 +51,7 @@ final class ObjectTypePair {
return String.format("preserveType: %b, type: %s, obj: %s", preserveType, type, obj); return String.format("preserveType: %b, type: %s, obj: %s", preserveType, type, obj);
} }
<HANDLER> $Pair<HANDLER, ObjectTypePair> getMatchingHandler( <HANDLER> Pair<HANDLER, ObjectTypePair> getMatchingHandler(
ParameterizedTypeHandlerMap<HANDLER> handlers) { ParameterizedTypeHandlerMap<HANDLER> handlers) {
HANDLER handler = null; HANDLER handler = null;
if (!preserveType && obj != null) { if (!preserveType && obj != null) {
@ -60,12 +59,12 @@ final class ObjectTypePair {
ObjectTypePair moreSpecificType = toMoreSpecificType(); ObjectTypePair moreSpecificType = toMoreSpecificType();
handler = handlers.getHandlerFor(moreSpecificType.type); handler = handlers.getHandlerFor(moreSpecificType.type);
if (handler != null) { if (handler != null) {
return new $Pair<HANDLER, ObjectTypePair>(handler, moreSpecificType); return new Pair<HANDLER, ObjectTypePair>(handler, moreSpecificType);
} }
} }
// Try the specified type // Try the specified type
handler = handlers.getHandlerFor(type); handler = handlers.getHandlerFor(type);
return handler == null ? null : new $Pair<HANDLER, ObjectTypePair>(handler, this); return handler == null ? null : new Pair<HANDLER, ObjectTypePair>(handler, this);
} }
ObjectTypePair toMoreSpecificType() { ObjectTypePair toMoreSpecificType() {

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.gson.internal; package com.google.gson;
/** /**
* A simple object that holds onto a pair of object references, first and second. * A simple object that holds onto a pair of object references, first and second.
@ -25,11 +25,11 @@ package com.google.gson.internal;
* @param <FIRST> * @param <FIRST>
* @param <SECOND> * @param <SECOND>
*/ */
public final class $Pair<FIRST, SECOND> { final class Pair<FIRST, SECOND> {
public final FIRST first; public final FIRST first;
public final SECOND second; public final SECOND second;
public $Pair(FIRST first, SECOND second) { public Pair(FIRST first, SECOND second) {
this.first = first; this.first = first;
this.second = second; this.second = second;
} }
@ -42,11 +42,11 @@ public final class $Pair<FIRST, SECOND> {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof $Pair<?, ?>)) { if (!(o instanceof Pair<?, ?>)) {
return false; return false;
} }
$Pair<?, ?> that = ($Pair<?, ?>) o; Pair<?, ?> that = (Pair<?, ?>) o;
return equal(this.first, that.first) && equal(this.second, that.second); return equal(this.first, that.first) && equal(this.second, that.second);
} }

View File

@ -16,7 +16,6 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Pair;
import com.google.gson.internal.$Types; import com.google.gson.internal.$Types;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -40,15 +39,15 @@ final class ParameterizedTypeHandlerMap<T> {
private static final Logger logger = private static final Logger logger =
Logger.getLogger(ParameterizedTypeHandlerMap.class.getName()); Logger.getLogger(ParameterizedTypeHandlerMap.class.getName());
private final Map<Type, T> map = new HashMap<Type, T>(); private final Map<Type, T> map = new HashMap<Type, T>();
private final List<$Pair<Class<?>, T>> typeHierarchyList = new ArrayList<$Pair<Class<?>, T>>(); private final List<Pair<Class<?>, T>> typeHierarchyList = new ArrayList<Pair<Class<?>, T>>();
private boolean modifiable = true; private boolean modifiable = true;
public synchronized void registerForTypeHierarchy(Class<?> typeOfT, T value) { public synchronized void registerForTypeHierarchy(Class<?> typeOfT, T value) {
$Pair<Class<?>, T> pair = new $Pair<Class<?>, T>(typeOfT, value); Pair<Class<?>, T> pair = new Pair<Class<?>, T>(typeOfT, value);
registerForTypeHierarchy(pair); registerForTypeHierarchy(pair);
} }
public synchronized void registerForTypeHierarchy($Pair<Class<?>, T> pair) { public synchronized void registerForTypeHierarchy(Pair<Class<?>, T> pair) {
if (!modifiable) { if (!modifiable) {
throw new IllegalStateException("Attempted to modify an unmodifiable map."); throw new IllegalStateException("Attempted to modify an unmodifiable map.");
} }
@ -70,7 +69,7 @@ final class ParameterizedTypeHandlerMap<T> {
private int getIndexOfAnOverriddenHandler(Class<?> type) { private int getIndexOfAnOverriddenHandler(Class<?> type) {
for (int i = typeHierarchyList.size()-1; i >= 0; --i) { for (int i = typeHierarchyList.size()-1; i >= 0; --i) {
$Pair<Class<?>, T> entry = typeHierarchyList.get(i); Pair<Class<?>, T> entry = typeHierarchyList.get(i);
if (type.isAssignableFrom(entry.first)) { if (type.isAssignableFrom(entry.first)) {
return i; return i;
} }
@ -100,7 +99,7 @@ final class ParameterizedTypeHandlerMap<T> {
// Quite important to traverse the typeHierarchyList from stack bottom first since // Quite important to traverse the typeHierarchyList from stack bottom first since
// we want to register the handlers in the same order to preserve priority order // we want to register the handlers in the same order to preserve priority order
for (int i = other.typeHierarchyList.size()-1; i >= 0; --i) { for (int i = other.typeHierarchyList.size()-1; i >= 0; --i) {
$Pair<Class<?>, T> entry = other.typeHierarchyList.get(i); Pair<Class<?>, T> entry = other.typeHierarchyList.get(i);
int index = getIndexOfSpecificHandlerForTypeHierarchy(entry.first); int index = getIndexOfSpecificHandlerForTypeHierarchy(entry.first);
if (index < 0) { if (index < 0) {
registerForTypeHierarchy(entry); registerForTypeHierarchy(entry);
@ -118,7 +117,7 @@ final class ParameterizedTypeHandlerMap<T> {
// Quite important to traverse the typeHierarchyList from stack bottom first since // Quite important to traverse the typeHierarchyList from stack bottom first since
// we want to register the handlers in the same order to preserve priority order // we want to register the handlers in the same order to preserve priority order
for (int i = other.typeHierarchyList.size()-1; i >= 0; --i) { for (int i = other.typeHierarchyList.size()-1; i >= 0; --i) {
$Pair<Class<?>, T> entry = other.typeHierarchyList.get(i); Pair<Class<?>, T> entry = other.typeHierarchyList.get(i);
registerForTypeHierarchy(entry); registerForTypeHierarchy(entry);
} }
} }
@ -152,7 +151,7 @@ final class ParameterizedTypeHandlerMap<T> {
} }
private T getHandlerForTypeHierarchy(Class<?> type) { private T getHandlerForTypeHierarchy(Class<?> type) {
for ($Pair<Class<?>, T> entry : typeHierarchyList) { for (Pair<Class<?>, T> entry : typeHierarchyList) {
if (entry.first.isAssignableFrom(type)) { if (entry.first.isAssignableFrom(type)) {
return entry.second; return entry.second;
} }
@ -186,7 +185,7 @@ final class ParameterizedTypeHandlerMap<T> {
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder("{mapForTypeHierarchy:{"); StringBuilder sb = new StringBuilder("{mapForTypeHierarchy:{");
boolean first = true; boolean first = true;
for ($Pair<Class<?>, T> entry : typeHierarchyList) { for (Pair<Class<?>, T> entry : typeHierarchyList) {
if (first) { if (first) {
first = false; first = false;
} else { } else {

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.gson.internal; package com.google.gson;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -22,14 +22,16 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.gson.internal.$Preconditions;
/** /**
* Contains static utility methods pertaining to primitive types and their * Contains static utility methods pertaining to primitive types and their
* corresponding wrapper types. * corresponding wrapper types.
* *
* @author Kevin Bourrillion * @author Kevin Bourrillion
*/ */
public final class $Primitives { final class Primitives {
private $Primitives() {} private Primitives() {}
/** A map from primitive types to their corresponding wrapper types. */ /** A map from primitive types to their corresponding wrapper types. */
private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER_TYPE; private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER_TYPE;

View File

@ -16,7 +16,6 @@
package com.google.gson; package com.google.gson;
import com.google.gson.ObjectNavigator.Visitor; import com.google.gson.ObjectNavigator.Visitor;
import com.google.gson.internal.$LruCache;
import com.google.gson.internal.$Types; import com.google.gson.internal.$Types;
import com.google.gson.internal.$Preconditions; import com.google.gson.internal.$Preconditions;
@ -34,10 +33,10 @@ import java.util.List;
* @author Jesse Wilson * @author Jesse Wilson
*/ */
final class ReflectingFieldNavigator { final class ReflectingFieldNavigator {
private static final $LruCache<Type, List<Class<?>>> classCache = private static final LruCache<Type, List<Class<?>>> classCache =
new $LruCache<Type, List<Class<?>>>(500); new LruCache<Type, List<Class<?>>>(500);
private static final $LruCache<Class<?>, Field[]> fieldsCache = private static final LruCache<Class<?>, Field[]> fieldsCache =
new $LruCache<Class<?>, Field[]>(500); new LruCache<Class<?>, Field[]>(500);
private final ExclusionStrategy exclusionStrategy; private final ExclusionStrategy exclusionStrategy;

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.gson.internal; package com.google.gson;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectStreamClass; import java.io.ObjectStreamClass;
@ -27,10 +27,10 @@ import java.lang.reflect.Method;
* @author Joel Leitch * @author Joel Leitch
* @author Jesse Wilson * @author Jesse Wilson
*/ */
public abstract class $UnsafeAllocator { abstract class UnsafeAllocator {
public abstract <T> T newInstance(Class<T> c) throws Exception; public abstract <T> T newInstance(Class<T> c) throws Exception;
public static $UnsafeAllocator create() { public static UnsafeAllocator create() {
// try JVM // try JVM
// public class Unsafe { // public class Unsafe {
// public Object allocateInstance(Class<?> type); // public Object allocateInstance(Class<?> type);
@ -41,7 +41,7 @@ public abstract class $UnsafeAllocator {
f.setAccessible(true); f.setAccessible(true);
final Object unsafe = f.get(null); final Object unsafe = f.get(null);
final Method allocateInstance = unsafeClass.getMethod("allocateInstance", Class.class); final Method allocateInstance = unsafeClass.getMethod("allocateInstance", Class.class);
return new $UnsafeAllocator() { return new UnsafeAllocator() {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T newInstance(Class<T> c) throws Exception { public <T> T newInstance(Class<T> c) throws Exception {
@ -60,7 +60,7 @@ public abstract class $UnsafeAllocator {
final Method newInstance = ObjectInputStream.class final Method newInstance = ObjectInputStream.class
.getDeclaredMethod("newInstance", Class.class, Class.class); .getDeclaredMethod("newInstance", Class.class, Class.class);
newInstance.setAccessible(true); newInstance.setAccessible(true);
return new $UnsafeAllocator() { return new UnsafeAllocator() {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T newInstance(Class<T> c) throws Exception { public <T> T newInstance(Class<T> c) throws Exception {
@ -83,7 +83,7 @@ public abstract class $UnsafeAllocator {
final Method newInstance = ObjectStreamClass.class final Method newInstance = ObjectStreamClass.class
.getDeclaredMethod("newInstance", Class.class, int.class); .getDeclaredMethod("newInstance", Class.class, int.class);
newInstance.setAccessible(true); newInstance.setAccessible(true);
return new $UnsafeAllocator() { return new UnsafeAllocator() {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T newInstance(Class<T> c) throws Exception { public <T> T newInstance(Class<T> c) throws Exception {
@ -94,7 +94,7 @@ public abstract class $UnsafeAllocator {
} }
// give up // give up
return new $UnsafeAllocator() { return new UnsafeAllocator() {
@Override @Override
public <T> T newInstance(Class<T> c) { public <T> T newInstance(Class<T> c) {
throw new UnsupportedOperationException("Cannot allocate " + c); throw new UnsupportedOperationException("Cannot allocate " + c);

View File

@ -16,13 +16,11 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Cache;
import com.google.gson.internal.$LruCache;
import junit.framework.TestCase; import junit.framework.TestCase;
/** /**
* Unit test for the {@link $LruCache} class. * Unit test for the {@link LruCache} class.
* *
* @author Inderjeet Singh * @author Inderjeet Singh
* @author Joel Leitch * @author Joel Leitch
@ -30,7 +28,7 @@ import junit.framework.TestCase;
public class LruCacheTest extends TestCase { public class LruCacheTest extends TestCase {
public void testCacheHitAndMiss() throws Exception { public void testCacheHitAndMiss() throws Exception {
$Cache<String, Integer> cache = new $LruCache<String, Integer>(3); Cache<String, Integer> cache = new LruCache<String, Integer>(3);
String key = "key1"; String key = "key1";
assertNull(cache.getElement(key)); assertNull(cache.getElement(key));
@ -44,7 +42,7 @@ public class LruCacheTest extends TestCase {
} }
public void testCacheKeyOverwrite() throws Exception { public void testCacheKeyOverwrite() throws Exception {
$Cache<String, Integer> cache = new $LruCache<String, Integer>(3); Cache<String, Integer> cache = new LruCache<String, Integer>(3);
String key = "key1"; String key = "key1";
assertNull(cache.getElement(key)); assertNull(cache.getElement(key));
@ -56,7 +54,7 @@ public class LruCacheTest extends TestCase {
} }
public void testCacheEviction() throws Exception { public void testCacheEviction() throws Exception {
$Cache<String, Integer> cache = new $LruCache<String, Integer>(5); Cache<String, Integer> cache = new LruCache<String, Integer>(5);
cache.addElement("key1", 1); cache.addElement("key1", 1);
cache.addElement("key2", 2); cache.addElement("key2", 2);

View File

@ -16,7 +16,6 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Primitives;
import com.google.gson.internal.$Types; import com.google.gson.internal.$Types;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -51,7 +50,7 @@ public class ParamterizedTypeFixtures {
private String getExpectedJson(Object obj) { private String getExpectedJson(Object obj) {
Class<?> clazz = obj.getClass(); Class<?> clazz = obj.getClass();
if ($Primitives.isWrapperType($Primitives.wrap(clazz))) { if (Primitives.isWrapperType(Primitives.wrap(clazz))) {
return obj.toString(); return obj.toString();
} else if (obj.getClass().equals(String.class)) { } else if (obj.getClass().equals(String.class)) {
return "\"" + obj.toString() + "\""; return "\"" + obj.toString() + "\"";
@ -127,7 +126,7 @@ public class ParamterizedTypeFixtures {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static<T> String getExpectedJson(MyParameterizedType<T> obj) { public static<T> String getExpectedJson(MyParameterizedType<T> obj) {
Class<T> clazz = (Class<T>) obj.value.getClass(); Class<T> clazz = (Class<T>) obj.value.getClass();
boolean addQuotes = !clazz.isArray() && !$Primitives.unwrap(clazz).isPrimitive(); boolean addQuotes = !clazz.isArray() && !Primitives.unwrap(clazz).isPrimitive();
StringBuilder sb = new StringBuilder("{\""); StringBuilder sb = new StringBuilder("{\"");
sb.append(obj.value.getClass().getSimpleName()).append("\":"); sb.append(obj.value.getClass().getSimpleName()).append("\":");
if (addQuotes) { if (addQuotes) {
@ -156,7 +155,7 @@ public class ParamterizedTypeFixtures {
Class<?> rawType = $Types.getRawType(genericClass); Class<?> rawType = $Types.getRawType(genericClass);
String className = rawType.getSimpleName(); String className = rawType.getSimpleName();
T value = (T) json.getAsJsonObject().get(className).getAsObject(); T value = (T) json.getAsJsonObject().get(className).getAsObject();
if ($Primitives.isPrimitive(genericClass)) { if (Primitives.isPrimitive(genericClass)) {
PrimitiveTypeAdapter typeAdapter = new PrimitiveTypeAdapter(); PrimitiveTypeAdapter typeAdapter = new PrimitiveTypeAdapter();
value = (T) typeAdapter.adaptType(value, rawType); value = (T) typeAdapter.adaptType(value, rawType);
} }

View File

@ -16,7 +16,6 @@
package com.google.gson; package com.google.gson;
import com.google.gson.internal.$Primitives;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -31,8 +30,8 @@ final class PrimitiveTypeAdapter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T adaptType(Object from, Class<T> to) { public <T> T adaptType(Object from, Class<T> to) {
Class<?> aClass = $Primitives.wrap(to); Class<?> aClass = Primitives.wrap(to);
if ($Primitives.isWrapperType(aClass)) { if (Primitives.isWrapperType(aClass)) {
if (aClass == Character.class) { if (aClass == Character.class) {
String value = from.toString(); String value = from.toString();
if (value.length() == 1) { if (value.length() == 1) {