Package randoop.types

Class ClassOrInterfaceType

All Implemented Interfaces:
Comparable<Type>
Direct Known Subclasses:
NonParameterizedType, ParameterizedType

public abstract class ClassOrInterfaceType extends ReferenceType
Represents a class or interface type as defined in JLS Section 4.3.

This abstract class corresponds to this grammar production in the JLS:

   ClassOrInterfaceType:
     ClassType
     InterfaceType
 
InterfaceType is syntactically the same as ClassType. Therefore, ClassType and InterfaceType do not exist as subclasses of this class, Rather, the subclasses of this type distinguish between types with parameters (ParameterizedType), and types without (NonParameterizedType).
  • Field Details

    • debug

      private static boolean debug
      Set to true to enable debug output to standard out.
    • enclosingType

      protected ClassOrInterfaceType enclosingType
      The enclosing type. Non-null only if this is a nested type (either a member type or a nested static type).
  • Constructor Details

    • ClassOrInterfaceType

      public ClassOrInterfaceType()
  • Method Details

    • forClass

      public static ClassOrInterfaceType forClass(Class<?> classType)
      Translates a Class object that represents a class or interface into a ClassOrInterfaceType object. If the object has parameters, then delegates to ParameterizedType.forClass(Class). Otherwise, creates a NonParameterizedType object from the given object.
      Parameters:
      classType - the class type to translate
      Returns:
      the ClassOrInterfaceType object created from the given class type
    • forType

      public static ClassOrInterfaceType forType(Type type)
      Creates a ClassOrInterfaceType object for a given java.lang.reflect.Type reference. If type is a java.lang.reflect.ParameterizedType, then calls ParameterizedType.forType(java.lang.reflect.Type). Otherwise, if type is a Class object, calls forClass(Class).
      Parameters:
      type - the type reference
      Returns:
      the ClassOrInterfaceType object for the given type
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • substitute

      public abstract ClassOrInterfaceType substitute(Substitution substitution)
      Applies a substitution to a ReferenceType. If the type is parameterized then replaces type variables that occur, otherwise returns this type.

      This abstract method forces typing of substitutions applied to ReferenceType objects without an explicit cast.

      This abstract method allows substitutions to be applied to ClassOrInterfaceType objects without casting.

      Specified by:
      substitute in class ReferenceType
      Parameters:
      substitution - the type substitution
      Returns:
      the type created by applying the substitution to this type
    • substitute

      final ClassOrInterfaceType substitute(Substitution substitution, ClassOrInterfaceType type)
      Applies the substitution to the enclosing type of this type and adds the result as the enclosing class of the given type.
      Parameters:
      substitution - the substitution to apply to the enclosing type
      type - the type to which resulting enclosing type is to be added; will be side-effected
      Returns:
      the type with enclosing type added if needed
    • applyCaptureConversion

      public abstract ClassOrInterfaceType applyCaptureConversion()
      Description copied from class: Type
      Applies a capture conversion to this type.
      Overrides:
      applyCaptureConversion in class ReferenceType
      Returns:
      a copy of this type with wildcards replaced by type conversion
    • applyCaptureConversion

      final ClassOrInterfaceType applyCaptureConversion(ClassOrInterfaceType type)
      Applies capture conversion to the enclosing type of this type and adds the result as the enclosing class of the given type.
      Parameters:
      type - this type with capture conversion applied; will be side-effected
      Returns:
      the type with converted enclosing type
    • getSimpleName

      public String getSimpleName()
      Returns the name of this class type. Does not include package, enclosing classes, or type arguments.
      Specified by:
      getSimpleName in class Type
      Returns:
      the name of this class
    • getCanonicalName

      public String getCanonicalName()
      Description copied from class: Type
      Returns the name of this type as the "canonical name" of the underlying runtime class. Identical to Type.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).
      Overrides:
      getCanonicalName in class Type
      Returns:
      the fully-qualified canonical name of this type
    • getFqName

      public String getFqName()
      Description copied from class: Type
      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>".
      Specified by:
      getFqName in class Type
      Returns:
      the fully-qualified type name for this type
    • getBinaryName

      public String getBinaryName()
      Description copied from class: Type
      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>".
      Specified by:
      getBinaryName in class Type
      Returns:
      the binary name for this type
    • getUnqualifiedBinaryName

      public String getUnqualifiedBinaryName()
      Description copied from class: Type
      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>".
      Overrides:
      getUnqualifiedBinaryName in class Type
      Returns:
      the unqualified name of this type
    • getInterfaces

      public abstract List<ClassOrInterfaceType> getInterfaces()
      Returns the interface types directly implemented by this class or interface type. Preserves the order in the reflection method Class.getGenericInterfaces(). If no interfaces are implemented, then returns the empty list.
      Returns:
      the list of interfaces directly implemented by this type
    • getPackage

      public Package getPackage()
      Returns the package of the runtime class of this type.
      Returns:
      the package of the runtime class of this type, or null if there is none
    • getPackagePrefix

      String getPackagePrefix()
      Return the package part of a type name, including the final period. Returns the empty string for a type in the unnamed package.
      Returns:
      the package part of a type name, or ""
    • getRawtype

      public abstract NonParameterizedType getRawtype()
      Returns the non-parameterized form of this class type.
      Overrides:
      getRawtype in class Type
      Returns:
      the non-parameterized form of this class type
    • getMatchingSupertype

      public InstantiatedType getMatchingSupertype(GenericClassType goalType)
      Finds the parameterized type that is a supertype of this class that also matches the given generic class. For example, if class C<T> implements Comparator<T> and class A extends C<String>, then when applied to A, this method would return C<String> when given C<T>, and Comparator<String> when given Comparator<E>. Returns null if there is no such type.

      Performs a depth-first search of the supertype relation for this type. If the goal type is an interface, then searches the interfaces of this type first.

      Parameters:
      goalType - the generic class type
      Returns:
      the instantiated type matching the goal type, or null
    • getInstantiatingSubstitution

      public Substitution getInstantiatingSubstitution(ReferenceType goalType)
      Description copied from class: ReferenceType
      Computes a substitution that can be applied to the type variables of the generic goal type to instantiate operations of this type, possibly inherited from from the goal type. The substitution will unify this type or a supertype of this type with the given goal type.

      If there is no unifying substitution, returns null.

      Overrides:
      getInstantiatingSubstitution in class ReferenceType
      Parameters:
      goalType - the generic type for which a substitution is needed
      Returns:
      a substitution unifying this type or a supertype of this type with the goal type
    • getSuperclass

      public abstract ClassOrInterfaceType getSuperclass()
      Return the type for the superclass for this class.
      Returns:
      superclass of this type, or the Object type if this type has no superclass
    • getSuperTypes

      public Collection<ClassOrInterfaceType> getSuperTypes()
      Return the set of all of the supertypes of this type.
      Returns:
      the set of all supertypes of this type
    • getImmediateSupertypes

      public List<ClassOrInterfaceType> getImmediateSupertypes()
      Return the immediate supertypes of this type.
      Returns:
      the immediate supertypes of this type
    • getAllSupertypesInclusive

      public Collection<ClassOrInterfaceType> getAllSupertypesInclusive()
      Return all supertypes of this type, including itself.
      Returns:
      all supertypes of this type, including itself
    • isAbstract

      public abstract boolean isAbstract()
      Indicate whether this class is abstract.
      Returns:
      true if this class is abstract, false otherwise
    • isGeneric

      public boolean isGeneric(boolean ignoreWildcards)
      Description copied from class: Type
      Indicate whether this type is generic. A type is generic if it has one or more type variables.
      Overrides:
      isGeneric in class Type
      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
    • isInstantiationOf

      public boolean isInstantiationOf(ReferenceType otherType)
      Indicates whether this type is an instantiation of a more general type.

      For a general ReferenceType, this is only true if the other type is the same, or the other type is a type variable for which this type satisfies the bounds. Other cases are handled by the overriding implementations isInstantiationOf(ReferenceType), InstantiatedType.isInstantiationOf(ReferenceType), and TypeVariable.isInstantiationOf(ReferenceType).

      For a ClassOrInterfaceType that is a member class, if otherType is also a member class, then the enclosing type of this type must instantiate the enclosing type of otherType.

      Overrides:
      isInstantiationOf in class ReferenceType
      Parameters:
      otherType - the general reference type
      Returns:
      true if this type instantiates the other reference type, false otherwise
    • isNestedClass

      public final boolean isNestedClass()
      Indicate whether this class is a nested class.
      Returns:
      true iff this class is a nested class
    • isMemberClass

      public final boolean isMemberClass()
      Indicate whether this class is a member of another class. (see JLS section 8.5).
      Returns:
      true if this class is a member class, false otherwise
    • isParameterized

      public boolean isParameterized()
      Description copied from class: Type
      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

      Overrides:
      isParameterized in class Type
      Returns:
      true if this type is a parameterized type, false otherwise
    • isStatic

      public abstract boolean isStatic()
      Indicates whether this class is static.
      Returns:
      true if this class is static, 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.2 of JLS for JavaSE 8.
      Overrides:
      isSubtypeOf in class ReferenceType
      Parameters:
      otherType - the possible supertype
      Returns:
      true if this type is a subtype of the given type, false otherwise
      See Also:
    • hasWildcard

      public boolean hasWildcard()
      Description copied from class: Type
      Indicates whether this type has a wildcard anywhere within it.
      Overrides:
      hasWildcard in class Type
      Returns:
      true if this type has a wildcard, false otherwise
    • hasCaptureVariable

      public boolean hasCaptureVariable()
      Description copied from class: Type
      Indicates whether this ReferenceType has a capture variable.
      Overrides:
      hasCaptureVariable in class Type
      Returns:
      true iff this type has a capture variable
    • setEnclosingType

      protected void setEnclosingType(ClassOrInterfaceType enclosingType)
      Sets the enclosing type for this class type.
      Parameters:
      enclosingType - the type for the class enclosing the declaration of this type
    • getTypeArguments

      public List<TypeArgument> getTypeArguments()
      Returns the type arguments for this type.
      Returns:
      the list of type arguments
    • getTypeParameters

      public List<TypeVariable> getTypeParameters()
      Description copied from class: ReferenceType
      Returns the list of type parameters for this type. Reference types that may have type parameters include ArrayType (such as E[]), and subclasses of ParameterizedType.
      Overrides:
      getTypeParameters in class ReferenceType
      Returns:
      the type parameters for this type
    • isClassOrInterfaceType

      public boolean isClassOrInterfaceType()
      Description copied from class: Type
      Indicate whether this type is a class or interface type.
      Overrides:
      isClassOrInterfaceType in class Type
      Returns:
      true if this type is a class or interface type; false, otherwise