org.eclipse.wst.jsdt.core.dom
Class AST

java.lang.Object
  extended by org.eclipse.wst.jsdt.core.dom.AST

public final class AST
extends java.lang.Object

Umbrella owner and abstract syntax tree node factory. An 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 JavaScriptUnit), 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 JavaScript 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.

JavaScript 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("var abc;\nfunction X() {}\n");
 ASTParser parser = ASTParser.newParser(AST.JLS3);
 parser.setSource(doc.get().toCharArray());
 JavaScriptUnit cu = (JavaScriptUnit) parser.createAST(null);
 cu.recordModifications();
 AST ast = cu.getAST();
 FunctionDeclaration id = ast.newFunctionDeclaration();
 id.setName(ast.newName("X2");
 cu.statements().add(id); // add 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), but this class is not intended to be subclassed.

See Also:
ASTParser, Provisional API: This class/interface is part of an interim API that is still under development and expected to change significantly before reaching stability. It is being made available at this early stage to solicit feedback from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken (repeatedly) as the API evolves.

Field Summary
static int JLS2
          Deprecated. Clients should use the JLS3 AST API instead.
static int JLS3
          Constant for indicating the AST API that handles ECMAScript 4.
 
Constructor Summary
AST()
          Deprecated. Clients should port their code to use the new JLS3 AST API and call AST.newAST(AST.JLS3) instead of using this constructor.
AST(java.util.Map options)
          Deprecated. Clients should port their code to use the new JLS3 AST API and call AST.newAST(AST.JLS3) instead of using this constructor.
 
Method Summary
 int apiLevel()
          Return the API level supported by this AST.
static JavaScriptUnit convertCompilationUnit(int level, org.eclipse.wst.jsdt.internal.compiler.ast.CompilationUnitDeclaration compilationUnitDeclaration, char[] source, java.util.Map options, boolean isResolved, org.eclipse.wst.jsdt.internal.core.CompilationUnit workingCopy, int reconcileFlags, IProgressMonitor monitor)
          Internal method.
 ASTNode createInstance(java.lang.Class nodeClass)
          Creates an unparented node of the given node class (non-abstract subclass of ASTNode).
 ASTNode createInstance(int nodeType)
          Creates an unparented node of the given node type.
 boolean hasBindingsRecovery()
          Returns true if the ast tree was created with bindings recovery, false otherwise
 boolean hasResolvedBindings()
          Returns true if the ast tree was created with bindings, false otherwise
 boolean hasStatementsRecovery()
          Returns true if the ast tree was created with statements recovery, false otherwise
 long modificationCount()
          Returns the modification count for this AST.
 AnonymousClassDeclaration newAnonymousClassDeclaration()
          Creates and returns a new unparented anonymous class declaration node owned by this AST.
 ArrayAccess newArrayAccess()
          Creates and returns a new unparented array access expression node owned by this AST.
 ArrayCreation newArrayCreation()
          Creates and returns a new unparented array creation expression node owned by this AST.
 ArrayInitializer newArrayInitializer()
          Creates and returns a new unparented array initializer node owned by this AST.
 ArrayType newArrayType(Type componentType)
          Creates and returns a new unparented array type node with the given component type, which may be another array type.
 ArrayType newArrayType(Type elementType, int dimensions)
          Creates and returns a new unparented array type node with the given element type and number of dimensions.
 AssertStatement newAssertStatement()
           
 Assignment newAssignment()
          Creates and returns a new unparented assignment expression node owned by this AST.
static AST newAST(int level)
          Creates a new JavaScript abstract syntax tree (AST) following the specified set of API rules.
 Block newBlock()
          Creates an unparented block node owned by this AST, for an empty list of statements.
 BlockComment newBlockComment()
          Creates and returns a new block comment placeholder node.
 BooleanLiteral newBooleanLiteral(boolean value)
          Creates and returns a new unparented boolean literal node.
 BreakStatement newBreakStatement()
          Creates an unparented break statement node owned by this AST.
 CastExpression newCastExpression()
          Creates and returns a new unparented cast expression node owned by this AST.
 CatchClause newCatchClause()
          Creates a new unparented catch clause node owned by this AST.
 CharacterLiteral newCharacterLiteral()
          Creates and returns a new unparented character literal node.
 ClassInstanceCreation newClassInstanceCreation()
          Creates and returns a new unparented class instance creation ("new") expression node owned by this AST.
 JavaScriptUnit newCompilationUnit()
          Deprecated. Use newJavaScriptUnit() instead
 ConditionalExpression newConditionalExpression()
          Creates and returns a new unparented conditional expression node owned by this AST.
 ConstructorInvocation newConstructorInvocation()
          Creates an unparented alternate constructor ("this(...);") invocation statement node owned by this AST.
 ContinueStatement newContinueStatement()
          Creates an unparented continue statement node owned by this AST.
 DoStatement newDoStatement()
          Creates a new unparented do statement node owned by this AST.
 EmptyStatement newEmptyStatement()
          Creates a new unparented empty statement node owned by this AST.
 EnhancedForStatement newEnhancedForStatement()
           
 ExpressionStatement newExpressionStatement(Expression expression)
          Creates a new unparented expression statement node owned by this AST, for the given expression.
 FieldAccess newFieldAccess()
          Creates and returns a new unparented field access expression node owned by this AST.
 FieldDeclaration newFieldDeclaration(VariableDeclarationFragment fragment)
          Creates a new unparented field declaration node owned by this AST, for the given variable declaration fragment.
 ForInStatement newForInStatement()
          Creates a new unparented for..in statement node owned by this AST.
 ForStatement newForStatement()
          Creates a new unparented for statement node owned by this AST.
 FunctionDeclaration newFunctionDeclaration()
          Creates an unparented method declaration node owned by this AST.
 FunctionExpression newFunctionExpression()
          Creates and returns a new unparented function expression node owned by this AST.
 FunctionInvocation newFunctionInvocation()
          Creates an unparented method invocation expression node owned by this AST.
 FunctionRef newFunctionRef()
          Creates and returns a new method reference node.
 FunctionRefParameter newFunctionRefParameter()
          Creates and returns a new method reference node.
 IfStatement newIfStatement()
          Creates a new unparented if statement node owned by this AST.
 ImportDeclaration newImportDeclaration()
          Creates an unparented import declaration node owned by this AST.
 InferredType newInferredType(java.lang.String typeName)
          Creates and returns a new inferred type node with the given type name.
 InfixExpression newInfixExpression()
          Creates and returns a new unparented infix expression node owned by this AST.
 Initializer newInitializer()
           
 InstanceofExpression newInstanceofExpression()
          Creates and returns a new unparented instanceof expression node owned by this AST.
 JSdoc newJavadoc()
          Deprecated. Use newJSdoc() instead
 JavaScriptUnit newJavaScriptUnit()
          Creates an unparented javaScript unit node owned by this AST.
 JSdoc newJSdoc()
          Creates and returns a new doc comment node.
 LabeledStatement newLabeledStatement()
          Creates a new unparented labeled statement node owned by this AST.
 LineComment newLineComment()
          Creates and returns a new line comment placeholder node.
 ListExpression newListExpression()
           
 MemberRef newMemberRef()
          Creates and returns a new member reference node.
 FunctionDeclaration newMethodDeclaration()
          Deprecated. Use newFunctionDeclaration() instead
 FunctionInvocation newMethodInvocation()
          Deprecated. Use newFunctionInvocation() instead
 FunctionRef newMethodRef()
          Deprecated. Use newFunctionRef() instead
 FunctionRefParameter newMethodRefParameter()
          Deprecated. Use newFunctionRefParameter() instead
 Modifier newModifier(Modifier.ModifierKeyword keyword)
          Creates and returns a new unparented modifier node for the given modifier.
 java.util.List newModifiers(int flags)
          Creates and returns a list of new unparented modifier nodes for the given modifier flags.
 Name newName(java.lang.String qualifiedName)
          Creates and returns a new unparented name node for the given name.
 Name newName(java.lang.String[] identifiers)
          Creates and returns a new unparented name node for the given name segments.
 NullLiteral newNullLiteral()
          Creates and returns a new unparented null literal node.
 NumberLiteral newNumberLiteral()
          Creates and returns a new unparented number literal node.
 NumberLiteral newNumberLiteral(java.lang.String literal)
          Creates and returns a new unparented number literal node.
 ObjectLiteral newObjectLiteral()
          Creates and returns a new unparented object literal expression node owned by this AST.
 ObjectLiteralField newObjectLiteralField()
          Creates and returns a new unparented object literal field expression node owned by this AST.
 PackageDeclaration newPackageDeclaration()
          Creates an unparented package declaration node owned by this AST.
 ParameterizedType newParameterizedType(Type type)
          Creates and returns a new unparented parameterized type node with the given type and an empty list of type arguments.
 ParenthesizedExpression newParenthesizedExpression()
          Creates and returns a new unparented parenthesized expression node owned by this AST.
 PostfixExpression newPostfixExpression()
          Creates and returns a new unparented postfix expression node owned by this AST.
 PrefixExpression newPrefixExpression()
          Creates and returns a new unparented prefix expression node owned by this AST.
 PrimitiveType newPrimitiveType(PrimitiveType.Code typeCode)
          Creates and returns a new unparented primitive type node with the given type code.
 QualifiedName newQualifiedName(Name qualifier, SimpleName name)
          Creates and returns a new unparented qualified name node for the given qualifier and simple name child node.
 QualifiedType newQualifiedType(Type qualifier, SimpleName name)
          Creates and returns a new unparented qualified type node with the given qualifier type and name.
 RegularExpressionLiteral newRegularExpressionLiteral()
          Creates and returns a new Regular Expression literal node.
 ReturnStatement newReturnStatement()
          Creates a new unparented return statement node owned by this AST.
 SimpleName newSimpleName(java.lang.String identifier)
          Creates and returns a new unparented simple name node for the given identifier.
 SimpleType newSimpleType(Name typeName)
          Creates and returns a new unparented simple type node with the given type name.
 SingleVariableDeclaration newSingleVariableDeclaration()
          Creates an unparented single variable declaration node owned by this AST.
 StringLiteral newStringLiteral()
          Creates and returns a new unparented string literal node for the empty string literal.
 SuperConstructorInvocation newSuperConstructorInvocation()
          Creates an unparented alternate super constructor ("super(...);") invocation statement node owned by this AST.
 SuperFieldAccess newSuperFieldAccess()
          Creates and returns a new unparented super field access expression node owned by this AST.
 SuperMethodInvocation newSuperMethodInvocation()
          Creates an unparented "super" method invocation expression node owned by this AST.
 SwitchCase newSwitchCase()
          Creates a new unparented switch case statement node owned by this AST.
 SwitchStatement newSwitchStatement()
          Creates a new unparented switch statement node owned by this AST.
 TagElement newTagElement()
          Creates and returns a new tag element node.
 TextElement newTextElement()
          Creates and returns a new text element node.
 ThisExpression newThisExpression()
          Creates and returns a new unparented "this" expression node owned by this AST.
 ThrowStatement newThrowStatement()
          Creates a new unparented throw statement node owned by this AST.
 TryStatement newTryStatement()
          Creates a new unparented try statement node owned by this AST.
 TypeDeclaration newTypeDeclaration()
          Creates an unparented class declaration node owned by this AST.
 TypeDeclarationStatement newTypeDeclarationStatement(AbstractTypeDeclaration decl)
          Creates a new unparented local type declaration statement node owned by this AST, for the given type declaration.
 TypeDeclarationStatement newTypeDeclarationStatement(TypeDeclaration decl)
          Creates a new unparented local type declaration statement node owned by this AST, for the given type declaration.
 TypeLiteral newTypeLiteral()
          Creates and returns a new unparented type literal expression node owned by this AST.
 TypeParameter newTypeParameter()
          Creates and returns a new unparented type parameter type node with an unspecified type variable name and an empty list of type bounds.
 UndefinedLiteral newUndefinedLiteral()
          Creates and returns a new unparented 'undefined' literal node.
 VariableDeclarationExpression newVariableDeclarationExpression(VariableDeclarationFragment fragment)
          Creates a new unparented local variable declaration expression node owned by this AST, for the given variable declaration fragment.
 VariableDeclarationFragment newVariableDeclarationFragment()
          Creates an unparented variable declaration fragment node owned by this AST.
 VariableDeclarationStatement newVariableDeclarationStatement(VariableDeclarationFragment fragment)
          Creates a new unparented local variable declaration statement node owned by this AST, for the given variable declaration fragment.
 WhileStatement newWhileStatement()
          Creates a new unparented while statement node owned by this AST.
 WildcardType newWildcardType()
          Creates and returns a new unparented wildcard type node with no type bound.
 WithStatement newWithStatement()
          Creates a new unparented with statement node owned by this AST.
 ITypeBinding resolveWellKnownType(java.lang.String name)
          Returns the type binding for a "well known" type.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JLS2

public static final int JLS2
Deprecated. Clients should use the JLS3 AST API instead.
Constant for indicating the AST API that handles standard Javascript. This API is capable of handling all constructs in the JavaScript language as described in the ECMA-262 Specification.

See Also:
Constant Field Values

JLS3

public static final int JLS3
Constant for indicating the AST API that handles ECMAScript 4. This API is capable of handling all constructs in the JavaScript language as described in the ECMAScript 4 Specification. ECMAScript 4 is a superset of all earlier versions of the JavaScript language.

See Also:
Constant Field Values
Constructor Detail

AST

public AST()
Deprecated. Clients should port their code to use the new JLS3 AST API and call AST.newAST(AST.JLS3) instead of using this constructor.

Creates a new, empty abstract syntax tree using default options.

See Also:
JavaScriptCore.getDefaultOptions()

AST

public AST(java.util.Map options)
Deprecated. Clients should port their code to use the new JLS3 AST API and call AST.newAST(AST.JLS3) instead of using this constructor.

Creates a new, empty abstract syntax tree using the given options.

Parameters:
options - the table of options (key type: String; value type: String)
See Also:
JavaScriptCore.getDefaultOptions()
Method Detail

convertCompilationUnit

public static JavaScriptUnit convertCompilationUnit(int level,
                                                    org.eclipse.wst.jsdt.internal.compiler.ast.CompilationUnitDeclaration compilationUnitDeclaration,
                                                    char[] source,
                                                    java.util.Map options,
                                                    boolean isResolved,
                                                    org.eclipse.wst.jsdt.internal.core.CompilationUnit workingCopy,
                                                    int reconcileFlags,
                                                    IProgressMonitor monitor)
Internal method.

This method converts the given internal compiler AST for the given source string into a javaScript unit. This method is not intended to be called by clients.

Parameters:
level - the API level; one of the LEVEL constants
compilationUnitDeclaration - an internal AST node for a javaScript unit declaration
source - the string of the JavaScript javaScript unit
options - validator options
workingCopy - the working copy that the AST is created from
monitor - the progress monitor used to report progress and request cancelation, or null if none
isResolved - whether the given javaScript unit declaration is resolved
Returns:
the javaScript unit node

newAST

public static AST newAST(int level)
Creates a new JavaScript abstract syntax tree (AST) following the specified set of API rules.

Parameters:
level - the API level; one of the LEVEL constants
Returns:
new AST instance following the specified set of API rules.
Throws:
java.lang.IllegalArgumentException - if:
  • the API level is not one of the LEVEL constants

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:

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

apiLevel

public int apiLevel()
Return the API level supported by this AST.

Returns:
level the API level; one of the JLS*LEVEL declared on AST; assume this set is open-ended

resolveWellKnownType

public ITypeBinding resolveWellKnownType(java.lang.String name)
Returns the type binding for a "well known" type.

Note that bindings are generally unavailable unless requested when the AST is being built.

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

createInstance

public ASTNode createInstance(java.lang.Class nodeClass)
Creates an unparented node of the given node class (non-abstract subclass of ASTNode).

Parameters:
nodeClass - AST node class
Returns:
a new unparented node owned by this AST
Throws:
java.lang.IllegalArgumentException - if nodeClass is null or is not a concrete node type class

createInstance

public ASTNode createInstance(int nodeType)
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 on ASTNode
Returns:
a new unparented node owned by this AST
Throws:
java.lang.IllegalArgumentException - if nodeType is not a legal AST node type

newSimpleName

public SimpleName newSimpleName(java.lang.String identifier)
Creates and returns a new unparented simple name node for the given identifier. The identifier should be a legal JavaScript 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:
java.lang.IllegalArgumentException - if the identifier is invalid

newQualifiedName

public QualifiedName newQualifiedName(Name qualifier,
                                      SimpleName name)
Creates and returns a new unparented qualified name node for the given qualifier and simple name child node.

Parameters:
qualifier - the qualifier name node
name - the simple name being qualified
Returns:
a new unparented qualified name node
Throws:
java.lang.IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent

newName

public Name newName(java.lang.String[] identifiers)
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 JavaScript 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 JavaScript identifier
Returns:
a new unparented name node
Throws:
java.lang.IllegalArgumentException - if:
  • the identifier is invalid
  • the list of identifiers is empty

newName

public Name newName(java.lang.String qualifiedName)
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 a QualifiedName if the name has dots, and a SimpleName otherwise. Each of the name segments should be legal JavaScript 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 JavaScript identifiers or separating '.'s.

Parameters:
qualifiedName - string consisting of 1 or more name segments, each of which is a legal JavaScript identifier, separated by single dots '.'
Returns:
a new unparented name node
Throws:
java.lang.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 JavaScript identifiers

newSimpleType

public SimpleType newSimpleType(Name typeName)
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.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Parameters:
typeName - the name of the class or interface
Returns:
a new unparented simple type node
Throws:
java.lang.IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent

newArrayType

public ArrayType newArrayType(Type componentType)
Creates and returns a new unparented array type node with the given component type, which may be another array type.

Parameters:
componentType - the component type (possibly another array type)
Returns:
a new unparented array type node
Throws:
java.lang.IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent
  • a cycle in would be created

newArrayType

public ArrayType newArrayType(Type elementType,
                              int dimensions)
Creates and returns a new unparented array type node with the given element type and number of dimensions.

Note that if the element type passed in is an array type, the element type of the result will not be the same as what was passed in.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Parameters:
elementType - the element type (never an array type)
dimensions - the number of dimensions, a positive number
Returns:
a new unparented array type node
Throws:
java.lang.IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent
  • a cycle in would be created
  • the element type is null
  • the element type is an array type
  • the number of dimensions is lower than 1
  • the number of dimensions is greater than 1000

newPrimitiveType

public PrimitiveType newPrimitiveType(PrimitiveType.Code typeCode)
Creates and returns a new unparented primitive type node with the given type code.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Parameters:
typeCode - one of the primitive type code constants declared in PrimitiveType
Returns:
a new unparented primitive type node
Throws:
java.lang.IllegalArgumentException - if the primitive type code is invalid

newInferredType

public InferredType newInferredType(java.lang.String typeName)
Creates and returns a new inferred type node with the given type name.

Parameters:
typeName - the name of the inferred type
Returns:
a new unparented inferred type node
Throws:
java.lang.IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent

newParameterizedType

public ParameterizedType newParameterizedType(Type type)
Creates and returns a new unparented parameterized type node with the given type and an empty list of type arguments.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Parameters:
type - the type that is parameterized
Returns:
a new unparented parameterized type node
Throws:
java.lang.IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent
java.lang.UnsupportedOperationException - if this operation is used in a JLS2 AST

newQualifiedType

public QualifiedType newQualifiedType(Type qualifier,
                                      SimpleName name)
Creates and returns a new unparented qualified type node with the given qualifier type and name.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Parameters:
qualifier - the qualifier type node
name - the simple name being qualified
Returns:
a new unparented qualified type node
Throws:
java.lang.IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent
java.lang.UnsupportedOperationException - if this operation is used in a JLS2 AST

newWildcardType

public WildcardType newWildcardType()
Creates and returns a new unparented wildcard type node with no type bound.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Returns:
a new unparented wildcard type node
Throws:
java.lang.UnsupportedOperationException - if this operation is used in a JLS2 AST

newCompilationUnit

public JavaScriptUnit newCompilationUnit()
Deprecated. Use newJavaScriptUnit() instead

Creates an unparented javaScript unit node owned by this AST. The javaScript unit initially has no package declaration, no import declarations, and no type declarations.

Returns:
the new unparented javaScript unit node

newJavaScriptUnit

public JavaScriptUnit newJavaScriptUnit()
Creates an unparented javaScript unit node owned by this AST. The javaScript unit initially has no package declaration, no import declarations, and no type declarations.

Returns:
the new unparented javaScript unit node

newPackageDeclaration

public PackageDeclaration newPackageDeclaration()
Creates an unparented package declaration node owned by this AST. The package declaration initially declares a package with an unspecified name.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Returns:
the new unparented package declaration node

newImportDeclaration

public ImportDeclaration 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.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Returns:
the new unparented import declaration node

newTypeDeclaration

public TypeDeclaration 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).

