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()
      Deprecated.
      (Since Spoon 7.0.0) use getTypeDeclaration() instead
      Gets the Java runtime class of the referenced type. This is a low-level feature, it should never been used. For CtTypeReference, use getTypeDeclaration() instead, in order to only stay in the Spoon world and manipulate CtType instead of java.lang.Class.
      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 the class type directly extended by this class. getSuperClass().getDeclaration()/getTypeDeclaration() returns the corresponding CtType (if in the source folder of Spoon).
      Specified by:
      getSuperclass in interface CtTypeInformation
      Returns:
      the class type directly extended by this class, or null if there is none or if the super class is not in the classpath (in noclasspath mode)
    • 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.