Package spoon.reflect.declaration
Interface CtType<T>
- All Superinterfaces:
Cloneable
,CtElement
,CtFormalTypeDeclarer
,CtModifiable
,CtNamedElement
,CtQueryable
,CtShadowable
,CtTypeInformation
,CtTypeMember
,CtVisitable
,FactoryAccessor
,Serializable
,SourcePositionHolder
- All Known Subinterfaces:
CtAnnotationType<T>
,CtClass<T>
,CtEnum<T>
,CtInterface<T>
,CtRecord
,CtTypeParameter
- All Known Implementing Classes:
CtAnnotationTypeImpl
,CtClassImpl
,CtEnumImpl
,CtInterfaceImpl
,CtRecordImpl
,CtTypeImpl
,CtTypeParameterImpl
public interface CtType<T>
extends CtNamedElement, CtTypeInformation, CtTypeMember, CtFormalTypeDeclarer, CtShadowable
This abstract element defines a super-type for classes and interfaces, which
can declare methods.
The type parameter T refers to the actual class that this type represents.
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionadd a field at a given position.add a field at the end of the field list.addFieldAtTop(CtField<F> field)
Adds a field at the top of the type (before static block).Adds a method to this type.addNestedType(CtType<N> nestedType)
Add a nested type.addSuperInterface(CtTypeReference<S> interfac)
addTypeMember(CtTypeMember member)
Adds a type member at the end of all type member of the type.addTypeMemberAt(int position, CtTypeMember member)
Adds a type member at a given position.clone()
Clone the element which calls this method in a new object.void
Replace all the code snippets that are found in this type by the corresponding Spoon AST.CtType<?>
copyType()
Copy the type, where copy means cloning + porting all the references in the clone from the old type to the new type.This is a low-level feature, it should never been used by non advanced users.Return all the methods that can be called on an instance of this type.CtField<?>
Gets a field from its name.Returns the fields that are directly declared by this class or interface.<R> CtMethod<R>
getMethod(String name, CtTypeReference<?>... parameterTypes)
Gets a method from its name and parameter types.<R> CtMethod<R>
getMethod(CtTypeReference<R> returnType, String name, CtTypeReference<?>... parameterTypes)
Gets a method from its return type, name, and parameter types.Returns the methods that are directly declared by this class or interface.getMethodsAnnotatedWith(CtTypeReference<?>... annotationTypes)
Returns the methods that are directly declared by this class or interface and annotated with one of the given annotations.getMethodsByName(String name)
Returns the methods that are directly declared by this class or interface and that have the given name.<N extends CtType<?>>
NgetNestedType(String name)
Gets a nested type from its name.Returns the declarations of the nested classes and interfaces that are directly declared by this class or interface.Gets the package where this type is declared.Returns the corresponding type reference.Returns the simple (unqualified) name of this element.Gets all type members of the type like fields, methods, anonymous block, etc.Set<CtTypeReference<?>>
getUsedTypes(boolean includeSamePackage)
Returns the types used by this type.boolean
Searches in the type for the given method.boolean
Returns true if this type is top-level (declared as the main type in a file).<F> boolean
removeField(CtField<F> field)
remove a Field<M> boolean
removeMethod(CtMethod<M> method)
Removes a method from this type.<N> boolean
removeNestedType(CtType<N> nestedType)
Remove a nested type.<S> boolean
removeSuperInterface(CtTypeReference<S> interfac)
boolean
removeTypeMember(CtTypeMember member)
Removes the type member.Sets all fields in the type.setMethods(Set<CtMethod<?>> methods)
Sets the methods of this type.setNestedTypes(Set<CtType<?>> nestedTypes)
Sets all nested types.setSuperclass(CtTypeReference<?> superClass)
Sets the superclass type.setSuperInterfaces(Set<CtTypeReference<?>> interfaces)
Sets the super interfaces of this type.setTypeMembers(List<CtTypeMember> members)
Removes all types members with these new members.Gets the entire class code with package and imports.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, setComments, setDocComment, setImplicit, setParent, setPosition, setPositions, setValueByRole, toString, toStringDebug, updateAllParentsBelow
Methods inherited from interface spoon.reflect.declaration.CtFormalTypeDeclarer
addFormalCtTypeParameter, addFormalCtTypeParameterAt, getFormalCtTypeParameters, removeFormalCtTypeParameter, setFormalCtTypeParameters
Methods inherited from interface spoon.reflect.declaration.CtModifiable
addModifier, getExtendedModifiers, getModifiers, getVisibility, hasModifier, isAbstract, isFinal, isNative, isPrivate, isProtected, isPublic, isStatic, isStrictfp, isSynchronized, isTransient, isVolatile, removeModifier, setExtendedModifiers, setModifiers, setVisibility
Methods inherited from interface spoon.reflect.declaration.CtNamedElement
setSimpleName
Methods inherited from interface spoon.reflect.visitor.chain.CtQueryable
filterChildren, map, map
Methods inherited from interface spoon.reflect.declaration.CtShadowable
isShadow, setShadow
Methods inherited from interface spoon.reflect.declaration.CtTypeInformation
getAllExecutables, getAllFields, getDeclaredExecutables, getDeclaredField, getDeclaredFields, getDeclaredOrInheritedField, getModifiers, getQualifiedName, getSuperclass, getSuperInterfaces, getTypeErasure, isAnnotationType, isAnonymous, isArray, isClass, isEnum, isGenerics, isInterface, isLocalType, isParameterized, isPrimitive, isSubtypeOf
Methods inherited from interface spoon.reflect.declaration.CtTypeMember
getDeclaringType, getTopLevelType
Methods inherited from interface spoon.reflect.visitor.CtVisitable
accept
Methods inherited from interface spoon.processing.FactoryAccessor
getFactory, setFactory
-
Field Details
-
INNERTTYPE_SEPARATOR
The string separator in a Java innertype qualified name.- See Also:
- Constant Field Values
-
NAME_UNKNOWN
Used in no classpath when we don't have any information to build the name of the type.- 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 interfaceCtNamedElement
-
getUsedTypes
Returns the types used by this type.- Parameters:
includeSamePackage
- set to true if the method should return also the types located in the same package as the current type
-
getActualClass
This is a low-level feature, it should never been used by non advanced users. See full documentation atCtTypeReference.getActualClass()
. -
getField
Gets a field from its name.- Returns:
- null if does not exit
-
getFields
Returns the fields that are directly declared by this class or interface. Includes enum constants. Derived fromgetTypeMembers()
-
getNestedType
Gets a nested type from its name. -
getNestedTypes
Returns the declarations of the nested classes and interfaces that are directly declared by this class or interface. -
getPackage
CtPackage getPackage()Gets the package where this type is declared. -
toStringWithImports
String toStringWithImports()Gets the entire class code with package and imports. -
getReference
CtTypeReference<T> getReference()Returns the corresponding type reference. Overrides the return type.- Specified by:
getReference
in interfaceCtNamedElement
-
isTopLevel
boolean isTopLevel()Returns true if this type is top-level (declared as the main type in a file). -
addFieldAtTop
Adds a field at the top of the type (before static block). Note that the position of these field will be negative to be written at the top of the type.- Parameters:
field
-- Returns:
- true if the field is added.
-
addField
add a field at the end of the field list.- Parameters:
field
-- Returns:
- true if this element changed as a result of the call
-
addField
add a field at a given position.- Parameters:
field
-- Returns:
- true if this element changed as a result of the call
-
setFields
Sets all fields in the type. -
removeField
remove a Field- Parameters:
field
-- Returns:
- true if this element changed as a result of the call
-
addNestedType
Add a nested type.- Parameters:
nestedType
-- Returns:
- true if this element changed as a result of the call
-
removeNestedType
Remove a nested type.- Parameters:
nestedType
-- Returns:
- true if this element changed as a result of the call
-
setNestedTypes
Sets all nested types. -
compileAndReplaceSnippets
void compileAndReplaceSnippets()Replace all the code snippets that are found in this type by the corresponding Spoon AST. -
getAllMethods
Return all the methods that can be called on an instance of this type. It recursively collects all methods (both concrete and abstract) from all super-classes and all super-interfaces. It deduplicates methods with the same signature, which are defined several times somewhere in the type hierarchy. Warning: this method can be really slow due to signature deduplication. It includes all methods: the methods of types whose source code is in the Spoon model, the methods of types from the JDK and from libraries present in the classpath, the methods of java.lang.Object (for all CtClass objects). However, in noclasspath mode, it does not include methods from unknown types. If methods are overridden somewhere in the type hierarchy, it returns only top methods (ie method definitions). -
getMethod
<R> CtMethod<R> getMethod(CtTypeReference<R> returnType, String name, CtTypeReference<?>... parameterTypes)Gets a method from its return type, name, and parameter types.- Returns:
- null if does not exit
-
getMethod
Gets a method from its name and parameter types.- Returns:
- null if does not exit
-
getMethods
Returns the methods that are directly declared by this class or interface. Derived fromgetTypeMembers()
-
getMethodsAnnotatedWith
Returns the methods that are directly declared by this class or interface and annotated with one of the given annotations. -
getMethodsByName
Returns the methods that are directly declared by this class or interface and that have the given name. -
hasMethod
Searches in the type for the given method. Super classes and implemented interfaces are considered. The matching criterion is that the signatures are identical.- Parameters:
method
- The method to search for in the class.- Returns:
- True: the type has the given method. False otherwise.
-
setMethods
Sets the methods of this type. -
addMethod
Adds a method to this type. -
removeMethod
Removes a method from this type. -
setSuperclass
Sets the superclass type. -
setSuperInterfaces
Sets the super interfaces of this type. -
addSuperInterface
- Parameters:
interfac
-- Returns:
- true if this element changed as a result of the call
-
removeSuperInterface
- Parameters:
interfac
-- Returns:
- true if this element changed as a result of the call
-
getTypeMembers
List<CtTypeMember> getTypeMembers()Gets all type members of the type like fields, methods, anonymous block, etc. -
addTypeMember
Adds a type member at the end of all type member of the type. -
addTypeMemberAt
Adds a type member at a given position. Think to use this method if the order is important for you. -
removeTypeMember
Removes the type member. -
setTypeMembers
Removes all types members with these new members. -
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 interfaceCtNamedElement
-
copyType
CtType<?> copyType()Copy the type, where copy means cloning + porting all the references in the clone from the old type to the new type. The copied type is added to the same package (and this to the factory as well). A new unique method name is given for each copy, and this method can be called several times.
-