Top Description Inners Fields Constructors Methods
com.github.javaparser.ast

public Class CompilationUnit

extends Node
Class Inheritance
Imports
com.github.javaparser.*, .TokenRange, com.github.javaparser.ast.body.AnnotationDeclaration, .ClassOrInterfaceDeclaration, .EnumDeclaration, .TypeDeclaration, com.github.javaparser.ast.comments.Comment, .JavadocComment, com.github.javaparser.ast.expr.Name, com.github.javaparser.ast.modules.ModuleDeclaration, com.github.javaparser.ast.nodeTypes.NodeWithName, com.github.javaparser.ast.observer.ObservableProperty, com.github.javaparser.ast.visitor.CloneVisitor, .GenericVisitor, .VoidVisitor, com.github.javaparser.metamodel.CompilationUnitMetaModel, .InternalProperty, .JavaParserMetaModel, .OptionalProperty, com.github.javaparser.printer.PrettyPrinter, com.github.javaparser.utils.ClassUtils, .CodeGenerationUtils, .Utils, java.io.IOException, java.nio.charset.Charset, java.nio.file.Files, .Path, .Paths, java.util.List, .Optional, java.util.function.Function, com.github.javaparser.ast.Node, .Generated

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, Storage

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
CompilationUnit.Storage

Information about where this compilation unit was loaded from.

Field Summary

Modifier and TypeField and Description
private NodeList<ImportDeclaration>
private ModuleDeclaration
private PackageDeclaration
private CompilationUnit.Storage
private NodeList<TypeDeclaration<?>>
Inherited from com.github.javaparser.ast.Node:
ABSOLUTE_BEGIN_LINEABSOLUTE_END_LINENODE_BY_BEGIN_POSITIONprettyPrinterNoCommentsConfigurationSYMBOL_RESOLVER_KEY

Constructor Summary

AccessConstructor and Description
public
public
CompilationUnit(String packageDeclaration)

public
public
CompilationUnit(TokenRange tokenRange, PackageDeclaration packageDeclaration, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types, ModuleDeclaration module)

This constructor is used by the parser and is considered private.

Method Summary

Modifier and TypeMethod and Description
public <R, A> R
accept(final GenericVisitor<R, A>
the visitor implementation
v
,
final A
the argument passed to the visitor (of type A)
arg
)

Implements com.github.javaparser.ast.visitor.Visitable.accept.

Accept method for visitor support.
public <A> void
accept(final VoidVisitor<A>
the visitor implementation
v
,
final A
any value relevant for the visitor (of type A)
arg
)

Implements com.github.javaparser.ast.visitor.Visitable.accept.

Accept method for visitor support.
public AnnotationDeclaration

Returns:

the newly created class
addAnnotationDeclaration
(String
the annotation name
name
)

Add a public annotation declaration to the types of this compilation unit

public AnnotationDeclaration

Returns:

the newly created class
addAnnotationDeclaration
(String
the annotation name
name
,
Modifier.Keyword...
the modifiers (like Modifier.PUBLIC)
modifiers
)

Add an annotation declaration to the types of this compilation unit

public ClassOrInterfaceDeclaration

Returns:

the newly created class
addClass
(String
the class name
name
)

Add a public class to the types of this compilation unit

public ClassOrInterfaceDeclaration

Returns:

the newly created class
addClass
(String
the class name
name
,
Modifier.Keyword...
the modifiers (like Modifier.PUBLIC)
modifiers
)

Add a class to the types of this compilation unit

public EnumDeclaration

Returns:

the newly created class
addEnum
(String
the enum name
name
)

Add a public enum to the types of this compilation unit

public EnumDeclaration

Returns:

the newly created class
addEnum
(String
the enum name
name
,
Modifier.Keyword...
the modifiers (like Modifier.PUBLIC)
modifiers
)

Add an enum to the types of this compilation unit

public CompilationUnit
addImport(ImportDeclaration importDeclaration)

public CompilationUnit

Returns:

this, the CompilationUnit
addImport
(String
the import name
name
)

Add an import to the list of ImportDeclaration of this compilation unit shorthand for addImport(String, boolean, boolean) with name,false,false

public CompilationUnit

Returns:

this, the CompilationUnit
addImport
(Class<?>
the class to import
clazz
)

Add an import to the list of ImportDeclaration of this compilation unit shorthand for addImport(String) with clazz.getName()

public CompilationUnit

Returns:

