Interface CtType<T>

    • Field Detail

      • INNERTTYPE_SEPARATOR

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

        static final java.lang.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 Detail

      • getSimpleName

        java.lang.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

        java.util.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

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

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

        java.util.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​(java.lang.String name)
        Gets a nested type from its name.
      • getNestedTypes

        java.util.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

        java.lang.String toStringWithImports()
        Gets the entire class code with package and imports.
      • 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​(java.util.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​(java.util.Set<CtType<?>> nestedTypes)
        Sets all nested types.
      • getAllMethods

        java.util.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,
                                  java.lang.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​(java.lang.String name,
                                  CtTypeReference<?>... parameterTypes)
        Gets a method from its name and parameter types.
        Returns:
        null if does not exit
      • getMethods

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

        java.util.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

        java.util.List<CtMethod<?>> getMethodsByName​(java.lang.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​(java.util.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​(java.util.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

        java.util.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​(java.util.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.