Class CompilationUnit

java.lang.Object
com.github.javaparser.ast.Node
com.github.javaparser.ast.CompilationUnit
All Implemented Interfaces:
NodeWithRange<Node>, NodeWithTokenRange<Node>, Observable, Visitable, HasParentNode<Node>, Cloneable

public class CompilationUnit extends Node

This class represents the entire compilation unit. Each java file denotes a compilation unit.

A compilation unit start with an optional package declaration, followed by zero or more import declarations, followed by zero or more type declarations.
Author:
Julio Vilmar Gesser
See Also:
PackageDeclaration, ImportDeclaration, TypeDeclaration, CompilationUnit.Storage
  • Constructor Details

  • Method Details

    • 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)
    • printer

      public CompilationUnit printer(Printer printer)
      Declare a specific printer
    • getPrinter

      protected Printer getPrinter()
      Overrides:
      getPrinter in class Node
    • getPrinter

      protected Printer getPrinter(PrinterConfiguration config)
      Overrides:
      getPrinter in class Node
    • getComments

      @Deprecated public List<Comment> getComments()
      Deprecated.
      getComments was a too generic name and it could be confused with getComment or getAllContainedComments Use getAllComments() instead
    • getAllComments

      public List<Comment> getAllComments()
      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
    • getImport

      public ImportDeclaration getImport(int i)
    • 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
    • getTypes

      public NodeList<TypeDeclaration<?>> getTypes()
      Return the list of top level types declared in this compilation unit.
      If there are no types declared, none is returned.
      Returns:
      the list of types or none null if there is no type
      See Also:
      AnnotationDeclaration, ClassOrInterfaceDeclaration, EnumDeclaration
    • 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
    • setImport

      public CompilationUnit setImport(int i, ImportDeclaration imports)
    • addImport

      public CompilationUnit addImport(ImportDeclaration importDeclaration)
      adds an import if not implicitly imported by java (i.e. java.lang) or added before. Asterisk imports overrule the other imports within the same package.
      Parameters:
      importDeclaration -
      Returns:
      this
    • 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
    • setTypes

      public CompilationUnit setTypes(NodeList<TypeDeclaration<?>> types)
      Sets the list of types declared in this compilation unit.
    • setType

      public CompilationUnit setType(int i, TypeDeclaration<?> type)
    • addType

      public CompilationUnit addType(TypeDeclaration<?> type)
    • 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)
      Add an import to the list of ImportDeclaration of this compilation unit
      shorthand for addImport(String, boolean, boolean) with name,false,false
      Parameters:
      name - the import name
      Returns:
      this, the CompilationUnit
    • addImport

      public CompilationUnit addImport(Class<?> clazz)
      Add an import to the list of ImportDeclaration of this compilation unit
      shorthand for addImport(String) with clazz.getName()
      Parameters:
      clazz - the class to import
      Returns:
      this, the CompilationUnit
      Throws:
      IllegalArgumentException - if clazz is an anonymous or local class
    • 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.Keyword... 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.Keyword... 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.Keyword... 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.Keyword... 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)
    • getLocalDeclarationFromClassname

      public List<ClassOrInterfaceDeclaration> getLocalDeclarationFromClassname(String className)
      Try to get all local class declarations ending by its name (top level or inner class)
      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()
    • getModule

      public Optional<ModuleDeclaration> getModule()
      Returns:
      the module declared in this compilation unit.
    • setModule

      public CompilationUnit setModule(ModuleDeclaration module)
    • removeModule

      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.
    • setStorage

      public CompilationUnit setStorage(Path path)
    • setStorage

      public CompilationUnit setStorage(Path path, Charset charset)
    • setModule

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

      public void recalculatePositions()
      Recalculates the ranges of all nodes by looking at the sizes of the tokens. This is useful when you have manually inserted or deleted tokens and still want to use the ranges.
    • clone

      public CompilationUnit clone()
      Overrides:
      clone in class Node
    • getMetaModel

      public CompilationUnitMetaModel getMetaModel()
      Overrides:
      getMetaModel in class Node
      Returns:
      get JavaParser specific node introspection information.
    • replace

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