All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.common.reflect.TypeToken Maven / Gradle / Ivy

Go to download

Guava is a suite of core and expanded libraries that include utility classes, google's collections, io classes, and much much more.

There is a newer version: 33.1.0-jre
Show newest version
/*
 * Copyright (C) 2006 The Guava Authors
 *
 * 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.common.reflect;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import com.google.common.annotations.Beta;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ForwardingSet;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.common.primitives.Primitives;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * A {@link Type} with generics.
 *
 * 

Operations that are otherwise only available in {@link Class} are implemented to support * {@code Type}, for example {@link #isSubtypeOf}, {@link #isArray} and {@link #getComponentType}. * It also provides additional utilities such as {@link #getTypes}, {@link #resolveType}, etc. * *

There are three ways to get a {@code TypeToken} instance: * *

    *
  • Wrap a {@code Type} obtained via reflection. For example: {@code * TypeToken.of(method.getGenericReturnType())}. *
  • Capture a generic type with a (usually anonymous) subclass. For example: *
    {@code
     * new TypeToken>() {}
     * }
    *

    Note that it's critical that the actual type argument is carried by a subclass. The * following code is wrong because it only captures the {@code } type variable of the * {@code listType()} method signature; while {@code } is lost in erasure: *

    {@code
     * class Util {
     *   static  TypeToken> listType() {
     *     return new TypeToken>() {};
     *   }
     * }
     *
     * TypeToken> stringListType = Util.listType();
     * }
    *
  • Capture a generic type with a (usually anonymous) subclass and resolve it against a context * class that knows what the type parameters are. For example: *
    {@code
     * abstract class IKnowMyType {
     *   TypeToken type = new TypeToken(getClass()) {};
     * }
     * new IKnowMyType() {}.type => String
     * }
    *
* *

{@code TypeToken} is serializable when no type variable is contained in the type. * *

Note to Guice users: {@code} TypeToken is similar to Guice's {@code TypeLiteral} class except * that it is serializable and offers numerous additional utility methods. * * @author Bob Lee * @author Sven Mawson * @author Ben Yu * @since 12.0 */ @Beta @SuppressWarnings("serial") // SimpleTypeToken is the serialized form. public abstract class TypeToken extends TypeCapture implements Serializable { private final Type runtimeType; /** Resolver for resolving parameter and field types with {@link #runtimeType} as context. */ private transient @MonotonicNonNull TypeResolver invariantTypeResolver; /** Resolver for resolving covariant types with {@link #runtimeType} as context. */ private transient @MonotonicNonNull TypeResolver covariantTypeResolver; /** * Constructs a new type token of {@code T}. * *

Clients create an empty anonymous subclass. Doing so embeds the type parameter in the * anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure. * *

For example: * *

{@code
   * TypeToken> t = new TypeToken>() {};
   * }
