public final class CompilationUnit extends Node
This class represents the entire compilation unit. Each java file denotes a compilation unit.
The CompilationUnit is constructed following the syntax:
CompilationUnit ::= (
PackageDeclaration
)? (
ImportDeclaration
)* (
TypeDeclaration
)*
Node.ObserverRegistrationMode
ABSOLUTE_BEGIN_LINE, ABSOLUTE_END_LINE, NODE_BY_BEGIN_POSITION, prettyPrinterNoCommentsConfiguration
Constructor and Description |
---|
CompilationUnit() |
CompilationUnit(PackageDeclaration packageDeclaration,
NodeList<ImportDeclaration> imports,
NodeList<TypeDeclaration<?>> types) |
CompilationUnit(Range range,
PackageDeclaration packageDeclaration,
NodeList<ImportDeclaration> imports,
NodeList<TypeDeclaration<?>> types) |
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... 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(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
|
AnnotationDeclaration |
getAnnotationDeclarationByName(String annotationName)
Try to get an annotation by its name
|
ClassOrInterfaceDeclaration |
getClassByName(String className)
Try to get a class by its name
|
List<Comment> |
getComments()
Return a list containing all comments declared in this compilation unit.
|
EnumDeclaration |
getEnumByName(String enumName)
Try to get an enum 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. |
ClassOrInterfaceDeclaration |
getInterfaceByName(String interfaceName)
Try to get an interface by its name
|
List<NodeList<?>> |
getNodeLists()
The list of NodeLists owned by this node.
|
Optional<PackageDeclaration> |
getPackage()
Retrieves the package declaration of this compilation unit.
If this compilation unit has no package declaration (default package), null is returned. |
TypeDeclaration<?> |
getType(int i)
Convenience method that wraps
getTypes() .If i is out of bounds, throws IndexOutOfBoundsException. |
NodeList<TypeDeclaration<?>> |
getTypes()
Return the list of types declared in this compilation unit.
If there is no types declared, null is returned. |
CompilationUnit |
setImports(NodeList<ImportDeclaration> imports)
Sets the list of imports of this compilation unit.
|
CompilationUnit |
setPackage(PackageDeclaration pakage)
Sets or clear the package declarations of this compilation unit.
|
CompilationUnit |
setPackage(String s)
Shortcut: set the package declaration with a string.
|
CompilationUnit |
setPackageName(String name)
sets the package declaration of this compilation unit
|
CompilationUnit |
setTypes(NodeList<TypeDeclaration<?>> types)
Sets the list of types declared in this compilation unit.
|
addOrphanComment, clone, containsWithin, equals, getAllContainedComments, getBegin, getChildNodes, getComment, getData, getEnd, getNodesByType, getOrphanComments, getParentNode, getParentNodeForChildren, getRange, hasComment, hashCode, isPositionedAfter, isPositionedBefore, isRegistered, notifyPropertyChange, register, register, registerForSubtree, remove, setAsParentNodeOf, setBlockComment, setComment, setData, setLineComment, setParentNode, setRange, toString, toString, tryAddImportToParentCompilationUnit, unregister
finalize, getClass, notify, notifyAll, wait, wait, wait
getAncestorOfType, setAsParentNodeOf, setAsParentNodeOf
public CompilationUnit()
public CompilationUnit(PackageDeclaration packageDeclaration, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types)
public CompilationUnit(Range range, PackageDeclaration packageDeclaration, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types)
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 List<Comment> getComments()
JavadocComment
,
LineComment
,
BlockComment
public NodeList<ImportDeclaration> getImports()
null
if there is no import.null
if there is no importpublic ImportDeclaration getImport(int i)
public Optional<PackageDeclaration> getPackage()
null
is returned.null
public NodeList<TypeDeclaration<?>> getTypes()
null
is returned.null
null if there is no typeAnnotationDeclaration
,
ClassOrInterfaceDeclaration
,
EmptyTypeDeclaration
,
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 setPackage(PackageDeclaration pakage)
pakage
- the packageDeclaration declaration to set or null
to default packagepublic CompilationUnit setTypes(NodeList<TypeDeclaration<?>> types)
types
- the lis of typespublic CompilationUnit setPackageName(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
public 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... 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 ClassOrInterfaceDeclaration getClassByName(String className)
className
- the class name (case-sensitive)public ClassOrInterfaceDeclaration getInterfaceByName(String interfaceName)
interfaceName
- the interface name (case-sensitive)public EnumDeclaration getEnumByName(String enumName)
enumName
- the enum name (case-sensitive)public AnnotationDeclaration getAnnotationDeclarationByName(String annotationName)
annotationName
- the annotation name (case-sensitive)public List<NodeList<?>> getNodeLists()
Node
getNodeLists
in class Node
public CompilationUnit setPackage(String s)
Copyright © 2007–2016. All rights reserved.