NodeWithRange<Node>, NodeWithTokenRange<Node>, Observable, Visitable, HasParentNode<Node>, Cloneablepublic 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 | Description |
|---|---|---|
static class |
CompilationUnit.Storage |
Information about where this compilation unit was loaded from.
|
Node.ObserverRegistrationMode, Node.ParsednessABSOLUTE_BEGIN_LINE, ABSOLUTE_END_LINE, NODE_BY_BEGIN_POSITION, prettyPrinterNoCommentsConfiguration| Constructor | Description |
|---|---|
CompilationUnit() |
|
CompilationUnit(PackageDeclaration packageDeclaration,
NodeList<ImportDeclaration> imports,
NodeList<TypeDeclaration<?>> types,
ModuleDeclaration module) |
|
CompilationUnit(TokenRange tokenRange,
PackageDeclaration packageDeclaration,
NodeList<ImportDeclaration> imports,
NodeList<TypeDeclaration<?>> types,
ModuleDeclaration module) |
This constructor is used by the parser and is considered private.
|
CompilationUnit(String packageDeclaration) |
| Modifier and Type | Method | 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(String name) |
Add a public annotation declaration to the types of this compilation unit
|
AnnotationDeclaration |
addAnnotationDeclaration(String name,
Modifier... modifiers) |
Add an annotation declaration to the types of this compilation unit
|
ClassOrInterfaceDeclaration |
addClass(String name) |
Add a public class to the types of this compilation unit
|
ClassOrInterfaceDeclaration |
addClass(String name,
Modifier... modifiers) |
Add a class to the types of this compilation unit
|
EnumDeclaration |
addEnum(String name) |
Add a public enum to the types of this compilation unit
|
EnumDeclaration |
addEnum(String name,
Modifier... modifiers) |
Add an enum to the types of this compilation unit
|
CompilationUnit |
addImport(ImportDeclaration imports) |
|
CompilationUnit |
addImport(Class<?> clazz) |
Add an import to the list of
ImportDeclaration of this compilation unitshorthand for addImport(String) with clazz.getName() |
CompilationUnit |
addImport(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(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(String name) |
Add a public interface class to the types of this compilation unit
|
ClassOrInterfaceDeclaration |
addInterface(String name,
Modifier... modifiers) |
Add an interface to the types of this compilation unit
|
CompilationUnit |
addType(TypeDeclaration<?> type) |
|
CompilationUnit |
clone() |
|
Optional<AnnotationDeclaration> |
getAnnotationDeclarationByName(String annotationName) |
Try to get a top level annotation type declaration by its name
|
Optional<ClassOrInterfaceDeclaration> |
getClassByName(String className) |
Try to get a top level class declaration by its name
|
List<Comment> |
getComments() |
Return a list containing all comments declared in this compilation unit.
|
Optional<EnumDeclaration> |
getEnumByName(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. |
Optional<ClassOrInterfaceDeclaration> |
getInterfaceByName(String interfaceName) |
Try to get a top level interface declaration by its name
|
CompilationUnitMetaModel |
getMetaModel() |
|
Optional<ModuleDeclaration> |
getModule() |
|
List<NodeList<?>> |
getNodeLists() |
The list of NodeLists owned by this node.
|
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. |
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(String name) |
sets the package declaration of this compilation unit
|
CompilationUnit |
setStorage(Path path) |
|
CompilationUnit |
setType(int i,
TypeDeclaration<?> type) |
|
CompilationUnit |
setTypes(NodeList<TypeDeclaration<?>> types) |
Sets the list of types declared in this compilation unit.
|
getAncestorOfTypeaddOrphanComment, 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, unregistercontainsWithin, getBegin, getEnd, isPositionedAfter, isPositionedBeforepublic CompilationUnit()
public CompilationUnit(String packageDeclaration)
public CompilationUnit(PackageDeclaration packageDeclaration, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types, ModuleDeclaration module)
@Generated("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)
VisitableR - 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)
VisitableA - the type the argument passed for the visitorv - the visitor implementationarg - any value relevant for the visitor (of type A)public List<Comment> getComments()
JavadocComment,
LineComment,
BlockComment@Generated("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("com.github.javaparser.generator.core.node.PropertyGenerator") public Optional<PackageDeclaration> getPackageDeclaration()
Optional.none() is returned.none@Generated("com.github.javaparser.generator.core.node.PropertyGenerator") public NodeList<TypeDeclaration<?>> getTypes()
none is returned.none null if there is no typeAnnotationDeclaration,
ClassOrInterfaceDeclaration,
EnumDeclarationpublic TypeDeclaration<?> getType(int i)
getTypes().i is out of bounds, throws IndexOutOfBoundsException.i - the index of the type declaration to retrieve@Generated("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("com.github.javaparser.generator.core.node.PropertyGenerator") public CompilationUnit setPackageDeclaration(PackageDeclaration packageDeclaration)
packageDeclaration - the packageDeclaration declaration to set or null to default package@Generated("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(String name)
name - the name of the packageCompilationUnitpublic CompilationUnit addImport(String name)
ImportDeclaration of this compilation unitaddImport(String, boolean, boolean) with name,false,falsename - the import nameCompilationUnitpublic CompilationUnit addImport(Class<?> clazz)
ImportDeclaration of this compilation unitaddImport(String) with clazz.getName()clazz - the class to importCompilationUnitRuntimeException - if clazz is an anonymous or local classpublic CompilationUnit addImport(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 ".*"CompilationUnitpublic ClassOrInterfaceDeclaration addClass(String name)
name - the class namepublic ClassOrInterfaceDeclaration addClass(String name, Modifier... modifiers)
name - the class namemodifiers - the modifiers (like Modifier.PUBLIC)public ClassOrInterfaceDeclaration addInterface(String name)
name - the interface namepublic ClassOrInterfaceDeclaration addInterface(String name, Modifier... modifiers)
name - the interface namemodifiers - the modifiers (like Modifier.PUBLIC)public EnumDeclaration addEnum(String name)
name - the enum namepublic EnumDeclaration addEnum(String name, Modifier... modifiers)
name - the enum namemodifiers - the modifiers (like Modifier.PUBLIC)public AnnotationDeclaration addAnnotationDeclaration(String name)
name - the annotation namepublic AnnotationDeclaration addAnnotationDeclaration(String name, Modifier... modifiers)
name - the annotation namemodifiers - the modifiers (like Modifier.PUBLIC)public Optional<ClassOrInterfaceDeclaration> getClassByName(String className)
className - the class name (case-sensitive)public Optional<ClassOrInterfaceDeclaration> getInterfaceByName(String interfaceName)
interfaceName - the interface name (case-sensitive)public Optional<EnumDeclaration> getEnumByName(String enumName)
enumName - the enum name (case-sensitive)public Optional<AnnotationDeclaration> getAnnotationDeclarationByName(String annotationName)
annotationName - the annotation name (case-sensitive)@Generated("com.github.javaparser.generator.core.node.GetNodeListsGenerator") public List<NodeList<?>> getNodeLists()
NodegetNodeLists in class Node@Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") public boolean remove(Node node)
@Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") public CompilationUnit removePackageDeclaration()
@Generated("com.github.javaparser.generator.core.node.PropertyGenerator") public Optional<ModuleDeclaration> getModule()
@Generated("com.github.javaparser.generator.core.node.PropertyGenerator") public CompilationUnit setModule(ModuleDeclaration module)
@Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") public CompilationUnit removeModule()
public Optional<CompilationUnit.Storage> getStorage()
public CompilationUnit setStorage(Path path)
@Generated("com.github.javaparser.generator.core.node.CloneGenerator") public CompilationUnit clone()
@Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") public CompilationUnitMetaModel getMetaModel()
getMetaModel in class Node@Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") public boolean replace(Node node, Node replacementNode)
Copyright © 2007–2019. All rights reserved.