Package com.google.auto.common
Class MoreTypes
- java.lang.Object
-
- com.google.auto.common.MoreTypes
-
public final class MoreTypes extends Object
Utilities related toTypeMirror
instances.- Since:
- 2.0
- Author:
- Gregory Kick
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ArrayType
asArray(TypeMirror maybeArrayType)
Returns aArrayType
if theTypeMirror
represents a primitive array or throws anIllegalArgumentException
.static DeclaredType
asDeclared(TypeMirror maybeDeclaredType)
Returns aDeclaredType
if theTypeMirror
represents a declared type such as a class, interface, union/compound, or enum or throws anIllegalArgumentException
.static Element
asElement(TypeMirror typeMirror)
An alternate implementation ofTypes.asElement(javax.lang.model.type.TypeMirror)
that does not require aTypes
instance with the notable difference that it will throwIllegalArgumentException
instead of returning null if theTypeMirror
can not be converted to anElement
.static ErrorType
asError(TypeMirror maybeErrorType)
Returns aExecutableType
if theTypeMirror
represents an executable type such as may result from missing code, or bad compiles or throws anIllegalArgumentException
.static ExecutableType
asExecutable(TypeMirror maybeExecutableType)
Returns aExecutableType
if theTypeMirror
represents an executable type such as a method, constructor, or initializer or throws anIllegalArgumentException
.static TypeMirror
asMemberOf(Types types, DeclaredType container, VariableElement variable)
Resolves aVariableElement
parameter to a method or constructor based on the given container, or a member of a class.static NoType
asNoType(TypeMirror maybeNoType)
Returns aNoType
if theTypeMirror
represents an non-type such as void, or package, etc.static NullType
asNullType(TypeMirror maybeNullType)
Returns aNullType
if theTypeMirror
represents the null type or throws anIllegalArgumentException
.static PrimitiveType
asPrimitiveType(TypeMirror maybePrimitiveType)
Returns aPrimitiveType
if theTypeMirror
represents a primitive type or throws anIllegalArgumentException
.static TypeElement
asTypeElement(TypeMirror mirror)
static com.google.common.collect.ImmutableSet<TypeElement>
asTypeElements(Iterable<? extends TypeMirror> mirrors)
static TypeVariable
asTypeVariable(TypeMirror maybeTypeVariable)
Returns aTypeVariable
if theTypeMirror
represents a type variable or throws anIllegalArgumentException
.static WildcardType
asWildcard(WildcardType maybeWildcardType)
Returns aWildcardType
if theTypeMirror
represents a wildcard type or throws anIllegalArgumentException
.static com.google.common.base.Equivalence<TypeMirror>
equivalence()
static boolean
isType(TypeMirror type)
Returns true if the raw type underlying the givenTypeMirror
represents a type that can be referenced by aClass
.static boolean
isTypeOf(Class<?> clazz, TypeMirror type)
Returns true if the raw type underlying the givenTypeMirror
represents the same raw type as the givenClass
and throws an IllegalArgumentException if theTypeMirror
does not represent a type that can be referenced by aClass
static com.google.common.base.Optional<DeclaredType>
nonObjectSuperclass(Types types, Elements elements, DeclaredType type)
Returns the non-object superclass of the type with the proper type parameters.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()
-
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 aTypes
instance with the notable difference that it will throwIllegalArgumentException
instead of returning null if theTypeMirror
can not be converted to anElement
.- Throws:
NullPointerException
- iftypeMirror
isnull
IllegalArgumentException
- iftypeMirror
cannot 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)
Returns aArrayType
if theTypeMirror
represents a primitive array or throws anIllegalArgumentException
.
-
asDeclared
public static DeclaredType asDeclared(TypeMirror maybeDeclaredType)
Returns aDeclaredType
if theTypeMirror
represents a declared type such as a class, interface, union/compound, or enum or throws anIllegalArgumentException
.
-
asError
public static ErrorType asError(TypeMirror maybeErrorType)
Returns aExecutableType
if theTypeMirror
represents an executable type such as may result from missing code, or bad compiles or throws anIllegalArgumentException
.
-
asExecutable
public static ExecutableType asExecutable(TypeMirror maybeExecutableType)
Returns aExecutableType
if theTypeMirror
represents an executable type such as a method, constructor, or initializer or throws anIllegalArgumentException
.
-
asNoType
public static NoType asNoType(TypeMirror maybeNoType)
Returns aNoType
if theTypeMirror
represents an non-type such as void, or package, etc. or throws anIllegalArgumentException
.
-
asNullType
public static NullType asNullType(TypeMirror maybeNullType)
Returns aNullType
if theTypeMirror
represents the null type or throws anIllegalArgumentException
.
-
asPrimitiveType
public static PrimitiveType asPrimitiveType(TypeMirror maybePrimitiveType)
Returns aPrimitiveType
if theTypeMirror
represents a primitive type or throws anIllegalArgumentException
.
-
asTypeVariable
public static TypeVariable asTypeVariable(TypeMirror maybeTypeVariable)
Returns aTypeVariable
if theTypeMirror
represents a type variable or throws anIllegalArgumentException
.
-
asWildcard
public static WildcardType asWildcard(WildcardType maybeWildcardType)
Returns aWildcardType
if theTypeMirror
represents a wildcard type or throws anIllegalArgumentException
.
-
isType
public static boolean isType(TypeMirror type)
Returns true if the raw type underlying the givenTypeMirror
represents 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 givenTypeMirror
represents the same raw type as the givenClass
and throws an IllegalArgumentException if theTypeMirror
does 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)
Returns the non-object superclass of the type with the proper type parameters. An absent Optional is returned if there is no non-Object superclass.
-
asMemberOf
public static TypeMirror asMemberOf(Types types, DeclaredType container, VariableElement variable)
Resolves aVariableElement
parameter 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 acontainer
of typeSet<String>
, and a variable corresponding to theE e
parameter in theSet.add(E e)
method, this will return a TypeMirror forString
.
-
-