Interface CtTypeReference<T>

All Superinterfaces:
Cloneable, CtActualTypeContainer, CtElement, CtQueryable, CtReference, CtShadowable, CtTypeInformation, CtVisitable, FactoryAccessor, Serializable, SourcePositionHolder
All Known Subinterfaces:
CtArrayTypeReference<T>, CtIntersectionTypeReference<T>, CtTypeParameterReference, CtWildcardReference
All Known Implementing Classes:
CtArrayTypeReferenceImpl, CtIntersectionTypeReferenceImpl, CtTypeParameterReferenceImpl, CtTypeReferenceImpl, CtWildcardReferenceImpl

public interface CtTypeReference<T> extends CtReference, CtActualTypeContainer, CtTypeInformation, CtShadowable
This interface defines a reference to a CtType or sub-type.
  • Field Details

    • NULL_TYPE_NAME

      static final String NULL_TYPE_NAME
      The name of the null type ("<nulltype>").
      See Also:
      Constant Field Values
    • OMITTED_TYPE_ARG_NAME

      static final String OMITTED_TYPE_ARG_NAME
      Special type used as a type argument when actual type arguments can't be inferred.
      See Also:
      Constant Field Values
  • Method Details

    • getSimpleName

      String getSimpleName()
      Returns the simple (unqualified) name of this element. Following the compilation convention, if the type is a local type, the name starts with a numeric prefix (e.g. local class Foo has simple name 1Foo).
      Specified by:
      getSimpleName in interface CtReference
    • getActualClass

      Class<T> getActualClass()
      Gets the Java runtime class of the referenced type. This is a low-level feature, it should never been used, use getTypeDeclaration() instead in order to only stay in the Spoon world and manipulate CtType instead of java.lang.Class.

      Note (these are artifacts of the current implementation and might change at any time):
      1. it will return the version of the class your Environment.getInputClassLoader() loads. This might be a version spoon compiled (manually using SpoonModelBuilder.compile(spoon.SpoonModelBuilder.InputType...) or implicitly using Environment.shouldCompile()) or from your classpath (which might be null)
      2. it will run static initializers (executing arbitrary code from the analyzed project in the process)
      3. it will fail if the static initializers of the class fail
      4. the Class object may not reflect the CtType in case you made recent changes to it based on point 1
      Returns:
      the Java class or throws a SpoonClassNotFoundException if the class is not found.
      Throws:
      SpoonClassNotFoundException - if the class is not in the classpath
    • getDeclaration

      CtType<T> getDeclaration()
      Returns the CtType, that corresponds to the reference or null if the type declaration is not in the analyzed source files, getTypeDeclaration() is a newer and better alternative that less often returns null.
      Specified by:
      getDeclaration in interface CtReference
      Returns:
      the referenced element or null if the type declaration is not the analyzed source files.
    • getTypeDeclaration

      CtType<T> getTypeDeclaration()
      Returns the CtType that corresponds to the reference even if the type isn't in the Spoon source path (in this case, the Spoon elements are built with runtime reflection, and the resulting CtType is called a "shadow" class, see CtShadowable.isShadow()).
      Returns:
      the type declaration that corresponds to the reference or null if the reference points to a class that is not in the classpath.
    • getDeclaringType

      CtTypeReference<?> getDeclaringType()
      Gets the type that declares the referenced type.
      Returns:
      the declaring type if this references an inner class; null in other cases
    • getPackage

      CtPackageReference getPackage()
      Gets the package of the referenced type.
      Returns:
      the declaring package or null if this if a inner class
    • box

      CtTypeReference<?> box()
      Returns the corresponding non-primitive type for a primitive type (the same type otherwise).
    • unbox

      CtTypeReference<?> unbox()
      Returns the primitive type for a boxing type (unchanged if the type does not correspond to a boxing type).
    • setDeclaringType

      <C extends CtTypeReference<T>> C setDeclaringType(CtTypeReference<?> type)
      Sets the reference to the declaring type. Should be set to null if the referenced type is not a inner type.
    • setPackage

      <C extends CtTypeReference<T>> C setPackage(CtPackageReference pack)
      Sets the reference to the declaring package.
    • asCtIntersectionTypeReference

      CtIntersectionTypeReference<T> asCtIntersectionTypeReference()
      Casts the type reference in CtIntersectionTypeReference.
    • clone

      CtTypeReference<T> clone()
      Description copied from interface: CtElement
      Clone the element which calls this method in a new object. Note that that references are kept as is, and thus, so if you clone whole classes or methods, some parts of the cloned element (eg executable references) may still point to the initial element. In this case, consider using methods Refactoring.copyType(CtType) and Refactoring.copyMethod(CtMethod) instead which does additional work beyond cloning.
      Specified by:
      clone in interface CtElement
      Specified by:
      clone in interface CtReference
    • getSuperInterfaces

      Set<CtTypeReference<?>> getSuperInterfaces()
      Description copied from interface: CtTypeInformation
      Returns the interface types directly implemented by this class or extended by this interface.
      Specified by:
      getSuperInterfaces in interface CtTypeInformation
    • getSuperclass

      CtTypeReference<?> getSuperclass()
      Description copied from interface: CtTypeInformation
      Returns a reference to the type directly extended by this type.

      To get the CtType of the super class, use getDeclaration() or getTypeDeclaration() on the CtTypeReference returned by this method.

      Specified by:
      getSuperclass in interface CtTypeInformation
      Returns:
      the type explicitly extended by this type, or null if there is none or if the super type is not in the classpath (in noclasspath mode). If a class does not explicitly extend another class null is returned (not Object). For types like enums that implicitly extend a superclass like Enum, this method returns that class.
    • getModifiers

      Set<ModifierKind> getModifiers()
      Description copied from interface: CtTypeInformation
      Gets modifiers of this type.
      Specified by:
      getModifiers in interface CtTypeInformation
    • canAccess

      boolean canAccess(CtTypeReference<?> type)
      Checks visibility based on public, protected, package protected and private modifiers of type
      Parameters:
      type -
      Returns:
      true if this can access that type
    • canAccess

      boolean canAccess(CtTypeMember typeMember)
      Returns:
      true if this type can access that the `typeMember` in another type based on public, protected, package protected and private modifiers.
    • getTopLevelType

      CtTypeReference<?> getTopLevelType()
      Returns this, or top level type of this, if this is an inner type
    • getAccessType

      CtTypeReference<?> getAccessType()
      Computes nearest access path parent to this type from the context of this type reference. The context is defined by this.getParent(CtType.class). Normally the declaring type can be used as access path. For example in this class hierarchy
       class A {
          class B {
             class C {}
          }
       }
       
      The C.getAccessParentFrom(null) will return B, because B can be used to access C, using code like B.C
      But when some class (A or B) on the access path is not visible in type X, then we must found an alternative path. For example in case like, when A and B are invisible, e.g because of modifier protected:
       class D extends B {
       }
       class X extends D {
               class F extends C
       }
       
      The C.getAccessParentFrom(X) will return D, because D can be used to access C in scope of X.
      Returns:
      type reference which can be used to access this type in scope of contextType.
    • getTypeParameterDeclaration

      CtTypeParameter getTypeParameterDeclaration()
      If this type reference is used as a type argument (see CtActualTypeContainer.getActualTypeArguments()), returns the type parameter declaration in the target type, returns null otherwise. In the following example, getTypeParameterDeclaration of "String" returns the type parameter definition "X".
       class Dog<X>{}
       Dog<String>var = ...;
       
      In this other example, getTypeParameterDeclaration of T in Dog<T> returns the type parameter definition "X" (while getDeclaration() returns the "T" of Cat).
       class Dog<X>{}
       class Cat<T> {
       Dog<T> dog;
       }
       
    • setSimplyQualified

      CtTypeReference<T> setSimplyQualified(boolean isSimplyQualified)
      Parameters:
      isSimplyQualified - false then the reference is printed fully qualified name. true then only the type name is printed.
    • isSimplyQualified

      boolean isSimplyQualified()
      Returns:
      false then fully qualified name is printed. true then type simple name is printed.