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

        @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
        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

        @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
        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

        @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
        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

        @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
        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)
      • 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)
      • getNodeLists

        @Generated("com.github.javaparser.generator.core.node.GetNodeListsGenerator")
        public List<NodeList<?>> getNodeLists()
        Description copied from class: Node
        The list of NodeLists owned by this node.
        Overrides:
        getNodeLists in class Node
      • remove

        @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
        public boolean remove​(Node node)
        Overrides:
        remove in class Node
      • removePackageDeclaration

        @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
        public CompilationUnit removePackageDeclaration()
      • getModule

        @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
        public Optional<ModuleDeclaration> getModule()
        Returns:
        the module declared in this compilation unit.
      • removeModule

        @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
        public CompilationUnit removeModule()
      • 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.
      • replace

        @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
        public boolean replace​(Node node,
                               Node replacementNode)
        Overrides:
        replace in class Node