To create an enum declaration, use this method and then call TypeDeclaration.setEnumeration(true).

Note: This Method only applies to ECMAScript 4 which is not yet supported

Returns:
a new unparented type declaration node

newMethodDeclaration

public FunctionDeclaration newMethodDeclaration()
Deprecated. Use newFunctionDeclaration() instead

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 FunctionDeclaration.setConstructor(true) and FunctionDeclaration.setName(className).

Returns:
a new unparented method declaration node

newFunctionDeclaration

public FunctionDeclaration newFunctionDeclaration()
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 FunctionDeclaration.setConstructor(true) and FunctionDeclaration.setName(className).

Returns:
a new unparented method declaration node

newSingleVariableDeclaration

public SingleVariableDeclaration 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

newVariableDeclarationFragment

public VariableDeclarationFragment 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

newInitializer

public Initializer newInitializer()

newTypeParameter

public TypeParameter newTypeParameter()
Creates and returns a new unparented type parameter type node with an unspecified type variable name and an empty list of type bounds.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Returns:
a new unparented type parameter node
Throws:
java.lang.UnsupportedOperationException - if this operation is used in a JLS2 AST

newModifier

public Modifier newModifier(Modifier.ModifierKeyword keyword)
Creates and returns a new unparented modifier node for the given modifier.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Parameters:
keyword - one of the modifier keyword constants
Returns:
a new unparented modifier node
Throws:
java.lang.IllegalArgumentException - if the primitive type code is invalid
java.lang.UnsupportedOperationException - if this operation is used in a JLS2 AST

