Package com.google.auto.common
Class MoreTypes
- java.lang.Object
-
- com.google.auto.common.MoreTypes
-
public final class MoreTypes extends java.lang.ObjectUtilities related toTypeMirrorinstances.- Since:
- 2.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classMoreTypes.ArrayTypeVisitorprivate static classMoreTypes.AsElementVisitorprivate static classMoreTypes.CastingTypeVisitor<T>private static classMoreTypes.CastingUncheckedVisitorVisitor that tells whether a type is erased, in the sense of#castIsUnchecked.private static classMoreTypes.ComparedElementsprivate static classMoreTypes.DeclaredTypeVisitorprivate static classMoreTypes.EqualVisitorprivate static classMoreTypes.EqualVisitorParamprivate static classMoreTypes.ErrorTypeVisitorprivate static classMoreTypes.ExecutableTypeVisitorprivate static classMoreTypes.HashVisitorprivate static classMoreTypes.IntersectionTypeVisitorprivate static classMoreTypes.IsTypeOfprivate static classMoreTypes.IsTypeVisitorprivate static classMoreTypes.NoTypeVisitorprivate static classMoreTypes.NullTypeVisitorprivate static classMoreTypes.PrimitiveTypeVisitorprivate static classMoreTypes.ReferencedTypesprivate static classMoreTypes.TypeEquivalenceprivate static classMoreTypes.TypeVariableVisitorprivate static classMoreTypes.WildcardTypeVisitor
-
Field Summary
Fields Modifier and Type Field Description private static intHASH_MULTIPLIERprivate static intHASH_SEED
-
Constructor Summary
Constructors Modifier Constructor Description privateMoreTypes()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static javax.lang.model.type.ArrayTypeasArray(javax.lang.model.type.TypeMirror maybeArrayType)Returns aArrayTypeif theTypeMirrorrepresents an array or throws anIllegalArgumentException.static javax.lang.model.type.DeclaredTypeasDeclared(javax.lang.model.type.TypeMirror maybeDeclaredType)Returns aDeclaredTypeif theTypeMirrorrepresents a declared type such as a class, interface, union/compound, or enum or throws anIllegalArgumentException.static javax.lang.model.element.ElementasElement(javax.lang.model.type.TypeMirror typeMirror)An alternate implementation ofTypes.asElement(javax.lang.model.type.TypeMirror)that does not require aTypesinstance with the notable difference that it will throwIllegalArgumentExceptioninstead of returning null if theTypeMirrorcan not be converted to anElement.static javax.lang.model.type.ErrorTypeasError(javax.lang.model.type.TypeMirror maybeErrorType)Returns aExecutableTypeif theTypeMirrorrepresents an executable type such as may result from missing code, or bad compiles or throws anIllegalArgumentException.static javax.lang.model.type.ExecutableTypeasExecutable(javax.lang.model.type.TypeMirror maybeExecutableType)Returns aExecutableTypeif theTypeMirrorrepresents an executable type such as a method, constructor, or initializer or throws anIllegalArgumentException.static javax.lang.model.type.IntersectionTypeasIntersection(javax.lang.model.type.TypeMirror maybeIntersectionType)Returns anIntersectionTypeif theTypeMirrorrepresents an intersection-type or throws anIllegalArgumentException.static javax.lang.model.type.TypeMirrorasMemberOf(javax.lang.model.util.Types types, javax.lang.model.type.DeclaredType container, javax.lang.model.element.VariableElement variable)Resolves aVariableElementparameter to a method or constructor based on the given container, or a member of a class.static javax.lang.model.type.NoTypeasNoType(javax.lang.model.type.TypeMirror maybeNoType)Returns aNoTypeif theTypeMirrorrepresents an non-type such as void, or package, etc.static javax.lang.model.type.NullTypeasNullType(javax.lang.model.type.TypeMirror maybeNullType)Returns aNullTypeif theTypeMirrorrepresents the null type or throws anIllegalArgumentException.static javax.lang.model.type.PrimitiveTypeasPrimitiveType(javax.lang.model.type.TypeMirror maybePrimitiveType)Returns aPrimitiveTypeif theTypeMirrorrepresents a primitive type or throws anIllegalArgumentException.static javax.lang.model.element.TypeElementasTypeElement(javax.lang.model.type.TypeMirror mirror)static com.google.common.collect.ImmutableSet<javax.lang.model.element.TypeElement>asTypeElements(java.lang.Iterable<? extends javax.lang.model.type.TypeMirror> mirrors)static javax.lang.model.type.TypeVariableasTypeVariable(javax.lang.model.type.TypeMirror maybeTypeVariable)Returns aTypeVariableif theTypeMirrorrepresents a type variable or throws anIllegalArgumentException.static javax.lang.model.type.WildcardTypeasWildcard(javax.lang.model.type.TypeMirror maybeWildcardType)Returns aWildcardTypeif theTypeMirrorrepresents a wildcard type or throws anIllegalArgumentException.private static javax.lang.model.type.TypeMirrorenclosingType(javax.lang.model.type.DeclaredType t)Returns the type of the innermost enclosing instance, or null if there is none.private static booleanequal(javax.lang.model.type.TypeMirror a, javax.lang.model.type.TypeMirror b, java.util.Set<MoreTypes.ComparedElements> visiting)private static booleanequalLists(java.util.List<? extends javax.lang.model.type.TypeMirror> a, java.util.List<? extends javax.lang.model.type.TypeMirror> b, java.util.Set<MoreTypes.ComparedElements> visiting)static com.google.common.base.Equivalence<javax.lang.model.type.TypeMirror>equivalence()Returns anEquivalencethat can be used to compare types.private static inthash(javax.lang.model.type.TypeMirror mirror, java.util.Set<javax.lang.model.element.Element> visiting)private static inthashList(java.util.List<? extends javax.lang.model.type.TypeMirror> mirrors, java.util.Set<javax.lang.model.element.Element> visiting)static booleanisConversionFromObjectUnchecked(javax.lang.model.type.TypeMirror type)Returns true if castingObjectto the given type will elicit an unchecked warning from the compiler.private static booleanisObjectType(javax.lang.model.type.DeclaredType type)static booleanisType(javax.lang.model.type.TypeMirror type)Returns true if the raw type underlying the givenTypeMirrorrepresents a type that can be referenced by aClass.static booleanisTypeOf(java.lang.Class<?> clazz, javax.lang.model.type.TypeMirror type)Returns true if the raw type underlying the givenTypeMirrorrepresents the same raw type as the givenClassand throws an IllegalArgumentException if theTypeMirrordoes not represent a type that can be referenced by aClassstatic com.google.common.base.Optional<javax.lang.model.type.DeclaredType>nonObjectSuperclass(javax.lang.model.util.Types types, javax.lang.model.util.Elements elements, javax.lang.model.type.DeclaredType type)Returns the superclass oftype, with any type parameters bound bytype, orOptional.absent()iftypeis an interface orObjector its superclass isObject.static com.google.common.collect.ImmutableSet<javax.lang.model.element.TypeElement>referencedTypes(javax.lang.model.type.TypeMirror type)Returns the set of types that are referenced by the givenTypeMirror.
-
-
-
Field Detail
-
HASH_SEED
private static final int HASH_SEED
- See Also:
- Constant Field Values
-
HASH_MULTIPLIER
private static final int HASH_MULTIPLIER
- See Also:
- Constant Field Values
-
-
Method Detail
-
equivalence
public static com.google.common.base.Equivalence<javax.lang.model.type.TypeMirror> equivalence()
Returns anEquivalencethat can be used to compare types. The standard way to compare types isTypes.isSameType, but this alternative may be preferred in a number of cases:- If you don't have an instance of
Types. - If you want a reliable
hashCode()for the types, for example to construct a set of types usingHashSetwithEquivalence.wrap(Object). - If you want distinct type variables to be considered equal if they have the same names and bounds.
- If you want wildcard types to compare equal if they have the same bounds.
Types.isSameTypenever considers wildcards equal, even when comparing a type to itself.
- If you don't have an instance of
-
equal
private static boolean equal(javax.lang.model.type.TypeMirror a, javax.lang.model.type.TypeMirror b, java.util.Set<MoreTypes.ComparedElements> visiting)
-
enclosingType
private static javax.lang.model.type.TypeMirror enclosingType(javax.lang.model.type.DeclaredType t)
Returns the type of the innermost enclosing instance, or null if there is none. This is the same asDeclaredType.getEnclosingType()except that it returns null rather than NoType for a static type. We need this because of this bug whereby the Eclipse compiler returns a value for static classes that is not NoType.
-
equalLists
private static boolean equalLists(java.util.List<? extends javax.lang.model.type.TypeMirror> a, java.util.List<? extends javax.lang.model.type.TypeMirror> b, java.util.Set<MoreTypes.ComparedElements> visiting)
-
hashList
private static int hashList(java.util.List<? extends javax.lang.model.type.TypeMirror> mirrors, java.util.Set<javax.lang.model.element.Element> visiting)
-
hash
private static int hash(javax.lang.model.type.TypeMirror mirror, java.util.Set<javax.lang.model.element.Element> visiting)
-
referencedTypes
public static com.google.common.collect.ImmutableSet<javax.lang.model.element.TypeElement> referencedTypes(javax.lang.model.type.TypeMirror type)
Returns the set of types that are referenced by the givenTypeMirror.
-
asElement
public static javax.lang.model.element.Element asElement(javax.lang.model.type.TypeMirror typeMirror)
An alternate implementation ofTypes.asElement(javax.lang.model.type.TypeMirror)that does not require aTypesinstance with the notable difference that it will throwIllegalArgumentExceptioninstead of returning null if theTypeMirrorcan not be converted to anElement.- Throws:
java.lang.NullPointerException- iftypeMirrorisnulljava.lang.IllegalArgumentException- iftypeMirrorcannot be converted to anElement
-
asTypeElement
public static javax.lang.model.element.TypeElement asTypeElement(javax.lang.model.type.TypeMirror mirror)
-
asTypeElements
public static com.google.common.collect.ImmutableSet<javax.lang.model.element.TypeElement> asTypeElements(java.lang.Iterable<? extends javax.lang.model.type.TypeMirror> mirrors)
-
asArray
public static javax.lang.model.type.ArrayType asArray(javax.lang.model.type.TypeMirror maybeArrayType)
Returns aArrayTypeif theTypeMirrorrepresents an array or throws anIllegalArgumentException.
-
asDeclared
public static javax.lang.model.type.DeclaredType asDeclared(javax.lang.model.type.TypeMirror maybeDeclaredType)
Returns aDeclaredTypeif theTypeMirrorrepresents a declared type such as a class, interface, union/compound, or enum or throws anIllegalArgumentException.
-
asError
public static javax.lang.model.type.ErrorType asError(javax.lang.model.type.TypeMirror maybeErrorType)
Returns aExecutableTypeif theTypeMirrorrepresents an executable type such as may result from missing code, or bad compiles or throws anIllegalArgumentException.
-
asExecutable
public static javax.lang.model.type.ExecutableType asExecutable(javax.lang.model.type.TypeMirror maybeExecutableType)
Returns aExecutableTypeif theTypeMirrorrepresents an executable type such as a method, constructor, or initializer or throws anIllegalArgumentException.
-
asIntersection
public static javax.lang.model.type.IntersectionType asIntersection(javax.lang.model.type.TypeMirror maybeIntersectionType)
Returns anIntersectionTypeif theTypeMirrorrepresents an intersection-type or throws anIllegalArgumentException.
-
asNoType
public static javax.lang.model.type.NoType asNoType(javax.lang.model.type.TypeMirror maybeNoType)
Returns aNoTypeif theTypeMirrorrepresents an non-type such as void, or package, etc. or throws anIllegalArgumentException.
-
asNullType
public static javax.lang.model.type.NullType asNullType(javax.lang.model.type.TypeMirror maybeNullType)
Returns aNullTypeif theTypeMirrorrepresents the null type or throws anIllegalArgumentException.
-
asPrimitiveType
public static javax.lang.model.type.PrimitiveType asPrimitiveType(javax.lang.model.type.TypeMirror maybePrimitiveType)
Returns aPrimitiveTypeif theTypeMirrorrepresents a primitive type or throws anIllegalArgumentException.
-
asTypeVariable
public static javax.lang.model.type.TypeVariable asTypeVariable(javax.lang.model.type.TypeMirror maybeTypeVariable)
Returns aTypeVariableif theTypeMirrorrepresents a type variable or throws anIllegalArgumentException.
-
asWildcard
public static javax.lang.model.type.WildcardType asWildcard(javax.lang.model.type.TypeMirror maybeWildcardType)
Returns aWildcardTypeif theTypeMirrorrepresents a wildcard type or throws anIllegalArgumentException.
-
isType
public static boolean isType(javax.lang.model.type.TypeMirror type)
Returns true if the raw type underlying the givenTypeMirrorrepresents a type that can be referenced by aClass. If this returns true, thenisTypeOf(java.lang.Class<?>, javax.lang.model.type.TypeMirror)is guaranteed to not throw.
-
isTypeOf
public static boolean isTypeOf(java.lang.Class<?> clazz, javax.lang.model.type.TypeMirror type)Returns true if the raw type underlying the givenTypeMirrorrepresents the same raw type as the givenClassand throws an IllegalArgumentException if theTypeMirrordoes not represent a type that can be referenced by aClass
-
nonObjectSuperclass
public static com.google.common.base.Optional<javax.lang.model.type.DeclaredType> nonObjectSuperclass(javax.lang.model.util.Types types, javax.lang.model.util.Elements elements, javax.lang.model.type.DeclaredType type)Returns the superclass oftype, with any type parameters bound bytype, orOptional.absent()iftypeis an interface orObjector its superclass isObject.
-
isObjectType
private static boolean isObjectType(javax.lang.model.type.DeclaredType type)
-
asMemberOf
public static javax.lang.model.type.TypeMirror asMemberOf(javax.lang.model.util.Types types, javax.lang.model.type.DeclaredType container, javax.lang.model.element.VariableElement variable)Resolves aVariableElementparameter to a method or constructor based on the given container, or a member of a class. For parameters to a method or constructor, the variable's enclosing element must be a supertype of the container type. For example, given acontainerof typeSet<String>, and a variable corresponding to theE eparameter in theSet.add(E e)method, this will return a TypeMirror forString.
-
isConversionFromObjectUnchecked
public static boolean isConversionFromObjectUnchecked(javax.lang.model.type.TypeMirror type)
Returns true if castingObjectto the given type will elicit an unchecked warning from the compiler. Only type variables and parameterized types such asList<String>produce such warnings. There will be no warning if the type's only type parameters are simple wildcards, as inMap<?, ?>.
-
-