Package com.google.auto.common
Class MoreTypes
java.lang.Object
com.google.auto.common.MoreTypes
Utilities related to
TypeMirror
instances.- Since:
- 2.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
private static final class
private static class
private static class
Visitor that tells whether a type is erased, in the sense of#castIsUnchecked
.private static class
private static final class
private static final class
private static final class
private static final class
private static final class
private static final class
private static final class
private static final class
private static final class
private static final class
private static final class
private static final class
private static final class
private static final class
private static final class
private static final class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ArrayType
asArray
(TypeMirror maybeArrayType) 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 IntersectionType
asIntersection
(TypeMirror maybeIntersectionType) Returns anIntersectionType
if theTypeMirror
represents an intersection-type 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
(TypeMirror maybeWildcardType) Returns aWildcardType
if theTypeMirror
represents a wildcard type or throws anIllegalArgumentException
.private static TypeMirror
Returns the type of the innermost enclosing instance, or null if there is none.private static boolean
equal
(TypeMirror a, TypeMirror b, Set<MoreTypes.ComparedElements> visiting) private static boolean
equalLists
(List<? extends TypeMirror> a, List<? extends TypeMirror> b, Set<MoreTypes.ComparedElements> visiting) static com.google.common.base.Equivalence<TypeMirror>
Returns anEquivalence
that can be used to compare types.private static int
hash
(TypeMirror mirror, Set<Element> visiting) private static int
hashList
(List<? extends TypeMirror> mirrors, Set<Element> visiting) static boolean
Returns true if castingObject
to the given type will elicit an unchecked warning from the compiler.private static boolean
isObjectType
(DeclaredType type) 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) static com.google.common.collect.ImmutableSet<TypeElement>
referencedTypes
(TypeMirror type) Returns the set of types that are referenced by the givenTypeMirror
.
-
Field Details
-
HASH_SEED
private static final int HASH_SEED- See Also:
-
HASH_MULTIPLIER
private static final int HASH_MULTIPLIER- See Also:
-
-
Constructor Details
-
MoreTypes
private MoreTypes()
-
-
Method Details
-
equivalence
Returns anEquivalence
that 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 usingHashSet
withEquivalence.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.isSameType
never considers wildcards equal, even when comparing a type to itself.
- If you don't have an instance of
-
equal
-
enclosingType
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(List<? extends TypeMirror> a, List<? extends TypeMirror> b, Set<MoreTypes.ComparedElements> visiting) -
hashList
-
hash
-
referencedTypes
Returns the set of types that are referenced by the givenTypeMirror
. -
asElement
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
-
asTypeElements
public static com.google.common.collect.ImmutableSet<TypeElement> asTypeElements(Iterable<? extends TypeMirror> mirrors) -
asArray
-
asDeclared
Returns aDeclaredType
if theTypeMirror
represents a declared type such as a class, interface, union/compound, or enum or throws anIllegalArgumentException
. -
asError
Returns aExecutableType
if theTypeMirror
represents an executable type such as may result from missing code, or bad compiles or throws anIllegalArgumentException
. -
asExecutable
Returns aExecutableType
if theTypeMirror
represents an executable type such as a method, constructor, or initializer or throws anIllegalArgumentException
. -
asIntersection
Returns anIntersectionType
if theTypeMirror
represents an intersection-type or throws anIllegalArgumentException
. -
asNoType
Returns aNoType
if theTypeMirror
represents an non-type such as void, or package, etc. or throws anIllegalArgumentException
. -
asNullType
Returns aNullType
if theTypeMirror
represents the null type or throws anIllegalArgumentException
. -
asPrimitiveType
Returns aPrimitiveType
if theTypeMirror
represents a primitive type or throws anIllegalArgumentException
. -
asTypeVariable
Returns aTypeVariable
if theTypeMirror
represents a type variable or throws anIllegalArgumentException
. -
asWildcard
Returns aWildcardType
if theTypeMirror
represents a wildcard type or throws anIllegalArgumentException
. -
isType
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
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) -
isObjectType
-
asMemberOf
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
. -
isConversionFromObjectUnchecked
Returns true if castingObject
to 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<?, ?>
.
-