public final class TypesUtils extends Object
TypeMirrors. It complements Types, providing
methods that Types does not.| Modifier and Type | Method and Description |
|---|---|
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 TypeMirror |
asSuper(TypeMirror type,
TypeMirror superType,
ProcessingEnvironment env)
Returns
type as superType if superType is a super type of type;
otherwise, null. |
static @BinaryName String |
binaryName(TypeMirror type)
Returns the binary name.
|
static ArrayType |
createArrayType(TypeMirror componentType,
Types types)
Returns an
ArrayType with elements of type componentType. |
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)
If
typeMirror 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)
If
typeVar is a captured type variable, then returns its underlying wildcard; otherwise
returns null. |
static Class<?> |
getClassFromType(TypeMirror typeMirror)
Returns the
Class for a given TypeMirror. |
static TypeMirror |
getInnermostComponentType(ArrayType at)
Given an array type, returns the type with all array levels stripped off.
|
static DeclaredType |
getObjectTypeMirror(ProcessingEnvironment env)
Returns the
DeclaredType for java.lang.Object. |
static TypeKindUtils.PrimitiveConversionKind |
getPrimitiveConversionKind(PrimitiveType from,
PrimitiveType to)
Returns the type of primitive conversion from
from to to. |
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 TypeElement |
getTypeElement(TypeMirror type)
Returns the type element for
type if type is a class, interface, annotation
type, or enum. |
static TypeMirror |
greatestLowerBound(TypeMirror tm1,
TypeMirror tm2,
ProcessingEnvironment processingEnv)
Returns the greatest lower bound of two
TypeMirrors, ignoring any annotations on the
types. |
static boolean |
hasEnclosingType(TypeMirror type)
Returns true if
type has an enclosing type. |
static boolean |
isAnonymous(TypeMirror type)
Returns true iff the argument is an anonymous type.
|
static boolean |
isBooleanType(TypeMirror type)
Checks if the type represents a boolean type, that is either boolean (primitive type) or
java.lang.Boolean.
|
static boolean |
isBoxedFloating(TypeMirror type)
Returns true iff the argument is a boxed floating point type.
|
static boolean |
isBoxedPrimitive(TypeMirror 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 |
isCaptured(TypeMirror type)
Deprecated.
use
isCapturedTypeVariable(TypeMirror) instead |
static boolean |
isCapturedTypeVariable(TypeMirror type)
Returns true if
type is a type variable created during capture conversion. |
static boolean |
isClass(TypeMirror type)
Checks if the type represents the java.lang.Class declared type.
|
static boolean |
isClassType(TypeMirror type)
Returns whether a TypeMirror represents a class type.
|
static boolean |
isDeclaredOfName(TypeMirror type,
CharSequence qualifiedName)
Check if the type represents a declared type of the given qualified name.
|
static boolean |
isErasedSubtype(TypeMirror subtype,
TypeMirror supertype,
Types types)
Returns true if the erased type of subtype is a subtype of the erased type of supertype.
|
static boolean |
isFloatingPoint(TypeMirror type)
Return true if the argument TypeMirror is a (possibly boxed) floating point type.
|
static boolean |
isFloatingPrimitive(TypeMirror type)
Returns true iff the argument is a primitive floating point type.
|
static boolean |
isFunctionalInterface(TypeMirror type,
ProcessingEnvironment env)
Returns whether or not
type is a functional interface type (as defined in JLS 9.8). |
static boolean |
isImmutableTypeInJdk(TypeMirror type)
Return true if this is an immutable type in the JDK.
|
static boolean |
isIntegralPrimitive(TypeMirror type)
Returns true iff the argument is an integral primitive type.
|
static boolean |
isIntegralPrimitiveOrBoxed(TypeMirror type)
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)
Checks if 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)
Checks if the type represents a java.lang.String declared type.
|
static boolean |
isThrowable(TypeMirror type)
Returns true if type represents a Throwable type (e.g.
|
static TypeMirror |
leastUpperBound(TypeMirror tm1,
TypeMirror tm2,
ProcessingEnvironment processingEnv)
Returns the least upper bound of two
TypeMirrors, 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.
|
static TypeMirror |
substitute(TypeMirror type,
List<? extends TypeMirror> typeVariables,
List<? extends TypeMirror> typeArgs,
ProcessingEnvironment env)
Returns a new type mirror with the same type as
type where all the type variables in
typeVariables have been substituted with the type arguments in typeArgs. |
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 the
TypeMirror for a given Class. |
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 This field is
sometimes null, in that case this method will return null.
|
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.
|
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.
|
public static TypeMirror typeFromClass(Class<?> clazz, Types types, Elements elements)
TypeMirror for a given Class.clazz - a classtypes - the type utilitieselements - the element utiliitesclazzpublic static ArrayType createArrayType(TypeMirror componentType, Types types)
ArrayType with elements of type componentType.componentType - the component type of the created array typetypes - the type utilitiesArrayType whose elements have type componentTypepublic static Class<?> getClassFromType(TypeMirror typeMirror)
Class for a given TypeMirror. Returns Object.class if it
cannot determine anything more specific.typeMirror - a TypeMirrortypeMirrorpublic static @CanonicalNameOrEmpty String getQualifiedName(DeclaredType type)
type - the declared typepublic static String simpleTypeName(TypeMirror type)
type - a typepublic static @BinaryName String binaryName(TypeMirror type)
type - a typepublic static @Nullable TypeElement getTypeElement(TypeMirror type)
type if type is a class, interface, annotation
type, or enum. Otherwise, returns null.type - whose element is returnedtype if type is a class, interface, annotation
type, or enum; otherwise, returns nullpublic static TypeMirror getInnermostComponentType(ArrayType at)
at - an array typepublic static boolean areSameDeclaredTypes(com.sun.tools.javac.code.Type.ClassType t1,
com.sun.tools.javac.code.Type.ClassType t2)
This is needed because class Type.ClassType does not override equals.
t1 - the first type to testt2 - the second type to testpublic static boolean areSamePrimitiveTypes(TypeMirror left, TypeMirror right)
left - a typeright - a typepublic static boolean isObject(TypeMirror type)
type - the typepublic static boolean isClass(TypeMirror type)
type - the typepublic static boolean isString(TypeMirror type)
type - the typepublic static boolean isBooleanType(TypeMirror type)
type - the type to testpublic static boolean isDeclaredOfName(TypeMirror type, CharSequence qualifiedName)
type - the typepublic static boolean isBoxedPrimitive(TypeMirror type)
public static boolean isImmutableTypeInJdk(TypeMirror type)
This does not use immutability annotations and always returns false for user-defined classes.
public static boolean isThrowable(TypeMirror type)
public static boolean isAnonymous(TypeMirror type)
public static boolean isPrimitive(TypeMirror type)
public static boolean isPrimitiveOrBoxed(TypeMirror type)
type - a typepublic static boolean isNumeric(TypeMirror type)
type - a typepublic static boolean isNumericBoxed(TypeMirror type)
type - a typepublic static boolean isIntegralPrimitive(TypeMirror type)
type - a typepublic static boolean isIntegralPrimitiveOrBoxed(TypeMirror type)
type - the type to inspectpublic static boolean isBoxOf(TypeMirror declaredType, TypeMirror primitiveType)
declaredType - a type that might be a boxed typeprimitiveType - a type that might be a primitive typedeclaredType is a box of primitiveTypepublic static boolean isBoxedFloating(TypeMirror type)
type - type to testpublic static boolean isFloatingPrimitive(TypeMirror type)
type - type mirrorpublic static boolean isFloatingPoint(TypeMirror type)
type - the type to inspectpublic static boolean isClassType(TypeMirror type)
type - a type that might be a class typepublic static boolean hasEnclosingType(TypeMirror type)
type has an enclosing type.type - type to checkertype has an enclosing typepublic static boolean isFunctionalInterface(TypeMirror type, ProcessingEnvironment env)
type is a functional interface type (as defined in JLS 9.8).type - possible functional interface typeenv - ProcessingEnvironmenttype is a functional interface type (as defined in JLS 9.8)public static TypeMirror upperBound(TypeMirror type)
type - a typepublic static @Nullable TypeParameterElement wildcardToTypeParam(com.sun.tools.javac.code.Type.WildcardType wildcard)
null otherwisepublic static com.sun.tools.javac.code.Type wildUpperBound(TypeMirror tm, ProcessingEnvironment env)
public static DeclaredType getObjectTypeMirror(ProcessingEnvironment env)
DeclaredType for java.lang.Object.env - ProcessingEnvironmentDeclaredType for java.lang.Objectpublic static com.sun.tools.javac.code.Type wildLowerBound(TypeMirror tm, ProcessingEnvironment env)
public static @Nullable TypeMirror findConcreteUpperBound(TypeMirror boundedType)
null if this type is an unbounded
wildcardpublic static boolean isErasedSubtype(TypeMirror subtype, TypeMirror supertype, Types types)
subtype - possible subtypesupertype - possible supertypetypes - a Types object@Deprecated public static boolean isCaptured(TypeMirror type)
isCapturedTypeVariable(TypeMirror) insteadtype is a type variable created during capture conversion.type - a type mirrortype is a type variable created during capture conversionpublic static boolean isCapturedTypeVariable(TypeMirror type)
type is a type variable created during capture conversion.type - a type mirrortype is a type variable created during capture conversionpublic static @Nullable WildcardType getCapturedWildcard(TypeVariable typeVar)
typeVar is a captured type variable, then returns its underlying wildcard; otherwise
returns null.typeVar - a type variable that might be a captured type variabletypeVar is a captured type variable, then returns its underlying wildcard;
otherwise returns nullpublic static TypeMirror leastUpperBound(TypeMirror tm1, TypeMirror tm2, ProcessingEnvironment processingEnv)
TypeMirrors, ignoring any annotations on the
types.
Wrapper around Types.lub to add special handling for null types, primitives, and wildcards.
tm1 - a TypeMirrortm2 - a TypeMirrorprocessingEnv - the ProcessingEnvironment to usetm1 and tm2public static TypeMirror greatestLowerBound(TypeMirror tm1, TypeMirror tm2, ProcessingEnvironment processingEnv)
TypeMirrors, ignoring any annotations on the
types.
Wrapper around Types.glb to add special handling for null types, primitives, and wildcards.
tm1 - a TypeMirrortm2 - a TypeMirrorprocessingEnv - the ProcessingEnvironment to usetm1 and tm2public static @Nullable TypeMirror mostSpecific(List<TypeMirror> typeMirrors, ProcessingEnvironment processingEnv)
typeMirrors - a list of typesprocessingEnv - the ProcessingEnvironment to usepublic static TypeMirror substituteMethodReturnType(Element methodElement, TypeMirror substitutedReceiverType, ProcessingEnvironment env)
methodElement - a methodsubstitutedReceiverType - the receiver type, after substitutionenv - the environmentpublic static TypeMirror asSuper(TypeMirror type, TypeMirror superType, ProcessingEnvironment env)
type as superType if superType is a super type of type;
otherwise, null.type as superType if superType is a super type of type;
otherwise, nullpublic static @Nullable TypeMirror getSuperclass(TypeMirror type, Types types)
type - a typetypes - type utilitiespublic static TypeKindUtils.PrimitiveConversionKind getPrimitiveConversionKind(PrimitiveType from, PrimitiveType to)
from to to.from - a primitive typeto - a primitive typefrom to topublic static TypeMirror substitute(TypeMirror type, List<? extends TypeMirror> typeVariables, List<? extends TypeMirror> typeArgs, ProcessingEnvironment env)
type where all the type variables in
typeVariables have been substituted with the type arguments in typeArgs.
This is a wrapper around Types.subst(Type,
com.sun.tools.javac.util.List, com.sun.tools.javac.util.List).
type - type to do substitution intypeVariables - type variables that should be replaced with the type mirror at the same
index of typeArgstypeArgs - type mirrors that should replace the type variable at the same index of typeVariablesenv - processing environmenttype where all the type variables in
typeVariables have been substituted with the type arguments in typeArgspublic static int getArrayDepth(TypeMirror arrayType)
arrayType - an array typearrayTypepublic static TypeMirror freshTypeVariable(TypeMirror typeMirror, ProcessingEnvironment env)
typeMirror is a wildcard, returns a fresh type variable that will be used as a
captured type variable for it. If typeMirror is not a wildcard, returns typeMirror.typeMirror - a typeenv - processing environmenttypeMirror is a wildcard, otherwise typeMirrorpublic static List<TypeVariable> order(Collection<TypeVariable> collection, Types types)
collection - a collection of type variablestypes - type utilities