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

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 Details

    • INNERTTYPE_SEPARATOR

      static final String INNERTTYPE_SEPARATOR
      The string separator in a Java innertype qualified name.
      See Also:
      Constant Field Values
    • NAME_UNKNOWN

      static final String 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 interface CtNamedElement
    • getUsedTypes

      Set<CtTypeReference<?>> getUsedTypes(boolean includeSamePackage)
      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

      Class<T> getActualClass()
      Deprecated.
      (since Spoon 7.0.0) this will be removed from the public API
    • getField

      CtField<?> getField(String name)
      Gets a field from its name.
      Returns:
      null if does not exit
    • getFields

      List<CtField<?>> getFields()
      Returns the fields that are directly declared by this class or interface. Includes enum constants. Derived from getTypeMembers()
    • getNestedType

      <N extends CtType<?>> N getNestedType(String name)
      Gets a nested type from its name.
    • getNestedTypes

      Set<CtType<?>> 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 interface CtNamedElement
    • isTopLevel

      boolean isTopLevel()
      Returns true if this type is top-level (declared as the main type in a file).
    • addFieldAtTop

      <F,​ C extends CtType<T>> C addFieldAtTop(CtField<F> field)
      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

      <F,​ C extends CtType<T>> C addField(CtField<F> field)
      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

      <F,​ C extends CtType<T>> C addField(int index, CtField<F> field)
      add a field at a given position.
      Parameters:
      field -
      Returns:
      true if this element changed as a result of the call
    • setFields

      <C extends CtType<T>> C setFields(List<CtField<?>> fields)
      Sets all fields in the type.
    • removeField

      <F> boolean removeField(CtField<F> field)
      remove a Field
      Parameters:
      field -
      Returns:
      true if this element changed as a result of the call
    • addNestedType

      <N,​ C extends CtType<T>> C addNestedType(CtType<N> nestedType)
      Add a nested type.
      Parameters:
      nestedType -
      Returns:
      true if this element changed as a result of the call
    • removeNestedType

      <N> boolean removeNestedType(CtType<N> nestedType)
      Remove a nested type.
      Parameters:
      nestedType -
      Returns:
      true if this element changed as a result of the call
    • setNestedTypes

      <C extends CtType<T>> C setNestedTypes(Set<CtType<?>> nestedTypes)
      Sets all nested types.
    • compileAndReplaceSnippets

      void compileAndReplaceSnippets()
      Replace all the code snippets that are found in this type by the corresponding Spoon AST.
      See Also:
      CtCodeSnippet, CtCodeSnippetExpression, CtCodeSnippetStatement
    • getAllMethods

      Set<CtMethod<?>> 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

      <R> CtMethod<R> getMethod(String name, CtTypeReference<?>... parameterTypes)
      Gets a method from its name and parameter types.
      Returns:
      null if does not exit
    • getMethods

      Set<CtMethod<?>> getMethods()
      Returns the methods that are directly declared by this class or interface. Derived from getTypeMembers()
    • getMethodsAnnotatedWith

      Set<CtMethod<?>> getMethodsAnnotatedWith(CtTypeReference<?>... annotationTypes)
      Returns the methods that are directly declared by this class or interface and annotated with one of the given annotations.
    • getMethodsByName

      List<CtMethod<?>> getMethodsByName(String name)
      Returns the methods that are directly declared by this class or interface and that have the given name.
    • hasMethod

      boolean hasMethod(CtMethod<?> method)
      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

      <C extends CtType<T>> C setMethods(Set<CtMethod<?>> methods)
      Sets the methods of this type.
    • addMethod

      <M,​ C extends CtType<T>> C addMethod(CtMethod<M> method)
      Adds a method to this type.
    • removeMethod

      <M> boolean removeMethod(CtMethod<M> method)
      Removes a method from this type.
    • setSuperclass

      <C extends CtType<T>> C setSuperclass(CtTypeReference<?> superClass)
      Sets the superclass type.
    • setSuperInterfaces

      <C extends CtType<T>> C setSuperInterfaces(Set<CtTypeReference<?>> interfaces)
      Sets the super interfaces of this type.
    • addSuperInterface

      <S,​ C extends CtType<T>> C addSuperInterface(CtTypeReference<S> interfac)
      Parameters:
      interfac -
      Returns:
      true if this element changed as a result of the call
    • removeSuperInterface

      <S> boolean removeSuperInterface(CtTypeReference<S> interfac)
      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

      <C extends CtType<T>> C addTypeMember(CtTypeMember member)
      Adds a type member at the end of all type member of the type.
    • addTypeMemberAt

      <C extends CtType<T>> C addTypeMemberAt(int position, CtTypeMember member)
      Adds a type member at a given position. Think to use this method if the order is important for you.
    • removeTypeMember

      boolean removeTypeMember(CtTypeMember member)
      Removes the type member.
    • setTypeMembers

      <C extends CtType<T>> C setTypeMembers(List<CtTypeMember> members)
      Removes all types members with these new members.
    • clone

      CtType<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 CtNamedElement
    • 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.