Package org.checkerframework.javacutil
Class TypesUtils
java.lang.Object
org.checkerframework.javacutil.TypesUtils
A utility class that helps with
TypeMirror
s. It complements Types
, providing
methods that Types
does not.-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
areSame
(TypeVariable typeVariable1, TypeVariable typeVariable2) Returns true if the two type variables are the same type variable.static boolean
areSameDeclaredTypes
(com.sun.tools.javac.code.Type.ClassType t1, com.sun.tools.javac.code.Type.ClassType t2) Returns true iff the arguments are both the same declared types.static boolean
areSamePrimitiveTypes
(TypeMirror left, TypeMirror right) Returns true iff the arguments are both the same primitive type.static @Nullable TypeMirror
asSuper
(TypeMirror type, TypeMirror superType, ProcessingEnvironment env) Returnstype
assuperType
ifsuperType
is a super type oftype
; otherwise, null.static @BinaryName String
binaryName
(TypeMirror type) Returns the binary name of a type.static boolean
canBeNarrowingPrimitiveConversion
(TypeMirror type, Types types) Returns true if the type is byte, short, char, Byte, Short, or Character.static ArrayType
createArrayType
(TypeMirror componentType, Types types) Returns anArrayType
with elements of typecomponentType
.static @Nullable TypeMirror
findConcreteUpperBound
(TypeMirror boundedType) Given a bounded type (wildcard or typevar) get the concrete type of its upper bound.static TypeMirror
freshTypeVariable
(TypeMirror typeMirror, ProcessingEnvironment env) IftypeMirror
is a wildcard, returns a fresh type variable that will be used as a captured type variable for it.static int
getArrayDepth
(TypeMirror arrayType) Returns the depth of an array type.static @Nullable WildcardType
getCapturedWildcard
(TypeVariable typeVar) IftypeVar
is a captured type variable, then returns its underlying wildcard; otherwise returnsnull
.static Class
<?> getClassFromType
(TypeMirror typeMirror) Returns theClass
for a givenTypeMirror
.static TypeMirror
Given an array type, returns the type with all array levels stripped off.static DeclaredType
Returns theDeclaredType
forjava.lang.Object
.Returns the type of primitive conversion fromfrom
toto
.static @CanonicalNameOrEmpty String
getQualifiedName
(DeclaredType type) Gets the fully qualified name for a provided type.static @Nullable TypeMirror
getSuperclass
(TypeMirror type, Types types) Returns the superclass of the given class.static @Nullable DeclaredType
getSuperClassOrInterface
(TypeMirror type, Types types) Returns the superclass the given type.static @Nullable TypeElement
getTypeElement
(TypeMirror type) Returns the type element fortype
iftype
is a class, interface, annotation type, or enum.static TypeMirror
greatestLowerBound
(TypeMirror tm1, TypeMirror tm2, ProcessingEnvironment processingEnv) Returns the greatest lower bound of twoTypeMirror
s, ignoring any annotations on the types.static boolean
hasEnclosingType
(TypeMirror type) Returns true iftype
has an enclosing type.static boolean
Returns true iftype
is a wildcard with an explicit extends bound.static boolean
Returns true iftype
is a wildcard with an explicit super bound.static boolean
hasNoExplicitBound
(TypeMirror type) Returns true iftype
is an unbounded wildcard.static boolean
isAnonymous
(TypeMirror type) Returns true iff the argument is an anonymous type.static boolean
isBooleanType
(TypeMirror type) Returns true if the type is eitherboolean
(primitive type) orjava.lang.Boolean
.static boolean
isBoxedFloating
(TypeMirror type) Returns true iff the argument is a boxed floating point type.static boolean
isBoxedPrimitive
(TypeMirror type) Returns true iff thetype
represents a boxed primitive type.static boolean
isBoxOf
(TypeMirror declaredType, TypeMirror primitiveType) Returns true if declaredType is a Class that is used to box primitive type (e.g.static boolean
Returns true iftype
is a type variable created during capture conversion.static boolean
isCharType
(TypeMirror type) Returns true if the type ischar
orCharacter
.static boolean
isClass
(TypeMirror type) Return true iff the type represents a java.lang.Class declared type.static boolean
isClassType
(TypeMirror type) Returns whether a TypeMirror represents a class type.static boolean
isCompoundType
(TypeMirror type) Returns true if the given type is a compound type.static boolean
isDeclaredOfName
(TypeMirror type, CharSequence qualifiedName) Returns true iff the type represents a declared type of the given qualified name.static boolean
isDeclaredOfName
(TypeMirror type, Collection<String> qualifiedNames) Returns true iff the type represents a declared type whose fully-qualified name is any of the given names.static boolean
isErasedSubtype
(TypeMirror subtype, TypeMirror supertype, Types types) Returns true if the erased type ofsubtype
is a subtype of the erased type ofsupertype
.static boolean
isFloatingPoint
(TypeMirror type) Return true if the argument TypeMirror is a (possibly boxed) floating point type.static boolean
Returns true iff the argument is a primitive floating point type.static boolean
isFunctionalInterface
(TypeMirror type, ProcessingEnvironment env) Returns whether or nottype
is a functional interface type (as defined in JLS 9.8).static boolean
Returns true iff this is an immutable type in the JDK.static boolean
Returns true iff the argument is an integral primitive type.static boolean
Return true if the argument TypeMirror is a (possibly boxed) integral type.static boolean
isNumeric
(TypeMirror type) Returns true iff the argument is a primitive numeric type.static boolean
isNumericBoxed
(TypeMirror type) Returns true iff the argument is a boxed numeric type.static boolean
isObject
(TypeMirror type) Returns true iff the type represents a java.lang.Object declared type.static boolean
isPrimitive
(TypeMirror type) Returns true iff the argument is a primitive type.static boolean
isPrimitiveOrBoxed
(TypeMirror type) Returns true iff the argument is a primitive type or a boxed primitive type.static boolean
isString
(TypeMirror type) Returns true iff the type represents a java.lang.String declared type.static boolean
isThrowable
(TypeMirror type) Returns true iff type represents a Throwable type (e.g.static boolean
isUnboundedOrExtendsBounded
(WildcardType wildcardType) Returns true if this type is extends bounded or unbounded.static boolean
isUnboundedOrSuperBounded
(WildcardType wildcardType) Returns true if this type is super bounded or unbounded.static TypeMirror
leastUpperBound
(TypeMirror tm1, TypeMirror tm2, ProcessingEnvironment processingEnv) Returns the least upper bound of twoTypeMirror
s, ignoring any annotations on the types.static @Nullable TypeMirror
mostSpecific
(List<TypeMirror> typeMirrors, ProcessingEnvironment processingEnv) Returns the most specific type from the list, or null if none exists.static List
<TypeVariable> order
(Collection<TypeVariable> collection, Types types) Returns the list of type variables such that a type variable in the list only references type variables at a lower index than itself.static String
simpleTypeName
(TypeMirror type) Returns the simple type name, without annotations but including array brackets.static TypeMirror
substitute
(TypeMirror type, List<? extends TypeMirror> typeVariables, List<? extends TypeMirror> typeArgs, ProcessingEnvironment env) Returns a new type mirror with the same type astype
where all the type variables intypeVariables
have been substituted with the type arguments intypeArgs
.static TypeMirror
substituteMethodReturnType
(Element methodElement, TypeMirror substitutedReceiverType, ProcessingEnvironment env) Returns the return type of a method, given the receiver of the method call.static TypeMirror
typeFromClass
(Class<?> clazz, Types types, Elements elements) Returns theTypeMirror
for a givenClass
.static TypeMirror
upperBound
(TypeMirror type) If the argument is a bounded TypeVariable or WildcardType, return its non-variable, non-wildcard upper bound.static @Nullable TypeParameterElement
wildcardToTypeParam
(com.sun.tools.javac.code.Type.WildcardType wildcard) Get the type parameter for this wildcard from the underlying type's bound field.static @Nullable TypeParameterElement
wildcardToTypeParam
(WildcardType wildcard) Get the type parameter for this wildcard from the underlying type's bound field.static com.sun.tools.javac.code.Type
Version of com.sun.tools.javac.code.Types.wildLowerBound(Type) that works with both jdk8 (called upperBound there) and jdk8u.static com.sun.tools.javac.code.Type
Version of com.sun.tools.javac.code.Types.wildUpperBound(Type) that works with both jdk8 (called upperBound there) and jdk8u.
-
Method Details
-
typeFromClass
Returns theTypeMirror
for a givenClass
.- Parameters:
clazz
- a classtypes
- the type utilitieselements
- the element utilities- Returns:
- the TypeMirror for
clazz
-
createArrayType
Returns anArrayType
with elements of typecomponentType
.- Parameters:
componentType
- the component type of the created array typetypes
- the type utilities- Returns:
- an
ArrayType
whose elements have typecomponentType
-
getClassFromType
Returns theClass
for a givenTypeMirror
. ReturnsObject.class
if it cannot determine anything more specific.- Parameters:
typeMirror
- a TypeMirror- Returns:
- the class for
typeMirror
-
getQualifiedName
Gets the fully qualified name for a provided type. It returns an empty name if type is an anonymous type.- Parameters:
type
- the declared type- Returns:
- the name corresponding to that type
-
simpleTypeName
Returns the simple type name, without annotations but including array brackets.- Parameters:
type
- a type- Returns:
- the simple type name
-
binaryName
Returns the binary name of a type.- Parameters:
type
- a type- Returns:
- its binary name
-
getTypeElement
Returns the type element fortype
iftype
is a class, interface, annotation type, or enum. Otherwise, returns null.- Parameters:
type
- whose element is returned- Returns:
- the type element for
type
iftype
is a class, interface, annotation type, or enum; otherwise, returnsnull
-
getInnermostComponentType
Given an array type, returns the type with all array levels stripped off.- Parameters:
at
- an array type- Returns:
- the type with all array levels stripped off
-
areSameDeclaredTypes
public static boolean areSameDeclaredTypes(com.sun.tools.javac.code.Type.ClassType t1, com.sun.tools.javac.code.Type.ClassType t2) Returns true iff the arguments are both the same declared types.This is needed because class
Type.ClassType
does not override equals.- Parameters:
t1
- the first type to testt2
- the second type to test- Returns:
- whether the arguments are the same declared types
-
areSamePrimitiveTypes
Returns true iff the arguments are both the same primitive type.- Parameters:
left
- a typeright
- a type- Returns:
- whether the arguments are the same primitive type
-
isObject
Returns true iff the type represents a java.lang.Object declared type.- Parameters:
type
- the type to check- Returns:
- true iff type represents java.lang.Object
-
isClass
Return true iff the type represents a java.lang.Class declared type.- Parameters:
type
- the type to check- Returns:
- true iff type represents java.lang.Class
-
isString
Returns true iff the type represents a java.lang.String declared type.- Parameters:
type
- the type to check- Returns:
- true iff type represents java.lang.String
-
isBooleanType
Returns true if the type is eitherboolean
(primitive type) orjava.lang.Boolean
.- Parameters:
type
- the type to check- Returns:
- true iff type represents a boolean type
-
isCharType
Returns true if the type ischar
orCharacter
.- Parameters:
type
- a type- Returns:
- true if the type is
char
orCharacter
-
isDeclaredOfName
Returns true iff the type represents a declared type of the given qualified name.- Parameters:
type
- the typequalifiedName
- the name to checktype
against- Returns:
- true iff type represents a declared type of the qualified name
-
isDeclaredOfName
Returns true iff the type represents a declared type whose fully-qualified name is any of the given names.- Parameters:
type
- the typequalifiedNames
- fully-qualified type names to check for- Returns:
- true iff type represents a declared type whose fully-qualified name is one of the given names
-
isBoxedPrimitive
Returns true iff thetype
represents a boxed primitive type.- Parameters:
type
- the type to check- Returns:
- true iff type represents a boxed primitive type
-
isImmutableTypeInJdk
Returns true iff this is an immutable type in the JDK.This does not use immutability annotations and always returns false for user-defined classes.
- Parameters:
type
- the type to check- Returns:
- true iff this is an immutable type in the JDK
-
isThrowable
Returns true iff type represents a Throwable type (e.g. Exception, Error).- Parameters:
type
- the type to check- Returns:
- true iff type represents a Throwable type (e.g. Exception, Error)
-
isAnonymous
Returns true iff the argument is an anonymous type.- Parameters:
type
- the type to check- Returns:
- whether the argument is an anonymous type
-
isPrimitive
Returns true iff the argument is a primitive type.- Parameters:
type
- the type to check- Returns:
- whether the argument is a primitive type
-
isPrimitiveOrBoxed
Returns true iff the argument is a primitive type or a boxed primitive type.- Parameters:
type
- a type- Returns:
- true if the argument is a primitive type or a boxed primitive type
-
isNumeric
Returns true iff the argument is a primitive numeric type.- Parameters:
type
- a type- Returns:
- true if the argument is a primitive numeric type
-
isNumericBoxed
Returns true iff the argument is a boxed numeric type.- Parameters:
type
- a type- Returns:
- true if the argument is a boxed numeric type
-
isIntegralPrimitive
Returns true iff the argument is an integral primitive type.- Parameters:
type
- a type- Returns:
- whether the argument is an integral primitive type
-
isIntegralPrimitiveOrBoxed
Return true if the argument TypeMirror is a (possibly boxed) integral type.- Parameters:
type
- the type to inspect- Returns:
- true if type is an integral type
-
isBoxOf
Returns true if declaredType is a Class that is used to box primitive type (e.g. declaredType=java.lang.Double and primitiveType=22.5d )- Parameters:
declaredType
- a type that might be a boxed typeprimitiveType
- a type that might be a primitive type- Returns:
- true if
declaredType
is a box ofprimitiveType
-
isBoxedFloating
Returns true iff the argument is a boxed floating point type.- Parameters:
type
- type to test- Returns:
- whether the argument is a boxed floating point type
-
isFloatingPrimitive
Returns true iff the argument is a primitive floating point type.- Parameters:
type
- type mirror- Returns:
- whether the argument is a primitive floating point type
-
isFloatingPoint
Return true if the argument TypeMirror is a (possibly boxed) floating point type.- Parameters:
type
- the type to inspect- Returns:
- true if type is a floating point type
-
isClassType
Returns whether a TypeMirror represents a class type.- Parameters:
type
- a type that might be a class type- Returns:
- true if is a class type
-
isFunctionalInterface
Returns whether or nottype
is a functional interface type (as defined in JLS 9.8).- Parameters:
type
- possible functional interface typeenv
- the processing environment- Returns:
- whether or not
type
is a functional interface type (as defined in JLS 9.8)
-
isCompoundType
Returns true if the given type is a compound type.- Parameters:
type
- a type- Returns:
- true if the given type is a compound type
-
hasEnclosingType
Returns true iftype
has an enclosing type.- Parameters:
type
- type to checker- Returns:
- true if
type
has an enclosing type
-
upperBound
If the argument is a bounded TypeVariable or WildcardType, return its non-variable, non-wildcard upper bound. Otherwise, return the type itself.- Parameters:
type
- a type- Returns:
- the non-variable, non-wildcard upper bound of a type, if it has one, or itself if it has no bounds
-
wildcardToTypeParam
Get the type parameter for this wildcard from the underlying type's bound field. This field is sometimes null, in that case this method will return null.- Parameters:
wildcard
- wildcard type- Returns:
- the TypeParameterElement the wildcard is an argument to,
null
otherwise
-
wildcardToTypeParam
public static @Nullable TypeParameterElement wildcardToTypeParam(com.sun.tools.javac.code.Type.WildcardType wildcard) Get the type parameter for this wildcard from the underlying type's bound field. This field is sometimes null, in that case this method will return null.- Parameters:
wildcard
- wildcard type- Returns:
- the TypeParameterElement the wildcard is an argument to,
null
otherwise
-
wildUpperBound
public static com.sun.tools.javac.code.Type wildUpperBound(TypeMirror tm, ProcessingEnvironment env) Version of com.sun.tools.javac.code.Types.wildUpperBound(Type) that works with both jdk8 (called upperBound there) and jdk8u. -
getObjectTypeMirror
Returns theDeclaredType
forjava.lang.Object
.- Parameters:
env
-ProcessingEnvironment
- Returns:
- the
DeclaredType
forjava.lang.Object
-
wildLowerBound
public static com.sun.tools.javac.code.Type wildLowerBound(TypeMirror tm, ProcessingEnvironment env) Version of com.sun.tools.javac.code.Types.wildLowerBound(Type) that works with both jdk8 (called upperBound there) and jdk8u. -
findConcreteUpperBound
Given a bounded type (wildcard or typevar) get the concrete type of its upper bound. If the bounded type extends other bounded types, this method will iterate through their bounds until a class, interface, or intersection is found.- Returns:
- a type that is not a wildcard or typevar, or
null
if this type is an unbounded wildcard
-
hasNoExplicitBound
Returns true iftype
is an unbounded wildcard.- Parameters:
type
- the type to check- Returns:
- true if the given type is an unbounded wildcard
-
hasExplicitSuperBound
Returns true iftype
is a wildcard with an explicit super bound.- Parameters:
type
- thetype
to test- Returns:
- true if
type
is explicitly super bounded
-
hasExplicitExtendsBound
Returns true iftype
is a wildcard with an explicit extends bound.- Parameters:
type
- the type to test- Returns:
- true if
type
is a wildcard with an explicit extends bound
-
isUnboundedOrSuperBounded
Returns true if this type is super bounded or unbounded.- Parameters:
wildcardType
- the wildcard type to test- Returns:
- true if this type is super bounded or unbounded
-
isUnboundedOrExtendsBounded
Returns true if this type is extends bounded or unbounded.- Parameters:
wildcardType
- the wildcard type to test- Returns:
- true if this type is extends bounded or unbounded
-
isErasedSubtype
Returns true if the erased type ofsubtype
is a subtype of the erased type ofsupertype
.- Parameters:
subtype
- possible subtypesupertype
- possible supertypetypes
- a Types object- Returns:
- true if the erased type of subtype is a subtype of the erased type of supertype
-
isCapturedTypeVariable
Returns true iftype
is a type variable created during capture conversion.- Parameters:
type
- a type mirror- Returns:
- true if
type
is a type variable created during capture conversion
-
getCapturedWildcard
IftypeVar
is a captured type variable, then returns its underlying wildcard; otherwise returnsnull
.- Parameters:
typeVar
- a type variable that might be a captured type variable- Returns:
typeVar
is a captured type variable, then returns its underlying wildcard; otherwise returnsnull
-
leastUpperBound
public static TypeMirror leastUpperBound(TypeMirror tm1, TypeMirror tm2, ProcessingEnvironment processingEnv) Returns the least upper bound of twoTypeMirror
s, ignoring any annotations on the types.Wrapper around Types.lub to add special handling for null types, primitives, and wildcards.
- Parameters:
tm1
- aTypeMirror
tm2
- aTypeMirror
processingEnv
- theProcessingEnvironment
to use- Returns:
- the least upper bound of
tm1
andtm2
-
greatestLowerBound
public static TypeMirror greatestLowerBound(TypeMirror tm1, TypeMirror tm2, ProcessingEnvironment processingEnv) Returns the greatest lower bound of twoTypeMirror
s, ignoring any annotations on the types.Wrapper around Types.glb to add special handling for null types, primitives, and wildcards.
- Parameters:
tm1
- aTypeMirror
tm2
- aTypeMirror
processingEnv
- theProcessingEnvironment
to use- Returns:
- the greatest lower bound of
tm1
andtm2
-
mostSpecific
public static @Nullable TypeMirror mostSpecific(List<TypeMirror> typeMirrors, ProcessingEnvironment processingEnv) Returns the most specific type from the list, or null if none exists.- Parameters:
typeMirrors
- a list of typesprocessingEnv
- theProcessingEnvironment
to use- Returns:
- the most specific of the types, or null if none exists
-
substituteMethodReturnType
public static TypeMirror substituteMethodReturnType(Element methodElement, TypeMirror substitutedReceiverType, ProcessingEnvironment env) Returns the return type of a method, given the receiver of the method call.- Parameters:
methodElement
- a methodsubstitutedReceiverType
- the receiver type, after substitutionenv
- the environment- Returns:
- the return type of the method
-
asSuper
public static @Nullable TypeMirror asSuper(TypeMirror type, TypeMirror superType, ProcessingEnvironment env) Returnstype
assuperType
ifsuperType
is a super type oftype
; otherwise, null.- Returns:
type
assuperType
ifsuperType
is a super type oftype
; otherwise, null
-
getSuperclass
Returns the superclass of the given class. Returns null if there is not one.- Parameters:
type
- a typetypes
- type utilities- Returns:
- the superclass of the given class, or null
-
getSuperClassOrInterface
Returns the superclass the given type. If there is no superclass the first interface returned byTypes.directSupertypes(TypeMirror)
is returned. If the type has neither a superclass nor a superinterface, then null is returned.- Parameters:
type
- a typetypes
- type utilities- Returns:
- the superclass or super interface of the given type, or null
-
getPrimitiveConversionKind
public static TypeKindUtils.PrimitiveConversionKind getPrimitiveConversionKind(PrimitiveType from, PrimitiveType to) Returns the type of primitive conversion fromfrom
toto
.- Parameters:
from
- a primitive typeto
- a primitive type- Returns:
- the type of primitive conversion from
from
toto
-
substitute
public static TypeMirror substitute(TypeMirror type, List<? extends TypeMirror> typeVariables, List<? extends TypeMirror> typeArgs, ProcessingEnvironment env) Returns a new type mirror with the same type astype
where all the type variables intypeVariables
have been substituted with the type arguments intypeArgs
.This is a wrapper around
Types.subst(Type, com.sun.tools.javac.util.List, com.sun.tools.javac.util.List)
.- Parameters:
type
- type to do substitution intypeVariables
- type variables that should be replaced with the type mirror at the same index oftypeArgs
typeArgs
- type mirrors that should replace the type variable at the same index oftypeVariables
env
- processing environment- Returns:
- a new type mirror with the same type as
type
where all the type variables intypeVariables
have been substituted with the type arguments intypeArgs
-
getArrayDepth
Returns the depth of an array type.- Parameters:
arrayType
- an array type- Returns:
- the depth of
arrayType
-
freshTypeVariable
IftypeMirror
is a wildcard, returns a fresh type variable that will be used as a captured type variable for it. IftypeMirror
is not a wildcard, returnstypeMirror
.- Parameters:
typeMirror
- a typeenv
- processing environment- Returns:
- a fresh type variable if
typeMirror
is a wildcard, otherwisetypeMirror
-
order
Returns the list of type variables such that a type variable in the list only references type variables at a lower index than itself.- Parameters:
collection
- a collection of type variablestypes
- type utilities- Returns:
- the type variables ordered so that each type variable only references earlier type variables
-
canBeNarrowingPrimitiveConversion
Returns true if the type is byte, short, char, Byte, Short, or Character. All other narrowings require a cast. See JLS 5.1.3.- Parameters:
type
- a typetypes
- the type utilities- Returns:
- true if assignment to the type may be a narrowing
-
areSame
Returns true if the two type variables are the same type variable. Meaning they have the same name and the same enclosing element. UnlikeTypes.isSameType(TypeMirror, TypeMirror)
, they do not have to be the same object.This method is needed when a type has gone through type variable substitution, but only some of the type variables were substituted. Also, a new
TypeVariable
object is created as the type of a tree created byTreeBuilder
.- Parameters:
typeVariable1
- a type variabletypeVariable2
- a type variable- Returns:
- if the two type variables are the same type variable
-