Package randoop.types

Class Type

java.lang.Object
randoop.types.Type
All Implemented Interfaces:
Comparable<Type>
Direct Known Subclasses:
PrimitiveType, ReferenceType, VoidType

public abstract class Type extends Object implements Comparable<Type>
The superclass of a class hierarchy representing Java types defined in JLS Section 4.1. This class corresponds directly to Type defined in the JLS, which is defined by
   Type:
     ReferenceType
     PrimitiveType
 

The subclasses of this Type class should be used to represent types in Randoop test generation rather than the reflection types. Using reflection, each Java type has a Class<?> object, including primitive types. But, things get a little complicated for generic and parameterized types, where the Class object represents the raw type of the generic class, but also carries the type parameters of the generic class. More information about types is available through the subinterfaces of java.lang.reflect.Type, but working with generic and parameterized types is still awkward. This is in part because the correspondence to the JLS is unclear, but also because the provided methods do not implement all of the algorithms needed to work with types and type hierarchies as needed in Randoop. Effectively, the concrete subclasses of this class are facades for these reflective types, but they are identified with the definitions in the JLS, and provide the methods needed to test for assignability and test for subtypes.

Type objects are constructed using the methods forType(java.lang.reflect.Type), forClass(Class), or forName(String). These methods translate the reflection types into objects of subclasses of this type.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Applies a capture conversion to this type.
    int
    Compare this Type to another.
    static Type
    forClass(Class<?> classType)
    Translates a Class into a Type object.
    static Class<?>
    forFullyQualifiedName(@FqBinaryName String fullyQualifiedName)
    Returns the Class for a fully-qualified name (that may or may not be a multi-dimensional array).
    private static Class<?>
    forFullyQualifiedNameNonArray(@ClassGetName String fullyQualifiedName)
    Returns the Class for a class name.
    static Type
    forName(@ClassGetName String typeName)
    Returns a Type object for the given type name in Class.getName format.
    static Type
    forType(Type type)
    Returns a type constructed from the object referenced by a java.lang.reflect.Type reference.
    static Type
    Returns the type for the given Object reference.
    abstract String
    Returns the binary name of this type, including type arguments if this is a parameterized type (so, it isn't really a binary name).
    Returns the name of this type as the "canonical name" of the underlying runtime class.
    abstract String
    Returns the fully-qualified name of this type, including type arguments if this is a parameterized type.
    Returns the raw type for this type, which is this type except for generic types.
    abstract Class<?>
    Returns the runtime Class object for this type.
    abstract String
    Returns the name of this type without type arguments or package qualifiers.
    static Type
    getTypeforFullyQualifiedName(@FqBinaryName String fullyQualifiedName)
    Returns the Type for a fully-qualified name (that may or may not be a multi-dimensional array).
    Returns the name of this type without package name, but with type arguments if parameterized, and enclosing class if a member class.
    boolean
    Indicates whether this ReferenceType has a capture variable.
    boolean
    Indicates whether this type has a wildcard anywhere within it.
    boolean
    Indicates whether this object represents an array type.
    boolean
    isAssignableFrom(Type sourceType)
    Indicates whether there is an assignment conversion from a source Type to this type.
    <T> boolean
    Indicates whether there is an assignment conversion from the type of value to this type.
    boolean
    Indicates whether this is a boxed primitive type.
    boolean
    Indicates whether this type is the Class type.
    boolean
    Indicate whether this type is a class or interface type.
    boolean
    Indicates whether this is an enum type.
    final boolean
    Indicate whether this type is generic.
    boolean
    isGeneric(boolean ignoreWildcards)
    Indicate whether this type is generic.
    boolean
    Indicates whether this object is an interface type.
    boolean
    Indicates whether this is the type of a non-receiver term: primitive, boxed primitive, String, or Class.
    boolean
    Indicate whether this is the Object type.
    boolean
    Indicate whether this type is a parameterized type.
    boolean
    Indicates whether this is a primitive type.
    boolean
    Indicate whether this type is a rawtype of a generic class.
    boolean
    Indicates whether this is a reference type.
    boolean
    Indicates whether this type is the String type.
    boolean
    isSubtypeOf(Type otherType)
    Test whether this type is a subtype of the given type according to transitive closure of definition of the direct supertype relation in section 4.10 of JLS for Java SE 8.
    boolean
    Indicates whether this type is a type variable.
    boolean
    Indicate whether this type is void.
    private static int
    numDimensions(@FqBinaryName String typeName)
    Return the number of dimensions in the given type, which might or might not be an array.
    boolean
    Indicates whether the given Class<?> object is the runtime class of this type.
    substitute(Substitution substitution)
    Returns the type created by instantiating the type parameters of this type with ReferenceType objects.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Type

      public Type()
  • Method Details

    • forClass

      public static Type forClass(Class<?> classType)
      Translates a Class into a Type object. For primitive types, creates a PrimitiveType object. For reference types, delegates to ReferenceType.forClass(Class).
      Parameters:
      classType - the Class object for the type
      Returns:
      the Type object for the given reflection type
    • forName

      public static Type forName(@ClassGetName String typeName) throws ClassNotFoundException
      Returns a Type object for the given type name in Class.getName format. Uses reflection to find the corresponding type.

      Note that no method in Type returns the type name in this format. To get the name in this format from a Type object t, use t.getRuntimeClass().getName().

      Parameters:
      typeName - the name of a type
      Returns:
      the type object for the type with the name
      Throws:
      ClassNotFoundException - if name is not a recognized type
    • getTypeforFullyQualifiedName

      public static Type getTypeforFullyQualifiedName(@FqBinaryName String fullyQualifiedName) throws ClassNotFoundException
      Returns the Type for a fully-qualified name (that may or may not be a multi-dimensional array).
      Parameters:
      fullyQualifiedName - the fully-qualified binary name of a type, which uses "$" rather than "." to indicate a nested class. Array names such as int[] or java.lang.String[][] are also fully-qualified binary names. However, array definitions with sizes, such as int[3][], are not fully-qualified binary names.
      Returns:
      the type object for the type with the name
      Throws:
      ClassNotFoundException - if name is not a recognized type
    • forFullyQualifiedName

      public static Class<?> forFullyQualifiedName(@FqBinaryName String fullyQualifiedName) throws ClassNotFoundException
      Returns the Class for a fully-qualified name (that may or may not be a multi-dimensional array).
      Parameters:
      fullyQualifiedName - the fully-qualified binary name of a type, which uses uses "$" rather than "." to indicate a nested class
      Returns:
      the type object for the type with the name
      Throws:
      ClassNotFoundException - if name is not a recognized type
    • numDimensions

      private static int numDimensions(@FqBinaryName String typeName)
      Return the number of dimensions in the given type, which might or might not be an array.
      Parameters:
      typeName - a type name, possibly with some number of trailing "[]"
      Returns:
      the number of trailing "[]"
    • forFullyQualifiedNameNonArray

      private static Class<?> forFullyQualifiedNameNonArray(@ClassGetName String fullyQualifiedName) throws ClassNotFoundException
      Returns the Class for a class name. Does not support arrays.
      Parameters:
      fullyQualifiedName - the fully-qualified binary name of a non-array type, which uses uses "$" rather than "." to indicate a nested class
      Returns:
      the type object for the type with the name
      Throws:
      ClassNotFoundException - if name is not a recognized type
    • forValue

      public static Type forValue(Object value)
      Returns the type for the given Object reference.
      Parameters:
      value - the Object value
      Returns:
      the Type for the given value
    • forType

      public static Type forType(Type type)
      Returns a type constructed from the object referenced by a java.lang.reflect.Type reference.

      Note that when the type corresponds to a generic class type, this method returns the type variables from the getActualTypeArguments() method to maintain the guarantees needed for ClassOrInterfaceType.isSubtypeOf(Type).

      Parameters:
      type - the type to interpret
      Returns:
      a Type object corresponding to the given type
      Throws:
      IllegalArgumentException - if the type is a java.lang.reflect.WildcardType
    • getRuntimeClass

      public abstract Class<?> getRuntimeClass()
      Returns the runtime Class object for this type. For use when reflection is needed.

      Note that type variables and the null reference type do not have a runtime class, and this method will return null in those cases.

      This method should not be confused with the inherited Object.getClass() method, which returns the Class<?> for the Type object, and not of the represented type. For instance, if a Type object t represented the Java type int, then t.getRuntimeClass() would return int.class while t.getClass() would return Type.class.

      Returns:
      the Class that is the runtime representation of the type, or null if this type is a type variable or null reference type
    • getFqName

      public abstract String getFqName()
      Returns the fully-qualified name of this type, including type arguments if this is a parameterized type. For java.util.List<T> return "java.util.List<T>".
      Returns:
      the fully-qualified type name for this type
    • getBinaryName

      public abstract String getBinaryName()
      Returns the binary name of this type, including type arguments if this is a parameterized type (so, it isn't really a binary name). For java.util.List<T> return "java.util.List<T>".
      Returns:
      the binary name for this type
    • getSimpleName

      public abstract String getSimpleName()
      Returns the name of this type without type arguments or package qualifiers. For java.util.List<T>, returns "List".
      Returns:
      the name of this type without type arguments
    • getCanonicalName

      public String getCanonicalName()
      Returns the name of this type as the "canonical name" of the underlying runtime class. Identical to getFqName() except for types with type arguments. For java.util.List<T> returns "java.util.List". Returns null when Class<?>.getCanonicalName() does for the underlying Class<?> object (e.g., the type is a local or anonymous class, or array type where the component type that has no canonical name).
      Returns:
      the fully-qualified canonical name of this type
    • getUnqualifiedBinaryName

      public String getUnqualifiedBinaryName()
      Returns the name of this type without package name, but with type arguments if parameterized, and enclosing class if a member class. For instance, for java.util.List<T> would return "List<T>".
      Returns:
      the unqualified name of this type
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getRawtype

      public Type getRawtype()
      Returns the raw type for this type, which is this type except for generic types.
      Returns:
      the raw type corresponding to this type
    • runtimeClassIs

      public boolean runtimeClassIs(Class<?> c)
      Indicates whether the given Class<?> object is the runtime class of this type.
      Parameters:
      c - the Class<?> to check
      Returns:
      true if c is the runtime Class<?> of this type, false otherwise
    • isArray

      public boolean isArray()
      Indicates whether this object represents an array type.
      Returns:
      true if this object represents an array type, false otherwise
    • isBoxedPrimitive

      public boolean isBoxedPrimitive()
      Indicates whether this is a boxed primitive type.
      Returns:
      true if this type is a boxed primitive, false otherwise
    • isClass

      public boolean isClass()
      Indicates whether this type is the Class type.
      Returns:
      true if this type is the Class type, and false otherwise
    • isEnum

      public boolean isEnum()
      Indicates whether this is an enum type.
      Returns:
      true if this is an enum type, false otherwise
    • isGeneric

      public final boolean isGeneric()
      Indicate whether this type is generic. A type is generic if it has one or more type variables.
      Returns:
      true if this type is generic, false otherwise
    • isGeneric

      public boolean isGeneric(boolean ignoreWildcards)
      Indicate whether this type is generic. A type is generic if it has one or more type variables.
      Parameters:
      ignoreWildcards - if true, ignore wildcards; that is, treat wildcards as not making the operation generic
      Returns:
      true if this type is generic, false otherwise
    • isInterface

      public boolean isInterface()
      Indicates whether this object is an interface type.
      Returns:
      true if this object is an interface type, false otherwise
    • isObject

      public boolean isObject()
      Indicate whether this is the Object type.
      Returns:
      true if this is the Object type, false otherwise
    • isString

      public boolean isString()
      Indicates whether this type is the String type.
      Returns:
      true if this type is the String type, and false otherwise
    • isVoid

      public boolean isVoid()
      Indicate whether this type is void.
      Returns:
      true if this type is void, false otherwise
    • isParameterized

      public boolean isParameterized()
      Indicate whether this type is a parameterized type. A parameterized type is a type C<T1,...,Tk> that instantiates a generic class C<F1,...,Fk>.

      If inputType.isParameterized returns true, there are two possibilities: inputType instanceof InstantiatedType, or inputType is a member class and the enclosing type is a parameterized type

      Returns:
      true if this type is a parameterized type, false otherwise
    • hasWildcard

      public boolean hasWildcard()
      Indicates whether this type has a wildcard anywhere within it.
      Returns:
      true if this type has a wildcard, false otherwise
    • hasCaptureVariable

      public boolean hasCaptureVariable()
      Indicates whether this ReferenceType has a capture variable.
      Returns:
      true iff this type has a capture variable
    • isPrimitive

      public boolean isPrimitive()
      Indicates whether this is a primitive type.
      Returns:
      true if this type is primitive, false otherwise
    • isNonreceiverType

      public boolean isNonreceiverType()
      Indicates whether this is the type of a non-receiver term: primitive, boxed primitive, String, or Class.
      Returns:
      true iff this type is primitive, boxed primitive, String, or Class
    • isRawtype

      public boolean isRawtype()
      Indicate whether this type is a rawtype of a generic class. The rawtype is the runtime type of the class that has type parameters erased.
      Returns:
      true if this type is a rawtype of a generic class, false otherwise
    • isReferenceType

      public boolean isReferenceType()
      Indicates whether this is a reference type. Note: implementing classes should ensure that this is equivalent to !(this.isPrimitive())
      Returns:
      true if this type is a reference type, and false otherwise
    • isVariable

      public boolean isVariable()
      Indicates whether this type is a type variable.
      Returns:
      true if this type is a type variable, false otherwise
    • substitute

      public Type substitute(Substitution substitution)
      Returns the type created by instantiating the type parameters of this type with ReferenceType objects. Simply returns this type if it has no type parameters. In otherwords, this type is not a ParameterizedType, which includes GenericClassType.

      There are contexts in which it is necessary to apply a substitution to a Type and it is not clear whether the type is parameterized. In particular, this method is defined here because ArrayType can hold arbitrary types, including type variables and parameterized types.

      Parameters:
      substitution - the type substitution
      Returns:
      the Type constructed by substituting for type parameters in this type, or this type if this is not a generic class type
    • applyCaptureConversion

      public Type applyCaptureConversion()
      Applies a capture conversion to this type.
      Returns:
      a copy of this type with wildcards replaced by type conversion
    • isAssignableFrom

      public boolean isAssignableFrom(Type sourceType)
      Indicates whether there is an assignment conversion from a source Type to this type. (In other words, a value of the source type can be assigned to an l-value of this type.) Returns true if this is a legal assignment conversion: Variablethis = Expressionsourcetype.

      Based on the definition of assignment context in section 5.2 of the JDK 8 Java Language Specification, a value of one type is assignable to a variable of another type if the first type can be converted to the second by

      • an identity conversion (section 5.1.1),
      • a widening primitive conversion (section 5.1.2),
      • a widening reference conversion (section 5.1.5),
      • a boxing conversion (5.1.7), or
      • an unboxing conversion (section 5.1.8) possibly followed by a widening conversion.
      And, if after all those conversions, the type is a raw type, an unchecked conversion may occur.
      Parameters:
      sourceType - the type to test for assignability
      Returns:
      true if this type can be assigned from the source type, and false otherwise
    • isAssignableFromTypeOf

      public <T> boolean isAssignableFromTypeOf(T value)
      Indicates whether there is an assignment conversion from the type of value to this type. (Note this is equivalent to determining whether value can be assigned to an l-value of this type.) If the reference is null, then returns true only if this type is not primitive.
      Type Parameters:
      T - the type of the value
      Parameters:
      value - the element to check
      Returns:
      true if the type of value is assignable to this type, false otherwise
    • isSubtypeOf

      public boolean isSubtypeOf(Type otherType)
      Test whether this type is a subtype of the given type according to transitive closure of definition of the direct supertype relation in section 4.10 of JLS for Java SE 8.
      Parameters:
      otherType - the possible supertype
      Returns:
      true if this type is a subtype of the given type, false otherwise
    • isClassOrInterfaceType

      public boolean isClassOrInterfaceType()
      Indicate whether this type is a class or interface type.
      Returns:
      true if this type is a class or interface type; false, otherwise
    • compareTo

      public int compareTo(Type type)
      Compare this Type to another. Uses the names of the underlying Class<?> objects. Uses canonical names if both have them, otherwise uses Class<?>.getName().
      Specified by:
      compareTo in interface Comparable<Type>
      Parameters:
      type - the type to compare against
      Returns:
      -1 if this type precedes type, 1 if this type succeeds type, and 0 if they are equal.