Class CompilationUnit

    • Method Detail

      • accept

        public <R,​A> R accept​(GenericVisitor<R,​A> v,
                                    A arg)
        Description copied from interface: Visitable
        Accept method for visitor support.
        Type Parameters:
        R - the type of the return value of the visitor
        A - the type the user argument passed to the visitor
        Parameters:
        v - the visitor implementation
        arg - the argument passed to the visitor (of type A)
        Returns:
        the result of the visit (of type R)
      • accept

        public <A> void accept​(VoidVisitor<A> v,
                               A arg)
        Description copied from interface: Visitable
        Accept method for visitor support.
        Type Parameters:
        A - the type the argument passed for the visitor
        Parameters:
        v - the visitor implementation
        arg - any value relevant for the visitor (of type A)
      • getComments

        public List<Comment> getComments()
        Return a list containing all comments declared in this compilation unit. Including javadocs, line comments and block comments of all types, inner-classes and other members.
        If there is no comment, an empty list is returned.
        Returns:
        list with all comments of this compilation unit.
        See Also:
        JavadocComment, LineComment, BlockComment
      • getImports

        public NodeList<ImportDeclaration> getImports()
        Retrieves the list of imports declared in this compilation unit or null if there is no import.
        Returns:
        the list of imports or none if there is no import
      • getPackageDeclaration

        public Optional<PackageDeclaration> getPackageDeclaration()
        Retrieves the package declaration of this compilation unit.
        If this compilation unit has no package declaration (default package), Optional.none() is returned.
        Returns:
        the package declaration or none
      • getType

        public TypeDeclaration<?> getType​(int i)
        Convenience method that wraps getTypes().
        If i is out of bounds, throws IndexOutOfBoundsException.
        Parameters:
        i - the index of the type declaration to retrieve
      • setImports

        public CompilationUnit setImports​(NodeList<ImportDeclaration> imports)
        Sets the list of imports of this compilation unit. The list is initially null.
        Parameters:
        imports - the list of imports
      • setPackageDeclaration

        public CompilationUnit setPackageDeclaration​(PackageDeclaration packageDeclaration)
        Sets or clear the package declarations of this compilation unit.
        Parameters:
        packageDeclaration - the packageDeclaration declaration to set or null to default package
      • setPackageDeclaration

        public CompilationUnit setPackageDeclaration​(String name)
        sets the package declaration of this compilation unit
        Parameters:
        name - the name of the package
        Returns:
        this, the CompilationUnit
      • addImport

        public CompilationUnit addImport​(String name,
                                         boolean isStatic,
                                         boolean isAsterisk)
        Add an import to the list of ImportDeclaration of this compilation unit
        This method check if no import with the same name is already in the list
        Parameters:
        name - the import name
        isStatic - is it an "import static"
        isAsterisk - does the import end with ".*"
        Returns:
        this, the CompilationUnit
      • addClass

        public ClassOrInterfaceDeclaration addClass​(String name)
        Add a public class to the types of this compilation unit
        Parameters:
        name - the class name
        Returns:
        the newly created class
      • addClass

        public ClassOrInterfaceDeclaration addClass​(String name,
                                                    Modifier... modifiers)
        Add a class to the types of this compilation unit
        Parameters:
        name - the class name
        modifiers - the modifiers (like Modifier.PUBLIC)
        Returns:
        the newly created class
      • addInterface

        public ClassOrInterfaceDeclaration addInterface​(String name)
        Add a public interface class to the types of this compilation unit
        Parameters:
        name - the interface name
        Returns:
        the newly created class
      • addInterface

        public ClassOrInterfaceDeclaration addInterface​(String name,
                                                        Modifier... modifiers)
        Add an interface to the types of this compilation unit
        Parameters:
        name - the interface name
        modifiers - the modifiers (like Modifier.PUBLIC)
        Returns:
        the newly created class
      • addEnum

        public EnumDeclaration addEnum​(String name)
        Add a public enum to the types of this compilation unit
        Parameters:
        name - the enum name
        Returns:
        the newly created class
      • addEnum

        public EnumDeclaration addEnum​(String name,
                                       Modifier... modifiers)
        Add an enum to the types of this compilation unit
        Parameters:
        name - the enum name
        modifiers - the modifiers (like Modifier.PUBLIC)
        Returns:
        the newly created class
      • addAnnotationDeclaration

        public AnnotationDeclaration addAnnotationDeclaration​(String name)
        Add a public annotation declaration to the types of this compilation unit
        Parameters:
        name - the annotation name
        Returns:
        the newly created class
      • addAnnotationDeclaration

        public AnnotationDeclaration addAnnotationDeclaration​(String name,
                                                              Modifier... modifiers)
        Add an annotation declaration to the types of this compilation unit
        Parameters:
        name - the annotation name
        modifiers - the modifiers (like Modifier.PUBLIC)
        Returns:
        the newly created class
      • getClassByName

        public Optional<ClassOrInterfaceDeclaration> getClassByName​(String className)
        Try to get a top level class declaration by its name
        Parameters:
        className - the class name (case-sensitive)
      • getInterfaceByName

        public Optional<ClassOrInterfaceDeclaration> getInterfaceByName​(String interfaceName)
        Try to get a top level interface declaration by its name
        Parameters:
        interfaceName - the interface name (case-sensitive)
      • getEnumByName

        public Optional<EnumDeclaration> getEnumByName​(String enumName)
        Try to get a top level enum declaration by its name
        Parameters:
        enumName - the enum name (case-sensitive)
      • getPrimaryTypeName

        public Optional<String> getPrimaryTypeName()
        Returns:
        the name that the primary type in this file should have, according to the filename in CompilationUnit.Storage.getFileName(). Empty if no file information is present (when this compilation unit wasn't parsed from a file.)
      • getPrimaryType

        public Optional<TypeDeclaration<?>> getPrimaryType()
        Returns:
        the type whose name corresponds to the file name. Empty if no file information is present (when this compilation unit wasn't parsed from a file.) If for some strange reason there are multiple types of this name, the first one is returned.
      • getAnnotationDeclarationByName

        public Optional<AnnotationDeclaration> getAnnotationDeclarationByName​(String annotationName)
        Try to get a top level annotation type declaration by its name
        Parameters:
        annotationName - the annotation name (case-sensitive)
      • remove

        public boolean remove​(Node node)
        Overrides:
        remove in class Node
      • removePackageDeclaration

        public CompilationUnit removePackageDeclaration()
      • getStorage

        public Optional<CompilationUnit.Storage> getStorage()
        Returns:
        information about where this compilation unit was loaded from, or empty if it wasn't loaded from a file.
      • setModule

        public ModuleDeclaration setModule​(String name)
        Create (or overwrite) a module declaration in this compilation unit with name "name".
        Returns:
        the module
      • replace

        public boolean replace​(Node node,
                               Node replacementNode)
        Overrides:
        replace in class Node