Package spoon.reflect.reference
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionCasts the type reference inCtIntersectionTypeReference
.box()
Returns the corresponding non-primitive type for a primitive type (the same type otherwise).boolean
canAccess(CtTypeMember typeMember)
boolean
canAccess(CtTypeReference<?> type)
Checks visibility based on public, protected, package protected and private modifiers of typeclone()
Clone the element which calls this method in a new object.Computes nearest access path parent to this type from the context of this type reference.Gets the Java runtime class of the referenced type.Returns theCtType
, that corresponds to the reference ornull
if the type declaration is not in the analyzed source files,getTypeDeclaration()
is a newer and better alternative that less often returns null.Gets the type that declares the referenced type.Gets modifiers of this type.Gets the package of the referenced type.Returns the simple (unqualified) name of this element.Returns a reference to the type directly extended by this type.Set<CtTypeReference<?>>
Returns the interface types directly implemented by this class or extended by this interface.Returns this, or top level type of this, if this is an inner typeReturns theCtType
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, seeCtShadowable.isShadow()
).If this type reference is used as a type argument (seeCtActualTypeContainer.getActualTypeArguments()
), returns the type parameter declaration in the target type, returns null otherwise.boolean
<C extends CtTypeReference<T>>
CsetDeclaringType(CtTypeReference<?> type)
Sets the reference to the declaring type.<C extends CtTypeReference<T>>
CsetPackage(CtPackageReference pack)
Sets the reference to the declaring package.setSimplyQualified(boolean isSimplyQualified)
unbox()
Returns the primitive type for a boxing type (unchanged if the type does not correspond to a boxing type).Methods inherited from interface spoon.reflect.reference.CtActualTypeContainer
addActualTypeArgument, getActualTypeArguments, removeActualTypeArgument, setActualTypeArguments
Methods inherited from interface spoon.reflect.declaration.CtElement
addAnnotation, addComment, asIterable, delete, descendantIterator, getAllMetadata, getAnnotatedChildren, getAnnotation, getAnnotation, getAnnotations, getComments, getDirectChildren, getDocComment, getElements, getMetadata, getMetadataKeys, getOriginalSourceFragment, getParent, getParent, getParent, getPath, getPosition, getReferencedTypes, getRoleInParent, getShortRepresentation, getValueByRole, hasAnnotation, hasParent, isImplicit, isParentInitialized, prettyprint, putMetadata, removeAnnotation, removeComment, replace, replace, setAllMetadata, setAnnotations, setDocComment, setImplicit, setParent, setPosition, setPositions, setValueByRole, toString, toStringDebug, updateAllParentsBelow
Methods inherited from interface spoon.reflect.visitor.chain.CtQueryable
filterChildren, map, map
Methods inherited from interface spoon.reflect.reference.CtReference
setComments, setSimpleName
Methods inherited from interface spoon.reflect.declaration.CtShadowable
isShadow, setShadow
Methods inherited from interface spoon.reflect.declaration.CtTypeInformation
getAllExecutables, getAllFields, getDeclaredExecutables, getDeclaredField, getDeclaredFields, getDeclaredOrInheritedField, getQualifiedName, getTypeErasure, isAnnotationType, isAnonymous, isArray, isClass, isEnum, isGenerics, isInterface, isLocalType, isParameterized, isPrimitive, isSubtypeOf
Methods inherited from interface spoon.reflect.visitor.CtVisitable
accept
Methods inherited from interface spoon.processing.FactoryAccessor
getFactory, setFactory
-
Field Details
-
NULL_TYPE_NAME
The name of the null type ("<nulltype>").- See Also:
- Constant Field Values
-
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 interfaceCtReference
-
getActualClass
Gets the Java runtime class of the referenced type. This is a low-level feature, it should never been used, usegetTypeDeclaration()
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):-
it will return the version of the class your
Environment.getInputClassLoader()
loads. This might be a version spoon compiled (manually usingSpoonModelBuilder.compile(spoon.SpoonModelBuilder.InputType...)
or implicitly usingEnvironment.shouldCompile()
) or from your classpath (which might be null) - it will run static initializers (executing arbitrary code from the analyzed project in the process)
- it will fail if the static initializers of the class fail
- 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
-
it will return the version of the class your
-
getDeclaration
Returns theCtType
, that corresponds to the reference ornull
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 interfaceCtReference
- Returns:
- the referenced element or
null
if the type declaration is not the analyzed source files.
-
getTypeDeclaration
Returns theCtType
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, seeCtShadowable.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
Sets the reference to the declaring type. Should be set to null if the referenced type is not a inner type. -
setPackage
Sets the reference to the declaring package. -
asCtIntersectionTypeReference
CtIntersectionTypeReference<T> asCtIntersectionTypeReference()Casts the type reference inCtIntersectionTypeReference
. -
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 methodsRefactoring.copyType(CtType)
andRefactoring.copyMethod(CtMethod)
instead which does additional work beyond cloning.- Specified by:
clone
in interfaceCtElement
- Specified by:
clone
in interfaceCtReference
-
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 interfaceCtTypeInformation
-
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, usegetDeclaration()
orgetTypeDeclaration()
on theCtTypeReference
returned by this method.- Specified by:
getSuperclass
in interfaceCtTypeInformation
- 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 classnull
is returned (notObject
). For types like enums that implicitly extend a superclass likeEnum
, this method returns that class.
-
getModifiers
Set<ModifierKind> getModifiers()Description copied from interface:CtTypeInformation
Gets modifiers of this type.- Specified by:
getModifiers
in interfaceCtTypeInformation
-
canAccess
Checks visibility based on public, protected, package protected and private modifiers of type- Parameters:
type
-- Returns:
- true if this can access that type
-
canAccess
- 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 hierarchyclass A { class B { class C {} } }
The C.getAccessParentFrom(null) will return B, because B can be used to access C, using code likeB.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 modifierprotected
: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 (seeCtActualTypeContainer.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" (whilegetDeclaration()
returns the "T" of Cat).class Dog<X>{} class Cat<T> { Dog<T> dog; }
-
setSimplyQualified
- 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.
-