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.PackageDeclaration
,
ImportDeclaration
,
TypeDeclaration
,
CompilationUnit.Storage
Modifier and Type | Class and Description |
---|---|
static class |
CompilationUnit.Storage
Information about where this compilation unit was loaded from.
|
Node.BreadthFirstIterator, Node.DirectChildrenIterator, Node.ObserverRegistrationMode, Node.ParentsVisitor, Node.Parsedness, Node.PostOrderIterator, Node.PreOrderIterator, Node.TreeTraversal
ABSOLUTE_BEGIN_LINE, ABSOLUTE_END_LINE, LINE_SEPARATOR_KEY, NODE_BY_BEGIN_POSITION, prettyPrinterNoCommentsConfiguration, SYMBOL_RESOLVER_KEY
Constructor and Description |
---|
CompilationUnit() |
CompilationUnit(PackageDeclaration packageDeclaration,
NodeList<ImportDeclaration> imports,
NodeList<TypeDeclaration<?>> types,
ModuleDeclaration module) |
CompilationUnit(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(String name)
Add a public annotation declaration to the types of this compilation unit
|
AnnotationDeclaration |
addAnnotationDeclaration(String name,
Modifier.Keyword... 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.Keyword... 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.Keyword... modifiers)
Add an enum to the types of this compilation unit
|
CompilationUnit |
addImport(Class<?> clazz)
Add an import to the list of
ImportDeclaration of this compilation unitshorthand for addImport(String) with clazz.getName() |
CompilationUnit |
addImport(ImportDeclaration importDeclaration)
adds an import if not implicitly imported by java (i.e. java.lang) or
added before.
|
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.Keyword... modifiers)
Add an interface to the types of this compilation unit
|
CompilationUnit |
addType(TypeDeclaration<?> type) |
CompilationUnit |
clone() |
List<Comment> |
getAllComments()
Return a list containing all comments declared in this compilation unit.
|
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()
Deprecated.
getComments was a too generic name and it could be confused with getComment
or getAllContainedComments
Use
getAllComments() instead |
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() |
Optional<PackageDeclaration> |
getPackageDeclaration()
Retrieves the package declaration of this compilation unit.
|
Optional<TypeDeclaration<?>> |
getPrimaryType() |
Optional<String> |
getPrimaryTypeName() |
Optional<CompilationUnit.Storage> |
getStorage() |
TypeDeclaration<?> |
getType(int i)
Convenience method that wraps
getTypes() . |
NodeList<TypeDeclaration<?>> |
getTypes()
Return the list of top level types declared in this compilation unit.
|
void |
recalculatePositions()
Recalculates the ranges of all nodes by looking at the sizes of the tokens.
|
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) |
ModuleDeclaration |
setModule(String name)
Create (or overwrite) a module declaration in this compilation unit with name "name".
|
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 |
setStorage(Path path,
Charset charset) |
CompilationUnit |
setType(int i,
TypeDeclaration<?> type) |
CompilationUnit |
setTypes(NodeList<TypeDeclaration<?>> types)
Sets the list of types declared in this compilation unit.
|
addOrphanComment, containsData, customInitialization, equals, findAll, findAll, findCompilationUnit, findFirst, findFirst, findFirst, findRootNode, getAllContainedComments, getChildNodes, getChildNodesByType, getComment, getData, getDataKeys, getLineEndingStyle, getLineEndingStyleOrDefault, getNodesByType, getOrphanComments, getParentNode, getParentNodeForChildren, getParsed, getRange, getSymbolResolver, getTokenRange, getToStringPrettyPrinterConfiguration, hashCode, isAncestorOf, isRegistered, notifyPropertyChange, register, register, registerForSubtree, remove, removeComment, removeData, removeForced, removeOrphanComment, replace, setAsParentNodeOf, setAsParentNodeOf, setBlockComment, setComment, setData, setLineComment, setParentNode, setParsed, setRange, setTokenRange, setToStringPrettyPrinterConfiguration, stream, stream, toString, toString, tryAddImportToParentCompilationUnit, unregister, walk, walk, walk
finalize, getClass, notify, notifyAll, wait, wait, wait
findAncestor, findAncestor, isDescendantOf
containsWithin, containsWithinRange, getBegin, getEnd
public CompilationUnit()
public CompilationUnit(String packageDeclaration)
public CompilationUnit(PackageDeclaration packageDeclaration, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types, ModuleDeclaration module)
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)@Deprecated public List<Comment> getComments()
getAllComments()
insteadpublic List<Comment> getAllComments()
JavadocComment
,
LineComment
,
BlockComment
public NodeList<ImportDeclaration> getImports()
null
if there is no import.none
if there is no importpublic ImportDeclaration getImport(int i)
public Optional<PackageDeclaration> getPackageDeclaration()
Optional.none()
is returned.none
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 retrievepublic CompilationUnit setImports(NodeList<ImportDeclaration> imports)
null
.imports
- the list of importspublic CompilationUnit setImport(int i, ImportDeclaration imports)
public CompilationUnit addImport(ImportDeclaration importDeclaration)
importDeclaration
- this
public CompilationUnit setPackageDeclaration(PackageDeclaration packageDeclaration)
packageDeclaration
- the packageDeclaration declaration to set or null
to default packagepublic 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 packageCompilationUnit
public CompilationUnit addImport(String name)
ImportDeclaration
of this compilation unitaddImport(String, boolean, boolean)
with name,false,falsename
- the import nameCompilationUnit
public CompilationUnit addImport(Class<?> clazz)
ImportDeclaration
of this compilation unitaddImport(String)
with clazz.getName()clazz
- the class to importCompilationUnit
IllegalArgumentException
- 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 ".*"CompilationUnit
public ClassOrInterfaceDeclaration addClass(String name)
name
- the class namepublic ClassOrInterfaceDeclaration addClass(String name, Modifier.Keyword... modifiers)
name
- the class namemodifiers
- the modifiers (like Modifier.PUBLIC)public ClassOrInterfaceDeclaration addInterface(String name)
name
- the interface namepublic ClassOrInterfaceDeclaration addInterface(String name, Modifier.Keyword... modifiers)
name
- the interface namemodifiers
- the modifiers (like Modifier.PUBLIC)public EnumDeclaration addEnum(String name)
name
- the enum namepublic EnumDeclaration addEnum(String name, Modifier.Keyword... modifiers)
name
- the enum namemodifiers
- the modifiers (like Modifier.PUBLIC)public AnnotationDeclaration addAnnotationDeclaration(String name)
name
- the annotation namepublic AnnotationDeclaration addAnnotationDeclaration(String name, Modifier.Keyword... 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<String> getPrimaryTypeName()
CompilationUnit.Storage.getFileName()
.
Empty if no file information is present (when this compilation unit wasn't parsed from a file.)public Optional<TypeDeclaration<?>> getPrimaryType()
public Optional<AnnotationDeclaration> getAnnotationDeclarationByName(String annotationName)
annotationName
- the annotation name (case-sensitive)public CompilationUnit removePackageDeclaration()
public Optional<ModuleDeclaration> getModule()
public CompilationUnit setModule(ModuleDeclaration module)
public CompilationUnit removeModule()
public Optional<CompilationUnit.Storage> getStorage()
public CompilationUnit setStorage(Path path)
public CompilationUnit setStorage(Path path, Charset charset)
public ModuleDeclaration setModule(String name)
public void recalculatePositions()
public CompilationUnit clone()
public CompilationUnitMetaModel getMetaModel()
getMetaModel
in class Node
Copyright © 2007–2020. All rights reserved.