newModifiers

public java.util.List newModifiers(int flags)
Creates and returns a list of new unparented modifier nodes for the given modifier flags.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Parameters:
flags - bitwise or of modifier flags declared on Modifier
Returns:
a possibly empty list of new unparented modifier nodes (element type Modifier)
Throws:
java.lang.UnsupportedOperationException - if this operation is used in a JLS2 AST

newBlockComment

public BlockComment 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 javaScript units.

Returns:
a new unparented block comment node

newLineComment

public LineComment 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 javaScript units.

Returns:
a new unparented line comment node

newListExpression

public ListExpression newListExpression()

newJavadoc

public JSdoc newJavadoc()
Deprecated. Use newJSdoc() instead

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

newJSdoc

public JSdoc newJSdoc()
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

newTagElement

public TagElement newTagElement()
Creates and returns a new tag element node. Initially the new node has no tag name and an empty list of fragments.

Note that this node type is used only inside doc comments (JSdoc).

Returns:
a new unparented tag element node

newTextElement

public TextElement 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

newMemberRef

public MemberRef 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 (JSdoc).

Returns:
a new unparented member reference node

newMethodRef

public FunctionRef newMethodRef()
Deprecated. Use newFunctionRef() instead

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

newFunctionRef

public FunctionRef newFunctionRef()
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

