2008-09-01 05:13:32 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 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.reflect;
|
|
|
|
|
2011-04-06 02:26:57 +02:00
|
|
|
import com.google.gson.internal.$Gson$Types;
|
|
|
|
import com.google.gson.internal.$Gson$Preconditions;
|
2008-09-01 05:13:32 +02:00
|
|
|
import java.lang.reflect.GenericArrayType;
|
|
|
|
import java.lang.reflect.ParameterizedType;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.lang.reflect.TypeVariable;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
2010-12-03 09:07:13 +01:00
|
|
|
* Represents a generic type {@code T}. Java doesn't yet provide a way to
|
|
|
|
* represent generic types, so this class does. Forces clients to create a
|
|
|
|
* subclass of this class which enables retrieval the type information even at
|
|
|
|
* runtime.
|
|
|
|
*
|
|
|
|
* <p>For example, to create a type literal for {@code List<String>}, you can
|
|
|
|
* create an empty anonymous inner class:
|
2008-09-01 05:13:32 +02:00
|
|
|
*
|
|
|
|
* <p>
|
2011-03-21 23:30:35 +01:00
|
|
|
* {@code TypeToken<List<String>> list = new TypeToken<List<String>>() {};}
|
2010-12-03 09:07:13 +01:00
|
|
|
*
|
|
|
|
* <p>This syntax cannot be used to create type literals that have wildcard
|
|
|
|
* parameters, such as {@code Class<?>} or {@code List<? extends CharSequence>}.
|
2008-09-01 05:13:32 +02:00
|
|
|
*
|
|
|
|
* @author Bob Lee
|
|
|
|
* @author Sven Mawson
|
2010-12-03 09:07:13 +01:00
|
|
|
* @author Jesse Wilson
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
2010-12-03 09:07:13 +01:00
|
|
|
public class TypeToken<T> {
|
2008-09-01 05:13:32 +02:00
|
|
|
final Class<? super T> rawType;
|
|
|
|
final Type type;
|
2010-12-03 09:07:13 +01:00
|
|
|
final int hashCode;
|
2008-09-01 05:13:32 +02:00
|
|
|
|
|
|
|
/**
|
2010-12-03 09:07:13 +01:00
|
|
|
* Constructs a new type literal. Derives represented class from type
|
2008-09-01 05:13:32 +02:00
|
|
|
* parameter.
|
|
|
|
*
|
|
|
|
* <p>Clients create an empty anonymous subclass. Doing so embeds the type
|
2010-12-03 09:07:13 +01:00
|
|
|
* parameter in the anonymous class's type hierarchy so we can reconstitute it
|
|
|
|
* at runtime despite erasure.
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
protected TypeToken() {
|
|
|
|
this.type = getSuperclassTypeParameter(getClass());
|
2011-04-06 02:26:57 +02:00
|
|
|
this.rawType = (Class<? super T>) $Gson$Types.getRawType(type);
|
2010-12-03 09:07:13 +01:00
|
|
|
this.hashCode = type.hashCode();
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-03 09:07:13 +01:00
|
|
|
* Unsafe. Constructs a type literal manually.
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
2010-12-03 09:07:13 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
TypeToken(Type type) {
|
2011-04-06 02:26:57 +02:00
|
|
|
this.type = $Gson$Types.canonicalize($Gson$Preconditions.checkNotNull(type));
|
|
|
|
this.rawType = (Class<? super T>) $Gson$Types.getRawType(this.type);
|
2010-12-03 09:07:13 +01:00
|
|
|
this.hashCode = this.type.hashCode();
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-06 02:26:57 +02:00
|
|
|
* Returns the type from super class's type parameter in {@link $Gson$Types#canonicalize
|
2010-12-03 09:07:13 +01:00
|
|
|
* canonical form}.
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
|
|
|
static Type getSuperclassTypeParameter(Class<?> subclass) {
|
|
|
|
Type superclass = subclass.getGenericSuperclass();
|
2010-12-03 09:07:13 +01:00
|
|
|
if (superclass instanceof Class) {
|
2008-09-01 05:13:32 +02:00
|
|
|
throw new RuntimeException("Missing type parameter.");
|
|
|
|
}
|
2010-12-03 09:07:13 +01:00
|
|
|
ParameterizedType parameterized = (ParameterizedType) superclass;
|
2011-04-06 02:26:57 +02:00
|
|
|
return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-03 09:07:13 +01:00
|
|
|
* Returns the raw (non-generic) type for this type.
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
2010-12-03 09:07:13 +01:00
|
|
|
public final Class<? super T> getRawType() {
|
2008-09-01 05:13:32 +02:00
|
|
|
return rawType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets underlying {@code Type} instance.
|
|
|
|
*/
|
2010-12-03 09:07:13 +01:00
|
|
|
public final Type getType() {
|
2008-09-01 05:13:32 +02:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this type is assignable from the given class object.
|
2010-12-03 19:12:26 +01:00
|
|
|
*
|
|
|
|
* @deprecated this implementation may be inconsistent with javac for types
|
|
|
|
* with wildcards.
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
2010-12-03 19:12:26 +01:00
|
|
|
@Deprecated
|
2008-09-01 05:13:32 +02:00
|
|
|
public boolean isAssignableFrom(Class<?> cls) {
|
|
|
|
return isAssignableFrom((Type) cls);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this type is assignable from the given Type.
|
2010-12-03 19:12:26 +01:00
|
|
|
*
|
|
|
|
* @deprecated this implementation may be inconsistent with javac for types
|
|
|
|
* with wildcards.
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
2010-12-03 19:12:26 +01:00
|
|
|
@Deprecated
|
2008-09-01 05:13:32 +02:00
|
|
|
public boolean isAssignableFrom(Type from) {
|
|
|
|
if (from == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type.equals(from)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-23 19:45:16 +02:00
|
|
|
if (type instanceof Class<?>) {
|
2011-04-06 02:26:57 +02:00
|
|
|
return rawType.isAssignableFrom($Gson$Types.getRawType(from));
|
2008-09-01 05:13:32 +02:00
|
|
|
} else if (type instanceof ParameterizedType) {
|
|
|
|
return isAssignableFrom(from, (ParameterizedType) type,
|
|
|
|
new HashMap<String, Type>());
|
|
|
|
} else if (type instanceof GenericArrayType) {
|
2011-04-06 02:26:57 +02:00
|
|
|
return rawType.isAssignableFrom($Gson$Types.getRawType(from))
|
2008-09-01 05:13:32 +02:00
|
|
|
&& isAssignableFrom(from, (GenericArrayType) type);
|
|
|
|
} else {
|
|
|
|
throw buildUnexpectedTypeError(
|
|
|
|
type, Class.class, ParameterizedType.class, GenericArrayType.class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this type is assignable from the given type token.
|
2010-12-03 19:12:26 +01:00
|
|
|
*
|
|
|
|
* @deprecated this implementation may be inconsistent with javac for types
|
|
|
|
* with wildcards.
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
2010-12-03 19:12:26 +01:00
|
|
|
@Deprecated
|
2008-09-01 05:13:32 +02:00
|
|
|
public boolean isAssignableFrom(TypeToken<?> token) {
|
|
|
|
return isAssignableFrom(token.getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private helper function that performs some assignability checks for
|
|
|
|
* the provided GenericArrayType.
|
|
|
|
*/
|
|
|
|
private static boolean isAssignableFrom(Type from, GenericArrayType to) {
|
|
|
|
Type toGenericComponentType = to.getGenericComponentType();
|
|
|
|
if (toGenericComponentType instanceof ParameterizedType) {
|
|
|
|
Type t = from;
|
|
|
|
if (from instanceof GenericArrayType) {
|
|
|
|
t = ((GenericArrayType) from).getGenericComponentType();
|
2009-09-23 19:45:16 +02:00
|
|
|
} else if (from instanceof Class<?>) {
|
2008-09-01 05:13:32 +02:00
|
|
|
Class<?> classType = (Class<?>) from;
|
|
|
|
while (classType.isArray()) {
|
|
|
|
classType = classType.getComponentType();
|
|
|
|
}
|
|
|
|
t = classType;
|
|
|
|
}
|
|
|
|
return isAssignableFrom(t, (ParameterizedType) toGenericComponentType,
|
|
|
|
new HashMap<String, Type>());
|
|
|
|
}
|
|
|
|
// No generic defined on "to"; therefore, return true and let other
|
|
|
|
// checks determine assignability
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private recursive helper function to actually do the type-safe checking
|
|
|
|
* of assignability.
|
|
|
|
*/
|
|
|
|
private static boolean isAssignableFrom(Type from, ParameterizedType to,
|
|
|
|
Map<String, Type> typeVarMap) {
|
|
|
|
|
|
|
|
if (from == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (to.equals(from)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First figure out the class and any type information.
|
2011-04-06 02:26:57 +02:00
|
|
|
Class<?> clazz = $Gson$Types.getRawType(from);
|
2008-09-01 05:13:32 +02:00
|
|
|
ParameterizedType ptype = null;
|
|
|
|
if (from instanceof ParameterizedType) {
|
|
|
|
ptype = (ParameterizedType) from;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load up parameterized variable info if it was parameterized.
|
|
|
|
if (ptype != null) {
|
|
|
|
Type[] tArgs = ptype.getActualTypeArguments();
|
|
|
|
TypeVariable<?>[] tParams = clazz.getTypeParameters();
|
|
|
|
for (int i = 0; i < tArgs.length; i++) {
|
|
|
|
Type arg = tArgs[i];
|
|
|
|
TypeVariable<?> var = tParams[i];
|
2009-09-23 19:45:16 +02:00
|
|
|
while (arg instanceof TypeVariable<?>) {
|
2008-09-01 05:13:32 +02:00
|
|
|
TypeVariable<?> v = (TypeVariable<?>) arg;
|
|
|
|
arg = typeVarMap.get(v.getName());
|
|
|
|
}
|
|
|
|
typeVarMap.put(var.getName(), arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if they are equivalent under our current mapping.
|
|
|
|
if (typeEquals(ptype, to, typeVarMap)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Type itype : clazz.getGenericInterfaces()) {
|
|
|
|
if (isAssignableFrom(itype, to, new HashMap<String, Type>(typeVarMap))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interfaces didn't work, try the superclass.
|
|
|
|
Type sType = clazz.getGenericSuperclass();
|
2010-12-03 19:12:26 +01:00
|
|
|
return isAssignableFrom(sType, to, new HashMap<String, Type>(typeVarMap));
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if two parameterized types are exactly equal, under the variable
|
|
|
|
* replacement described in the typeVarMap.
|
|
|
|
*/
|
|
|
|
private static boolean typeEquals(ParameterizedType from,
|
|
|
|
ParameterizedType to, Map<String, Type> typeVarMap) {
|
|
|
|
if (from.getRawType().equals(to.getRawType())) {
|
|
|
|
Type[] fromArgs = from.getActualTypeArguments();
|
|
|
|
Type[] toArgs = to.getActualTypeArguments();
|
|
|
|
for (int i = 0; i < fromArgs.length; i++) {
|
|
|
|
if (!matches(fromArgs[i], toArgs[i], typeVarMap)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-03 09:07:13 +01:00
|
|
|
private static AssertionError buildUnexpectedTypeError(
|
|
|
|
Type token, Class<?>... expected) {
|
|
|
|
|
|
|
|
// Build exception message
|
|
|
|
StringBuilder exceptionMessage =
|
|
|
|
new StringBuilder("Unexpected type. Expected one of: ");
|
|
|
|
for (Class<?> clazz : expected) {
|
|
|
|
exceptionMessage.append(clazz.getName()).append(", ");
|
|
|
|
}
|
|
|
|
exceptionMessage.append("but got: ").append(token.getClass().getName())
|
|
|
|
.append(", for type token: ").append(token.toString()).append('.');
|
|
|
|
|
|
|
|
return new AssertionError(exceptionMessage.toString());
|
|
|
|
}
|
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
/**
|
|
|
|
* Checks if two types are the same or are equivalent under a variable mapping
|
|
|
|
* given in the type map that was provided.
|
|
|
|
*/
|
2010-12-03 19:12:26 +01:00
|
|
|
private static boolean matches(Type from, Type to, Map<String, Type> typeMap) {
|
|
|
|
return to.equals(from)
|
|
|
|
|| (from instanceof TypeVariable
|
|
|
|
&& to.equals(typeMap.get(((TypeVariable<?>) from).getName())));
|
2008-09-01 05:13:32 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-12-03 09:07:13 +01:00
|
|
|
@Override public final int hashCode() {
|
|
|
|
return this.hashCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override public final boolean equals(Object o) {
|
|
|
|
return o instanceof TypeToken<?>
|
2011-04-06 02:26:57 +02:00
|
|
|
&& $Gson$Types.equals(type, ((TypeToken<?>) o).type);
|
2010-12-03 09:07:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override public final String toString() {
|
2011-04-06 02:26:57 +02:00
|
|
|
return $Gson$Types.typeToString(type);
|
2010-12-03 09:07:13 +01:00
|
|
|
}
|
|
|
|
|
2008-09-01 05:13:32 +02:00
|
|
|
/**
|
2010-12-03 09:07:13 +01:00
|
|
|
* Gets type literal for the given {@code Type} instance.
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
2010-12-03 09:07:13 +01:00
|
|
|
public static TypeToken<?> get(Type type) {
|
|
|
|
return new TypeToken<Object>(type);
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-03 09:07:13 +01:00
|
|
|
* Gets type literal for the given {@code Class} instance.
|
2008-09-01 05:13:32 +02:00
|
|
|
*/
|
2010-12-03 09:07:13 +01:00
|
|
|
public static <T> TypeToken<T> get(Class<T> type) {
|
|
|
|
return new TypeToken<T>(type);
|
|
|
|
}
|
2008-09-01 05:13:32 +02:00
|
|
|
}
|