public final 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.PackageDeclaration
,
ImportDeclaration
,
TypeDeclaration
Modifier and Type | Class and Description |
---|---|
static class |
CompilationUnit.Storage
Information about where this compilation unit was loaded from.
|
Node.ObserverRegistrationMode, Node.Parsedness
Modifier and Type | Field and Description |
---|---|
private NodeList<ImportDeclaration> |
imports |
private ModuleDeclaration |
module |
private PackageDeclaration |
packageDeclaration |
private CompilationUnit.Storage |
storage |
private NodeList<TypeDeclaration<?>> |
types |
ABSOLUTE_BEGIN_LINE, ABSOLUTE_END_LINE, NODE_BY_BEGIN_POSITION, prettyPrinterNoCommentsConfiguration
Constructor and Description |
---|
CompilationUnit() |
CompilationUnit(PackageDeclaration packageDeclaration,
NodeList<ImportDeclaration> imports,
NodeList<TypeDeclaration<?>> types,
ModuleDeclaration module) |
CompilationUnit(java.lang.String packageDeclaration) |
CompilationUnit(TokenRange tokenRange,
PackageDeclaration packageDeclaration,
NodeList<ImportDeclaration> imports,
NodeList<TypeDeclaration<?>> types,
ModuleDeclaration module)
This constructor is used by the parser and is considered private.
|
Modifier and Type | Method and Description |
---|---|
<R,A> R |
accept(GenericVisitor<R,A> v,
A arg)
Accept method for visitor support.
|
<A> void |
accept(VoidVisitor<A> v,
A arg)
Accept method for visitor support.
|
AnnotationDeclaration |
addAnnotationDeclaration(java.lang.String name)
Add a public annotation declaration to the types of this compilation unit
|
AnnotationDeclaration |
addAnnotationDeclaration(java.lang.String name,
Modifier... modifiers)
Add an annotation declaration to the types of this compilation unit
|
ClassOrInterfaceDeclaration |
addClass(java.lang.String name)
Add a public class to the types of this compilation unit
|
ClassOrInterfaceDeclaration |
addClass(java.lang.String name,
Modifier... modifiers)
Add a class to the types of this compilation unit
|
EnumDeclaration |
addEnum(java.lang.String name)
Add a public enum to the types of this compilation unit
|
EnumDeclaration |
addEnum(java.lang.String name,
Modifier... modifiers)
Add an enum to the types of this compilation unit
|
CompilationUnit |
addImport(java.lang.Class<?> clazz)
Add an import to the list of
ImportDeclaration of this compilation unitshorthand for addImport(String) with clazz.getName() |
CompilationUnit |
addImport(ImportDeclaration imports) |
CompilationUnit |
addImport(java.lang.String name)
Add an import to the list of
ImportDeclaration of this compilation unitshorthand for addImport(String, boolean, boolean) with name,false,false |
CompilationUnit |
addImport(java.lang.String name,
boolean isStatic,
boolean isAsterisk)
Add an import to the list of
ImportDeclaration of this compilation unitThis method check if no import with the same name is already in the list |
ClassOrInterfaceDeclaration |
addInterface(java.lang.String name)
Add a public interface class to the types of this compilation unit
|
ClassOrInterfaceDeclaration |
addInterface(java.lang.String name,
Modifier... modifiers)
Add an interface to the types of this compilation unit
|
CompilationUnit |
addType(TypeDeclaration<?> type) |
CompilationUnit |
clone() |
java.util.Optional<AnnotationDeclaration> |
getAnnotationDeclarationByName(java.lang.String annotationName)
Try to get a top level annotation type declaration by its name
|
java.util.Optional<ClassOrInterfaceDeclaration> |
getClassByName(java.lang.String className)
Try to get a top level class declaration by its name
|
java.util.List<Comment> |
getComments()
Return a list containing all comments declared in this compilation unit.
|
java.util.Optional<EnumDeclaration> |
getEnumByName(java.lang.String enumName)
Try to get a top level enum declaration by its name
|
ImportDeclaration |
getImport(int i) |
NodeList<ImportDeclaration> |
getImports()
Retrieves the list of imports declared in this compilation unit or
null if there is no import. |
java.util.Optional<ClassOrInterfaceDeclaration> |
getInterfaceByName(java.lang.String interfaceName)
Try to get a top level interface declaration by its name
|
CompilationUnitMetaModel |
getMetaModel() |
java.util.Optional<ModuleDeclaration> |
getModule() |
java.util.List<NodeList<?>> |
getNodeLists()
The list of NodeLists owned by this node.
|
java.util.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. |
java.util.Optional<CompilationUnit.Storage> |
getStorage() |
TypeDeclaration<?> |
getType(int i)
Convenience method that wraps
getTypes() .If i is out of bounds, throws IndexOutOfBoundsException. |
NodeList<TypeDeclaration<?>> |
getTypes()
Return the list of top level types declared in this compilation unit.
If there are no types declared, none is returned. |
boolean |
remove(Node node) |
CompilationUnit |
removeModule() |
CompilationUnit |
removePackageDeclaration() |
boolean |
replace(Node node,
Node replacementNode) |
CompilationUnit |
setImport(int i,
ImportDeclaration imports) |
CompilationUnit |
setImports(NodeList<ImportDeclaration> imports)
Sets the list of imports of this compilation unit.
|
CompilationUnit |
setModule(ModuleDeclaration module) |
CompilationUnit |
setPackageDeclaration(PackageDeclaration packageDeclaration)
Sets or clear the package declarations of this compilation unit.
|
CompilationUnit |
setPackageDeclaration(java.lang.String name)
sets the package declaration of this compilation unit
|
CompilationUnit |
setStorage(java.nio.file.Path path) |
CompilationUnit |
setType(int i,
TypeDeclaration<?> type) |
CompilationUnit |
setTypes(NodeList<TypeDeclaration<?>> types)
Sets the list of types declared in this compilation unit.
|
addOrphanComment, customInitialization, equals, getAllContainedComments, getChildNodes, getChildNodesByType, getComment, getData, getNodesByType, getOrphanComments, getParentNode, getParentNodeForChildren, getParsed, getRange, getTokenRange, hasComment, hashCode, isRegistered, notifyPropertyChange, register, register, registerForSubtree, remove, removeComment, removeForced, removeOrphanComment, replace, setAsParentNodeOf, setAsParentNodeOf, setBlockComment, setComment, setData, setLineComment, setParentNode, setParsed, setRange, setTokenRange, toString, toString, tryAddImportToParentCompilationUnit, unregister
finalize, getClass, notify, notifyAll, wait, wait, wait
getAncestorOfType
containsWithin, getBegin, getEnd, isPositionedAfter, isPositionedBefore
private PackageDeclaration packageDeclaration
private NodeList<ImportDeclaration> imports
private NodeList<TypeDeclaration<?>> types
private ModuleDeclaration module
private CompilationUnit.Storage storage
public CompilationUnit()
public CompilationUnit(java.lang.String packageDeclaration)
public CompilationUnit(PackageDeclaration packageDeclaration, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types, ModuleDeclaration module)
@Generated(value="com.github.javaparser.generator.core.node.MainConstructorGenerator") public CompilationUnit(TokenRange tokenRange, PackageDeclaration packageDeclaration, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types, ModuleDeclaration module)
public <R,A> R accept(GenericVisitor<R,A> v, A arg)
Visitable
R
- the type of the return value of the visitorA
- the type the user argument passed to the visitorv
- the visitor implementationarg
- the argument passed to the visitor (of type A)public <A> void accept(VoidVisitor<A> v, A arg)
Visitable
A
- the type the argument passed for the visitorv
- the visitor implementationarg
- any value relevant for the visitor (of type A)public java.util.List<Comment> getComments()
JavadocComment
,
LineComment
,
BlockComment
@Generated(value="com.github.javaparser.generator.core.node.PropertyGenerator") public NodeList<ImportDeclaration> getImports()
null
if there is no import.none
if there is no importpublic ImportDeclaration getImport(int i)
@Generated(value="com.github.javaparser.generator.core.node.PropertyGenerator") public java.util.Optional<PackageDeclaration> getPackageDeclaration()
Optional.none()
is returned.none
@Generated(value="com.github.javaparser.generator.core.node.PropertyGenerator") public NodeList<TypeDeclaration<?>> getTypes()
none
is returned.none
null if there is no typeAnnotationDeclaration
,
ClassOrInterfaceDeclaration
,
EnumDeclaration
public TypeDeclaration<?> getType(int i)
getTypes()
.i
is out of bounds, throws IndexOutOfBoundsException.
i
- the index of the type declaration to retrieve@Generated(value="com.github.javaparser.generator.core.node.PropertyGenerator") public CompilationUnit setImports(NodeList<ImportDeclaration> imports)
null
.imports
- the list of importspublic CompilationUnit setImport(int i, ImportDeclaration imports)
public CompilationUnit addImport(ImportDeclaration imports)
@Generated(value="com.github.javaparser.generator.core.node.PropertyGenerator") public CompilationUnit setPackageDeclaration(PackageDeclaration packageDeclaration)
packageDeclaration
- the packageDeclaration declaration to set or null
to default package@Generated(value="com.github.javaparser.generator.core.node.PropertyGenerator") public CompilationUnit setTypes(NodeList<TypeDeclaration<?>> types)
public CompilationUnit setType(int i, TypeDeclaration<?> type)
public CompilationUnit addType(TypeDeclaration<?> type)
public CompilationUnit setPackageDeclaration(java.lang.String name)
name
- the name of the packageCompilationUnit
public CompilationUnit addImport(java.lang.String name)
ImportDeclaration
of this compilation unitaddImport(String, boolean, boolean)
with name,false,falsename
- the import nameCompilationUnit
public CompilationUnit addImport(java.lang.Class<?> clazz)
ImportDeclaration
of this compilation unitaddImport(String)
with clazz.getName()clazz
- the class to importCompilationUnit
java.lang.RuntimeException
- if clazz is an anonymous or local classpublic CompilationUnit addImport(java.lang.String name, boolean isStatic, boolean isAsterisk)
ImportDeclaration
of this compilation unitname
- the import nameisStatic
- is it an "import static"isAsterisk
- does the import end with ".*"CompilationUnit
public ClassOrInterfaceDeclaration addClass(java.lang.String name)
name
- the class namepublic ClassOrInterfaceDeclaration addClass(java.lang.String name, Modifier... modifiers)
name
- the class namemodifiers
- the modifiers (like Modifier.PUBLIC)public ClassOrInterfaceDeclaration addInterface(java.lang.String name)
name
- the interface namepublic ClassOrInterfaceDeclaration addInterface(java.lang.String name, Modifier... modifiers)
name
- the interface namemodifiers
- the modifiers (like Modifier.PUBLIC)public EnumDeclaration addEnum(java.lang.String name)
name
- the enum namepublic EnumDeclaration addEnum(java.lang.String name, Modifier... modifiers)
name
- the enum namemodifiers
- the modifiers (like Modifier.PUBLIC)public AnnotationDeclaration addAnnotationDeclaration(java.lang.String name)
name
- the annotation namepublic AnnotationDeclaration addAnnotationDeclaration(java.lang.String name, Modifier... modifiers)
name
- the annotation namemodifiers
- the modifiers (like Modifier.PUBLIC)public java.util.Optional<ClassOrInterfaceDeclaration> getClassByName(java.lang.String className)
className
- the class name (case-sensitive)public java.util.Optional<ClassOrInterfaceDeclaration> getInterfaceByName(java.lang.String interfaceName)
interfaceName
- the interface name (case-sensitive)public java.util.Optional<EnumDeclaration> getEnumByName(java.lang.String enumName)
enumName
- the enum name (case-sensitive)public java.util.Optional<AnnotationDeclaration> getAnnotationDeclarationByName(java.lang.String annotationName)
annotationName
- the annotation name (case-sensitive)@Generated(value="com.github.javaparser.generator.core.node.GetNodeListsGenerator") public java.util.List<NodeList<?>> getNodeLists()
Node
getNodeLists
in class Node
@Generated(value="com.github.javaparser.generator.core.node.RemoveMethodGenerator") public boolean remove(Node node)
@Generated(value="com.github.javaparser.generator.core.node.RemoveMethodGenerator") public CompilationUnit removePackageDeclaration()
@Generated(value="com.github.javaparser.generator.core.node.PropertyGenerator") public java.util.Optional<ModuleDeclaration> getModule()
@Generated(value="com.github.javaparser.generator.core.node.PropertyGenerator") public CompilationUnit setModule(ModuleDeclaration module)
@Generated(value="com.github.javaparser.generator.core.node.RemoveMethodGenerator") public CompilationUnit removeModule()
public java.util.Optional<CompilationUnit.Storage> getStorage()
public CompilationUnit setStorage(java.nio.file.Path path)
@Generated(value="com.github.javaparser.generator.core.node.CloneGenerator") public CompilationUnit clone()
@Generated(value="com.github.javaparser.generator.core.node.GetMetaModelGenerator") public CompilationUnitMetaModel getMetaModel()
getMetaModel
in class Node