newMethodRefParameter

public FunctionRefParameter newMethodRefParameter()
Deprecated. Use newFunctionRefParameter() instead

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 (JSdoc).

Returns:
a new unparented method reference parameter node

newFunctionRefParameter

public FunctionRefParameter newFunctionRefParameter()
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 (JSdoc).

Returns:
a new unparented method reference parameter 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:
java.lang.IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent
  • a cycle in would be created
  • the variable declaration fragment is null

newTypeDeclarationStatement

public TypeDeclarationStatement newTypeDeclarationStatement(TypeDeclaration decl)
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.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Parameters:
decl - the type declaration
Returns:
a new unparented local type declaration statement node
Throws:
java.lang.IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent
  • a cycle in would be created

newTypeDeclarationStatement

public TypeDeclarationStatement newTypeDeclarationStatement(AbstractTypeDeclaration decl)
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.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Parameters:
decl - the type declaration
Returns:
a new unparented local type declaration statement node
Throws:
java.lang.IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent
  • a cycle in would be created

newBlock

public Block newBlock()
Creates an unparented block node owned by this AST, for an empty list of statements.

Returns:
a new unparented, empty block node

newContinueStatement

public ContinueStatement newContinueStatement()
Creates an unparented continue statement node owned by this AST. The continue statement has no label.

