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:
-
Field Summary
Fields inherited from class spoon.support.reflect.reference.CtReferenceImpl
simplenameFields inherited from class spoon.support.reflect.declaration.CtElementImpl
ERROR_MESSAGE_TO_STRING, factory, LOGGER, parentFields inherited from interface spoon.reflect.reference.CtTypeReference
NULL_TYPE_NAME, OMITTED_TYPE_ARG_NAME -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAccepts 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).booleancanAccess(CtTypeMember typeMember) booleancanAccess(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 AnnotatedElementGets 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 ornullif 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 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 theCtTypethat 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()).This method returns a reference to the erased type.If this type reference is used as a type argument (seeCtActualTypeContainer.getActualTypeArguments()), returns the type parameter declaration in the target type, returns null otherwise.booleanReturns true if this type is an annotation type.booleanReturntrueif the referenced type is an anonymous typebooleanisArray()Returns true if this type represents an array likeObject[]orint[].booleanisClass()Returns true if this type is a class.booleanisEnum()Returns true if this type is an enum.booleanReturns true if it is not a concrete, resolvable class, it if refers to a type parameter directly or indirectly.booleanReturns true if this type is an interface.booleanReturnstrueif the referenced type is declared in an executable.booleanReturns true if it has any type parameter (generic or not).booleanChecks if the referenced type is a primitive type.booleanisShadow()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.booleanbooleanisSubtypeOf(CtTypeReference<?> type) Checks if this type is a subtype of the given type.booleanremoveActualTypeArgument(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, setSimpleNameMethods 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, updateAllParentsBelowMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods 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, updateAllParentsBelowMethods inherited from interface spoon.reflect.visitor.chain.CtQueryable
filterChildren, map, mapMethods inherited from interface spoon.reflect.reference.CtReference
setComments, setSimpleNameMethods inherited from interface spoon.reflect.reference.CtTypeReference
getSimpleNameMethods inherited from interface spoon.processing.FactoryAccessor
getFactory, setFactory
-
Constructor Details
-
CtTypeReferenceImpl
public CtTypeReferenceImpl()
-
-
Method Details
-
accept
Description copied from interface:CtVisitableAccepts a visitor- Specified by:
acceptin interfaceCtVisitable
-
box
Description copied from interface:CtTypeReferenceReturns the corresponding non-primitive type for a primitive type (the same type otherwise).- Specified by:
boxin interfaceCtTypeReference<T>
-
getActualClass
Description copied from interface:CtTypeReferenceGets the Java runtime class of the referenced type. This is a low-level feature, it should never been used, useCtTypeReference.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):-
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
- Specified by:
getActualClassin interfaceCtTypeReference<T>- Returns:
- the Java class or throws a
SpoonClassNotFoundExceptionif the class is not found.
-
it will return the version of the class your
-
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:CtActualTypeContainerGets the type arguments.- Specified by:
getActualTypeArgumentsin interfaceCtActualTypeContainer
-
getActualAnnotatedElement
- Specified by:
getActualAnnotatedElementin classCtReferenceImpl
-
getDeclaration
Description copied from interface:CtTypeReferenceReturns theCtType, that corresponds to the reference ornullif 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:
getDeclarationin interfaceCtReference- Specified by:
getDeclarationin interfaceCtTypeReference<T>- Returns:
- the referenced element or
nullif the type declaration is not the analyzed source files.
-
getTypeDeclaration
Description copied from interface:CtTypeReferenceReturns theCtTypethat 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:
getTypeDeclarationin 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:CtTypeReferenceGets the type that declares the referenced type.- Specified by:
getDeclaringTypein interfaceCtTypeReference<T>- Returns:
- the declaring type if this references an inner class; null in other cases
-
getPackage
Description copied from interface:CtTypeReferenceGets the package of the referenced type.- Specified by:
getPackagein interfaceCtTypeReference<T>- Returns:
- the declaring package or null if this if a inner class
-
getQualifiedName
Description copied from interface:CtTypeInformationReturns the fully qualified name of this type declaration.- Specified by:
getQualifiedNamein interfaceCtTypeInformation
-
isPrimitive
public boolean isPrimitive()Description copied from interface:CtTypeInformationChecks if the referenced type is a primitive type.It is a primitive type, if it is one of the following types:
- byte
- short
- int
- long
- float
- double
- boolean
- char
- void
For boxed types like
Integerthis method returnsfalse.- Specified by:
isPrimitivein interfaceCtTypeInformation- Returns:
trueif the referenced type is a primitive type
-
isSubtypeOf
Description copied from interface:CtTypeInformationChecks if this type is a subtype of the given type.- Specified by:
isSubtypeOfin interfaceCtTypeInformation- Parameters:
type- the type that might be a parent of this type.- Returns:
trueif the referenced type is a subtype of the given type, otherwisefalse. If this type is the same as the given type (typeX.isSubtypeOf(typeX)), this method returnstrue.
-
setActualTypeArguments
public <C extends CtActualTypeContainer> C setActualTypeArguments(List<? extends CtTypeReference<?>> actualTypeArguments) Description copied from interface:CtActualTypeContainerSets the type arguments.- Specified by:
setActualTypeArgumentsin interfaceCtActualTypeContainer
-
setDeclaringType
Description copied from interface:CtTypeReferenceSets the reference to the declaring type. Should be set to null if the referenced type is not a inner type.- Specified by:
setDeclaringTypein interfaceCtTypeReference<T>
-
setPackage
Description copied from interface:CtTypeReferenceSets the reference to the declaring package.- Specified by:
setPackagein interfaceCtTypeReference<T>
-
asCtIntersectionTypeReference
Description copied from interface:CtTypeReferenceCasts the type reference inCtIntersectionTypeReference.- Specified by:
asCtIntersectionTypeReferencein interfaceCtTypeReference<T>
-
unbox
Description copied from interface:CtTypeReferenceReturns the primitive type for a boxing type (unchanged if the type does not correspond to a boxing type).- Specified by:
unboxin interfaceCtTypeReference<T>
-
getDeclaredFields
Description copied from interface:CtTypeInformationGets the fields declared by this type.- Specified by:
getDeclaredFieldsin interfaceCtTypeInformation
-
getDeclaredField
Description copied from interface:CtTypeInformationGets a field from its name.- Specified by:
getDeclaredFieldin interfaceCtTypeInformation- Returns:
- a reference to the field with the name or
nullif it does not exist
-
getDeclaredOrInheritedField
Description copied from interface:CtTypeInformationGets a field from this type or any super type or any implemented interface by field name.- Specified by:
getDeclaredOrInheritedFieldin interfaceCtTypeInformation- Returns:
- a reference to the field with the name or
nullif it does not exist
-
getDeclaredExecutables
Description copied from interface:CtTypeInformationGets the executables declared by this type if applicable.- Specified by:
getDeclaredExecutablesin interfaceCtTypeInformation
-
getAllFields
Description copied from interface:CtTypeInformationGets the fields declared by this type and by all its supertypes if applicable.- Specified by:
getAllFieldsin interfaceCtTypeInformation
-
getAllExecutables
Description copied from interface:CtTypeInformationGets 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:
getAllExecutablesin interfaceCtTypeInformation
-
getModifiers
Description copied from interface:CtTypeInformationGets modifiers of this type.- Specified by:
getModifiersin interfaceCtTypeInformation- Specified by:
getModifiersin interfaceCtTypeReference<T>
-
getSuperclass
Description copied from interface:CtTypeInformationReturns a reference to the type directly extended by this type.To get the
CtTypeof the super class, useCtTypeReference.getDeclaration()orCtTypeReference.getTypeDeclaration()on theCtTypeReferencereturned by this method.- Specified by:
getSuperclassin interfaceCtTypeInformation- Specified by:
getSuperclassin interfaceCtTypeReference<T>- Returns:
- the type explicitly extended by this type, or
nullif there is none or if the super type is not in the classpath (in noclasspath mode). If a class does not explicitly extend another classnullis returned (notObject). For types like enums that implicitly extend a superclass likeEnum, this method returns that class.
-
getSuperInterfaces
Description copied from interface:CtTypeInformationReturns the interface types directly implemented by this class or extended by this interface.- Specified by:
getSuperInterfacesin interfaceCtTypeInformation- Specified by:
getSuperInterfacesin interfaceCtTypeReference<T>
-
isAnonymous
public boolean isAnonymous()Description copied from interface:CtTypeInformationReturntrueif the referenced type is an anonymous type- Specified by:
isAnonymousin interfaceCtTypeInformation
-
isLocalType
public boolean isLocalType()Description copied from interface:CtTypeInformationReturnstrueif the referenced type is declared in an executable. e.g. a type declared in a method or a lambda. This corresponds toisLocalClassofjava.lang.Class.// Type declared in a method. public void make() { class Cook { } } // Type declared in a lambda. s -> { class Cook { } }- Specified by:
isLocalTypein interfaceCtTypeInformation
-
addActualTypeArgument
public <C extends CtActualTypeContainer> C addActualTypeArgument(CtTypeReference<?> actualTypeArgument) Description copied from interface:CtActualTypeContainerAdds a type argument.- Specified by:
addActualTypeArgumentin interfaceCtActualTypeContainer
-
removeActualTypeArgument
Description copied from interface:CtActualTypeContainerRemoves a type argument.- Specified by:
removeActualTypeArgumentin interfaceCtActualTypeContainer
-
isClass
public boolean isClass()Description copied from interface:CtTypeInformationReturns true if this type is a class. Returns false for others (enum, interface, generics, annotation).- Specified by:
isClassin interfaceCtTypeInformation
-
isInterface
public boolean isInterface()Description copied from interface:CtTypeInformationReturns true if this type is an interface.- Specified by:
isInterfacein interfaceCtTypeInformation
-
isAnnotationType
public boolean isAnnotationType()Description copied from interface:CtTypeInformationReturns true if this type is an annotation type.- Specified by:
isAnnotationTypein interfaceCtTypeInformation
-
isEnum
public boolean isEnum()Description copied from interface:CtTypeInformationReturns true if this type is an enum.- Specified by:
isEnumin interfaceCtTypeInformation
-
canAccess
Description copied from interface:CtTypeReferenceChecks visibility based on public, protected, package protected and private modifiers of type- Specified by:
canAccessin interfaceCtTypeReference<T>- Returns:
- true if this can access that type
-
canAccess
- Specified by:
canAccessin 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:CtTypeReferenceReturns this, or top level type of this, if this is an inner type- Specified by:
getTopLevelTypein interfaceCtTypeReference<T>
-
getAccessType
Description copied from interface:CtTypeReferenceComputes 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:
getAccessTypein 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:CtShadowableWhen 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:
isShadowin interfaceCtShadowable- Returns:
- true if the element is a shadow element, otherwise false.
-
setShadow
Description copied from interface:CtShadowableMarks an element as shadow. To know what is a shadow element, see the javadoc ofCtShadowable.isShadow().- Specified by:
setShadowin interfaceCtShadowable
-
clone
Description copied from interface:CtElementClone 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:
clonein interfaceCtElement- Specified by:
clonein interfaceCtReference- Specified by:
clonein interfaceCtTypeReference<T>- Overrides:
clonein classCtReferenceImpl- Returns:
- a clone of this element. All children are cloned, but the parent of the returned clone is set to null.
-
getTypeParameterDeclaration
Description copied from interface:CtTypeReferenceIf 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:
getTypeParameterDeclarationin interfaceCtTypeReference<T>
-
isGenerics
public boolean isGenerics()Description copied from interface:CtTypeInformationReturns 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:
isGenericsin interfaceCtTypeInformation
-
isParameterized
public boolean isParameterized()Description copied from interface:CtTypeInformationReturns true if it has any type parameter (generic or not).- Specified by:
isParameterizedin interfaceCtTypeInformation
-
getTypeErasure
Description copied from interface:CtTypeInformationThis method returns a reference to the erased type.For example, this will return
ListforList<String>, orEnumfor the type parameterEin the enum declaration.- Specified by:
getTypeErasurein interfaceCtTypeInformation- Returns:
- a reference to the erased type
- See Also:
-
isSimplyQualified
public boolean isSimplyQualified()- Specified by:
isSimplyQualifiedin interfaceCtTypeReference<T>- Returns:
- false then fully qualified name is printed. true then type simple name is printed.
-
setSimplyQualified
- Specified by:
setSimplyQualifiedin 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()Description copied from interface:CtTypeInformationReturns true if this type represents an array likeObject[]orint[].- Specified by:
isArrayin interfaceCtTypeInformation- Returns:
- true if this type represents an array like
Object[]orint[]
-