Package spoon.support.reflect.reference
Class CtTypeReferenceImpl<T>
java.lang.Object
spoon.support.reflect.declaration.CtElementImpl
spoon.support.reflect.reference.CtReferenceImpl
spoon.support.reflect.reference.CtTypeReferenceImpl<T>
- All Implemented Interfaces:
Serializable
,Cloneable
,FactoryAccessor
,SourcePositionHolder
,CtElement
,CtShadowable
,CtTypeInformation
,CtActualTypeContainer
,CtReference
,CtTypeReference<T>
,CtQueryable
,CtVisitable
- Direct Known Subclasses:
CtArrayTypeReferenceImpl
,CtIntersectionTypeReferenceImpl
,CtTypeParameterReferenceImpl
- See Also:
- Serialized Form
-
Field Summary
Fields inherited from class spoon.support.reflect.reference.CtReferenceImpl
simplename
Fields inherited from class spoon.support.reflect.declaration.CtElementImpl
ERROR_MESSAGE_TO_STRING, factory, LOGGER, parent
Fields inherited from interface spoon.reflect.reference.CtTypeReference
NULL_TYPE_NAME, OMITTED_TYPE_ARG_NAME
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Accepts a visitor<C extends CtActualTypeContainer>
CaddActualTypeArgument(CtTypeReference<?> actualTypeArgument)
Adds a type argument.Casts 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.Finds the class requested ingetActualClass()
.Computes nearest access path parent to this type from the context of this type reference.protected AnnotatedElement
Gets the Java runtime class of the referenced type.List<CtTypeReference<?>>
Gets the type arguments.Gets the executables declared by this type and by all its supertypes (static/instance methods, constructors, anonymous static blocks) if applicable.Gets the fields declared by this type and by all its supertypes if applicable.Returns theCtType
, that corresponds to the reference ornull
if the type declaration is not in the analyzed source files,CtTypeReference.getTypeDeclaration()
is a newer and better alternative that less often returns null.Gets the executables declared by this type if applicable.getDeclaredField(String name)
Gets a field from its name.Gets the fields declared by this type.getDeclaredOrInheritedField(String fieldName)
Gets a field from this type or any super type or any implemented interface by field name.Gets the type that declares the referenced type.Gets modifiers of this type.Gets the package of the referenced type.Returns the fully qualified name of this type declaration.Returns the class type directly extended by this class.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
Returns true if this type is an annotation type.boolean
Returntrue
if the referenced type is a anonymous typeboolean
isArray()
boolean
isClass()
Returns true if this type is a class.boolean
isEnum()
Returns true if this type is an enum.boolean
Returns true if it is not a concrete, resolvable class, it if refers to a type parameter directly or indirectly.boolean
Returns true if this type is an interface.boolean
Returntrue
if the referenced type is declared in an executable.boolean
Returns true if it has any type parameter (generic or not).boolean
Returntrue
if the referenced type is a primitive type (int, double, boolean...).boolean
isShadow()
When an element isn't present in the factory (created in another factory), this element is considered as "shadow".boolean
boolean
isSubtypeOf(CtTypeReference<?> type)
Returns true if the referenced type is a sub-type of the given type.boolean
removeActualTypeArgument(CtTypeReference<?> actualTypeArgument)
Removes a type argument.<C extends CtActualTypeContainer>
CsetActualTypeArguments(List<? extends CtTypeReference<?>> actualTypeArguments)
Sets the type arguments.<C extends CtTypeReference<T>>
CsetDeclaringType(CtTypeReference<?> declaringType)
Sets the reference to the declaring type.<C extends CtTypeReference<T>>
CsetPackage(CtPackageReference pack)
Sets the reference to the declaring package.<E extends CtShadowable>
EsetShadow(boolean isShadow)
Marks an element as shadow.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 class spoon.support.reflect.reference.CtReferenceImpl
equals, getSimpleName, setComments, setSimpleName
Methods inherited from class spoon.support.reflect.declaration.CtElementImpl
addAnnotation, addComment, asIterable, comment, delete, descendantIterator, emptyList, emptySet, filterChildren, getAllMetadata, getAnnotatedChildren, getAnnotation, getAnnotation, getAnnotations, getComments, getDirectChildren, getDocComment, getElements, getFactory, getMetadata, getMetadataKeys, getOriginalSourceFragment, getParent, getParent, getParent, getPath, getPosition, getReferencedTypes, getRoleInParent, getShortRepresentation, getValueByRole, hasAnnotation, hashCode, hasParent, isImplicit, isParentInitialized, map, map, prettyprint, putMetadata, removeAnnotation, removeComment, replace, replace, setAllMetadata, setAnnotations, setDocComment, setFactory, setImplicit, setParent, setPosition, setPositions, setValueByRole, toString, toStringDebug, unmodifiableList, updateAllParentsBelow
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
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.reference.CtTypeReference
getSimpleName
Methods inherited from interface spoon.processing.FactoryAccessor
getFactory, setFactory
-
Constructor Details
-
CtTypeReferenceImpl
public CtTypeReferenceImpl()
-
-
Method Details
-
accept
Description copied from interface:CtVisitable
Accepts a visitor- Specified by:
accept
in interfaceCtVisitable
-
box
Description copied from interface:CtTypeReference
Returns the corresponding non-primitive type for a primitive type (the same type otherwise).- Specified by:
box
in interfaceCtTypeReference<T>
-
getActualClass
Description copied from interface:CtTypeReference
Gets the Java runtime class of the referenced type. This is a low-level feature, it should never been used. For CtTypeReference, useCtTypeReference.getTypeDeclaration()
instead, in order to only stay in the Spoon world and manipulate CtType instead of java.lang.Class.- Specified by:
getActualClass
in interfaceCtTypeReference<T>
- Returns:
- the Java class or throws a
SpoonClassNotFoundException
if the class is not found.
-
findClass
Finds the class requested ingetActualClass()
. Looks for the class in the standard Java classpath, but also in the sourceClassPath given as option. -
getActualTypeArguments
Description copied from interface:CtActualTypeContainer
Gets the type arguments.- Specified by:
getActualTypeArguments
in interfaceCtActualTypeContainer
-
getActualAnnotatedElement
- Specified by:
getActualAnnotatedElement
in classCtReferenceImpl
-
getDeclaration
Description copied from interface:CtTypeReference
Returns theCtType
, that corresponds to the reference ornull
if the type declaration is not in the analyzed source files,CtTypeReference.getTypeDeclaration()
is a newer and better alternative that less often returns null.- Specified by:
getDeclaration
in interfaceCtReference
- Specified by:
getDeclaration
in interfaceCtTypeReference<T>
- Returns:
- the referenced element or
null
if the type declaration is not the analyzed source files.
-
getTypeDeclaration
Description copied from interface:CtTypeReference
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()
).- Specified by:
getTypeDeclaration
in interfaceCtTypeReference<T>
- 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
Description copied from interface:CtTypeReference
Gets the type that declares the referenced type.- Specified by:
getDeclaringType
in interfaceCtTypeReference<T>
- Returns:
- the declaring type if this references an inner class; null in other cases
-
getPackage
Description copied from interface:CtTypeReference
Gets the package of the referenced type.- Specified by:
getPackage
in interfaceCtTypeReference<T>
- Returns:
- the declaring package or null if this if a inner class
-
getQualifiedName
Description copied from interface:CtTypeInformation
Returns the fully qualified name of this type declaration.- Specified by:
getQualifiedName
in interfaceCtTypeInformation
-
isPrimitive
public boolean isPrimitive()Description copied from interface:CtTypeInformation
Returntrue
if the referenced type is a primitive type (int, double, boolean...).- Specified by:
isPrimitive
in interfaceCtTypeInformation
-
isSubtypeOf
Description copied from interface:CtTypeInformation
Returns true if the referenced type is a sub-type of the given type. Returns true is type is self, it means: typeX.isSubtypeOf(typeX) is true too- Specified by:
isSubtypeOf
in interfaceCtTypeInformation
-
setActualTypeArguments
public <C extends CtActualTypeContainer> C setActualTypeArguments(List<? extends CtTypeReference<?>> actualTypeArguments)Description copied from interface:CtActualTypeContainer
Sets the type arguments.- Specified by:
setActualTypeArguments
in interfaceCtActualTypeContainer
-
setDeclaringType
Description copied from interface:CtTypeReference
Sets the reference to the declaring type. Should be set to null if the referenced type is not a inner type.- Specified by:
setDeclaringType
in interfaceCtTypeReference<T>
-
setPackage
Description copied from interface:CtTypeReference
Sets the reference to the declaring package.- Specified by:
setPackage
in interfaceCtTypeReference<T>
-
asCtIntersectionTypeReference
Description copied from interface:CtTypeReference
Casts the type reference inCtIntersectionTypeReference
.- Specified by:
asCtIntersectionTypeReference
in interfaceCtTypeReference<T>
-
unbox
Description copied from interface:CtTypeReference
Returns the primitive type for a boxing type (unchanged if the type does not correspond to a boxing type).- Specified by:
unbox
in interfaceCtTypeReference<T>
-
getDeclaredFields
Description copied from interface:CtTypeInformation
Gets the fields declared by this type.- Specified by:
getDeclaredFields
in interfaceCtTypeInformation
-
getDeclaredField
Description copied from interface:CtTypeInformation
Gets a field from its name.- Specified by:
getDeclaredField
in interfaceCtTypeInformation
- Returns:
- null if does not exit
-
getDeclaredOrInheritedField
Description copied from interface:CtTypeInformation
Gets a field from this type or any super type or any implemented interface by field name.- Specified by:
getDeclaredOrInheritedField
in interfaceCtTypeInformation
- Returns:
- null if does not exit
-
getDeclaredExecutables
Description copied from interface:CtTypeInformation
Gets the executables declared by this type if applicable.- Specified by:
getDeclaredExecutables
in interfaceCtTypeInformation
-
getAllFields
Description copied from interface:CtTypeInformation
Gets the fields declared by this type and by all its supertypes if applicable.- Specified by:
getAllFields
in interfaceCtTypeInformation
-
getAllExecutables
Description copied from interface:CtTypeInformation
Gets the executables declared by this type and by all its supertypes (static/instance methods, constructors, anonymous static blocks) if applicable. This method returns:- static, instance and default methods
- constructors
- Specified by:
getAllExecutables
in interfaceCtTypeInformation
-
getModifiers
Description copied from interface:CtTypeInformation
Gets modifiers of this type.- Specified by:
getModifiers
in interfaceCtTypeInformation
- Specified by:
getModifiers
in interfaceCtTypeReference<T>
-
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 interfaceCtTypeInformation
- Specified by:
getSuperclass
in interfaceCtTypeReference<T>
- 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)
-
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
- Specified by:
getSuperInterfaces
in interfaceCtTypeReference<T>
-
isAnonymous
public boolean isAnonymous()Description copied from interface:CtTypeInformation
Returntrue
if the referenced type is a anonymous type- Specified by:
isAnonymous
in interfaceCtTypeInformation
-
isLocalType
public boolean isLocalType()Description copied from interface:CtTypeInformation
Returntrue
if the referenced type is declared in an executable. e.g. a type declared in a method or a lambda. This corresponds toisLocalClass
ofjava.lang.Class
.// Type declared in a method. public void make() { class Cook { } } // Type declared in a lambda. s -> { class Cook { } }
- Specified by:
isLocalType
in interfaceCtTypeInformation
-
addActualTypeArgument
public <C extends CtActualTypeContainer> C addActualTypeArgument(CtTypeReference<?> actualTypeArgument)Description copied from interface:CtActualTypeContainer
Adds a type argument.- Specified by:
addActualTypeArgument
in interfaceCtActualTypeContainer
-
removeActualTypeArgument
Description copied from interface:CtActualTypeContainer
Removes a type argument.- Specified by:
removeActualTypeArgument
in interfaceCtActualTypeContainer
-
isClass
public boolean isClass()Description copied from interface:CtTypeInformation
Returns true if this type is a class. Returns false for others (enum, interface, generics, annotation).- Specified by:
isClass
in interfaceCtTypeInformation
-
isInterface
public boolean isInterface()Description copied from interface:CtTypeInformation
Returns true if this type is an interface.- Specified by:
isInterface
in interfaceCtTypeInformation
-
isAnnotationType
public boolean isAnnotationType()Description copied from interface:CtTypeInformation
Returns true if this type is an annotation type.- Specified by:
isAnnotationType
in interfaceCtTypeInformation
-
isEnum
public boolean isEnum()Description copied from interface:CtTypeInformation
Returns true if this type is an enum.- Specified by:
isEnum
in interfaceCtTypeInformation
-
canAccess
Description copied from interface:CtTypeReference
Checks visibility based on public, protected, package protected and private modifiers of type- Specified by:
canAccess
in interfaceCtTypeReference<T>
- Returns:
- true if this can access that type
-
canAccess
- Specified by:
canAccess
in interfaceCtTypeReference<T>
- Returns:
- true if this type can access that the `typeMember` in another type based on public, protected, package protected and private modifiers.
-
getTopLevelType
Description copied from interface:CtTypeReference
Returns this, or top level type of this, if this is an inner type- Specified by:
getTopLevelType
in interfaceCtTypeReference<T>
-
getAccessType
Description copied from interface:CtTypeReference
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.- Specified by:
getAccessType
in interfaceCtTypeReference<T>
- Returns:
- type reference which can be used to access this type in scope of contextType.
-
isShadow
public boolean isShadow()Description copied from interface:CtShadowable
When an element isn't present in the factory (created in another factory), this element is considered as "shadow". e.g., a shadow element can be a CtType of java.lang.Class built when we callCtTypeReference.getTypeDeclaration()
on a reference of java.lang.Class.- Specified by:
isShadow
in interfaceCtShadowable
- Returns:
- true if the element is a shadow element, otherwise false.
-
setShadow
Description copied from interface:CtShadowable
Marks an element as shadow. To know what is a shadow element, see the javadoc ofCtShadowable.isShadow()
.- Specified by:
setShadow
in interfaceCtShadowable
-
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
- Specified by:
clone
in interfaceCtTypeReference<T>
- Overrides:
clone
in classCtReferenceImpl
-
getTypeParameterDeclaration
Description copied from interface:CtTypeReference
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" (whileCtTypeReference.getDeclaration()
returns the "T" of Cat).class Dog<X>{} class Cat<T> { Dog<T> dog; }
- Specified by:
getTypeParameterDeclaration
in interfaceCtTypeReference<T>
-
isGenerics
public boolean isGenerics()Description copied from interface:CtTypeInformation
Returns true if it is not a concrete, resolvable class, it if refers to a type parameter directly or indirectly. Direct: "T foo" isGenerics returns true. Indirect: List<T>, or Set<List<T>> isGenerics returns true- Specified by:
isGenerics
in interfaceCtTypeInformation
-
isParameterized
public boolean isParameterized()Description copied from interface:CtTypeInformation
Returns true if it has any type parameter (generic or not).- Specified by:
isParameterized
in interfaceCtTypeInformation
-
getTypeErasure
- Specified by:
getTypeErasure
in interfaceCtTypeInformation
- Returns:
- the type erasure, which is computed by the java compiler to ensure that no new classes are created for parametrized types so that generics incur no runtime overhead. See https://docs.oracle.com/javase/tutorial/java/generics/erasure.html
-
isSimplyQualified
public boolean isSimplyQualified()- Specified by:
isSimplyQualified
in interfaceCtTypeReference<T>
- Returns:
- false then fully qualified name is printed. true then type simple name is printed.
-
setSimplyQualified
- Specified by:
setSimplyQualified
in interfaceCtTypeReference<T>
- Parameters:
isSimplyQualified
- false then the reference is printed fully qualified name. true then only the type name is printed.
-
isArray
public boolean isArray()- Specified by:
isArray
in interfaceCtTypeInformation
- Returns:
- true if this represents an array e.g. Object[] or int[]
-