Returns:
a new unparented continue statement node

newBreakStatement

public BreakStatement newBreakStatement()
Creates an unparented break statement node owned by this AST. The break statement has no label.

Returns:
a new unparented break statement node

newExpressionStatement

public ExpressionStatement newExpressionStatement(Expression expression)
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:
java.lang.IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent
  • a cycle in would be created

newIfStatement

public IfStatement 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

newWhileStatement

public WhileStatement 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

newWithStatement

public WithStatement newWithStatement()
Creates a new unparented with 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 with statement node

newDoStatement

public DoStatement 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

newTryStatement

public TryStatement newTryStatement()
Creates a new unparented try statement node owned by this AST. By default, the try statement has an empty block, no catch clauses, and no finally block.

Returns:
a new unparented try statement node

newCatchClause

public CatchClause 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

newReturnStatement

public ReturnStatement 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

newThrowStatement

public ThrowStatement 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

newAssertStatement

public AssertStatement newAssertStatement()

newEmptyStatement

public EmptyStatement newEmptyStatement()
Creates a new unparented empty statement node owned by this AST.

Returns:
a new unparented empty statement node

newLabeledStatement

public LabeledStatement 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

newSwitchStatement

public SwitchStatement 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

newSwitchCase

public SwitchCase newSwitchCase()
Creates a new unparented switch case statement node owned by this AST. By default, the expression is unspecified, but legal.

