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
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 ClassesModifier and TypeClassDescriptionstatic class
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
ConstructorsConstructorDescriptionCompilationUnit(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
Modifier and TypeMethodDescription<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.addAnnotationDeclaration(String name)
Add a public annotation declaration to the types of this compilation unitaddAnnotationDeclaration(String name, Modifier.Keyword... modifiers)
Add an annotation declaration to the types of this compilation unitAdd a public class to the types of this compilation unitaddClass(String name, Modifier.Keyword... modifiers)
Add a class to the types of this compilation unitAdd a public enum to the types of this compilation unitaddEnum(String name, Modifier.Keyword... modifiers)
Add an enum to the types of this compilation unitaddImport(ImportDeclaration importDeclaration)
adds an import if not implicitly imported by java (i.e.Add an import to the list ofImportDeclaration
of this compilation unit
shorthand foraddImport(String)
with clazz.getName()Add an import to the list ofImportDeclaration
of this compilation unit
shorthand foraddImport(String, boolean, boolean)
with name,false,falseAdd an import to the list ofImportDeclaration
of this compilation unit
This method check if no import with the same name is already in the listaddInterface(String name)
Add a public interface class to the types of this compilation unitaddInterface(String name, Modifier.Keyword... modifiers)
Add an interface to the types of this compilation unitaddType(TypeDeclaration<?> type)
clone()
Return a list containing all comments declared in this compilation unit.getAnnotationDeclarationByName(String annotationName)
Try to get a top level annotation type declaration by its namegetClassByName(String className)
Try to get a top level class declaration by its nameDeprecated.getEnumByName(String enumName)
Try to get a top level enum declaration by its namegetImport(int i)
Retrieves the list of imports declared in this compilation unit ornull
if there is no import.getInterfaceByName(String interfaceName)
Try to get a top level interface declaration by its namegetLocalDeclarationFromClassname(String className)
Try to get all local class declarations ending by its name (top level or inner class)Retrieves the package declaration of this compilation unit.
If this compilation unit has no package declaration (default package),Optional.none()
is returned.protected Printer
protected Printer
getPrinter(PrinterConfiguration config)
getType(int i)
Convenience method that wrapsgetTypes()
.
Ifi
is out of bounds, throwsIndexOutOfBoundsException.
getTypes()
Return the list of top level types declared in this compilation unit.
If there are no types declared,none
is returned.Declare a specific printervoid
Recalculates the ranges of all nodes by looking at the sizes of the tokens.boolean
boolean
setImport(int i, ImportDeclaration imports)
setImports(NodeList<ImportDeclaration> imports)
Sets the list of imports of this compilation unit.setModule(ModuleDeclaration module)
Create (or overwrite) a module declaration in this compilation unit with name "name".setPackageDeclaration(PackageDeclaration packageDeclaration)
Sets or clear the package declarations of this compilation unit.setPackageDeclaration(String name)
sets the package declaration of this compilation unitsetStorage(Path path)
setStorage(Path path, Charset charset)
setType(int i, TypeDeclaration<?> type)
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, findCompilationUnit, 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, hasParentNode, isDescendantOf
Methods inherited from interface com.github.javaparser.ast.nodeTypes.NodeWithRange
containsWithin, containsWithinRange, getBegin, getEnd, hasRange
-
Constructor Details
-
CompilationUnit
public CompilationUnit() -
CompilationUnit
-
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 Details
-
accept
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
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
Declare a specific printer -
getPrinter
- Overrides:
getPrinter
in classNode
-
getPrinter
- Overrides:
getPrinter
in classNode
-
getComments
Deprecated.getComments was a too generic name and it could be confused with getComment or getAllContainedComments UsegetAllComments()
instead -
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
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
-
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
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
Convenience method that wrapsgetTypes()
.
Ifi
is out of bounds, throwsIndexOutOfBoundsException.
- Parameters:
i
- the index of the type declaration to retrieve
-
setImports
Sets the list of imports of this compilation unit. The list is initiallynull
.- Parameters:
imports
- the list of imports
-
setImport
-
addImport
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
Sets or clear the package declarations of this compilation unit.- Parameters:
packageDeclaration
- the packageDeclaration declaration to set ornull
to default package
-
setTypes
Sets the list of types declared in this compilation unit. -
setType
-
addType
-
setPackageDeclaration
sets the package declaration of this compilation unit- Parameters:
name
- the name of the package- Returns:
- this, the
CompilationUnit
-
addImport
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
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
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
Add a public class to the types of this compilation unit- Parameters:
name
- the class name- Returns:
- the newly created class
-
addClass
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
Add a public interface class to the types of this compilation unit- Parameters:
name
- the interface name- Returns:
- the newly created class
-
addInterface
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
Add a public enum to the types of this compilation unit- Parameters:
name
- the enum name- Returns:
- the newly created class
-
addEnum
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
Add a public annotation declaration to the types of this compilation unit- Parameters:
name
- the annotation name- Returns:
- the newly created class
-
addAnnotationDeclaration
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
Try to get a top level class declaration by its name- Parameters:
className
- the class name (case-sensitive)
-
getLocalDeclarationFromClassname
Try to get all local class declarations ending by its name (top level or inner class)- Parameters:
className
- the class name (case-sensitive)
-
getInterfaceByName
Try to get a top level interface declaration by its name- Parameters:
interfaceName
- the interface name (case-sensitive)
-
getEnumByName
Try to get a top level enum declaration by its name- Parameters:
enumName
- the enum name (case-sensitive)
-
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
- 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
Try to get a top level annotation type declaration by its name- Parameters:
annotationName
- the annotation name (case-sensitive)
-
remove
-
removePackageDeclaration
-
getModule
- Returns:
- the module declared in this compilation unit.
-
setModule
-
removeModule
-
getStorage
- Returns:
- information about where this compilation unit was loaded from, or empty if it wasn't loaded from a file.
-
setStorage
-
setStorage
-
setModule
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
-
getMetaModel
- Overrides:
getMetaModel
in classNode
- Returns:
- get JavaParser specific node introspection information.
-
replace
-
getAllComments()
instead