Package com.github.javaparser.ast
Class CompilationUnit
- java.lang.Object
-
- com.github.javaparser.ast.Node
-
- com.github.javaparser.ast.CompilationUnit
-
- All Implemented Interfaces:
NodeWithRange<Node>
,NodeWithTokenRange<Node>
,Observable
,Visitable
,HasParentNode<Node>
,Cloneable
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.- Author:
- Julio Vilmar Gesser
- See Also:
PackageDeclaration
,ImportDeclaration
,TypeDeclaration
,CompilationUnit.Storage
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CompilationUnit.Storage
Information about where this compilation unit was loaded from.-
Nested classes/interfaces inherited from class com.github.javaparser.ast.Node
Node.BreadthFirstIterator, Node.DirectChildrenIterator, Node.ObserverRegistrationMode, Node.ParentsVisitor, Node.Parsedness, Node.PostOrderIterator, Node.PreOrderIterator, Node.TreeTraversal
-
-
Field Summary
-
Fields inherited from class com.github.javaparser.ast.Node
ABSOLUTE_BEGIN_LINE, ABSOLUTE_END_LINE, LINE_SEPARATOR_KEY, NODE_BY_BEGIN_POSITION, PHANTOM_KEY, prettyPrinterNoCommentsConfiguration, PRINTER_KEY, SYMBOL_RESOLVER_KEY
-
-
Constructor Summary
Constructors 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)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <R,A>
Raccept(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 unitAnnotationDeclaration
addAnnotationDeclaration(String name, Modifier.Keyword... modifiers)
Add an annotation declaration to the types of this compilation unitClassOrInterfaceDeclaration
addClass(String name)
Add a public class to the types of this compilation unitClassOrInterfaceDeclaration
addClass(String name, Modifier.Keyword... modifiers)
Add a class to the types of this compilation unitEnumDeclaration
addEnum(String name)
Add a public enum to the types of this compilation unitEnumDeclaration
addEnum(String name, Modifier.Keyword... modifiers)
Add an enum to the types of this compilation unitCompilationUnit
addImport(ImportDeclaration importDeclaration)
adds an import if not implicitly imported by java (i.e. java.lang) or added before.CompilationUnit
addImport(Class<?> clazz)
Add an import to the list ofImportDeclaration
of this compilation unit
shorthand foraddImport(String)
with clazz.getName()CompilationUnit
addImport(String name)
Add an import to the list ofImportDeclaration
of this compilation unit
shorthand foraddImport(String, boolean, boolean)
with name,false,falseCompilationUnit
addImport(String name, boolean isStatic, boolean isAsterisk)
Add an import to the list ofImportDeclaration
of this compilation unit
This method check if no import with the same name is already in the listClassOrInterfaceDeclaration
addInterface(String name)
Add a public interface class to the types of this compilation unitClassOrInterfaceDeclaration
addInterface(String name, Modifier.Keyword... modifiers)
Add an interface to the types of this compilation unitCompilationUnit
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 nameOptional<ClassOrInterfaceDeclaration>
getClassByName(String className)
Try to get a top level class declaration by its nameList<Comment>
getComments()
Deprecated.getComments was a too generic name and it could be confused with getComment or getAllContainedComments UsegetAllComments()
insteadOptional<EnumDeclaration>
getEnumByName(String enumName)
Try to get a top level enum declaration by its nameImportDeclaration
getImport(int i)
NodeList<ImportDeclaration>
getImports()
Retrieves the list of imports declared in this compilation unit ornull
if there is no import.Optional<ClassOrInterfaceDeclaration>
getInterfaceByName(String interfaceName)
Try to get a top level interface declaration by its nameList<ClassOrInterfaceDeclaration>
getLocalDeclarationFromClassname(String className)
Try to get all local class declarations ending by its name (top level or inner class)CompilationUnitMetaModel
getMetaModel()
Optional<ModuleDeclaration>
getModule()
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<TypeDeclaration<?>>
getPrimaryType()
Optional<String>
getPrimaryTypeName()
protected Printer
getPrinter()
protected Printer
getPrinter(PrinterConfiguration config)
Optional<RecordDeclaration>
getRecordByName(String recordName)
Try to get a top level record declaration by its nameOptional<CompilationUnit.Storage>
getStorage()
TypeDeclaration<?>
getType(int i)
Convenience method that wrapsgetTypes()
.
Ifi
is out of bounds, throwsIndexOutOfBoundsException.
NodeList<TypeDeclaration<?>>
getTypes()
Return the list of top level types declared in this compilation unit.
If there are no types declared,none
is returned.CompilationUnit
printer(Printer printer)
Declare a specific printervoid
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 unitCompilationUnit
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.-
Methods inherited from class com.github.javaparser.ast.Node
addOrphanComment, containsData, createDefaultPrinter, createDefaultPrinter, customInitialization, equals, findAll, findAll, findAll, findByRange, findCompilationUnit, findData, findFirst, findFirst, findFirst, findRootNode, getAllContainedComments, getChildNodes, getChildNodesByType, getComment, getData, getDataKeys, getDefaultPrinterConfiguration, getLineEndingStyle, getLineEndingStyleOrDefault, getNodesByType, getOrphanComments, getParentNode, getParentNodeForChildren, getParsed, getRange, getSymbolResolver, getTokenRange, hashCode, hasScope, isAncestorOf, isPhantom, isRegistered, notifyPropertyChange, register, register, registerForSubtree, remove, removeComment, removeData, removeForced, removeOrphanComment, replace, setAsParentNodeOf, setAsParentNodeOf, setBlockComment, setComment, setData, setLineComment, setParentNode, setParsed, setRange, setTokenRange, stream, stream, toString, toString, tryAddImportToParentCompilationUnit, unregister, walk, walk, walk
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.github.javaparser.HasParentNode
findAncestor, findAncestor, findAncestor, hasParentNode, isDescendantOf
-
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithRange
containsWithin, containsWithinRange, getBegin, getEnd, hasRange
-
-
-
-
Constructor Detail
-
CompilationUnit
public CompilationUnit()
-
CompilationUnit
public CompilationUnit(String packageDeclaration)
-
CompilationUnit
public CompilationUnit(PackageDeclaration packageDeclaration, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types, ModuleDeclaration module)
-
CompilationUnit
public CompilationUnit(TokenRange tokenRange, PackageDeclaration packageDeclaration, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types, ModuleDeclaration module)
This constructor is used by the parser and is considered private.
-
-
Method Detail
-
accept
public <R,A> R accept(GenericVisitor<R,A> v, A arg)
Description copied from interface:Visitable
Accept method for visitor support.- Type Parameters:
R
- the type of the return value of the visitorA
- the type the user argument passed to the visitor- Parameters:
v
- the visitor implementationarg
- the argument passed to the visitor (of type A)- Returns:
- the result of the visit (of type R)
-
accept
public <A> void accept(VoidVisitor<A> v, A arg)
Description copied from interface:Visitable
Accept method for visitor support.- Type Parameters:
A
- the type the argument passed for the visitor- Parameters:
v
- the visitor implementationarg
- any value relevant for the visitor (of type A)
-
printer
public CompilationUnit printer(Printer printer)
Declare a specific printer
-
getPrinter
protected Printer getPrinter()
- Overrides:
getPrinter
in classNode
-
getPrinter
protected Printer getPrinter(PrinterConfiguration config)
- Overrides:
getPrinter
in classNode
-
getComments
@Deprecated public List<Comment> getComments()
Deprecated.getComments was a too generic name and it could be confused with getComment or getAllContainedComments UsegetAllComments()
instead
-
getAllComments
public List<Comment> getAllComments()
Return a list containing all comments declared in this compilation unit. Including javadocs, line comments and block comments of all types, inner-classes and other members.
If there is no comment, an empty list is returned.- Returns:
- list with all comments of this compilation unit.
- See Also:
JavadocComment
,LineComment
,BlockComment
-
getImports
public NodeList<ImportDeclaration> getImports()
Retrieves the list of imports declared in this compilation unit ornull
if there is no import.- Returns:
- the list of imports or
none
if there is no import
-
getImport
public ImportDeclaration getImport(int i)
-
getPackageDeclaration
public 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.- Returns:
- the package declaration or
none
-
getTypes
public NodeList<TypeDeclaration<?>> getTypes()
Return the list of top level types declared in this compilation unit.
If there are no types declared,none
is returned.- Returns:
- the list of types or
none
null if there is no type - See Also:
AnnotationDeclaration
,ClassOrInterfaceDeclaration
,EnumDeclaration
-
getType
public TypeDeclaration<?> getType(int i)
Convenience method that wrapsgetTypes()
.
Ifi
is out of bounds, throwsIndexOutOfBoundsException.
- Parameters:
i
- the index of the type declaration to retrieve
-
setImports
public CompilationUnit setImports(NodeList<ImportDeclaration> imports)
Sets the list of imports of this compilation unit. The list is initiallynull
.- Parameters:
imports
- the list of imports
-
setImport
public CompilationUnit setImport(int i, ImportDeclaration imports)
-
addImport
public CompilationUnit addImport(ImportDeclaration importDeclaration)
adds an import if not implicitly imported by java (i.e. java.lang) or added before. Asterisk imports overrule the other imports within the same package.- Parameters:
importDeclaration
-- Returns:
this
-
setPackageDeclaration
public CompilationUnit setPackageDeclaration(PackageDeclaration packageDeclaration)
Sets or clear the package declarations of this compilation unit.- Parameters:
packageDeclaration
- the packageDeclaration declaration to set ornull
to default package
-
setTypes
public CompilationUnit setTypes(NodeList<TypeDeclaration<?>> types)
Sets the list of types declared in this compilation unit.
-
setType
public CompilationUnit setType(int i, TypeDeclaration<?> type)
-
addType
public CompilationUnit addType(TypeDeclaration<?> type)
-
setPackageDeclaration
public CompilationUnit setPackageDeclaration(String name)
sets the package declaration of this compilation unit- Parameters:
name
- the name of the package- Returns:
- this, the
CompilationUnit
-
addImport
public CompilationUnit addImport(String name)
Add an import to the list ofImportDeclaration
of this compilation unit
shorthand foraddImport(String, boolean, boolean)
with name,false,false- Parameters:
name
- the import name- Returns:
- this, the
CompilationUnit
-
addImport
public CompilationUnit addImport(Class<?> clazz)
Add an import to the list ofImportDeclaration
of this compilation unit
shorthand foraddImport(String)
with clazz.getName()- Parameters:
clazz
- the class to import- Returns:
- this, the
CompilationUnit
- Throws:
IllegalArgumentException
- if clazz is an anonymous or local class
-
addImport
public CompilationUnit addImport(String name, boolean isStatic, boolean isAsterisk)
Add an import to the list ofImportDeclaration
of this compilation unit
This method check if no import with the same name is already in the list- Parameters:
name
- the import nameisStatic
- is it an "import static"isAsterisk
- does the import end with ".*"- Returns:
- this, the
CompilationUnit
-
addClass
public ClassOrInterfaceDeclaration addClass(String name)
Add a public class to the types of this compilation unit- Parameters:
name
- the class name- Returns:
- the newly created class
-
addClass
public ClassOrInterfaceDeclaration addClass(String name, Modifier.Keyword... modifiers)
Add a class to the types of this compilation unit- Parameters:
name
- the class namemodifiers
- the modifiers (like Modifier.PUBLIC)- Returns:
- the newly created class
-
addInterface
public ClassOrInterfaceDeclaration addInterface(String name)
Add a public interface class to the types of this compilation unit- Parameters:
name
- the interface name- Returns:
- the newly created class
-
addInterface
public ClassOrInterfaceDeclaration addInterface(String name, Modifier.Keyword... modifiers)
Add an interface to the types of this compilation unit- Parameters:
name
- the interface namemodifiers
- the modifiers (like Modifier.PUBLIC)- Returns:
- the newly created class
-
addEnum
public EnumDeclaration addEnum(String name)
Add a public enum to the types of this compilation unit- Parameters:
name
- the enum name- Returns:
- the newly created class
-
addEnum
public EnumDeclaration addEnum(String name, Modifier.Keyword... modifiers)
Add an enum to the types of this compilation unit- Parameters:
name
- the enum namemodifiers
- the modifiers (like Modifier.PUBLIC)- Returns:
- the newly created class
-
addAnnotationDeclaration
public AnnotationDeclaration addAnnotationDeclaration(String name)
Add a public annotation declaration to the types of this compilation unit- Parameters:
name
- the annotation name- Returns:
- the newly created class
-
addAnnotationDeclaration
public AnnotationDeclaration addAnnotationDeclaration(String name, Modifier.Keyword... modifiers)
Add an annotation declaration to the types of this compilation unit- Parameters:
name
- the annotation namemodifiers
- the modifiers (like Modifier.PUBLIC)- Returns:
- the newly created class
-
getClassByName
public Optional<ClassOrInterfaceDeclaration> getClassByName(String className)
Try to get a top level class declaration by its name- Parameters:
className
- the class name (case-sensitive)
-
getLocalDeclarationFromClassname
public List<ClassOrInterfaceDeclaration> getLocalDeclarationFromClassname(String className)
Try to get all local class declarations ending by its name (top level or inner class)- Parameters:
className
- the class name (case-sensitive)
-
getInterfaceByName
public Optional<ClassOrInterfaceDeclaration> getInterfaceByName(String interfaceName)
Try to get a top level interface declaration by its name- Parameters:
interfaceName
- the interface name (case-sensitive)
-
getEnumByName
public Optional<EnumDeclaration> getEnumByName(String enumName)
Try to get a top level enum declaration by its name- Parameters:
enumName
- the enum name (case-sensitive)
-
getPrimaryTypeName
public Optional<String> getPrimaryTypeName()
- Returns:
- the name that the primary type in this file should have, according to the filename in
CompilationUnit.Storage.getFileName()
. Empty if no file information is present (when this compilation unit wasn't parsed from a file.)
-
getPrimaryType
public Optional<TypeDeclaration<?>> getPrimaryType()
- Returns:
- the type whose name corresponds to the file name. Empty if no file information is present (when this compilation unit wasn't parsed from a file.) If for some strange reason there are multiple types of this name, the first one is returned.
-
getAnnotationDeclarationByName
public Optional<AnnotationDeclaration> getAnnotationDeclarationByName(String annotationName)
Try to get a top level annotation type declaration by its name- Parameters:
annotationName
- the annotation name (case-sensitive)
-
getRecordByName
public Optional<RecordDeclaration> getRecordByName(String recordName)
Try to get a top level record declaration by its name- Parameters:
recordName
- the enum name (case-sensitive)
-
removePackageDeclaration
public CompilationUnit removePackageDeclaration()
-
getModule
public Optional<ModuleDeclaration> getModule()
- Returns:
- the module declared in this compilation unit.
-
setModule
public CompilationUnit setModule(ModuleDeclaration module)
-
removeModule
public CompilationUnit removeModule()
-
getStorage
public Optional<CompilationUnit.Storage> getStorage()
- Returns:
- information about where this compilation unit was loaded from, or empty if it wasn't loaded from a file.
-
setStorage
public CompilationUnit setStorage(Path path)
-
setStorage
public CompilationUnit setStorage(Path path, Charset charset)
-
setModule
public ModuleDeclaration setModule(String name)
Create (or overwrite) a module declaration in this compilation unit with name "name".- Returns:
- the module
-
recalculatePositions
public void recalculatePositions()
Recalculates the ranges of all nodes by looking at the sizes of the tokens. This is useful when you have manually inserted or deleted tokens and still want to use the ranges.
-
clone
public CompilationUnit clone()
-
getMetaModel
public CompilationUnitMetaModel getMetaModel()
- Overrides:
getMetaModel
in classNode
- Returns:
- get JavaParser specific node introspection information.
-
-