Returns:
a new unparented switch case node

newForStatement

public ForStatement 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

newForInStatement

public ForInStatement newForInStatement()
Creates a new unparented for..in 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..in statement node

newEnhancedForStatement

public EnhancedForStatement newEnhancedForStatement()

newStringLiteral

public StringLiteral newStringLiteral()
Creates and returns a new unparented string literal node for the empty string literal.

Returns:
a new unparented string literal node

newCharacterLiteral

public CharacterLiteral 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

newRegularExpressionLiteral

public RegularExpressionLiteral newRegularExpressionLiteral()
Creates and returns a new Regular Expression literal node. Initially the node has an unspecified character literal.

Returns:
a new unparented regular expression literal node

newNumberLiteral

public NumberLiteral newNumberLiteral(java.lang.String literal)
Creates and returns a new unparented number literal node.

Parameters:
literal - the token for the numeric literal as it would appear in JavaScript source code
Returns:
a new unparented number literal node
Throws:
java.lang.IllegalArgumentException - if the literal is null

newNumberLiteral

public NumberLiteral newNumberLiteral()
Creates and returns a new unparented number literal node. Initially the number literal token is "0".

Returns:
a new unparented number literal node

newNullLiteral

public NullLiteral newNullLiteral()
Creates and returns a new unparented null literal node.

