Package com.google.auto.common
Class MoreTypes
- java.lang.Object
-
- com.google.auto.common.MoreTypes
-
public final class MoreTypes extends Object
Utilities related toTypeMirrorinstances.- Since:
- 2.0
- Author:
- Gregory Kick
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ArrayTypeasArray(TypeMirror maybeArrayType)static DeclaredTypeasDeclared(TypeMirror maybeDeclaredType)Returns aDeclaredTypeif theTypeMirrorrepresents a declared type such as a class, interface, union/compound, or enum or throws anIllegalArgumentException.static ElementasElement(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 ErrorTypeasError(TypeMirror maybeErrorType)Returns aExecutableTypeif theTypeMirrorrepresents an executable type such as may result from missing code, or bad compiles or throws anIllegalArgumentException.static ExecutableTypeasExecutable(TypeMirror maybeExecutableType)Returns aExecutableTypeif theTypeMirrorrepresents an executable type such as a method, constructor, or initializer or throws anIllegalArgumentException.static IntersectionTypeasIntersection(TypeMirror maybeIntersectionType)Returns anIntersectionTypeif theTypeMirrorrepresents an intersection-type or throws anIllegalArgumentException.static TypeMirrorasMemberOf(Types types, DeclaredType container, VariableElement variable)Resolves aVariableElementparameter to a method or constructor based on the given container, or a member of a class.static NoTypeasNoType(TypeMirror maybeNoType)Returns aNoTypeif theTypeMirrorrepresents an non-type such as void, or package, etc.static NullTypeasNullType(TypeMirror maybeNullType)Returns aNullTypeif theTypeMirrorrepresents the null type or throws anIllegalArgumentException.static PrimitiveTypeasPrimitiveType(TypeMirror maybePrimitiveType)Returns aPrimitiveTypeif theTypeMirrorrepresents a primitive type or throws anIllegalArgumentException.static TypeElementasTypeElement(TypeMirror mirror)static com.google.common.collect.ImmutableSet<TypeElement>asTypeElements(Iterable<? extends TypeMirror> mirrors)static TypeVariableasTypeVariable(TypeMirror maybeTypeVariable)Returns aTypeVariableif theTypeMirrorrepresents a type variable or throws anIllegalArgumentException.static WildcardTypeasWildcard(TypeMirror maybeWildcardType)Returns aWildcardTypeif theTypeMirrorrepresents a wildcard type or throws anIllegalArgumentException.static com.google.common.base.Equivalence<TypeMirror>equivalence()Returns anEquivalencethat can be used to compare types.static booleanisConversionFromObjectUnchecked(TypeMirror type)Returns true if castingObjectto the given type will elicit an unchecked warning from the compiler.static booleanisType(TypeMirror type)Returns true if the raw type underlying the givenTypeMirrorrepresents a type that can be referenced by aClass.static booleanisTypeOf(Class<?> clazz, 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<DeclaredType>nonObjectSuperclass(Types types, Elements elements, DeclaredType type)static com.google.common.collect.ImmutableSet<TypeElement>referencedTypes(TypeMirror type)Returns the set of types that are referenced by the givenTypeMirror.
-
-
-
Method Detail
-
equivalence
public static com.google.common.base.Equivalence<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
-
referencedTypes
public static com.google.common.collect.ImmutableSet<TypeElement> referencedTypes(TypeMirror type)
Returns the set of types that are referenced by the givenTypeMirror.
-
asElement
public static Element asElement(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:
NullPointerException- iftypeMirrorisnullIllegalArgumentException- iftypeMirrorcannot be converted to anElement
-
asTypeElement
public static TypeElement asTypeElement(TypeMirror mirror)
-
asTypeElements
public static com.google.common.collect.ImmutableSet<TypeElement> asTypeElements(Iterable<? extends TypeMirror> mirrors)
-
asArray
public static ArrayType asArray(TypeMirror maybeArrayType)
-
asDeclared
public static DeclaredType asDeclared(TypeMirror maybeDeclaredType)
Returns aDeclaredTypeif theTypeMirrorrepresents a declared type such as a class, interface, union/compound, or enum or throws anIllegalArgumentException.
-
asError
public static ErrorType asError(TypeMirror maybeErrorType)
Returns aExecutableTypeif theTypeMirrorrepresents an executable type such as may result from missing code, or bad compiles or throws anIllegalArgumentException.
-
asExecutable
public static ExecutableType asExecutable(TypeMirror maybeExecutableType)
Returns aExecutableTypeif theTypeMirrorrepresents an executable type such as a method, constructor, or initializer or throws anIllegalArgumentException.
-
asIntersection
public static IntersectionType asIntersection(TypeMirror maybeIntersectionType)
Returns anIntersectionTypeif theTypeMirrorrepresents an intersection-type or throws anIllegalArgumentException.
-
asNoType
public static NoType asNoType(TypeMirror maybeNoType)
Returns aNoTypeif theTypeMirrorrepresents an non-type such as void, or package, etc. or throws anIllegalArgumentException.
-
asNullType
public static NullType asNullType(TypeMirror maybeNullType)
Returns aNullTypeif theTypeMirrorrepresents the null type or throws anIllegalArgumentException.
-
asPrimitiveType
public static PrimitiveType asPrimitiveType(TypeMirror maybePrimitiveType)
Returns aPrimitiveTypeif theTypeMirrorrepresents a primitive type or throws anIllegalArgumentException.
-
asTypeVariable
public static TypeVariable asTypeVariable(TypeMirror maybeTypeVariable)
Returns aTypeVariableif theTypeMirrorrepresents a type variable or throws anIllegalArgumentException.
-
asWildcard
public static WildcardType asWildcard(TypeMirror maybeWildcardType)
Returns aWildcardTypeif theTypeMirrorrepresents a wildcard type or throws anIllegalArgumentException.
-
isType
public static boolean isType(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(Class<?> clazz, 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<DeclaredType> nonObjectSuperclass(Types types, Elements elements, DeclaredType type)
-
asMemberOf
public static TypeMirror asMemberOf(Types types, DeclaredType container, 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(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<?, ?>.
-
-