this, the CompilationUnit
addImport
(String
the import name
name
,
boolean
is it an "import static"
isStatic
,
boolean
does the import end with ".*"
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

public ClassOrInterfaceDeclaration

Returns:

the newly created class
addInterface
(String
the interface name
name
)

Add a public interface class to the types of this compilation unit

public ClassOrInterfaceDeclaration

Returns:

the newly created class
addInterface
(String
the interface name
name
,
Modifier.Keyword...
the modifiers (like Modifier.PUBLIC)
modifiers
)

Add an interface to the types of this compilation unit

public CompilationUnit
public CompilationUnit
clone()

Overrides com.github.javaparser.ast.Node.clone.

Creates and returns a copy of this object.
public Optional<AnnotationDeclaration>
getAnnotationDeclarationByName(String
the annotation name (case-sensitive)
annotationName
)

Try to get a top level annotation type declaration by its name

public Optional<ClassOrInterfaceDeclaration>
getClassByName(String
the class name (case-sensitive)
className
)

Try to get a top level class declaration by its name

public List<Comment>

Returns:

list with all comments of this compilation unit.
getComments
()

Return a list containing all comments declared in this compilation unit.

public Optional<EnumDeclaration>
getEnumByName(String
the enum name (case-sensitive)
enumName
)

Try to get a top level enum declaration by its name

public ImportDeclaration
getImport(int i)

public NodeList<ImportDeclaration>

Returns:

the list of imports or none if there is no import
getImports
()

Retrieves the list of imports declared in this compilation unit or null if there is no import.

public Optional<ClassOrInterfaceDeclaration>
getInterfaceByName(String
the interface name (case-sensitive)
interfaceName
)

Try to get a top level interface declaration by its name

public CompilationUnitMetaModel
public Optional<ModuleDeclaration>

Returns:

the module declared in this compilation unit.
getModule
()

public Optional<PackageDeclaration>

Returns:

the package declaration or none
getPackageDeclaration
()

Retrieves the package declaration of this compilation unit.

public Optional<TypeDeclaration<?>>

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.
getPrimaryType
()

public Optional<String>

Returns:

the name that the primary type in this file should have, according to the filename in Storage#getFileName(). Empty if no file information is present (when this compilation unit wasn't parsed from a file.)
getPrimaryTypeName
()

public Optional<CompilationUnit.Storage>

Returns:

information about where this compilation unit was loaded from, or empty if it wasn't loaded from a file.
getStorage
()

public TypeDeclaration<?>
getType(int
the index of the type declaration to retrieve
i
)

Convenience method that wraps getTypes().

public NodeList<TypeDeclaration<?>>

Returns:

the list of types or none null if there is no type
getTypes
()

Return the list of top level types declared in this compilation unit.

public void
recalculatePositions()

Recalculates the ranges of all nodes by looking at the sizes of the tokens.

public boolean
public CompilationUnit
public CompilationUnit
public boolean
replace(Node node, Node replacementNode)

Overrides com.github.javaparser.ast.Node.replace.

public CompilationUnit
setImport(int i, ImportDeclaration imports)

public CompilationUnit
setImports(final NodeList<ImportDeclaration>
the list of imports
imports
)

Sets the list of imports of this compilation unit.

public CompilationUnit
public ModuleDeclaration

Returns:

the module
setModule
(String name)

Create (or overwrite) a module declaration in this compilation unit with name "name".

public CompilationUnit
setPackageDeclaration(final PackageDeclaration
the packageDeclaration declaration to set or null to default package
packageDeclaration
)

Sets or clear the package declarations of this compilation unit.

public CompilationUnit

Returns:

this, the CompilationUnit
setPackageDeclaration
(String
the name of the package
name
)

sets the package declaration of this compilation unit

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

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

public CompilationUnit
setTypes(final NodeList<TypeDeclaration<?>> types)

Sets the list of types declared in this compilation unit.

Inherited from com.github.javaparser.ast.Node:
addOrphanCommentcontainsDatacustomInitializationequalsfindAllfindAllfindCompilationUnitfindFirstfindFirstfindFirstfindRootNodegetAllContainedCommentsgetChildNodesgetChildNodesByTypegetCommentgetDatagetDataKeysgetNodesByTypegetOrphanCommentsgetParentNodegetParentNodeForChildrengetParsedgetRangegetSymbolResolvergetTokenRangegetToStringPrettyPrinterConfigurationhashCodeisAncestorOfisRegisterednotifyPropertyChangeregisterregisterregisterForSubtreeremoveremoveCommentremoveDataremoveForcedremoveOrphanCommentreplacesetAsParentNodeOfsetAsParentNodeOfsetBlockCommentsetCommentsetDatasetLineCommentsetParentNodesetParsedsetRangesetTokenRangesetToStringPrettyPrinterConfigurationstreamstreamtoStringtoStringtryAddImportToParentCompilationUnitunregisterwalkwalkwalk