Returns:
a new unparented null literal node

newUndefinedLiteral

public UndefinedLiteral newUndefinedLiteral()
Creates and returns a new unparented 'undefined' literal node.

Returns:
a new unparented 'undefined' literal node

newBooleanLiteral

public BooleanLiteral newBooleanLiteral(boolean value)
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

newAssignment

public Assignment 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

newMethodInvocation

public FunctionInvocation newMethodInvocation()
Deprecated. Use newFunctionInvocation() instead

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

newFunctionInvocation

public FunctionInvocation newFunctionInvocation()
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

newSuperMethodInvocation

public SuperMethodInvocation 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.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Returns:
a new unparented "super" method invocation expression node

newConstructorInvocation

public ConstructorInvocation 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.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Returns:
a new unparented alternate constructor invocation statement node

newSuperConstructorInvocation

public SuperConstructorInvocation 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.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Returns:
a new unparented super constructor invocation statement node

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:
java.lang.IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent
  • a cycle in would be created
  • the given fragment is null
  • a cycle in would be created

newFieldDeclaration

public FieldDeclaration newFieldDeclaration(VariableDeclarationFragment fragment)
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 implements BodyDeclaration). Additional variable declaration fragments can be added afterwards.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Parameters:
fragment - the variable declaration fragment
Returns:
a new unparented field declaration node
Throws:
java.lang.IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent
  • a cycle in would be created
  • the given fragment is null

