Package com.google.auto.common
Class MoreTypes
- java.lang.Object
-
- com.google.auto.common.MoreTypes
-
public final class MoreTypes extends java.lang.Object
Utilities related toTypeMirror
instances.- Since:
- 2.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
MoreTypes.ArrayTypeVisitor
private static class
MoreTypes.AsElementVisitor
private static class
MoreTypes.CastingTypeVisitor<T>
private static class
MoreTypes.ComparedElements
private static class
MoreTypes.DeclaredTypeVisitor
private static class
MoreTypes.EqualVisitor
private static class
MoreTypes.EqualVisitorParam
private static class
MoreTypes.ErrorTypeVisitor
private static class
MoreTypes.ExecutableTypeVisitor
private static class
MoreTypes.HashVisitor
private static class
MoreTypes.IsTypeOf
private static class
MoreTypes.IsTypeVisitor
private static class
MoreTypes.NoTypeVisitor
private static class
MoreTypes.NullTypeVisitor
private static class
MoreTypes.PrimitiveTypeVisitor
private static class
MoreTypes.ReferencedTypes
private static class
MoreTypes.TypeEquivalence
private static class
MoreTypes.TypeVariableVisitor
private static class
MoreTypes.WildcardTypeVisitor
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.reflect.Method
GET_BOUNDS
private static int
HASH_MULTIPLIER
private static int
HASH_SEED
private static java.lang.Class<?>
INTERSECTION_TYPE
-
Constructor Summary
Constructors Modifier Constructor Description private
MoreTypes()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static javax.lang.model.type.ArrayType
asArray(javax.lang.model.type.TypeMirror maybeArrayType)
Returns aArrayType
if theTypeMirror
represents a primitive array or throws anIllegalArgumentException
.static javax.lang.model.type.DeclaredType
asDeclared(javax.lang.model.type.TypeMirror maybeDeclaredType)
Returns aDeclaredType
if theTypeMirror
represents a declared type such as a class, interface, union/compound, or enum or throws anIllegalArgumentException
.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 aTypes
instance with the notable difference that it will throwIllegalArgumentException
instead of returning null if theTypeMirror
can not be converted to anElement
.static javax.lang.model.type.ErrorType
asError(javax.lang.model.type.TypeMirror maybeErrorType)
Returns aExecutableType
if theTypeMirror
represents an executable type such as may result from missing code, or bad compiles or throws anIllegalArgumentException
.static javax.lang.model.type.ExecutableType
asExecutable(javax.lang.model.type.TypeMirror maybeExecutableType)
Returns aExecutableType
if theTypeMirror
represents an executable type such as a method, constructor, or initializer or throws anIllegalArgumentException
.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 aVariableElement
parameter to a method or constructor based on the given container, or a member of a class.static javax.lang.model.type.NoType
asNoType(javax.lang.model.type.TypeMirror maybeNoType)
Returns aNoType
if theTypeMirror
represents an non-type such as void, or package, etc.static javax.lang.model.type.NullType
asNullType(javax.lang.model.type.TypeMirror maybeNullType)
Returns aNullType
if theTypeMirror
represents the null type or throws anIllegalArgumentException
.static javax.lang.model.type.PrimitiveType
asPrimitiveType(javax.lang.model.type.TypeMirror maybePrimitiveType)
Returns aPrimitiveType
if theTypeMirror
represents a primitive type or throws anIllegalArgumentException
.static javax.lang.model.element.TypeElement
asTypeElement(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.TypeVariable
asTypeVariable(javax.lang.model.type.TypeMirror maybeTypeVariable)
Returns aTypeVariable
if theTypeMirror
represents a type variable or throws anIllegalArgumentException
.static javax.lang.model.type.WildcardType
asWildcard(javax.lang.model.type.TypeMirror maybeWildcardType)
Returns aWildcardType
if theTypeMirror
represents a wildcard type or throws anIllegalArgumentException
.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.private static boolean
equal(javax.lang.model.type.TypeMirror a, javax.lang.model.type.TypeMirror b, java.util.Set<MoreTypes.ComparedElements> visiting)
private static boolean
equalIntersectionTypes(javax.lang.model.type.TypeMirror a, javax.lang.model.type.TypeMirror b, java.util.Set<MoreTypes.ComparedElements> visiting)
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)
static com.google.common.base.Equivalence<javax.lang.model.type.TypeMirror>
equivalence()
private static int
hash(javax.lang.model.type.TypeMirror mirror, java.util.Set<javax.lang.model.element.Element> visiting)
private static int
hashList(java.util.List<? extends javax.lang.model.type.TypeMirror> mirrors, java.util.Set<javax.lang.model.element.Element> visiting)
private static boolean
isIntersectionType(javax.lang.model.type.TypeMirror t)
static boolean
isType(javax.lang.model.type.TypeMirror type)
Returns true if the raw type underlying the givenTypeMirror
represents a type that can be referenced by aClass
.static boolean
isTypeOf(java.lang.Class<?> clazz, javax.lang.model.type.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<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 non-object superclass of the type with the proper type parameters.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
-
INTERSECTION_TYPE
private static final java.lang.Class<?> INTERSECTION_TYPE
-
GET_BOUNDS
private static final java.lang.reflect.Method GET_BOUNDS
-
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()
-
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.
-
isIntersectionType
private static boolean isIntersectionType(javax.lang.model.type.TypeMirror t)
-
equalIntersectionTypes
private static boolean equalIntersectionTypes(javax.lang.model.type.TypeMirror a, javax.lang.model.type.TypeMirror b, java.util.Set<MoreTypes.ComparedElements> visiting)
-
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 aTypes
instance with the notable difference that it will throwIllegalArgumentException
instead of returning null if theTypeMirror
can not be converted to anElement
.- Throws:
java.lang.NullPointerException
- iftypeMirror
isnull
java.lang.IllegalArgumentException
- iftypeMirror
cannot 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 aArrayType
if theTypeMirror
represents a primitive array or throws anIllegalArgumentException
.
-
asDeclared
public static javax.lang.model.type.DeclaredType asDeclared(javax.lang.model.type.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 javax.lang.model.type.ErrorType asError(javax.lang.model.type.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 javax.lang.model.type.ExecutableType asExecutable(javax.lang.model.type.TypeMirror maybeExecutableType)
Returns aExecutableType
if theTypeMirror
represents an executable type such as a method, constructor, or initializer or throws anIllegalArgumentException
.
-
asNoType
public static javax.lang.model.type.NoType asNoType(javax.lang.model.type.TypeMirror maybeNoType)
Returns aNoType
if theTypeMirror
represents 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 aNullType
if theTypeMirror
represents the null type or throws anIllegalArgumentException
.
-
asPrimitiveType
public static javax.lang.model.type.PrimitiveType asPrimitiveType(javax.lang.model.type.TypeMirror maybePrimitiveType)
Returns aPrimitiveType
if theTypeMirror
represents a primitive type or throws anIllegalArgumentException
.
-
asTypeVariable
public static javax.lang.model.type.TypeVariable asTypeVariable(javax.lang.model.type.TypeMirror maybeTypeVariable)
Returns aTypeVariable
if theTypeMirror
represents a type variable or throws anIllegalArgumentException
.
-
asWildcard
public static javax.lang.model.type.WildcardType asWildcard(javax.lang.model.type.TypeMirror maybeWildcardType)
Returns aWildcardType
if theTypeMirror
represents 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 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(java.lang.Class<?> clazz, javax.lang.model.type.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<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 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 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 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
.
-
-