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
)*
ABSOLUTE_BEGIN_LINE, ABSOLUTE_END_LINE, NODE_BY_BEGIN_POSITION
Constructor and Description |
---|
CompilationUnit() |
CompilationUnit(Optional<PackageDeclaration> pakage,
NodeList<ImportDeclaration> imports,
NodeList<TypeDeclaration<?>> types) |
CompilationUnit(Range range,
Optional<PackageDeclaration> pakage,
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
|
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
|
Optional<PackageDeclaration> |
getPackage()
Retrieves the package declaration of this compilation unit.
If this compilation unit has no package declaration (default package), null is returned. |
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(Optional<PackageDeclaration> pakage)
Sets or clear the package declarations of this compilation unit.
|
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, getBackwardsCompatibleChildrenNodes, getBegin, getChildrenNodes, getComment, getEnd, getNodesByType, getOrphanComments, getParentNode, getParentNodeOfType, getRange, getUserData, hashCode, isPositionedAfter, isPositionedBefore, remove, setAsParentNodeOf, setAsParentNodeOf, setAsParentNodeOf, setBegin, setBlockComment, setComment, setEnd, setLineComment, setParentNode, setRange, setUserData, toString, toStringWithoutComments, tryAddImportToParentCompilationUnit
public CompilationUnit()
public CompilationUnit(Optional<PackageDeclaration> pakage, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types)
public CompilationUnit(Range range, Optional<PackageDeclaration> pakage, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types)
public <R,A> R accept(GenericVisitor<R,A> v, A arg)
Node
public <A> void accept(VoidVisitor<A> v, A arg)
Node
public List<Comment> getComments()
null
is returned.null
JavadocComment
,
LineComment
,
BlockComment
public NodeList<ImportDeclaration> getImports()
null
if there is no import.null
if there is no importpublic 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 CompilationUnit setImports(NodeList<ImportDeclaration> imports)
null
.imports
- the list of importspublic CompilationUnit setPackage(Optional<PackageDeclaration> pakage)
pakage
- the pakage 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)Copyright © 2007–2016. All rights reserved.