newThisExpression

public ThisExpression 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

newFieldAccess

public FieldAccess 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

newSuperFieldAccess

public SuperFieldAccess 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.

Note: This Method only applies to ECMAScript 4 which is not yet supported

Returns:
a new unparented super field access expression node

newTypeLiteral

public TypeLiteral newTypeLiteral()
Creates and returns a new unparented type literal expression node owned by this AST. By default, the type is unspecified (but legal).

Note: This Method only applies to ECMAScript 4 which is not yet supported

Returns:
a new unparented type literal node

newCastExpression

public CastExpression 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

newFunctionExpression

public FunctionExpression newFunctionExpression()
Creates and returns a new unparented function expression node owned by this AST.

Returns:
a new unparented function expression node

newObjectLiteral

public ObjectLiteral newObjectLiteral()
Creates and returns a new unparented object literal expression node owned by this AST.

Returns:
a new unparented object literal expression node

newObjectLiteralField

public ObjectLiteralField newObjectLiteralField()
Creates and returns a new unparented object literal field expression node owned by this AST.

Returns:
a new unparented object literal field expression node

newParenthesizedExpression

public ParenthesizedExpression 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

newInfixExpression

public InfixExpression 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

newInstanceofExpression

public InstanceofExpression 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

newPostfixExpression

public PostfixExpression 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

public PrefixExpression 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

newArrayAccess

public ArrayAccess 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

public ArrayCreation 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.

Returns:
a new unparented array creation expression node

newClassInstanceCreation

public ClassInstanceCreation 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

newAnonymousClassDeclaration

public AnonymousClassDeclaration 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

newArrayInitializer

public ArrayInitializer 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

newConditionalExpression

public ConditionalExpression 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

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

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

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