*/ protected TypeToken() { this.runtimeType = capture(); checkState( !(runtimeType instanceof TypeVariable), "Cannot construct a TypeToken for a type variable.\n" + "You probably meant to call new TypeToken<%s>(getClass()) " + "that can resolve the type variable for you.\n" + "If you do need to create a TypeToken of a type variable, " + "please use TypeToken.of() instead.", runtimeType); } /** * Constructs a new type token of {@code T} while resolving free type variables in the context of * {@code declaringClass}. * *

Clients create an empty anonymous subclass. Doing so embeds the type parameter in the * anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure. * *

For example: * *

{@code
   * abstract class IKnowMyType {
   *   TypeToken getMyType() {
   *     return new TypeToken(getClass()) {};
   *   }
   * }
   *
   * new IKnowMyType() {}.getMyType() => String
   * }
*/ protected TypeToken(Class declaringClass) { Type captured = super.capture(); if (captured instanceof Class) { this.runtimeType = captured; } else { this.runtimeType = TypeResolver.covariantly(declaringClass).resolveType(captured); } } private TypeToken(Type type) { this.runtimeType = checkNotNull(type); } /** Returns an instance of type token that wraps {@code type}. */ public static TypeToken of(Class type) { return new SimpleTypeToken(type); } /** Returns an instance of type token that wraps {@code type}. */ public static TypeToken of(Type type) { return new SimpleTypeToken<>(type); } /** * Returns the raw type of {@code T}. Formally speaking, if {@code T} is returned by {@link * java.lang.reflect.Method#getGenericReturnType}, the raw type is what's returned by {@link * java.lang.reflect.Method#getReturnType} of the same method object. Specifically: * *
    *
  • If {@code T} is a {@code Class} itself, {@code T} itself is returned. *
  • If {@code T} is a {@link ParameterizedType}, the raw type of the parameterized type is * returned. *
  • If {@code T} is a {@link GenericArrayType}, the returned type is the corresponding array * class. For example: {@code List[] => List[]}. *
  • If {@code T} is a type variable or a wildcard type, the raw type of the first upper bound * is returned. For example: {@code => Foo}. *
*/ public final Class getRawType() { // For wildcard or type variable, the first bound determines the runtime type. Class rawType = getRawTypes().iterator().next(); @SuppressWarnings("unchecked") // raw type is |T| Class result = (Class) rawType; return result; } /** Returns the represented type. */ public final Type getType() { return runtimeType; } /** * Returns a new {@code TypeToken} where type variables represented by {@code typeParam} are * substituted by {@code typeArg}. For example, it can be used to construct {@code Map} for * any {@code K} and {@code V} type: * *
{@code
   * static  TypeToken> mapOf(
   *     TypeToken keyType, TypeToken valueType) {
   *   return new TypeToken>() {}
   *       .where(new TypeParameter() {}, keyType)
   *       .where(new TypeParameter() {}, valueType);
   * }
   * }
* * @param The parameter type * @param typeParam the parameter type variable * @param typeArg the actual type to substitute */ public final TypeToken where(TypeParameter typeParam, TypeToken typeArg) { TypeResolver resolver = new TypeResolver() .where( ImmutableMap.of( new TypeResolver.TypeVariableKey(typeParam.typeVariable), typeArg.runtimeType)); // If there's any type error, we'd report now rather than later. return new SimpleTypeToken(resolver.resolveType(runtimeType)); } /** * Returns a new {@code TypeToken} where type variables represented by {@code typeParam} are * substituted by {@code typeArg}. For example, it can be used to construct {@code Map} for * any {@code K} and {@code V} type: * *
{@code
   * static  TypeToken> mapOf(
   *     Class keyType, Class valueType) {
   *   return new TypeToken>() {}
   *       .where(new TypeParameter() {}, keyType)
   *       .where(new TypeParameter() {}, valueType);
   * }
   * }
* * @param The parameter type * @param typeParam the parameter type variable * @param typeArg the actual type to substitute */ public final TypeToken where(TypeParameter typeParam, Class typeArg) { return where(typeParam, of(typeArg)); } /** * Resolves the given {@code type} against the type context represented by this type. For example: * *
{@code
   * new TypeToken>() {}.resolveType(
   *     List.class.getMethod("get", int.class).getGenericReturnType())
   * => String.class
   * }
*/ public final TypeToken resolveType(Type type) { checkNotNull(type); // Being conservative here because the user could use resolveType() to resolve a type in an // invariant context. return of(getInvariantTypeResolver().resolveType(type)); } private TypeToken resolveSupertype(Type type) { TypeToken supertype = of(getCovariantTypeResolver().resolveType(type)); // super types' type mapping is a subset of type mapping of this type. supertype.covariantTypeResolver = covariantTypeResolver; supertype.invariantTypeResolver = invariantTypeResolver; return supertype; } /** * Returns the generic superclass of this type or {@code null} if the type represents {@link * Object} or an interface. This method is similar but different from {@link * Class#getGenericSuperclass}. For example, {@code new TypeToken() * {}.getGenericSuperclass()} will return {@code new TypeToken>() {}}; while * {@code StringArrayList.class.getGenericSuperclass()} will return {@code ArrayList}, where * {@code E} is the type variable declared by class {@code ArrayList}. * *

If this type is a type variable or wildcard, its first upper bound is examined and returned * if the bound is a class or extends from a class. This means that the returned type could be a * type variable too. */ final @Nullable TypeToken getGenericSuperclass() { if (runtimeType instanceof TypeVariable) { // First bound is always the super class, if one exists. return boundAsSuperclass(((TypeVariable) runtimeType).getBounds()[0]); } if (runtimeType instanceof WildcardType) { // wildcard has one and only one upper bound. return boundAsSuperclass(((WildcardType) runtimeType).getUpperBounds()[0]); } Type superclass = getRawType().getGenericSuperclass(); if (superclass == null) { return null; } @SuppressWarnings("unchecked") // super class of T TypeToken superToken = (TypeToken) resolveSupertype(superclass); return superToken; } private @Nullable TypeToken boundAsSuperclass(Type bound) { TypeToken token = of(bound); if (token.getRawType().isInterface()) { return null; } @SuppressWarnings("unchecked") // only upper bound of T is passed in. TypeToken superclass = (TypeToken) token; return superclass; } /** * Returns the generic interfaces that this type directly {@code implements}. This method is * similar but different from {@link Class#getGenericInterfaces()}. For example, {@code new * TypeToken>() {}.getGenericInterfaces()} will return a list that contains {@code * new TypeToken>() {}}; while {@code List.class.getGenericInterfaces()} will * return an array that contains {@code Iterable}, where the {@code T} is the type variable * declared by interface {@code Iterable}. * *

If this type is a type variable or wildcard, its upper bounds are examined and those that * are either an interface or upper-bounded only by interfaces are returned. This means that the * returned types could include type variables too. */ final ImmutableList> getGenericInterfaces() { if (runtimeType instanceof TypeVariable) { return boundsAsInterfaces(((TypeVariable) runtimeType).getBounds()); } if (runtimeType instanceof WildcardType) { return boundsAsInterfaces(((WildcardType) runtimeType).getUpperBounds()); } ImmutableList.Builder> builder = ImmutableList.builder(); for (Type interfaceType : getRawType().getGenericInterfaces()) { @SuppressWarnings("unchecked") // interface of T TypeToken resolvedInterface = (TypeToken) resolveSupertype(interfaceType); builder.add(resolvedInterface); } return builder.build(); } private ImmutableList> boundsAsInterfaces(Type[] bounds) { ImmutableList.Builder> builder = ImmutableList.builder(); for (Type bound : bounds) { @SuppressWarnings("unchecked") // upper bound of T TypeToken boundType = (TypeToken) of(bound); if (boundType.getRawType().isInterface()) { builder.add(boundType); } } return builder.build(); } /** * Returns the set of interfaces and classes that this type is or is a subtype of. The returned * types are parameterized with proper type arguments. * *

Subtypes are always listed before supertypes. But the reverse is not true. A type isn't * necessarily a subtype of all the types following. Order between types without subtype * relationship is arbitrary and not guaranteed. * *

If this type is a type variable or wildcard, upper bounds that are themselves type variables * aren't included (their super interfaces and superclasses are). */ public final TypeSet getTypes() { return new TypeSet(); } /** * Returns the generic form of {@code superclass}. For example, if this is {@code * ArrayList}, {@code Iterable} is returned given the input {@code * Iterable.class}. */ public final TypeToken getSupertype(Class superclass) { checkArgument( this.someRawTypeIsSubclassOf(superclass), "%s is not a super class of %s", superclass, this); if (runtimeType instanceof TypeVariable) { return getSupertypeFromUpperBounds(superclass, ((TypeVariable) runtimeType).getBounds()); } if (runtimeType instanceof WildcardType) { return getSupertypeFromUpperBounds(superclass, ((WildcardType) runtimeType).getUpperBounds()); } if (superclass.isArray()) { return getArraySupertype(superclass); } @SuppressWarnings("unchecked") // resolved supertype TypeToken supertype = (TypeToken) resolveSupertype(toGenericType(superclass).runtimeType); return supertype; } /** * Returns subtype of {@code this} with {@code subclass} as the raw class. For example, if this is * {@code Iterable} and {@code subclass} is {@code List}, {@code List} is * returned. */ public final TypeToken getSubtype(Class subclass) { checkArgument( !(runtimeType instanceof TypeVariable), "Cannot get subtype of type variable <%s>", this); if (runtimeType instanceof WildcardType) { return getSubtypeFromLowerBounds(subclass, ((WildcardType) runtimeType).getLowerBounds()); } // unwrap array type if necessary if (isArray()) { return getArraySubtype(subclass); } // At this point, it's either a raw class or parameterized type. checkArgument( getRawType().isAssignableFrom(subclass), "%s isn't a subclass of %s", subclass, this); Type resolvedTypeArgs = resolveTypeArgsForSubclass(subclass); @SuppressWarnings("unchecked") // guarded by the isAssignableFrom() statement above TypeToken subtype = (TypeToken) of(resolvedTypeArgs); checkArgument( subtype.isSubtypeOf(this), "%s does not appear to be a subtype of %s", subtype, this); return subtype; } /** * Returns true if this type is a supertype of the given {@code type}. "Supertype" is defined * according to the rules for type * arguments introduced with Java generics. * * @since 19.0 */ public final boolean isSupertypeOf(TypeToken type) { return type.isSubtypeOf(getType()); } /** * Returns true if this type is a supertype of the given {@code type}. "Supertype" is defined * according to the rules for type * arguments introduced with Java generics. * * @since 19.0 */ public final boolean isSupertypeOf(Type type) { return of(type).isSubtypeOf(getType()); } /** * Returns true if this type is a subtype of the given {@code type}. "Subtype" is defined * according to the rules for type * arguments introduced with Java generics. * * @since 19.0 */ public final boolean isSubtypeOf(TypeToken type) { return isSubtypeOf(type.getType()); } /** * Returns true if this type is a subtype of the given {@code type}. "Subtype" is defined * according to the rules for type * arguments introduced with Java generics. * * @since 19.0 */ public final boolean isSubtypeOf(Type supertype) { checkNotNull(supertype); if (supertype instanceof WildcardType) { // if 'supertype' is , 'this' can be: // Foo, SubFoo, . // if 'supertype' is , nothing is a subtype. return any(((WildcardType) supertype).getLowerBounds()).isSupertypeOf(runtimeType); } // if 'this' is wildcard, it's a suptype of to 'supertype' if any of its "extends" // bounds is a subtype of 'supertype'. if (runtimeType instanceof WildcardType) { // is of no use in checking 'from' being a subtype of 'to'. return any(((WildcardType) runtimeType).getUpperBounds()).isSubtypeOf(supertype); } // if 'this' is type variable, it's a subtype if any of its "extends" // bounds is a subtype of 'supertype'. if (runtimeType instanceof TypeVariable) { return runtimeType.equals(supertype) || any(((TypeVariable) runtimeType).getBounds()).isSubtypeOf(supertype); } if (runtimeType instanceof GenericArrayType) { return of(supertype).isSupertypeOfArray((GenericArrayType) runtimeType); } // Proceed to regular Type subtype check if (supertype instanceof Class) { return this.someRawTypeIsSubclassOf((Class) supertype); } else if (supertype instanceof ParameterizedType) { return this.isSubtypeOfParameterizedType((ParameterizedType) supertype); } else if (supertype instanceof GenericArrayType) { return this.isSubtypeOfArrayType((GenericArrayType) supertype); } else { // to instanceof TypeVariable return false; } } /** * Returns true if this type is known to be an array type, such as {@code int[]}, {@code T[]}, * {@code []>} etc. */ public final boolean isArray() { return getComponentType() != null; } /** * Returns true if this type is one of the nine primitive types (including {@code void}). * * @since 15.0 */ public final boolean isPrimitive() { return (runtimeType instanceof Class) && ((Class) runtimeType).isPrimitive(); } /** * Returns the corresponding wrapper type if this is a primitive type; otherwise returns {@code * this} itself. Idempotent. * * @since 15.0 */ public final TypeToken wrap() { if (isPrimitive()) { @SuppressWarnings("unchecked") // this is a primitive class Class type = (Class) runtimeType; return of(Primitives.wrap(type)); } return this; } private boolean isWrapper() { return Primitives.allWrapperTypes().contains(runtimeType); } /** * Returns the corresponding primitive type if this is a wrapper type; otherwise returns {@code * this} itself. Idempotent. * * @since 15.0 */ public final TypeToken unwrap() { if (isWrapper()) { @SuppressWarnings("unchecked") // this is a wrapper class Class type = (Class) runtimeType; return of(Primitives.unwrap(type)); } return this; } /** * Returns the array component type if this type represents an array ({@code int[]}, {@code T[]}, * {@code []>} etc.), or else {@code null} is returned. */ public final @Nullable TypeToken getComponentType() { Type componentType = Types.getComponentType(runtimeType); if (componentType == null) { return null; } return of(componentType); } /** * Returns the {@link Invokable} for {@code method}, which must be a member of {@code T}. * * @since 14.0 */ public final Invokable method(Method method) { checkArgument( this.someRawTypeIsSubclassOf(method.getDeclaringClass()), "%s not declared by %s", method, this); return new Invokable.MethodInvokable(method) { @Override Type getGenericReturnType() { return getCovariantTypeResolver().resolveType(super.getGenericReturnType()); } @Override Type[] getGenericParameterTypes() { return getInvariantTypeResolver().resolveTypesInPlace(super.getGenericParameterTypes()); } @Override Type[] getGenericExceptionTypes() { return getCovariantTypeResolver().resolveTypesInPlace(super.getGenericExceptionTypes()); } @Override public TypeToken getOwnerType() { return TypeToken.this; } @Override public String toString() { return getOwnerType() + "." + super.toString(); } }; } /** * Returns the {@link Invokable} for {@code constructor}, which must be a member of {@code T}. * * @since 14.0 */ public final Invokable constructor(Constructor constructor) { checkArgument( constructor.getDeclaringClass() == getRawType(), "%s not declared by %s", constructor, getRawType()); return new Invokable.ConstructorInvokable(constructor) { @Override Type getGenericReturnType() { return getCovariantTypeResolver().resolveType(super.getGenericReturnType()); } @Override Type[] getGenericParameterTypes() { return getInvariantTypeResolver().resolveTypesInPlace(super.getGenericParameterTypes()); } @Override Type[] getGenericExceptionTypes() { return getCovariantTypeResolver().resolveTypesInPlace(super.getGenericExceptionTypes()); } @Override public TypeToken getOwnerType() { return TypeToken.this; } @Override public String toString() { return getOwnerType() + "(" + Joiner.on(", ").join(getGenericParameterTypes()) + ")"; } }; } /** * The set of interfaces and classes that {@code T} is or is a subtype of. {@link Object} is not * included in the set if this type is an interface. * * @since 13.0 */ public class TypeSet extends ForwardingSet> implements Serializable { private transient @MonotonicNonNull ImmutableSet> types; TypeSet() {} /** Returns the types that are interfaces implemented by this type. */ public TypeSet interfaces() { return new InterfaceSet(this); } /** Returns the types that are classes. */ public TypeSet classes() { return new ClassSet(); } @Override protected Set> delegate() { ImmutableSet> filteredTypes = types; if (filteredTypes == null) { // Java has no way to express ? super T when we parameterize TypeToken vs. Class. @SuppressWarnings({"unchecked", "rawtypes"}) ImmutableList> collectedTypes = (ImmutableList) TypeCollector.FOR_GENERIC_TYPE.collectTypes(TypeToken.this); return (types = FluentIterable.from(collectedTypes) .filter(TypeFilter.IGNORE_TYPE_VARIABLE_OR_WILDCARD) .toSet()); } else { return filteredTypes; } } /** Returns the raw types of the types in this set, in the same order. */ public Set> rawTypes() { // Java has no way to express ? super T when we parameterize TypeToken vs. Class. @SuppressWarnings({"unchecked", "rawtypes"}) ImmutableList> collectedTypes = (ImmutableList) TypeCollector.FOR_RAW_TYPE.collectTypes(getRawTypes()); return ImmutableSet.copyOf(collectedTypes); } private static final long serialVersionUID = 0; } private final class InterfaceSet extends TypeSet { private final transient TypeSet allTypes; private transient @MonotonicNonNull ImmutableSet> interfaces; InterfaceSet(TypeSet allTypes) { this.allTypes = allTypes; } @Override protected Set> delegate() { ImmutableSet> result = interfaces; if (result == null) { return (interfaces = FluentIterable.from(allTypes).filter(TypeFilter.INTERFACE_ONLY).toSet()); } else { return result; } } @Override public TypeSet interfaces() { return this; } @Override public Set> rawTypes() { // Java has no way to express ? super T when we parameterize TypeToken vs. Class. @SuppressWarnings({"unchecked", "rawtypes"}) ImmutableList> collectedTypes = (ImmutableList) TypeCollector.FOR_RAW_TYPE.collectTypes(getRawTypes()); return FluentIterable.from(collectedTypes) .filter( new Predicate>() { @Override public boolean apply(Class type) { return type.isInterface(); } }) .toSet(); } @Override public TypeSet classes() { throw new UnsupportedOperationException("interfaces().classes() not supported."); } private Object readResolve() { return getTypes().interfaces(); } private static final long serialVersionUID = 0; } private final class ClassSet extends TypeSet { private transient @MonotonicNonNull ImmutableSet> classes; @Override protected Set> delegate() { ImmutableSet> result = classes; if (result == null) { @SuppressWarnings({"unchecked", "rawtypes"}) ImmutableList> collectedTypes = (ImmutableList) TypeCollector.FOR_GENERIC_TYPE.classesOnly().collectTypes(TypeToken.this); return (classes = FluentIterable.from(collectedTypes) .filter(TypeFilter.IGNORE_TYPE_VARIABLE_OR_WILDCARD) .toSet()); } else { return result; } } @Override public TypeSet classes() { return this; } @Override public Set> rawTypes() { // Java has no way to express ? super T when we parameterize TypeToken vs. Class. @SuppressWarnings({"unchecked", "rawtypes"}) ImmutableList> collectedTypes = (ImmutableList) TypeCollector.FOR_RAW_TYPE.classesOnly().collectTypes(getRawTypes()); return ImmutableSet.copyOf(collectedTypes); } @Override public TypeSet interfaces() { throw new UnsupportedOperationException("classes().interfaces() not supported."); } private Object readResolve() { return getTypes().classes(); } private static final long serialVersionUID = 0; } private enum TypeFilter implements Predicate> { IGNORE_TYPE_VARIABLE_OR_WILDCARD { @Override public boolean apply(TypeToken type) { return !(type.runtimeType instanceof TypeVariable || type.runtimeType instanceof WildcardType); } }, INTERFACE_ONLY { @Override public boolean apply(TypeToken type) { return type.getRawType().isInterface(); } } } /** * Returns true if {@code o} is another {@code TypeToken} that represents the same {@link Type}. */ @Override public boolean equals(@Nullable Object o) { if (o instanceof TypeToken) { TypeToken that = (TypeToken) o; return runtimeType.equals(that.runtimeType); } return false; } @Override public int hashCode() { return runtimeType.hashCode(); } @Override public String toString() { return Types.toString(runtimeType); } /** Implemented to support serialization of subclasses. */ protected Object writeReplace() { // TypeResolver just transforms the type to our own impls that are Serializable // except TypeVariable. return of(new TypeResolver().resolveType(runtimeType)); } /** * Ensures that this type token doesn't contain type variables, which can cause unchecked type * errors for callers like {@link TypeToInstanceMap}. */ @CanIgnoreReturnValue final TypeToken rejectTypeVariables() { new TypeVisitor() { @Override void visitTypeVariable(TypeVariable type) { throw new IllegalArgumentException( runtimeType + "contains a type variable and is not safe for the operation"); } @Override void visitWildcardType(WildcardType type) { visit(type.getLowerBounds()); visit(type.getUpperBounds()); } @Override void visitParameterizedType(ParameterizedType type) { visit(type.getActualTypeArguments()); visit(type.getOwnerType()); } @Override void visitGenericArrayType(GenericArrayType type) { visit(type.getGenericComponentType()); } }.visit(runtimeType); return this; } private boolean someRawTypeIsSubclassOf(Class superclass) { for (Class rawType : getRawTypes()) { if (superclass.isAssignableFrom(rawType)) { return true; } } return false; } private boolean isSubtypeOfParameterizedType(ParameterizedType supertype) { Class matchedClass = of(supertype).getRawType(); if (!someRawTypeIsSubclassOf(matchedClass)) { return false; } TypeVariable[] typeVars = matchedClass.getTypeParameters(); Type[] supertypeArgs = supertype.getActualTypeArguments(); for (int i = 0; i < typeVars.length; i++) { Type subtypeParam = getCovariantTypeResolver().resolveType(typeVars[i]); // If 'supertype' is "List" // and 'this' is StringArrayList, // First step is to figure out StringArrayList "is-a" List where = String. // String is then matched against , the supertypeArgs[0]. if (!of(subtypeParam).is(supertypeArgs[i], typeVars[i])) { return false; } } // We only care about the case when the supertype is a non-static inner class // in which case we need to make sure the subclass's owner type is a subtype of the // supertype's owner. return Modifier.isStatic(((Class) supertype.getRawType()).getModifiers()) || supertype.getOwnerType() == null || isOwnedBySubtypeOf(supertype.getOwnerType()); } private boolean isSubtypeOfArrayType(GenericArrayType supertype) { if (runtimeType instanceof Class) { Class fromClass = (Class) runtimeType; if (!fromClass.isArray()) { return false; } return of(fromClass.getComponentType()).isSubtypeOf(supertype.getGenericComponentType()); } else if (runtimeType instanceof GenericArrayType) { GenericArrayType fromArrayType = (GenericArrayType) runtimeType; return of(fromArrayType.getGenericComponentType()) .isSubtypeOf(supertype.getGenericComponentType()); } else { return false; } } private boolean isSupertypeOfArray(GenericArrayType subtype) { if (runtimeType instanceof Class) { Class thisClass = (Class) runtimeType; if (!thisClass.isArray()) { return thisClass.isAssignableFrom(Object[].class); } return of(subtype.getGenericComponentType()).isSubtypeOf(thisClass.getComponentType()); } else if (runtimeType instanceof GenericArrayType) { return of(subtype.getGenericComponentType()) .isSubtypeOf(((GenericArrayType) runtimeType).getGenericComponentType()); } else { return false; } } /** * {@code A.is(B)} is defined as {@code Foo.isSubtypeOf(Foo)}. * *

Specifically, returns true if any of the following conditions is met: * *

    *
  1. 'this' and {@code formalType} are equal. *
  2. 'this' and {@code formalType} have equal canonical form. *
  3. {@code formalType} is {@code } and 'this' is a subtype of {@code Foo}. *
  4. {@code formalType} is {@code } and 'this' is a supertype of {@code Foo}. *
* * Note that condition 2 isn't technically accurate under the context of a recursively bounded * type variables. For example, {@code Enum>} canonicalizes to {@code Enum} * where {@code E} is the type variable declared on the {@code Enum} class declaration. It's * technically not true that {@code Foo>>} is a subtype of {@code * Foo>} according to JLS. See testRecursiveWildcardSubtypeBug() for a real example. * *

It appears that properly handling recursive type bounds in the presence of implicit type * bounds is not easy. For now we punt, hoping that this defect should rarely cause issues in real * code. * * @param formalType is {@code Foo} a supertype of {@code Foo}? * @param declaration The type variable in the context of a parameterized type. Used to infer type * bound when {@code formalType} is a wildcard with implicit upper bound. */ private boolean is(Type formalType, TypeVariable declaration) { if (runtimeType.equals(formalType)) { return true; } if (formalType instanceof WildcardType) { WildcardType your = canonicalizeWildcardType(declaration, (WildcardType) formalType); // if "formalType" is , "this" can be: // Foo, SubFoo, , , or // . // if "formalType" is , "this" can be: // Foo, SuperFoo, or . return every(your.getUpperBounds()).isSupertypeOf(runtimeType) && every(your.getLowerBounds()).isSubtypeOf(runtimeType); } return canonicalizeWildcardsInType(runtimeType).equals(canonicalizeWildcardsInType(formalType)); } /** * In reflection, {@code Foo.getUpperBounds()[0]} is always {@code Object.class}, even when Foo * is defined as {@code Foo}. Thus directly calling {@code .is(String.class)} * will return false. To mitigate, we canonicalize wildcards by enforcing the following * invariants: * *

    *
  1. {@code canonicalize(t)} always produces the equal result for equivalent types. For * example both {@code Enum} and {@code Enum>} canonicalize to {@code * Enum}. *
  2. {@code canonicalize(t)} produces a "literal" supertype of t. For example: {@code Enum>} canonicalizes to {@code Enum}, which is a supertype (if we disregard * the upper bound is implicitly an Enum too). *
  3. If {@code canonicalize(A) == canonicalize(B)}, then {@code Foo.isSubtypeOf(Foo)} * and vice versa. i.e. {@code A.is(B)} and {@code B.is(A)}. *
  4. {@code canonicalize(canonicalize(A)) == canonicalize(A)}. *
*/ private static Type canonicalizeTypeArg(TypeVariable declaration, Type typeArg) { return typeArg instanceof WildcardType ? canonicalizeWildcardType(declaration, ((WildcardType) typeArg)) : canonicalizeWildcardsInType(typeArg); } private static Type canonicalizeWildcardsInType(Type type) { if (type instanceof ParameterizedType) { return canonicalizeWildcardsInParameterizedType((ParameterizedType) type); } if (type instanceof GenericArrayType) { return Types.newArrayType( canonicalizeWildcardsInType(((GenericArrayType) type).getGenericComponentType())); } return type; } // WARNING: the returned type may have empty upper bounds, which may violate common expectations // by user code or even some of our own code. It's fine for the purpose of checking subtypes. // Just don't ever let the user access it. private static WildcardType canonicalizeWildcardType( TypeVariable declaration, WildcardType type) { Type[] declared = declaration.getBounds(); List upperBounds = new ArrayList<>(); for (Type bound : type.getUpperBounds()) { if (!any(declared).isSubtypeOf(bound)) { upperBounds.add(canonicalizeWildcardsInType(bound)); } } return new Types.WildcardTypeImpl(type.getLowerBounds(), upperBounds.toArray(new Type[0])); } private static ParameterizedType canonicalizeWildcardsInParameterizedType( ParameterizedType type) { Class rawType = (Class) type.getRawType(); TypeVariable[] typeVars = rawType.getTypeParameters(); Type[] typeArgs = type.getActualTypeArguments(); for (int i = 0; i < typeArgs.length; i++) { typeArgs[i] = canonicalizeTypeArg(typeVars[i], typeArgs[i]); } return Types.newParameterizedTypeWithOwner(type.getOwnerType(), rawType, typeArgs); } private static Bounds every(Type[] bounds) { // Every bound must match. On any false, result is false. return new Bounds(bounds, false); } private static Bounds any(Type[] bounds) { // Any bound matches. On any true, result is true. return new Bounds(bounds, true); } private static class Bounds { private final Type[] bounds; private final boolean target; Bounds(Type[] bounds, boolean target) { this.bounds = bounds; this.target = target; } boolean isSubtypeOf(Type supertype) { for (Type bound : bounds) { if (of(bound).isSubtypeOf(supertype) == target) { return target; } } return !target; } boolean isSupertypeOf(Type subtype) { TypeToken type = of(subtype); for (Type bound : bounds) { if (type.isSubtypeOf(bound) == target) { return target; } } return !target; } } private ImmutableSet> getRawTypes() { final ImmutableSet.Builder> builder = ImmutableSet.builder(); new TypeVisitor() { @Override void visitTypeVariable(TypeVariable t) { visit(t.getBounds()); } @Override void visitWildcardType(WildcardType t) { visit(t.getUpperBounds()); } @Override void visitParameterizedType(ParameterizedType t) { builder.add((Class) t.getRawType()); } @Override void visitClass(Class t) { builder.add(t); } @Override void visitGenericArrayType(GenericArrayType t) { builder.add(Types.getArrayClass(of(t.getGenericComponentType()).getRawType())); } }.visit(runtimeType); // Cast from ImmutableSet> to ImmutableSet> @SuppressWarnings({"unchecked", "rawtypes"}) ImmutableSet> result = (ImmutableSet) builder.build(); return result; } private boolean isOwnedBySubtypeOf(Type supertype) { for (TypeToken type : getTypes()) { Type ownerType = type.getOwnerTypeIfPresent(); if (ownerType != null && of(ownerType).isSubtypeOf(supertype)) { return true; } } return false; } /** * Returns the owner type of a {@link ParameterizedType} or enclosing class of a {@link Class}, or * null otherwise. */ private @Nullable Type getOwnerTypeIfPresent() { if (runtimeType instanceof ParameterizedType) { return ((ParameterizedType) runtimeType).getOwnerType(); } else if (runtimeType instanceof Class) { return ((Class) runtimeType).getEnclosingClass(); } else { return null; } } /** * Returns the type token representing the generic type declaration of {@code cls}. For example: * {@code TypeToken.getGenericType(Iterable.class)} returns {@code Iterable}. * *

If {@code cls} isn't parameterized and isn't a generic array, the type token of the class is * returned. */ @VisibleForTesting static TypeToken toGenericType(Class cls) { if (cls.isArray()) { Type arrayOfGenericType = Types.newArrayType( // If we are passed with int[].class, don't turn it to GenericArrayType toGenericType(cls.getComponentType()).runtimeType); @SuppressWarnings("unchecked") // array is covariant TypeToken result = (TypeToken) of(arrayOfGenericType); return result; } TypeVariable>[] typeParams = cls.getTypeParameters(); Type ownerType = cls.isMemberClass() && !Modifier.isStatic(cls.getModifiers()) ? toGenericType(cls.getEnclosingClass()).runtimeType : null; if ((typeParams.length > 0) || ((ownerType != null) && ownerType != cls.getEnclosingClass())) { @SuppressWarnings("unchecked") // Like, it's Iterable for Iterable.class TypeToken type = (TypeToken) of(Types.newParameterizedTypeWithOwner(ownerType, cls, typeParams)); return type; } else { return of(cls); } } private TypeResolver getCovariantTypeResolver() { TypeResolver resolver = covariantTypeResolver; if (resolver == null) { resolver = (covariantTypeResolver = TypeResolver.covariantly(runtimeType)); } return resolver; } private TypeResolver getInvariantTypeResolver() { TypeResolver resolver = invariantTypeResolver; if (resolver == null) { resolver = (invariantTypeResolver = TypeResolver.invariantly(runtimeType)); } return resolver; } private TypeToken getSupertypeFromUpperBounds( Class supertype, Type[] upperBounds) { for (Type upperBound : upperBounds) { @SuppressWarnings("unchecked") // T's upperbound is . TypeToken bound = (TypeToken) of(upperBound); if (bound.isSubtypeOf(supertype)) { @SuppressWarnings({"rawtypes", "unchecked"}) // guarded by the isSubtypeOf check. TypeToken result = bound.getSupertype((Class) supertype); return result; } } throw new IllegalArgumentException(supertype + " isn't a super type of " + this); } private TypeToken getSubtypeFromLowerBounds(Class subclass, Type[] lowerBounds) { for (Type lowerBound : lowerBounds) { @SuppressWarnings("unchecked") // T's lower bound is TypeToken bound = (TypeToken) of(lowerBound); // Java supports only one lowerbound anyway. return bound.getSubtype(subclass); } throw new IllegalArgumentException(subclass + " isn't a subclass of " + this); } private TypeToken getArraySupertype(Class supertype) { // with component type, we have lost generic type information // Use raw type so that compiler allows us to call getSupertype() @SuppressWarnings("rawtypes") TypeToken componentType = checkNotNull(getComponentType(), "%s isn't a super type of %s", supertype, this); // array is covariant. component type is super type, so is the array type. @SuppressWarnings("unchecked") // going from raw type back to generics TypeToken componentSupertype = componentType.getSupertype(supertype.getComponentType()); @SuppressWarnings("unchecked") // component type is super type, so is array type. TypeToken result = (TypeToken) // If we are passed with int[].class, don't turn it to GenericArrayType of(newArrayClassOrGenericArrayType(componentSupertype.runtimeType)); return result; } private TypeToken getArraySubtype(Class subclass) { // array is covariant. component type is subtype, so is the array type. TypeToken componentSubtype = getComponentType().getSubtype(subclass.getComponentType()); @SuppressWarnings("unchecked") // component type is subtype, so is array type. TypeToken result = (TypeToken) // If we are passed with int[].class, don't turn it to GenericArrayType of(newArrayClassOrGenericArrayType(componentSubtype.runtimeType)); return result; } private Type resolveTypeArgsForSubclass(Class subclass) { // If both runtimeType and subclass are not parameterized, return subclass // If runtimeType is not parameterized but subclass is, process subclass as a parameterized type // If runtimeType is a raw type (i.e. is a parameterized type specified as a Class), we // return subclass as a raw type if (runtimeType instanceof Class && ((subclass.getTypeParameters().length == 0) || (getRawType().getTypeParameters().length != 0))) { // no resolution needed return subclass; } // class Base {} // class Sub extends Base {} // Base.subtype(Sub.class): // Sub.getSupertype(Base.class) => Base // => X=String, Y=Integer // => Sub=Sub TypeToken genericSubtype = toGenericType(subclass); @SuppressWarnings({"rawtypes", "unchecked"}) // subclass isn't Type supertypeWithArgsFromSubtype = genericSubtype.getSupertype((Class) getRawType()).runtimeType; return new TypeResolver() .where(supertypeWithArgsFromSubtype, runtimeType) .resolveType(genericSubtype.runtimeType); } /** * Creates an array class if {@code componentType} is a class, or else, a {@link * GenericArrayType}. This is what Java7 does for generic array type parameters. */ private static Type newArrayClassOrGenericArrayType(Type componentType) { return Types.JavaVersion.JAVA7.newArrayType(componentType); } private static final class SimpleTypeToken extends TypeToken { SimpleTypeToken(Type type) { super(type); } private static final long serialVersionUID = 0; } /** * Collects parent types from a sub type. * * @param The type "kind". Either a TypeToken, or Class. */ private abstract static class TypeCollector { static final TypeCollector> FOR_GENERIC_TYPE = new TypeCollector>() { @Override Class getRawType(TypeToken type) { return type.getRawType(); } @Override Iterable> getInterfaces(TypeToken type) { return type.getGenericInterfaces(); } @Override @Nullable TypeToken getSuperclass(TypeToken type) { return type.getGenericSuperclass(); } }; static final TypeCollector> FOR_RAW_TYPE = new TypeCollector>() { @Override Class getRawType(Class type) { return type; } @Override Iterable> getInterfaces(Class type) { return Arrays.asList(type.getInterfaces()); } @Override @Nullable Class getSuperclass(Class type) { return type.getSuperclass(); } }; /** For just classes, we don't have to traverse interfaces. */ final TypeCollector classesOnly() { return new ForwardingTypeCollector(this) { @Override Iterable getInterfaces(K type) { return ImmutableSet.of(); } @Override ImmutableList collectTypes(Iterable types) { ImmutableList.Builder builder = ImmutableList.builder(); for (K type : types) { if (!getRawType(type).isInterface()) { builder.add(type); } } return super.collectTypes(builder.build()); } }; } final ImmutableList collectTypes(K type) { return collectTypes(ImmutableList.of(type)); } ImmutableList collectTypes(Iterable types) { // type -> order number. 1 for Object, 2 for anything directly below, so on so forth. Map map = Maps.newHashMap(); for (K type : types) { collectTypes(type, map); } return sortKeysByValue(map, Ordering.natural().reverse()); } /** Collects all types to map, and returns the total depth from T up to Object. */ @CanIgnoreReturnValue private int collectTypes(K type, Map map) { Integer existing = map.get(type); if (existing != null) { // short circuit: if set contains type it already contains its supertypes return existing; } // Interfaces should be listed before Object. int aboveMe = getRawType(type).isInterface() ? 1 : 0; for (K interfaceType : getInterfaces(type)) { aboveMe = Math.max(aboveMe, collectTypes(interfaceType, map)); } K superclass = getSuperclass(type); if (superclass != null) { aboveMe = Math.max(aboveMe, collectTypes(superclass, map)); } /* * TODO(benyu): should we include Object for interface? Also, CharSequence[] and Object[] for * String[]? * */ map.put(type, aboveMe + 1); return aboveMe + 1; } private static ImmutableList sortKeysByValue( final Map map, final Comparator valueComparator) { Ordering keyOrdering = new Ordering() { @Override public int compare(K left, K right) { return valueComparator.compare(map.get(left), map.get(right)); } }; return keyOrdering.immutableSortedCopy(map.keySet()); } abstract Class getRawType(K type); abstract Iterable getInterfaces(K type); abstract @Nullable K getSuperclass(K type); private static class ForwardingTypeCollector extends TypeCollector { private final TypeCollector delegate; ForwardingTypeCollector(TypeCollector delegate) { this.delegate = delegate; } @Override Class getRawType(K type) { return delegate.getRawType(type); } @Override Iterable getInterfaces(K type) { return delegate.getInterfaces(type); } @Override K getSuperclass(K type) { return delegate.getSuperclass(type); } } } // This happens to be the hash of the class as of now. So setting it makes a backward compatible // change. Going forward, if any incompatible change is added, we can change the UID back to 1. private static final long serialVersionUID = 3637540370352322684L; }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy