Class AST
AST
instance serves as the common owner of any number of
AST nodes, and as the factory for creating new AST nodes owned by that
instance.
Abstract syntax trees may be hand constructed by clients, using the
newTYPE
factory methods to create new nodes, and the
various setCHILD
methods
(see ASTNode
and its subclasses)
to connect them together.
Each AST node belongs to a unique AST instance, called the owning AST. The children of an AST node always have the same owner as their parent node. If a node from one AST is to be added to a different AST, the subtree must be cloned first to ensures that the added nodes have the correct owning AST.
There can be any number of AST nodes owned by a single AST instance that are
unparented. Each of these nodes is the root of a separate little tree of nodes.
The method ASTNode.getRoot()
navigates from any node to the root
of the tree that it is contained in. Ordinarily, an AST instance has one main
tree (rooted at a CompilationUnit
), with newly-created nodes appearing
as additional roots until they are parented somewhere under the main tree.
One can navigate from any node to its AST instance, but not conversely.
The class ASTParser
parses a string
containing a Java source code and returns an abstract syntax tree
for it. The resulting nodes carry source ranges relating the node back to
the original source characters.
Compilation units created by ASTParser
from a
source document can be serialized after arbitrary modifications
with minimal loss of original formatting. Here is an example:
Document doc = new Document("import java.util.List;\nclass X {}\n"); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(doc.get().toCharArray()); CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.recordModifications(); AST ast = cu.getAST(); ImportDeclaration id = ast.newImportDeclaration(); id.setName(ast.newName(new String[] {"java", "util", "Set"}); cu.imports().add(id); // add import declaration at end TextEdit edits = cu.rewrite(document, null); UndoEdit undo = edits.apply(document);
See also ASTRewrite
for
an alternative way to describe and serialize changes to a
read-only AST.
Clients may create instances of this class using newAST(int, boolean)
,
but this class is not intended to be subclassed.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Deprecated.static final int
Deprecated.Clients should use thegetJLSLatest()
AST API instead.static final int
Deprecated.Clients should use thegetJLSLatest()
AST API instead.static final int
Deprecated.Clients should use thegetJLSLatest()
AST API instead.static final int
Deprecated.Clients should use thegetJLSLatest()
AST API instead.static final int
Deprecated.Clients should use thegetJLSLatest()
AST API instead.static final int
Deprecated.Clients should use thegetJLSLatest()
AST API instead.static final int
Deprecated.Clients should use thegetJLSLatest()
AST API instead.static final int
Deprecated.Clients should use thegetJLSLatest()
AST API instead.static final int
Deprecated.static final int
Deprecated.static final int
Deprecated.Clients should use thegetJLSLatest()
AST API instead.static final int
Constant for indicating the AST API that handles JLS20.static final int
Constant for indicating the AST API that handles JLS21.static final int
Constant for indicating the AST API that handles JLS22.static final int
Constant for indicating the AST API that handles JLS23.static final int
Deprecated.Clients should use thegetJLSLatest()
AST API instead.static final int
Deprecated.Clients should use thegetJLSLatest()
AST API instead.static final int
Deprecated.Clients should use thegetJLSLatest()
AST API instead.static final int
Deprecated.Clients should use thegetJLSLatest()
AST API instead. -
Constructor Summary
ConstructorDescriptionAST()
Deprecated.Clients should port their code to use the latest JLS* AST API and callAST.newAST(AST.JLS12, false)
instead of using this constructor.Creates a new Java abstract syntax tree Following option keys are significant:"org.eclipse.jdt.core.compiler.source"
indicates the api level and source compatibility mode (as perJavaCore
) - defaults to 1.3"1.3"
means the source code is as per JDK 1.3 and api levelJLS3
."1.4", "1.5", "1.6", "1.7" "1.8"
implies the respective source JDK levels 1.4, 1.5, 1.6, 1.7 and api levelJLS4
."1.8"
implies the respective source JDK level 1.8 and api levelJLS8
."9", "10", "11", "12", "13", "14", "15", "16" and "17"
implies the respective JDK levels 9, 10, 11, 12, 13, 14, 15, 16 and 17 and api levelsJLS9
,JLS10
,JLS11
,JLS12
,JLS13
JLS14
,JLS15
,JLS16
andJLS17
. Additional legal values may be added later."org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures"
- indicates whether the preview is enabled or disabled legal values are"enabled"
and"disabled"
implying preview enabled and disabled respectively. -
Method Summary
Modifier and TypeMethodDescriptionint
apiLevel()
Return the API level supported by this AST.static CompilationUnit
convertCompilationUnit
(int level, org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration compilationUnitDeclaration, char[] source, Map options, boolean isResolved, org.eclipse.jdt.internal.core.CompilationUnit workingCopy, int reconcileFlags, org.eclipse.core.runtime.IProgressMonitor monitor) Deprecated.Use org.eclipse.jdt.core.dom.AST.convertCompilationUnit(int, CompilationUnitDeclaration, Map, boolean, CompilationUnit, int, IProgressMonitor) insteadstatic CompilationUnit
convertCompilationUnit
(int level, org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration compilationUnitDeclaration, Map options, boolean isResolved, org.eclipse.jdt.internal.core.CompilationUnit workingCopy, int reconcileFlags, org.eclipse.core.runtime.IProgressMonitor monitor) Internal method.createInstance
(int nodeType) Creates an unparented node of the given node type.createInstance
(Class nodeClass) Creates an unparented node of the given node class (non-abstract subclass ofASTNode
).static int
Returns latest supported JLS levelboolean
Returns true if the ast tree was created with bindings recovery, false otherwiseboolean
Returns true if the ast tree was created with bindings, false otherwiseboolean
Returns true if the ast tree was created with statements recovery, false otherwiseboolean
boolean
long
Returns the modification count for this AST.Creates and returns a new unparented annotation type declaration node for an unspecified, but legal, name; no modifiers; no javadoc; and an empty list of member declarations.Creates and returns a new unparented annotation type member declaration node for an unspecified, but legal, member name and type; no modifiers; no javadoc; and no default value.Creates and returns a new unparented anonymous class declaration node owned by this AST.Creates and returns a new unparented array access expression node owned by this AST.Creates and returns a new unparented array creation expression node owned by this AST.Creates and returns a new unparented array initializer node owned by this AST.newArrayType
(Type elementType) Creates and returns a new unparented array type node with the given element type, which cannot be an array type for API levels JLS8 and later.newArrayType
(Type elementType, int dimensions) Creates and returns a new unparented array type node with the given element type and number of dimensions.Creates a new unparented assert statement node owned by this AST.Creates and returns a new unparented assignment expression node owned by this AST.static AST
newAST
(int level) Deprecated.Clients should port their code to use the latest JLS* AST API and callAST.newAST(AST.JLS12, false)
instead of using this constructor.static AST
newAST
(int level, boolean previewEnabled) Creates a new Java abstract syntax tree (AST) following the specified set of API rules.static AST
Creates a new Java abstract syntax tree Following option keys are significant:"org.eclipse.jdt.core.compiler.source"
indicates the api level and source compatibility mode (as perJavaCore
) - defaults to 1.3"1.3"
means the source code is as per JDK 1.3 and api levelJLS3
."1.4", "1.5", "1.6", "1.7" "1.8"
implies the respective source JDK levels 1.4, 1.5, 1.6, 1.7 and api levelJLS4
."1.8"
implies the respective source JDK level 1.8 and api levelJLS8
."9", "10", "11", "12" and "13"
implies the respective JDK levels 9, 10, 11, 12 and 13 and api levelsJLS9
,JLS10
,JLS11
,JLS12
andJLS13
. Additional legal values may be added later."org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures"
- indicates whether the preview is enabled or disabled legal values are"enabled"
and"disabled"
implying preview enabled and disabled respectively.newBlock()
Creates an unparented block node owned by this AST, for an empty list of statements.Creates and returns a new block comment placeholder node.newBooleanLiteral
(boolean value) Creates and returns a new unparented boolean literal node.Creates an unparented break statement node owned by this AST.Creates and returns a new unparented default case expression node.Creates and returns a new unparented cast expression node owned by this AST.Creates a new unparented catch clause node owned by this AST.Creates and returns a new unparented character literal node.Creates and returns a new unparented class instance creation ("new") expression node owned by this AST.Creates an unparented compilation unit node owned by this AST.Creates and returns a new unparented conditional expression node owned by this AST.Creates an unparented alternate constructor ("this(...);") invocation statement node owned by this AST.Creates an unparented continue statement node owned by this AST.Creates an unparented creation reference node owned by this AST.Creates and returns a new unparented annotatable dimension node (Supported only in JLS8 level).Creates a new unparented do statement node owned by this AST.Creates and returns a new unparented `either or multi-pattern` nodeCreates a new unparented empty statement node owned by this AST.Creates a new unparented enhanced for statement node owned by this AST.Creates an unparented enum constant declaration node owned by this AST.Creates an unparented enum declaration node owned by this AST.Creates and returns a new unparented exports directive node for an unspecified, but legal, name; no target modulesCreates an unparented expression method reference node owned by this AST.newExpressionStatement
(Expression expression) Creates a new unparented expression statement node owned by this AST, for the given expression.Creates and returns a new unparented field access expression node owned by this AST.Creates a new unparented field declaration node owned by this AST, for the given variable declaration fragment.Creates a new unparented for statement node owned by this AST.Creates and returns a new unparented guarded pattern node with an unspecified pattern variable name and a null expression.Creates a new unparented if statement node owned by this AST.Creates an unparented class declaration node owned by this AST.Creates an unparented import declaration node owned by this AST.Creates and returns a new unparented infix expression node owned by this AST.Creates an unparented initializer node owned by this AST, with an empty block.Creates and returns a new unparented instanceof expression node owned by this AST.Creates a new unparented intersection type node owned by this AST.Creates and returns a new doc comment node.Creates and returns a new doc comment region node.Creates and returns a new text element node.Creates a new unparented labeled statement node owned by this AST.Creates an unparented lambda expression node owned by this AST.Creates and returns a new line comment placeholder node.Creates and returns a new unparented marker annotation node with an unspecified type name.Creates and returns a new member reference node.Creates and returns a new unparented member value pair node with an unspecified member name and value.Creates an unparented method declaration node owned by this AST.Creates an unparented method invocation expression node owned by this AST.Creates and returns a new method reference node.Creates and returns a new method reference node.newModifier
(Modifier.ModifierKeyword keyword) Creates and returns a new unparented modifier node for the given modifier.newModifiers
(int flags) Creates and returns a list of new unparented modifier nodes for the given modifier flags.Creates and returns a new unparented module declaration node for an unspecified, but legal, name; no modifiers; no javadoc; and an empty list of statements.Creates and returns a new unparented module modifier node for the given module modifier.Creates and returns a new unparented name node for the given name.Creates and returns a new unparented name node for the given name segments.newNameQualifiedType
(Name qualifier, SimpleName name) Creates and returns a new unparented name qualified type node with the given qualifier and name.Creates and returns a new unparented normal annotation node with an unspecified type name and an empty list of member value pairs.Creates and returns a new unparented null literal node.Creates and returns a new unparented null pattern node .Creates and returns a new unparented number literal node.newNumberLiteral
(String literal) Creates and returns a new unparented number literal node.Creates and returns a new unparented opens directive node for an unspecified, but legal, name; no target modulesCreates an unparented package declaration node owned by this AST.newParameterizedType
(Type type) Creates and returns a new unparented parameterized type node with the given type and an empty list of type arguments.Creates and returns a new unparented parenthesized expression node owned by this AST.Creates and returns a new unparented instanceof expression node owned by this AST.Creates and returns a new unparented postfix expression node owned by this AST.Creates and returns a new unparented prefix expression node owned by this AST.newPrimitiveType
(PrimitiveType.Code typeCode) Creates and returns a new unparented primitive type node with the given type code.Creates and returns a new unparented provides directive node for an unspecified, but legal, type; no target typesnewQualifiedName
(Name qualifier, SimpleName name) Creates and returns a new unparented qualified name node for the given qualifier and simple name child node.newQualifiedType
(Type qualifier, SimpleName name) Creates and returns a new unparented qualified type node with the given qualifier type and name.Creates an unparented record declaration node owned by this AST.Creates and returns a new unparented type pattern node with an unspecified pattern variable.Creates and returns a new unparented requires directive node for an unspecified, but legal, name;Creates a new unparented return statement node owned by this AST.newSimpleName
(String identifier) Creates and returns a new unparented simple name node for the given identifier.newSimpleType
(Name typeName) Creates and returns a new unparented simple type node with the given type name.Creates and returns a new unparented single member annotation node with an unspecified type name and value.Creates an unparented single variable declaration node owned by this AST.Creates and returns a new unparented string literal node for the empty string literal.Creates an unparented alternate super constructor ("super(...);") invocation statement node owned by this AST.Creates and returns a new unparented super field access expression node owned by this AST.Creates an unparented "super" method invocation expression node owned by this AST.Creates and returns a new unparented super method reference node owned by this AST.Creates a new unparented switch case statement node owned by this AST.Creates and returns a new unparented switch expression node owned by this AST.Creates a new unparented switch statement node owned by this AST.Creates a new unparented synchronized statement node owned by this AST.Creates and returns a new tag element node.Creates and returns a new tag property node.Creates an unparented yield statement node owned by this AST.Creates and returns a new text element node.Creates and returns a new unparented "this" expression node owned by this AST.Creates a new unparented throw statement node owned by this AST.Creates a new unparented try statement node owned by this AST.Creates an unparented class declaration node owned by this AST.Creates a new unparented local type declaration statement node owned by this AST, for the given type declaration.Creates a new unparented local type declaration statement node owned by this AST, for the given type declaration.Creates and returns a new unparented type literal expression node owned by this AST.Creates an unparented type method reference node owned by this AST.Creates and returns a new unparented type parameter type node with an unspecified type variable name and an empty list of type bounds.Creates and returns a new unparented type pattern node with an unspecified pattern variable.Creates a new unparented union type node owned by this AST.Creates and returns a new unparented uses directive node for an unspecified, but legal, name;Creates a new unparented local variable declaration expression node owned by this AST, for the given variable declaration fragment.Creates an unparented variable declaration fragment node owned by this AST.Creates a new unparented local variable declaration statement node owned by this AST, for the given variable declaration fragment.Creates a new unparented while statement node owned by this AST.Creates and returns a new unparented wildcard type node with no type bound.Creates an unparented yield statement node owned by this AST.static CompilationUnit
parseCompilationUnit
(char[] source) Deprecated.UseASTParser
instead.static CompilationUnit
parseCompilationUnit
(char[] source, String unitName, IJavaProject project) Deprecated.UseASTParser
instead.static CompilationUnit
parseCompilationUnit
(IClassFile classFile, boolean resolveBindings) Deprecated.UseASTParser
instead.static CompilationUnit
parseCompilationUnit
(ICompilationUnit unit, boolean resolveBindings) Deprecated.UseASTParser
instead.resolveWellKnownType
(String name) Returns the type binding for a "well known" type.
-
Field Details
-
JLS2
public static final int JLS2Deprecated.Clients should use thegetJLSLatest()
AST API instead.Constant for indicating the AST API that handles JLS2.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Second Edition (JLS2). JLS2 is a superset of all earlier versions of the Java language, and the JLS2 API can be used to manipulate programs written in all versions of the Java language up to and including J2SE 1.4.
- Since:
- 3.0
- See Also:
-
JLS3
public static final int JLS3Deprecated.Clients should use thegetJLSLatest()
AST API instead.Constant for indicating the AST API that handles JLS3.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Third Edition (JLS3). JLS3 is a superset of all earlier versions of the Java language, and the JLS3 API can be used to manipulate programs written in all versions of the Java language up to and including J2SE 5 (aka JDK 1.5).
- Since:
- 3.1
- See Also:
-
JLS4
public static final int JLS4Deprecated.Clients should use thegetJLSLatest()
AST API instead.Constant for indicating the AST API that handles JLS4 (aka JLS7).This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 7 Edition (JLS7) as specified by JSR336. JLS4 is a superset of all earlier versions of the Java language, and the JLS4 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 7 (aka JDK 1.7).
- Since:
- 3.7.1
- See Also:
-
JLS8
public static final int JLS8Deprecated.Clients should use thegetJLSLatest()
AST API instead.Constant for indicating the AST API that handles JLS8.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 8 Edition (JLS8) as specified by JSR337. JLS8 is a superset of all earlier versions of the Java language, and the JLS8 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 8 (aka JDK 1.8).
- Since:
- 3.10
- See Also:
-
JLS9
public static final int JLS9Deprecated.Clients should use thegetJLSLatest()
AST API instead.Constant for indicating the AST API that handles JLS9.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 9 Edition (JLS9). JLS9 is a superset of all earlier versions of the Java language, and the JLS9 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 9 (aka JDK 9).
- Since:
- 3.14
- See Also:
-
JLS10
public static final int JLS10Deprecated.Clients should use thegetJLSLatest()
AST API instead.Constant for indicating the AST API that handles JLS10.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 10 Edition (JLS10). JLS10 is a superset of all earlier versions of the Java language, and the JLS10 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 10 (aka JDK 10).
- Since:
- 3.14
- See Also:
-
JLS11
public static final int JLS11Deprecated.Clients should use thegetJLSLatest()
AST API instead.Constant for indicating the AST API that handles JLS11.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 11 Edition (JLS11). JLS11 is a superset of all earlier versions of the Java language, and the JLS11 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 11 (aka JDK 11).
- Since:
- 3.16
- See Also:
-
JLS12
public static final int JLS12Deprecated.Clients should use thegetJLSLatest()
AST API instead.Constant for indicating the AST API that handles JLS12.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 12 Edition (JLS12). JLS12 is a superset of all earlier versions of the Java language, and the JLS12 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 12 (aka JDK 12).
- Since:
- 3.18
- See Also:
-
JLS13
public static final int JLS13Deprecated.Clients should use thegetJLSLatest()
AST API instead.Constant for indicating the AST API that handles JLS13.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 13 Edition (JLS13). JLS13 is a superset of all earlier versions of the Java language, and the JLS13 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 13 (aka JDK 13).
- Since:
- 3.20
- See Also:
-
JLS14
public static final int JLS14Deprecated.Clients should use thegetJLSLatest()
AST API instead.Constant for indicating the AST API that handles JLS14.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 14 Edition (JLS14). JLS14 is a superset of all earlier versions of the Java language, and the JLS14 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 14(aka JDK 14).
- Since:
- 3.22
- See Also:
-
JLS15
public static final int JLS15Deprecated.Clients should use thegetJLSLatest()
AST API instead.Constant for indicating the AST API that handles JLS15.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 15 Edition (JLS15). JLS15 is a superset of all earlier versions of the Java language, and the JLS15 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 15(aka JDK 15).
- Since:
- 3.24
- See Also:
-
JLS16
public static final int JLS16Deprecated.Clients should use thegetJLSLatest()
AST API instead.Constant for indicating the AST API that handles JLS16.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 16 Edition (JLS16). JLS16 is a superset of all earlier versions of the Java language, and the JLS16 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 16(aka JDK 16).
- Since:
- 3.26
- See Also:
-
JLS17
public static final int JLS17Deprecated.Clients should use thegetJLSLatest()
AST API instead.Constant for indicating the AST API that handles JLS17.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 17 Edition (JLS17). JLS17 is a superset of all earlier versions of the Java language, and the JLS17 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 17(aka JDK 17).
- Since:
- 3.28
- See Also:
-
JLS18
public static final int JLS18Deprecated.Constant for indicating the AST API that handles JLS17.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 18 Edition (JLS18). JLS18 is a superset of all earlier versions of the Java language, and the JLS18 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 18(aka JDK 18).
- Since:
- 3.30
- See Also:
-
JLS19
public static final int JLS19Deprecated.Constant for indicating the AST API that handles JLS17.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 18 Edition (JLS18). JLS18 is a superset of all earlier versions of the Java language, and the JLS18 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 18(aka JDK 18).
- Since:
- 3.32
- See Also:
-
JLS20
public static final int JLS20Constant for indicating the AST API that handles JLS20.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 20 Edition (JLS20). JLS20 is a superset of all earlier versions of the Java language, and the JLS20 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 20(aka JDK 20).
- Since:
- 3.34
- See Also:
-
JLS21
public static final int JLS21Constant for indicating the AST API that handles JLS21.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 21 Edition (JLS21). JLS21 is a superset of all earlier versions of the Java language, and the JLS21 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 21(aka JDK 21).
- Since:
- 3.36
- See Also:
-
JLS22
public static final int JLS22Constant for indicating the AST API that handles JLS22.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 22 Edition (JLS22). JLS22 is a superset of all earlier versions of the Java language, and the JLS22 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 22(aka JDK 22).
- Since:
- 3.38
- See Also:
-
JLS23
public static final int JLS23Constant for indicating the AST API that handles JLS23.This API is capable of handling all constructs in the Java language as described in the Java Language Specification, Java SE 23 Edition (JLS23). JLS23 is a superset of all earlier versions of the Java language, and the JLS23 API can be used to manipulate programs written in all versions of the Java language up to and including Java SE 23(aka JDK 23).
- Since:
- 3.38
- See Also:
-
JLS_Latest
public static final int JLS_LatestDeprecated.usegetJLSLatest()
- Since:
- 3.26 This provides the latest JLS level.
- See Also:
-
-
Constructor Details
-
AST
public AST()Deprecated.Clients should port their code to use the latest JLS* AST API and callAST.newAST(AST.JLS12, false)
instead of using this constructor.Creates a new, empty abstract syntax tree using default options.- See Also:
-
AST
Creates a new Java abstract syntax tree Following option keys are significant:"org.eclipse.jdt.core.compiler.source"
indicates the api level and source compatibility mode (as perJavaCore
) - defaults to 1.3-
"1.3"
means the source code is as per JDK 1.3 and api levelJLS3
. "1.4", "1.5", "1.6", "1.7" "1.8"
implies the respective source JDK levels 1.4, 1.5, 1.6, 1.7 and api levelJLS4
."1.8"
implies the respective source JDK level 1.8 and api levelJLS8
."9", "10", "11", "12", "13", "14", "15", "16" and "17"
implies the respective JDK levels 9, 10, 11, 12, 13, 14, 15, 16 and 17 and api levelsJLS9
,JLS10
,JLS11
,JLS12
,JLS13
JLS14
,JLS15
,JLS16
andJLS17
.- Additional legal values may be added later.
-
"org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures"
- indicates whether the preview is enabled or disabled legal values are"enabled"
and"disabled"
implying preview enabled and disabled respectively. preview enabling has an effect only with the latest ast level.
- Parameters:
options
- the table of options (key type:String
; value type:String
)- See Also:
-
-
Method Details
-
convertCompilationUnit
public static CompilationUnit convertCompilationUnit(int level, org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration compilationUnitDeclaration, char[] source, Map options, boolean isResolved, org.eclipse.jdt.internal.core.CompilationUnit workingCopy, int reconcileFlags, org.eclipse.core.runtime.IProgressMonitor monitor) Deprecated.Use org.eclipse.jdt.core.dom.AST.convertCompilationUnit(int, CompilationUnitDeclaration, Map, boolean, CompilationUnit, int, IProgressMonitor) insteadInternal method.This method converts the given internal compiler AST for the given source string into a compilation unit. This method is not intended to be called by clients.
- Parameters:
level
- the API level; one of theJLS*
level constantscompilationUnitDeclaration
- an internal AST node for a compilation unit declarationsource
- the string of the Java compilation unitoptions
- compiler optionsworkingCopy
- the working copy that the AST is created frommonitor
- the progress monitor used to report progress and request cancellation, ornull
if noneisResolved
- whether the given compilation unit declaration is resolved- Returns:
- the compilation unit node
- Restriction:
- This method is not intended to be referenced by clients.
-
convertCompilationUnit
public static CompilationUnit convertCompilationUnit(int level, org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration compilationUnitDeclaration, Map options, boolean isResolved, org.eclipse.jdt.internal.core.CompilationUnit workingCopy, int reconcileFlags, org.eclipse.core.runtime.IProgressMonitor monitor) Internal method.This method converts the given internal compiler AST for the given source string into a compilation unit. This method is not intended to be called by clients.
- Parameters:
level
- the API level; one of theJLS*
level constantscompilationUnitDeclaration
- an internal AST node for a compilation unit declarationoptions
- compiler optionsworkingCopy
- the working copy that the AST is created frommonitor
- the progress monitor used to report progress and request cancellation, ornull
if noneisResolved
- whether the given compilation unit declaration is resolved- Returns:
- the compilation unit node
- Since:
- 3.4
- Restriction:
- This method is not intended to be referenced by clients.
-
newAST
Deprecated.Clients should port their code to use the latest JLS* AST API and callAST.newAST(AST.JLS12, false)
instead of using this constructor.Creates a new Java abstract syntax tree (AST) following the specified set of API rules.Clients should use this method specifying
JLS12
as the AST level in all cases, even when dealing with source of earlier JDK versions like 1.3 or 1.4.- Parameters:
level
- the API level; one of theJLS*
level constants- Returns:
- new AST instance following the specified set of API rules.
- Throws:
IllegalArgumentException
- if:- the API level is not one of the
JLS*
level constants
- the API level is not one of the
- Since:
- 3.0
-
newAST
Creates a new Java abstract syntax tree (AST) following the specified set of API rules.Clients should use this method specifying
JLS12
as the AST level in all cases, even when dealing with source of earlier JDK versions like 1.3 or 1.4.- Parameters:
level
- the API level; one of theJLS*
level constantspreviewEnabled
-true
if preview feature is enabled elsefalse
- Returns:
- new AST instance following the specified set of API rules.
- Throws:
IllegalArgumentException
- if:- the API level is not one of the
JLS*
level constants
- the API level is not one of the
- Since:
- 3.19
-
newAST
Creates a new Java abstract syntax tree Following option keys are significant:"org.eclipse.jdt.core.compiler.source"
indicates the api level and source compatibility mode (as perJavaCore
) - defaults to 1.3-
"1.3"
means the source code is as per JDK 1.3 and api levelJLS3
. "1.4", "1.5", "1.6", "1.7" "1.8"
implies the respective source JDK levels 1.4, 1.5, 1.6, 1.7 and api levelJLS4
."1.8"
implies the respective source JDK level 1.8 and api levelJLS8
."9", "10", "11", "12" and "13"
implies the respective JDK levels 9, 10, 11, 12 and 13 and api levelsJLS9
,JLS10
,JLS11
,JLS12
andJLS13
.- Additional legal values may be added later.
-
"org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures"
- indicates whether the preview is enabled or disabled legal values are"enabled"
and"disabled"
implying preview enabled and disabled respectively. preview enabling has an effect only with the latest ast level.
- Parameters:
options
- the table of options- Since:
- 3.20
- See Also:
-
parseCompilationUnit
Deprecated.UseASTParser
instead.Parses the given string as a Java compilation unit and creates and returns a corresponding abstract syntax tree.The returned compilation unit node is the root node of a new AST. Each node in the subtree carries source range(s) information relating back to positions in the given source string (the given source string itself is not remembered with the AST). The source range usually begins at the first character of the first token corresponding to the node; leading whitespace and comments are not included. The source range usually extends through the last character of the last token corresponding to the node; trailing whitespace and comments are not included. There are a handful of exceptions (including compilation units and the various body declarations); the specification for these node type spells out the details. Source ranges nest properly: the source range for a child is always within the source range of its parent, and the source ranges of sibling nodes never overlap. If a syntax error is detected while parsing, the relevant node(s) of the tree will be flagged as
MALFORMED
.This method does not compute binding information; all
resolveBinding
methods applied to nodes of the resulting AST returnnull
.- Parameters:
source
- the string to be parsed as a Java compilation unit- Returns:
- the compilation unit node
- Since:
- 2.0
- See Also:
-
parseCompilationUnit
public static CompilationUnit parseCompilationUnit(char[] source, String unitName, IJavaProject project) Deprecated.UseASTParser
instead.Parses the given string as the hypothetical contents of the named compilation unit and creates and returns a corresponding abstract syntax tree.The returned compilation unit node is the root node of a new AST. Each node in the subtree carries source range(s) information relating back to positions in the given source string (the given source string itself is not remembered with the AST). The source range usually begins at the first character of the first token corresponding to the node; leading whitespace and comments are not included. The source range usually extends through the last character of the last token corresponding to the node; trailing whitespace and comments are not included. There are a handful of exceptions (including compilation units and the various body declarations); the specification for these node type spells out the details. Source ranges nest properly: the source range for a child is always within the source range of its parent, and the source ranges of sibling nodes never overlap. If a syntax error is detected while parsing, the relevant node(s) of the tree will be flagged as
MALFORMED
.If the given project is not
null
, the various names and types appearing in the compilation unit can be resolved to "bindings" by calling theresolveBinding
methods. These bindings draw connections between the different parts of a program, and generally afford a more powerful vantage point for clients who wish to analyze a program's structure more deeply. These bindings come at a considerable cost in both time and space, however, and should not be requested frivolously. The additional space is not reclaimed until the AST, all its nodes, and all its bindings become garbage. So it is very important to not retain any of these objects longer than absolutely necessary. Bindings are resolved at the time the AST is created. Subsequent modifications to the AST do not affect the bindings returned byresolveBinding
methods in any way; these methods return the same binding as before the AST was modified (including modifications that rearrange subtrees by reparenting nodes). If the given project isnull
, the analysis does not go beyond parsing and building the tree, and allresolveBinding
methods returnnull
from the outset.The name of the compilation unit must be supplied for resolving bindings. This name should be suffixed by a dot ('.') followed by one of the
Java-like extensions
and match the name of the main (public) class or interface declared in the source. For example, if the source declares a public class named "Foo", the name of the compilation can be "Foo.java". For the purposes of resolving bindings, types declared in the source string hide types by the same name available through the classpath of the given project.- Parameters:
source
- the string to be parsed as a Java compilation unitunitName
- the name of the compilation unit that would contain the source string, ornull
ifjavaProject
is alsonull
project
- the Java project used to resolve names, ornull
if bindings are not resolved- Returns:
- the compilation unit node
- Since:
- 2.0
- See Also:
-
parseCompilationUnit
Deprecated.UseASTParser
instead.Parses the source string corresponding to the given Java class file element and creates and returns a corresponding abstract syntax tree. The source string is obtained from the Java model element usingIClassFile.getSource()
, and is only available for a class files with attached source.The returned compilation unit node is the root node of a new AST. Each node in the subtree carries source range(s) information relating back to positions in the source string (the source string is not remembered with the AST). The source range usually begins at the first character of the first token corresponding to the node; leading whitespace and comments are not included. The source range usually extends through the last character of the last token corresponding to the node; trailing whitespace and comments are not included. There are a handful of exceptions (including compilation units and the various body declarations); the specification for these node type spells out the details. Source ranges nest properly: the source range for a child is always within the source range of its parent, and the source ranges of sibling nodes never overlap. If a syntax error is detected while parsing, the relevant node(s) of the tree will be flagged as
MALFORMED
.If
resolveBindings
istrue
, the various names and types appearing in the compilation unit can be resolved to "bindings" by calling theresolveBinding
methods. These bindings draw connections between the different parts of a program, and generally afford a more powerful vantage point for clients who wish to analyze a program's structure more deeply. These bindings come at a considerable cost in both time and space, however, and should not be requested frivolously. The additional space is not reclaimed until the AST, all its nodes, and all its bindings become garbage. So it is very important to not retain any of these objects longer than absolutely necessary. Bindings are resolved at the time the AST is created. Subsequent modifications to the AST do not affect the bindings returned byresolveBinding
methods in any way; these methods return the same binding as before the AST was modified (including modifications that rearrange subtrees by reparenting nodes). IfresolveBindings
isfalse
, the analysis does not go beyond parsing and building the tree, and allresolveBinding
methods returnnull
from the outset.- Parameters:
classFile
- the Java model class file whose corresponding source code is to be parsedresolveBindings
-true
if bindings are wanted, andfalse
if bindings are not of interest- Returns:
- the compilation unit node
- Throws:
IllegalArgumentException
- if the given Java element does not exist or if its source string cannot be obtained- Since:
- 2.1
- See Also:
-
parseCompilationUnit
Deprecated.UseASTParser
instead.Parses the source string of the given Java model compilation unit element and creates and returns a corresponding abstract syntax tree. The source string is obtained from the Java model element usingICompilationUnit.getSource()
.The returned compilation unit node is the root node of a new AST. Each node in the subtree carries source range(s) information relating back to positions in the source string (the source string is not remembered with the AST). The source range usually begins at the first character of the first token corresponding to the node; leading whitespace and comments are not included. The source range usually extends through the last character of the last token corresponding to the node; trailing whitespace and comments are not included. There are a handful of exceptions (including compilation units and the various body declarations); the specification for these node type spells out the details. Source ranges nest properly: the source range for a child is always within the source range of its parent, and the source ranges of sibling nodes never overlap. If a syntax error is detected while parsing, the relevant node(s) of the tree will be flagged as
MALFORMED
.If
resolveBindings
istrue
, the various names and types appearing in the compilation unit can be resolved to "bindings" by calling theresolveBinding
methods. These bindings draw connections between the different parts of a program, and generally afford a more powerful vantage point for clients who wish to analyze a program's structure more deeply. These bindings come at a considerable cost in both time and space, however, and should not be requested frivolously. The additional space is not reclaimed until the AST, all its nodes, and all its bindings become garbage. So it is very important to not retain any of these objects longer than absolutely necessary. Bindings are resolved at the time the AST is created. Subsequent modifications to the AST do not affect the bindings returned byresolveBinding
methods in any way; these methods return the same binding as before the AST was modified (including modifications that rearrange subtrees by reparenting nodes). IfresolveBindings
isfalse
, the analysis does not go beyond parsing and building the tree, and allresolveBinding
methods returnnull
from the outset.- Parameters:
unit
- the Java model compilation unit whose source code is to be parsedresolveBindings
-true
if bindings are wanted, andfalse
if bindings are not of interest- Returns:
- the compilation unit node
- Throws:
IllegalArgumentException
- if the given Java element does not exist or if its source string cannot be obtained- Since:
- 2.0
- See Also:
-
apiLevel
public int apiLevel()Return the API level supported by this AST.- Returns:
- level the API level; one of the
JLS*
level constants declared onAST
; assume this set is open-ended - Since:
- 3.0
-
createInstance
Creates an unparented node of the given node class (non-abstract subclass ofASTNode
).- Parameters:
nodeClass
- AST node class- Returns:
- a new unparented node owned by this AST
- Throws:
IllegalArgumentException
- ifnodeClass
isnull
or is not a concrete node type class or is not supported for this AST's API level- Since:
- 3.0
-
createInstance
Creates an unparented node of the given node type. This convenience method is equivalent to:createInstance(ASTNode.nodeClassForType(nodeType))
- Parameters:
nodeType
- AST node type, one of the node type constants declared onASTNode
- Returns:
- a new unparented node owned by this AST
- Throws:
IllegalArgumentException
- ifnodeType
is not a legal AST node type or if it's not supported for this AST's API level- Since:
- 3.0
-
hasBindingsRecovery
public boolean hasBindingsRecovery()Returns true if the ast tree was created with bindings recovery, false otherwise- Returns:
- true if the ast tree was created with bindings recovery, false otherwise
- Since:
- 3.3
-
hasResolvedBindings
public boolean hasResolvedBindings()Returns true if the ast tree was created with bindings, false otherwise- Returns:
- true if the ast tree was created with bindings, false otherwise
- Since:
- 3.3
-
hasStatementsRecovery
public boolean hasStatementsRecovery()Returns true if the ast tree was created with statements recovery, false otherwise- Returns:
- true if the ast tree was created with statements recovery, false otherwise
- Since:
- 3.3
-
modificationCount
public long modificationCount()Returns the modification count for this AST. The modification count is a non-negative value that increases (by 1 or perhaps by more) as this AST or its nodes are changed. The initial value is unspecified.The following things count as modifying an AST:
- creating a new node owned by this AST,
- adding a child to a node owned by this AST,
- removing a child from a node owned by this AST,
- setting a non-node attribute of a node owned by this AST.
Operations which do not entail creating or modifying existing nodes do not increase the modification count.
N.B. This method may be called several times in the course of a single client operation. The only promise is that the modification count increases monotonically as the AST or its nodes change; there is no promise that a modifying operation increases the count by exactly 1.
- Returns:
- the current value (non-negative) of the modification counter of this AST
-
newAnnotationTypeDeclaration
Creates and returns a new unparented annotation type declaration node for an unspecified, but legal, name; no modifiers; no javadoc; and an empty list of member declarations.- Returns:
- a new unparented annotation type declaration node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newAnnotationTypeMemberDeclaration
Creates and returns a new unparented annotation type member declaration node for an unspecified, but legal, member name and type; no modifiers; no javadoc; and no default value.- Returns:
- a new unparented annotation type member declaration node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newAnonymousClassDeclaration
Creates and returns a new unparented anonymous class declaration node owned by this AST. By default, the body declaration list is empty.- Returns:
- a new unparented anonymous class declaration node
-
newArrayAccess
Creates and returns a new unparented array access expression node owned by this AST. By default, the array and index expression are both unspecified (but legal).- Returns:
- a new unparented array access expression node
-
newArrayCreation
Creates and returns a new unparented array creation expression node owned by this AST. By default, the array type is an unspecified 1-dimensional array, the list of dimensions is empty, and there is no array initializer.Examples:
// new String[len] ArrayCreation ac1 = ast.newArrayCreation(); ac1.setType( ast.newArrayType( ast.newSimpleType(ast.newSimpleName("String")))); ac1.dimensions().add(ast.newSimpleName("len")); // new double[7][24][] ArrayCreation ac2 = ast.newArrayCreation(); ac2.setType( ast.newArrayType( ast.newPrimitiveType(PrimitiveType.DOUBLE), 3)); ac2.dimensions().add(ast.newNumberLiteral("7")); ac2.dimensions().add(ast.newNumberLiteral("24")); // new int[] {1, 2} ArrayCreation ac3 = ast.newArrayCreation(); ac3.setType( ast.newArrayType( ast.newPrimitiveType(PrimitiveType.INT))); ArrayInitializer ai = ast.newArrayInitializer(); ac3.setInitializer(ai); ai.expressions().add(ast.newNumberLiteral("1")); ai.expressions().add(ast.newNumberLiteral("2"));
- Returns:
- a new unparented array creation expression node
-
newArrayInitializer
Creates and returns a new unparented array initializer node owned by this AST. By default, the initializer has no expressions.- Returns:
- a new unparented array initializer node
-
newArrayType
Creates and returns a new unparented array type node with the given element type, which cannot be an array type for API levels JLS8 and later. By default, the array type has one non-annotated dimension.For JLS4 and before, the given component type may be another array type.
- Parameters:
elementType
- element type for API level JLS8 and later, or the component type (possibly another array type) for levels less than JLS8- Returns:
- a new unparented array type node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
- API level is JLS8 or later and type is an array type
-
newArrayType
Creates and returns a new unparented array type node with the given element type and number of dimensions.For JLS4 and before, the element type passed in can be an array type, but in that case, the element type of the result will not be the same as what was passed in. For JLS4 and before, the dimensions cannot be 0.
- Parameters:
elementType
- the element type (cannot be an array type for JLS8 and later)dimensions
- the number of dimensions, a non-negative number- Returns:
- a new unparented array type node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
- the element type is null
- the number of dimensions is lower than 0 (for JLS4 and before: lower than 1)
- the number of dimensions is greater than 255
- for levels from JLS8 and later, if the element type is an array type
-
newAssertStatement
Creates a new unparented assert statement node owned by this AST. By default, the first expression is unspecified, but legal, and has no message expression.- Returns:
- a new unparented assert statement node
-
newAssignment
Creates and returns a new unparented assignment expression node owned by this AST. By default, the assignment operator is "=" and the left and right hand side expressions are unspecified, but legal, names.- Returns:
- a new unparented assignment expression node
-
newBlock
Creates an unparented block node owned by this AST, for an empty list of statements.- Returns:
- a new unparented, empty block node
-
newBlockComment
Creates and returns a new block comment placeholder node.Note that this node type is used to recording the source range where a comment was found in the source string. These comment nodes are normally found (only) in the comment table for parsed compilation units.
- Returns:
- a new unparented block comment node
- Since:
- 3.0
-
newBooleanLiteral
Creates and returns a new unparented boolean literal node.For example, the assignment expression
foo = true
is generated by the following snippet:Assignment e= ast.newAssignment(); e.setLeftHandSide(ast.newSimpleName("foo")); e.setRightHandSide(ast.newBooleanLiteral(true));
- Parameters:
value
- the boolean value- Returns:
- a new unparented boolean literal node
-
newBreakStatement
Creates an unparented break statement node owned by this AST. The break statement has no label/identifier/expression and is not implicit.- Returns:
- a new unparented break statement node
-
newCaseDefaultExpression
Creates and returns a new unparented default case expression node.- Returns:
- a new unparented default case expression node
- Since:
- 3.28
-
newCastExpression
Creates and returns a new unparented cast expression node owned by this AST. By default, the type and expression are unspecified (but legal).- Returns:
- a new unparented cast expression node
-
newCatchClause
Creates a new unparented catch clause node owned by this AST. By default, the catch clause declares an unspecified, but legal, exception declaration and has an empty block.- Returns:
- a new unparented catch clause node
-
newCharacterLiteral
Creates and returns a new unparented character literal node. Initially the node has an unspecified character literal.- Returns:
- a new unparented character literal node
-
newClassInstanceCreation
Creates and returns a new unparented class instance creation ("new") expression node owned by this AST. By default, there is no qualifying expression, no type parameters, an unspecified (but legal) type name, an empty list of arguments, and does not declare an anonymous class declaration.- Returns:
- a new unparented class instance creation expression node
-
newCompilationUnit
Creates an unparented compilation unit node owned by this AST. The compilation unit initially has no package declaration, no import declarations, and no type declarations.- Returns:
- the new unparented compilation unit node
-
newConditionalExpression
Creates and returns a new unparented conditional expression node owned by this AST. By default, the condition and both expressions are unspecified (but legal).- Returns:
- a new unparented array conditional expression node
-
newConstructorInvocation
Creates an unparented alternate constructor ("this(...);") invocation statement node owned by this AST. By default, the lists of arguments and type arguments are both empty.Note that this type of node is a Statement, whereas a regular method invocation is an Expression. The only valid use of these statements are as the first statement of a constructor body.
- Returns:
- a new unparented alternate constructor invocation statement node
-
newContinueStatement
Creates an unparented continue statement node owned by this AST. The continue statement has no label.- Returns:
- a new unparented continue statement node
-
newCreationReference
Creates an unparented creation reference node owned by this AST. By default, the type is unspecified (but legal), and there are no type arguments.- Returns:
- a new unparented creation reference expression node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2, JLS3 or JLS4 AST- Since:
- 3.10
-
newDoStatement
Creates a new unparented do statement node owned by this AST. By default, the expression is unspecified (but legal), and the body statement is an empty block.- Returns:
- a new unparented do statement node
-
newEmptyStatement
Creates a new unparented empty statement node owned by this AST.- Returns:
- a new unparented empty statement node
-
newEnhancedForStatement
Creates a new unparented enhanced for statement node owned by this AST. By default, the paramter and expression are unspecified but legal subtrees, and the body is an empty block.- Returns:
- a new unparented throw statement node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newEnumConstantDeclaration
Creates an unparented enum constant declaration node owned by this AST. The name of the constant is an unspecified, but legal, name; no doc comment; no modifiers or annotations; no arguments; and does not declare an anonymous class.- Returns:
- a new unparented enum constant declaration node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newEnumDeclaration
Creates an unparented enum declaration node owned by this AST. The name of the enum is an unspecified, but legal, name; no doc comment; no modifiers or annotations; no superinterfaces; and empty lists of enum constants and body declarations.- Returns:
- a new unparented enum declaration node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newExportsStatement
Creates and returns a new unparented exports directive node for an unspecified, but legal, name; no target modules- Returns:
- a new unparented exports directive node
- Throws:
UnsupportedOperationException
- if this operation is used in an AST with level less than JLS9- Since:
- 3.14
-
newExpressionMethodReference
Creates an unparented expression method reference node owned by this AST. By default, the expression and method name are unspecified (but legal), and there are no type arguments.- Returns:
- a new unparented expression method reference expression node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2, JLS3 or JLS4 AST- Since:
- 3.10
-
newExpressionStatement
Creates a new unparented expression statement node owned by this AST, for the given expression.This method can be used to convert an expression (
Expression
) into a statement (Type
) by wrapping it. Note, however, that the result is only legal for limited expression types, including method invocations, assignments, and increment/decrement operations.- Parameters:
expression
- the expression- Returns:
- a new unparented statement node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
-
newDimension
Creates and returns a new unparented annotatable dimension node (Supported only in JLS8 level).- Returns:
- a new unparented annotatable dimension node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
UnsupportedOperationException
- if this operation is used in a JLS2, JLS3 or JLS4 AST- Since:
- 3.10
-
newFieldAccess
Creates and returns a new unparented field access expression node owned by this AST. By default, the expression and field are both unspecified, but legal, names.- Returns:
- a new unparented field access expression node
-
newFieldDeclaration
Creates a new unparented field declaration node owned by this AST, for the given variable declaration fragment. By default, there are no modifiers, no doc comment, and the base type is unspecified (but legal).This method can be used to wrap a variable declaration fragment (
VariableDeclarationFragment
) into a field declaration suitable for inclusion in the body of a type declaration (FieldDeclaration
implementsBodyDeclaration
). Additional variable declaration fragments can be added afterwards.- Parameters:
fragment
- the variable declaration fragment- Returns:
- a new unparented field declaration node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
- the given fragment is null
-
newForStatement
Creates a new unparented for statement node owned by this AST. By default, there are no initializers, no condition expression, no updaters, and the body is an empty block.- Returns:
- a new unparented for statement node
-
newGuardedPattern
Creates and returns a new unparented guarded pattern node with an unspecified pattern variable name and a null expression.- Returns:
- a new unparented guarded pattern node
- Since:
- 3.28
-
newIfStatement
Creates a new unparented if statement node owned by this AST. By default, the expression is unspecified (but legal), the then statement is an empty block, and there is no else statement.- Returns:
- a new unparented if statement node
-
newImportDeclaration
Creates an unparented import declaration node owned by this AST. The import declaration initially contains a single-type import of a type with an unspecified name.- Returns:
- the new unparented import declaration node
-
newInfixExpression
Creates and returns a new unparented infix expression node owned by this AST. By default, the operator and left and right operand are unspecified (but legal), and there are no extended operands.- Returns:
- a new unparented infix expression node
-
newInitializer
Creates an unparented initializer node owned by this AST, with an empty block. By default, the initializer has no modifiers and an empty block.- Returns:
- a new unparented initializer node
-
newInstanceofExpression
Creates and returns a new unparented instanceof expression node owned by this AST. By default, the operator and left and right operand are unspecified (but legal).- Returns:
- a new unparented instanceof expression node
-
newJavadoc
Creates and returns a new doc comment node. Initially the new node has an empty list of tag elements (and, for backwards compatability, an unspecified, but legal, doc comment string)- Returns:
- a new unparented doc comment node
-
newJavaDocRegion
Creates and returns a new doc comment region node. Initially the new node has an empty list of tag elements (and, for backwards compatability, an unspecified, but legal, doc comment string)- Returns:
- a new unparented doc comment node
- Since:
- 3.30
-
newJavaDocTextElement
Creates and returns a new text element node. Initially the new node has an empty text string.Note that this node type is used only inside doc comments (
Javadoc
).- Returns:
- a new unparented JavaDoc text element node
- Since:
- 3.31
-
newLabeledStatement
Creates a new unparented labeled statement node owned by this AST. By default, the label and statement are both unspecified, but legal.- Returns:
- a new unparented labeled statement node
-
newLambdaExpression
Creates an unparented lambda expression node owned by this AST. By default, the new lambda expression has parentheses enabled, contains an empty argument list, and the body is an empty block.- Returns:
- a new unparented lambda expression node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2, JLS3 or JLS4 AST- Since:
- 3.10
-
newLineComment
Creates and returns a new line comment placeholder node.Note that this node type is used to recording the source range where a comment was found in the source string. These comment nodes are normally found (only) in the comment table for parsed compilation units.
- Returns:
- a new unparented line comment node
- Since:
- 3.0
-
newMarkerAnnotation
Creates and returns a new unparented marker annotation node with an unspecified type name.- Returns:
- a new unparented marker annotation node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newMemberRef
Creates and returns a new member reference node. Initially the new node has no qualifier name and an unspecified, but legal, member name.Note that this node type is used only inside doc comments (
Javadoc
).- Returns:
- a new unparented member reference node
- Since:
- 3.0
-
newMemberValuePair
Creates and returns a new unparented member value pair node with an unspecified member name and value.- Returns:
- a new unparented member value pair node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newMethodDeclaration
Creates an unparented method declaration node owned by this AST. By default, the declaration is for a method of an unspecified, but legal, name; no modifiers; no doc comment; no parameters; return type void; no extra array dimensions; no thrown exceptions; and no body (as opposed to an empty body).To create a constructor, use this method and then call
MethodDeclaration.setConstructor(true)
andMethodDeclaration.setName(className)
.- Returns:
- a new unparented method declaration node
-
newMethodInvocation
Creates an unparented method invocation expression node owned by this AST. By default, the name of the method is unspecified (but legal) there is no receiver expression, no type arguments, and the list of arguments is empty.- Returns:
- a new unparented method invocation expression node
-
newMethodRef
Creates and returns a new method reference node. Initially the new node has no qualifier name, an unspecified, but legal, method name, and an empty parameter list.Note that this node type is used only inside doc comments (
Javadoc
).- Returns:
- a new unparented method reference node
- Since:
- 3.0
-
newMethodRefParameter
Creates and returns a new method reference node. Initially the new node has an unspecified, but legal, type, not variable arity, and no parameter name.Note that this node type is used only inside doc comments (
Javadoc
).- Returns:
- a new unparented method reference parameter node
- Since:
- 3.0
-
newModifier
Creates and returns a new unparented modifier node for the given modifier.- Parameters:
keyword
- one of the modifier keyword constants- Returns:
- a new unparented modifier node
- Throws:
IllegalArgumentException
- if the primitive type code is invalidUnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newModuleModifier
Creates and returns a new unparented module modifier node for the given module modifier.- Parameters:
keyword
- one of the module modifier keyword constants- Returns:
- a new unparented module modifier node
- Throws:
IllegalArgumentException
- if the primitive type code is invalidUnsupportedOperationException
- if this operation is used in an AST with level less than JLS9- Since:
- 3.14
-
newModifiers
Creates and returns a list of new unparented modifier nodes for the given modifier flags. When multiple modifiers are requested, the modifier nodes will appear in the following order:public protected private abstract default static final synchronized native strictfp transient volatile
This order is consistent with the recommendations in JLS8 ("*Modifier:" rules in chapters 8 and 9).
- Parameters:
flags
- bitwise or of modifier flags declared onModifier
- Returns:
- a possibly empty list of new unparented modifier nodes
(element type
Modifier
) - Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newModuleDeclaration
Creates and returns a new unparented module declaration node for an unspecified, but legal, name; no modifiers; no javadoc; and an empty list of statements.- Returns:
- a new unparented module declaration node
- Throws:
UnsupportedOperationException
- if this operation is used in an AST with level less than JLS9- Since:
- 3.14
-
newName
Creates and returns a new unparented name node for the given name. The name string must consist of 1 or more name segments separated by single dots '.'. Returns aQualifiedName
if the name has dots, and aSimpleName
otherwise. Each of the name segments should be legal Java identifiers (this constraint may or may not be enforced), and there must be at least one name segment. The string must not contains white space, '<', '>', '[', ']', or other any other characters that are not part of the Java identifiers or separating '.'s.- Parameters:
qualifiedName
- string consisting of 1 or more name segments, each of which is a legal Java identifier, separated by single dots '.'- Returns:
- a new unparented name node
- Throws:
IllegalArgumentException
- if:- the string is empty
- the string begins or ends in a '.'
- the string has adjacent '.'s
- the segments between the '.'s are not valid Java identifiers
- Since:
- 3.1
-
newName
Creates and returns a new unparented name node for the given name segments. Returns a simple name if there is only one name segment, and a qualified name if there are multiple name segments. Each of the name segments should be legal Java identifiers (this constraint may or may not be enforced), and there must be at least one name segment.- Parameters:
identifiers
- a list of 1 or more name segments, each of which is a legal Java identifier- Returns:
- a new unparented name node
- Throws:
IllegalArgumentException
- if:- the identifier is invalid
- the list of identifiers is empty
-
newNameQualifiedType
Creates and returns a new unparented name qualified type node with the given qualifier and name.- Parameters:
qualifier
- the name qualifier name nodename
- the simple name being qualified- Returns:
- a new unparented qualified type node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
UnsupportedOperationException
- if this operation is used in a JLS2, JLS3 and JLS4 AST- Since:
- 3.10
-
newNormalAnnotation
Creates and returns a new unparented normal annotation node with an unspecified type name and an empty list of member value pairs.- Returns:
- a new unparented normal annotation node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newNullLiteral
Creates and returns a new unparented null literal node.- Returns:
- a new unparented null literal node
-
newNullPattern
Creates and returns a new unparented null pattern node .- Returns:
- a new unparented null pattern node
- Since:
- 3.28
-
newNumberLiteral
Creates and returns a new unparented number literal node. Initially the number literal token is"0"
.- Returns:
- a new unparented number literal node
-
newNumberLiteral
Creates and returns a new unparented number literal node.- Parameters:
literal
- the token for the numeric literal as it would appear in Java source code- Returns:
- a new unparented number literal node
- Throws:
IllegalArgumentException
- if the literal is null
-
newOpensDirective
Creates and returns a new unparented opens directive node for an unspecified, but legal, name; no target modules- Returns:
- a new unparented opens directive node
- Throws:
UnsupportedOperationException
- if this operation is used in an AST with level less than JLS9- Since:
- 3.14
-
newPackageDeclaration
Creates an unparented package declaration node owned by this AST. The package declaration initially declares a package with an unspecified name.- Returns:
- the new unparented package declaration node
-
newParameterizedType
Creates and returns a new unparented parameterized type node with the given type and an empty list of type arguments.- Parameters:
type
- the type that is parameterized- Returns:
- a new unparented parameterized type node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newParenthesizedExpression
Creates and returns a new unparented parenthesized expression node owned by this AST. By default, the expression is unspecified (but legal).- Returns:
- a new unparented parenthesized expression node
-
newPatternInstanceofExpression
Creates and returns a new unparented instanceof expression node owned by this AST. By default, the operator and left and right operand are unspecified (but legal).- Returns:
- a new unparented instanceof expression node
- Since:
- 3.26
-
newPostfixExpression
Creates and returns a new unparented postfix expression node owned by this AST. By default, the operator and operand are unspecified (but legal).- Returns:
- a new unparented postfix expression node
-
newPrefixExpression
Creates and returns a new unparented prefix expression node owned by this AST. By default, the operator and operand are unspecified (but legal).- Returns:
- a new unparented prefix expression node
-
newPrimitiveType
Creates and returns a new unparented primitive type node with the given type code.- Parameters:
typeCode
- one of the primitive type code constants declared inPrimitiveType
- Returns:
- a new unparented primitive type node
- Throws:
IllegalArgumentException
- if the primitive type code is invalid
-
newProvidesDirective
Creates and returns a new unparented provides directive node for an unspecified, but legal, type; no target types- Returns:
- a new unparented provides directive node
- Throws:
UnsupportedOperationException
- if this operation is used in an AST with level less than JLS9- Since:
- 3.14
-
newQualifiedName
Creates and returns a new unparented qualified name node for the given qualifier and simple name child node.- Parameters:
qualifier
- the qualifier name nodename
- the simple name being qualified- Returns:
- a new unparented qualified name node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
-
newQualifiedType
Creates and returns a new unparented qualified type node with the given qualifier type and name.- Parameters:
qualifier
- the qualifier type nodename
- the simple name being qualified- Returns:
- a new unparented qualified type node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newRecordDeclaration
Creates an unparented record declaration node owned by this AST. The name of the class is an unspecified, but legal, name; no modifiers; no doc comment; no superclass or superinterfaces; and an empty record body.- Returns:
- a new unparented type declaration node
- Throws:
UnsupportedOperationException
- if this operation is used in an AST with level less than JLS16- Since:
- 3.23
-
newRecordPattern
Creates and returns a new unparented type pattern node with an unspecified pattern variable.- Returns:
- a new unparented type pattern node
- Since:
- 3.32
-
newEitherOrMultiPattern
Creates and returns a new unparented `either or multi-pattern` node- Returns:
- a new unparented `either or multi-pattern` node
- Since:
- 3.38
-
newRequiresDirective
Creates and returns a new unparented requires directive node for an unspecified, but legal, name;- Returns:
- a new unparented requires directive node
- Throws:
UnsupportedOperationException
- if this operation is used in an AST with level less than JLS9- Since:
- 3.14
-
newReturnStatement
Creates a new unparented return statement node owned by this AST. By default, the return statement has no expression.- Returns:
- a new unparented return statement node
-
newSimpleName
Creates and returns a new unparented simple name node for the given identifier. The identifier should be a legal Java identifier, but not a keyword, boolean literal ("true", "false") or null literal ("null").- Parameters:
identifier
- the identifier- Returns:
- a new unparented simple name node
- Throws:
IllegalArgumentException
- if the identifier is invalid
-
newSimpleType
Creates and returns a new unparented simple type node with the given type name.This method can be used to convert a name (
Name
) into a type (Type
) by wrapping it.- Parameters:
typeName
- the name of the class or interface- Returns:
- a new unparented simple type node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
-
newSingleMemberAnnotation
Creates and returns a new unparented single member annotation node with an unspecified type name and value.- Returns:
- a new unparented single member annotation node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newSingleVariableDeclaration
Creates an unparented single variable declaration node owned by this AST. By default, the declaration is for a variable with an unspecified, but legal, name and type; no modifiers; no array dimensions after the variable; no initializer; not variable arity.- Returns:
- a new unparented single variable declaration node
-
newStringLiteral
Creates and returns a new unparented string literal node for the empty string literal.- Returns:
- a new unparented string literal node
-
newSuperConstructorInvocation
Creates an unparented alternate super constructor ("super(...);") invocation statement node owned by this AST. By default, there is no qualifier, no type arguments, and the list of arguments is empty.Note that this type of node is a Statement, whereas a regular super method invocation is an Expression. The only valid use of these statements are as the first statement of a constructor body.
- Returns:
- a new unparented super constructor invocation statement node
-
newSuperFieldAccess
Creates and returns a new unparented super field access expression node owned by this AST. By default, the expression and field are both unspecified, but legal, names.- Returns:
- a new unparented super field access expression node
-
newSuperMethodInvocation
Creates an unparented "super" method invocation expression node owned by this AST. By default, the name of the method is unspecified (but legal), there is no qualifier, no type arguments, and the list of arguments is empty.- Returns:
- a new unparented "super" method invocation expression node
-
newSuperMethodReference
Creates and returns a new unparented super method reference node owned by this AST. By default, the name of the method is unspecified (but legal), and there is no qualifier and no type arguments.- Returns:
- a new unparented super method reference node
- Since:
- 3.10
-
newSwitchExpression
Creates and returns a new unparented switch expression node owned by this AST. By default, the expression is unspecified, but legal, and there are no statements or switch cases.- Returns:
- a new unparented labeled switch expression node
- Since:
- 3.18
-
newSwitchCase
Creates a new unparented switch case statement node owned by this AST. By default, the node has no expression, but legal, and switchLabeledRule is false which indicates ":".- Returns:
- a new unparented switch case node
-
newSwitchStatement
Creates a new unparented switch statement node owned by this AST. By default, the expression is unspecified, but legal, and there are no statements or switch cases.- Returns:
- a new unparented labeled statement node
-
newSynchronizedStatement
Creates a new unparented synchronized statement node owned by this AST. By default, the expression is unspecified, but legal, and the body is an empty block.- Returns:
- a new unparented synchronized statement node
-
newTagElement
Creates and returns a new tag element node. Initially the new node has no tag name and an empty list of fragments and properties.Note that this node type is used only inside doc comments (
Javadoc
).- Returns:
- a new unparented tag element node
- Since:
- 3.0
-
newTagProperty
Creates and returns a new tag property node. Initially the new node has no property name and value.Note that this node type is used only inside doc comments (
Javadoc
).- Returns:
- a new unparented tag element node
- Since:
- 3.30
-
newTextBlock
Creates an unparented yield statement node owned by this AST. The yield statement has no label/identifier/expression and is not implicit.- Returns:
- a new unparented yield statement node
- Since:
- 3.24
-
newTextElement
Creates and returns a new text element node. Initially the new node has an empty text string.Note that this node type is used only inside doc comments (
Javadoc
).- Returns:
- a new unparented text element node
- Since:
- 3.0
-
newThisExpression
Creates and returns a new unparented "this" expression node owned by this AST. By default, there is no qualifier.- Returns:
- a new unparented "this" expression node
-
newThrowStatement
Creates a new unparented throw statement node owned by this AST. By default, the expression is unspecified, but legal.- Returns:
- a new unparented throw statement node
-
newTryStatement
Creates a new unparented try statement node owned by this AST. By default, the try statement has no resources, an empty block, no catch clauses, and no finally block.- Returns:
- a new unparented try statement node
-
newImplicitTypeDeclaration
Creates an unparented class declaration node owned by this AST. The name of the class is an unspecified, but legal, name; no modifiers; no doc comment; no superclass or superinterfaces; and an empty class body.- Returns:
- a new unparented type declaration node
- Since:
- 3.40
-
newTypeDeclaration
Creates an unparented class declaration node owned by this AST. The name of the class is an unspecified, but legal, name; no modifiers; no doc comment; no superclass or superinterfaces; and an empty class body.To create an interface, use this method and then call
TypeDeclaration.setInterface(true)
.- Returns:
- a new unparented type declaration node
-
newTypeDeclarationStatement
Creates a new unparented local type declaration statement node owned by this AST, for the given type declaration.This method can be used to convert any kind of type declaration (
AbstractTypeDeclaration
) into a statement (Statement
) by wrapping it.- Parameters:
decl
- the type declaration- Returns:
- a new unparented local type declaration statement node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
- Since:
- 3.0
-
newTypeDeclarationStatement
Creates a new unparented local type declaration statement node owned by this AST, for the given type declaration.This method can be used to convert a type declaration (
TypeDeclaration
) into a statement (Statement
) by wrapping it.- Parameters:
decl
- the type declaration- Returns:
- a new unparented local type declaration statement node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
-
newTypeLiteral
Creates and returns a new unparented type literal expression node owned by this AST. By default, the type is unspecified (but legal).- Returns:
- a new unparented type literal node
-
newTypeMethodReference
Creates an unparented type method reference node owned by this AST. By default, the type and method name are unspecified (but legal), and there are no type arguments.- Returns:
- a new unparented type method reference node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2, JLS3 or JLS4 AST- Since:
- 3.10
-
newTypeParameter
Creates and returns a new unparented type parameter type node with an unspecified type variable name and an empty list of type bounds.- Returns:
- a new unparented type parameter node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newTypePattern
Creates and returns a new unparented type pattern node with an unspecified pattern variable.- Returns:
- a new unparented type pattern node
- Since:
- 3.28
-
newUnionType
Creates a new unparented union type node owned by this AST. By default, the union type has no types.- Returns:
- a new unparented UnionType node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 or JLS3 AST- Since:
- 3.7.1
-
newUsesDirective
Creates and returns a new unparented uses directive node for an unspecified, but legal, name;- Returns:
- a new unparented uses directive node
- Throws:
UnsupportedOperationException
- if this operation is used in level less than JLS9- Since:
- 3.14
-
newIntersectionType
Creates a new unparented intersection type node owned by this AST. By default, the intersection type has no types.- Returns:
- a new unparented IntersectionType node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2, JLS3 or JLS4 AST- Since:
- 3.10
-
newVariableDeclarationExpression
public VariableDeclarationExpression newVariableDeclarationExpression(VariableDeclarationFragment fragment) Creates a new unparented local variable declaration expression node owned by this AST, for the given variable declaration fragment. By default, there are no modifiers and the base type is unspecified (but legal).This method can be used to convert a variable declaration fragment (
VariableDeclarationFragment
) into an expression (Expression
) by wrapping it. Additional variable declaration fragments can be added afterwards.- Parameters:
fragment
- the first variable declaration fragment- Returns:
- a new unparented variable declaration expression node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
- the given fragment is null
-
newVariableDeclarationFragment
Creates an unparented variable declaration fragment node owned by this AST. By default, the fragment is for a variable with an unspecified, but legal, name; no extra array dimensions; and no initializer.- Returns:
- a new unparented variable declaration fragment node
-
newVariableDeclarationStatement
public VariableDeclarationStatement newVariableDeclarationStatement(VariableDeclarationFragment fragment) Creates a new unparented local variable declaration statement node owned by this AST, for the given variable declaration fragment. By default, there are no modifiers and the base type is unspecified (but legal).This method can be used to convert a variable declaration fragment (
VariableDeclarationFragment
) into a statement (Statement
) by wrapping it. Additional variable declaration fragments can be added afterwards.- Parameters:
fragment
- the variable declaration fragment- Returns:
- a new unparented variable declaration statement node
- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
- the variable declaration fragment is null
-
newWhileStatement
Creates a new unparented while statement node owned by this AST. By default, the expression is unspecified (but legal), and the body statement is an empty block.- Returns:
- a new unparented while statement node
-
newWildcardType
Creates and returns a new unparented wildcard type node with no type bound.- Returns:
- a new unparented wildcard type node
- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
newYieldStatement
Creates an unparented yield statement node owned by this AST. The yield statement has no label/identifier/expression and is not implicit.- Returns:
- a new unparented yield statement node
- Since:
- 3.24
-
resolveWellKnownType
Returns the type binding for a "well known" type.Note that bindings are generally unavailable unless requested when the AST is being built.
The following type names are supported:
"boolean"
"byte"
"char"
"double"
"float"
"int"
"long"
"short"
"void"
"java.lang.AssertionError"
(since 3.7)"java.lang.Boolean"
(since 3.1)"java.lang.Byte"
(since 3.1)"java.lang.Character"
(since 3.1)"java.lang.Class"
"java.lang.Cloneable"
"java.lang.Double"
(since 3.1)"java.lang.Error"
"java.lang.Exception"
"java.lang.Float"
(since 3.1)"java.lang.Integer"
(since 3.1)"java.lang.Long"
(since 3.1)"java.lang.Object"
"java.lang.RuntimeException"
"java.lang.Short"
(since 3.1)"java.lang.String"
"java.lang.StringBuffer"
"java.lang.Throwable"
"java.lang.Void"
(since 3.1)"java.io.Serializable"
- Parameters:
name
- the name of a well known type- Returns:
- the corresponding type binding, or
null
if the named type is not considered well known or if no binding can be found for it
-
isPreviewEnabledSet
public boolean isPreviewEnabledSet()- Returns:
- If previewEnabled flag is set to
true
, returntrue
elsefalse
- Since:
- 3.21
- Restriction:
- This method is not intended to be referenced by clients.
-
isPreviewEnabled
public boolean isPreviewEnabled()- Returns:
- If preview is enabled and apiLevel is latest, return
true
elsefalse
- Since:
- 3.19
-
getJLSLatest
public static int getJLSLatest()Returns latest supported JLS level- Returns:
- the latest supported JLS level
- Since:
- 3.27
-
getJLSLatest()