Class TypesUtils

java.lang.Object
org.checkerframework.javacutil.TypesUtils

public final class TypesUtils extends Object
A utility class that helps with TypeMirrors. It complements Types, providing methods that Types does not.
  • Method Details

    • typeFromClass

      public static TypeMirror typeFromClass(Class<?> clazz, Types types, Elements elements)
      Returns the TypeMirror for a given Class.
      Parameters:
      clazz - a class
      types - the type utilities
      elements - the element utilities
      Returns:
      the TypeMirror for clazz
    • createArrayType

      public static ArrayType createArrayType(TypeMirror componentType, Types types)
      Returns an ArrayType with elements of type componentType.
      Parameters:
      componentType - the component type of the created array type
      types - the type utilities
      Returns:
      an ArrayType whose elements have type componentType
    • getClassFromType

      public static Class<?> getClassFromType(TypeMirror typeMirror)
      Returns the Class for a given TypeMirror. Returns Object.class if it cannot determine anything more specific.
      Parameters:
      typeMirror - a TypeMirror
      Returns:
      the class for typeMirror
    • getQualifiedName

      public static @CanonicalNameOrEmpty String getQualifiedName(DeclaredType type)
      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

      public static String simpleTypeName(TypeMirror type)
      Returns the simple type name, without annotations but including array brackets.
      Parameters:
      type - a type
      Returns:
      the simple type name
    • binaryName

      public static @BinaryName String binaryName(TypeMirror type)
      Returns the binary name of a type.
      Parameters:
      type - a type
      Returns:
      its binary name
    • getTypeElement

      public static @Nullable TypeElement getTypeElement(TypeMirror type)
      Returns the type element for type if type is a class, interface, annotation type, or enum. Otherwise, returns null.
      Parameters:
      type - whose element is returned
      Returns:
      the type element for type if type is a class, interface, annotation type, or enum; otherwise, returns null
    • getInnermostComponentType

      public static TypeMirror getInnermostComponentType(ArrayType at)
      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 test
      t2 - the second type to test
      Returns:
      whether the arguments are the same declared types
    • areSamePrimitiveTypes

      public static boolean areSamePrimitiveTypes(TypeMirror left, TypeMirror right)
      Returns true iff the arguments are both the same primitive type.
      Parameters:
      left - a type
      right - a type
      Returns:
      whether the arguments are the same primitive type
    • isObject

      public static boolean isObject(TypeMirror type)
      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

      public static boolean isClass(TypeMirror type)
      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

      public static boolean isString(TypeMirror type)
      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

      public static boolean isBooleanType(TypeMirror type)
      Returns true if the type is either boolean (primitive type) or java.lang.Boolean.
      Parameters:
      type - the type to check
      Returns:
      true iff type represents a boolean type
    • isCharType

      public static boolean isCharType(TypeMirror type)
      Returns true if the type is char or Character.
      Parameters:
      type - a type
      Returns:
      true if the type is char or Character
    • isDeclaredOfName

      public static boolean isDeclaredOfName(TypeMirror type, CharSequence qualifiedName)
      Returns true iff the type represents a declared type of the given qualified name.
      Parameters:
      type - the type
      qualifiedName - the name to check type against
      Returns:
      true iff type represents a declared type of the qualified name
    • isDeclaredOfName

      public 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.
      Parameters:
      type - the type
      qualifiedNames - 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

      public static boolean isBoxedPrimitive(TypeMirror type)
      Returns true iff the type represents a boxed primitive type.
      Parameters:
      type - the type to check
      Returns:
      true iff type represents a boxed primitive type
    • isImmutableTypeInJdk

      public static boolean isImmutableTypeInJdk(TypeMirror type)
      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

      public static boolean isThrowable(TypeMirror type)
      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

      public static boolean isAnonymous(TypeMirror type)
      Returns true iff the argument is an anonymous type.
      Parameters:
      type - the type to check
      Returns:
      whether the argument is an anonymous type
    • isPrimitive

      public static boolean isPrimitive(TypeMirror type)
      Returns true iff the argument is a primitive type.
      Parameters:
      type - the type to check
      Returns:
      whether the argument is a primitive type
    • isPrimitiveOrBoxed

      public static boolean isPrimitiveOrBoxed(TypeMirror type)
      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

      public static boolean isNumeric(TypeMirror type)
      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

      public static boolean isNumericBoxed(TypeMirror type)
      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

      public static boolean isIntegralPrimitive(TypeMirror type)
      Returns true iff the argument is an integral primitive type.
      Parameters:
      type - a type
      Returns:
      whether the argument is an integral primitive type
    • isIntegralPrimitiveOrBoxed

      public static boolean isIntegralPrimitiveOrBoxed(TypeMirror type)
      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

      public static boolean isBoxOf(TypeMirror declaredType, TypeMirror primitiveType)
      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 type
      primitiveType - a type that might be a primitive type
      Returns:
      true if declaredType is a box of primitiveType
    • isBoxedFloating

      public static boolean isBoxedFloating(TypeMirror type)
      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

      public static boolean isFloatingPrimitive(TypeMirror type)
      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

      public static boolean isFloatingPoint(TypeMirror type)
      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

      public static boolean isClassType(TypeMirror type)
      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

      public static boolean isFunctionalInterface(TypeMirror type, ProcessingEnvironment env)
      Returns whether or not type is a functional interface type (as defined in JLS 9.8).
      Parameters:
      type - possible functional interface type
      env - the processing environment
      Returns:
      whether or not type is a functional interface type (as defined in JLS 9.8)
    • isCompoundType

      public static boolean isCompoundType(TypeMirror type)
      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

      public static boolean hasEnclosingType(TypeMirror type)
      Returns true if type has an enclosing type.
      Parameters:
      type - type to checker
      Returns:
      true if type has an enclosing type
    • upperBound

      public static TypeMirror upperBound(TypeMirror type)
      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

      public static @Nullable TypeParameterElement wildcardToTypeParam(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
    • 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

      public static DeclaredType getObjectTypeMirror(ProcessingEnvironment env)
      Returns the DeclaredType for java.lang.Object.
      Parameters:
      env - ProcessingEnvironment
      Returns:
      the DeclaredType for java.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

      public static @Nullable TypeMirror findConcreteUpperBound(TypeMirror boundedType)
      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

      public static boolean hasNoExplicitBound(TypeMirror type)
      Returns true if type is an unbounded wildcard.
      Parameters:
      type - the type to check
      Returns:
      true if the given type is an unbounded wildcard
    • hasExplicitSuperBound

      public static boolean hasExplicitSuperBound(TypeMirror type)
      Returns true if type is a wildcard with an explicit super bound.
      Parameters:
      type - the type to test
      Returns:
      true if type is explicitly super bounded
    • hasExplicitExtendsBound

      public static boolean hasExplicitExtendsBound(TypeMirror type)
      Returns true if type 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

      public static boolean isUnboundedOrSuperBounded(WildcardType wildcardType)
      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

      public static boolean isUnboundedOrExtendsBounded(WildcardType wildcardType)
      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

      public 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.
      Parameters:
      subtype - possible subtype
      supertype - possible supertype
      types - a Types object
      Returns:
      true if the erased type of subtype is a subtype of the erased type of supertype
    • isCapturedTypeVariable

      public static boolean isCapturedTypeVariable(TypeMirror type)
      Returns true if type 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

      public static @Nullable WildcardType getCapturedWildcard(TypeVariable typeVar)
      If typeVar is a captured type variable, then returns its underlying wildcard; otherwise returns null.
      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 returns null
    • leastUpperBound

      public static TypeMirror leastUpperBound(TypeMirror tm1, TypeMirror tm2, ProcessingEnvironment processingEnv)
      Returns the least upper bound of two TypeMirrors, ignoring any annotations on the types.

      Wrapper around Types.lub to add special handling for null types, primitives, and wildcards.

      Parameters:
      tm1 - a TypeMirror
      tm2 - a TypeMirror
      processingEnv - the ProcessingEnvironment to use
      Returns:
      the least upper bound of tm1 and tm2
    • greatestLowerBound

      public static TypeMirror greatestLowerBound(TypeMirror tm1, TypeMirror tm2, ProcessingEnvironment processingEnv)
      Returns the greatest lower bound of two TypeMirrors, ignoring any annotations on the types.

      Wrapper around Types.glb to add special handling for null types, primitives, and wildcards.

      Parameters:
      tm1 - a TypeMirror
      tm2 - a TypeMirror
      processingEnv - the ProcessingEnvironment to use
      Returns:
      the greatest lower bound of tm1 and tm2
    • 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 types
      processingEnv - the ProcessingEnvironment 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 method
      substitutedReceiverType - the receiver type, after substitution
      env - the environment
      Returns:
      the return type of the method
    • asSuper

      public static @Nullable TypeMirror asSuper(TypeMirror type, TypeMirror superType, ProcessingEnvironment env)
      Returns type as superType if superType is a super type of type; otherwise, null.
      Returns:
      type as superType if superType is a super type of type; otherwise, null
    • getSuperclass

      public static @Nullable TypeMirror getSuperclass(TypeMirror type, Types types)
      Returns the superclass of the given class. Returns null if there is not one.
      Parameters:
      type - a type
      types - type utilities
      Returns:
      the superclass of the given class, or null
    • getSuperClassOrInterface

      public static @Nullable DeclaredType getSuperClassOrInterface(TypeMirror type, Types types)
      Returns the superclass the given type. If there is no superclass the first interface returned by Types.directSupertypes(TypeMirror) is returned. If the type has neither a superclass nor a superinterface, then null is returned.
      Parameters:
      type - a type
      types - 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 from from to to.
      Parameters:
      from - a primitive type
      to - a primitive type
      Returns:
      the type of primitive conversion from from to to
    • 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 as 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).

      Parameters:
      type - type to do substitution in
      typeVariables - type variables that should be replaced with the type mirror at the same index of typeArgs
      typeArgs - type mirrors that should replace the type variable at the same index of typeVariables
      env - processing environment
      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
    • getArrayDepth

      public static int getArrayDepth(TypeMirror arrayType)
      Returns the depth of an array type.
      Parameters:
      arrayType - an array type
      Returns:
      the depth of arrayType
    • freshTypeVariable

      public 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. If typeMirror is not a wildcard, returns typeMirror.
      Parameters:
      typeMirror - a type
      env - processing environment
      Returns:
      a fresh type variable if typeMirror is a wildcard, otherwise typeMirror
    • order

      public 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.
      Parameters:
      collection - a collection of type variables
      types - type utilities
      Returns:
      the type variables ordered so that each type variable only references earlier type variables
    • canBeNarrowingPrimitiveConversion

      public static boolean canBeNarrowingPrimitiveConversion(TypeMirror type, Types types)
      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 type
      types - the type utilities
      Returns:
      true if assignment to the type may be a narrowing
    • areSame

      @EqualsMethod public static boolean areSame(TypeVariable typeVariable1, TypeVariable typeVariable2)
      Returns true if the two type variables are the same type variable. Meaning they have the same name and the same enclosing element. Unlike Types.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 by TreeBuilder.

      Parameters:
      typeVariable1 - a type variable
      typeVariable2 - a type variable
      Returns:
      if the two type variables are the same type variable