|
Eclipse JDT 2.0 |
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.eclipse.jdt.core.dom.AST
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
new
factory methods to create new nodes, and the
various set
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.
The static method parseCompilationUnit
parses a string
containing a Java compilation unit and returns the abstract syntax tree
for it. The resulting nodes carry source ranges relating the node back to
the original source characters. Optional name and type resolution can also
be requested at the time the AST is created.
Clients may create instances of this class, which is not intended to be subclassed.
parseCompilationUnit(org.eclipse.jdt.core.ICompilationUnit, boolean)
,
ASTNode
Constructor Summary | |
AST()
Creates a new, empty abstract syntax tree using default options. |
|
AST(Map options)
Creates a new, empty abstract syntax tree using the given options. |
Method Summary | |
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()
Creates a new unparented assert statement node owned by this AST. |
Assignment |
newAssignment()
Creates and returns a new unparented assignment expression node owned by this AST. |
Block |
newBlock()
Creates an unparented block node owned by this AST, for an empty list of statements. |
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. |
CompilationUnit |
newCompilationUnit()
Creates an unparented compilation unit node owned by this AST. |
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. |
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. |
ForStatement |
newForStatement()
Creates a new unparented for statement node owned by this AST. |
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. |
InfixExpression |
newInfixExpression()
Creates and returns a new unparented infix expression node owned by this AST. |
Initializer |
newInitializer()
Creates an unparented initializer node owned by this AST, with an empty block. |
InstanceofExpression |
newInstanceofExpression()
Creates and returns a new unparented instanceof expression node owned by this AST. |
Javadoc |
newJavadoc()
Creates and returns a new Javadoc comment node. |
LabeledStatement |
newLabeledStatement()
Creates a new unparented labeled statement node owned by this AST. |
MethodDeclaration |
newMethodDeclaration()
Creates an unparented method declaration node owned by this AST. |
MethodInvocation |
newMethodInvocation()
Creates an unparented method invocation expression node owned by this AST. |
Name |
newName(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(String literal)
Creates and returns a new unparented number literal node. |
PackageDeclaration |
newPackageDeclaration()
Creates an unparented package declaration node owned by this AST. |
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. |
ReturnStatement |
newReturnStatement()
Creates a new unparented return statement node owned by this AST. |
SimpleName |
newSimpleName(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. |
SynchronizedStatement |
newSynchronizedStatement()
Creates a new unparented synchronized statement node owned by this AST. |
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(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. |
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. |
static CompilationUnit |
parseCompilationUnit(char[] source)
Parses the given string as a Java compilation unit and creates and returns a corresponding abstract syntax tree. |
static CompilationUnit |
parseCompilationUnit(char[] source,
String unitName,
IJavaProject project)
Parses the given string as the hypothetical contents of the named compilation unit and creates and returns a corresponding abstract syntax tree. |
static CompilationUnit |
parseCompilationUnit(ICompilationUnit unit,
boolean resolveBindings)
Parses the given string as a Java compilation unit and creates and returns a corresponding abstract syntax tree. |
ITypeBinding |
resolveWellKnownType(String name)
Returns the type binding for a "well known" type. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public AST()
JavaCore.getDefaultOptions()
public AST(Map options)
Following option keys are significant:
"org.eclipse.jdt.core.compiler.source"
-
indicates source compatibility mode (as per JavaCore
);
"1.3"
means the source code is as per JDK 1.3;
"1.4"
means the source code is as per JDK 1.4
(assert
is a keyword);
additional legal values may be added later.
options
- the table of options (key type: String
;
value type: String
)JavaCore.getDefaultOptions()
Method Detail |
public long modificationCount()
The following things count as modifying an AST:
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.
public static CompilationUnit parseCompilationUnit(ICompilationUnit unit, boolean resolveBindings)
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). If a syntax error is detected while
parsing, the relevant node(s) of the tree will be flagged as
MALFORMED
.
If resolveBindings
is true
, the various names
and types appearing in the compilation unit can be resolved to "bindings"
by calling the resolveBinding
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. Note that bindings can only be resolved while the AST remains
in its original unmodified state. Once the AST is modified, all
resolveBinding
methods return null
.
If resolveBindings
is false
, the analysis
does not go beyond parsing and building the tree, and all
resolveBinding
methods return null
from the
outset.
unit
- the Java model compilation unit whose source code is to be parsedresolveBindings
- true
if bindings are wanted,
and false
if bindings are not of interest
ASTNode.getFlags()
,
ASTNode.MALFORMED
,
ASTNode.getStartPosition()
,
ASTNode.getLength()
public static CompilationUnit parseCompilationUnit(char[] source, String unitName, IJavaProject project)
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). 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 the resolveBinding
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. Note that bindings can only be resolved while the AST remains
in its original unmodified state. Once the AST is modified, all
resolveBinding
methods return null
.
If the given project is null
, the analysis
does not go beyond parsing and building the tree, and all
resolveBinding
methods return null
from the
outset.
The name of the compilation unit must be supplied for resolving bindings. This name should include the ".java" suffix 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 should 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.
source
- the string to be parsed as a Java compilation unitunitName
- the name of the compilation unit that would contain the source
string, or null
if javaProject
is also null
project
- the Java project used to resolve names, or
null
if bindings are not resolved
ASTNode.getFlags()
,
ASTNode.MALFORMED
,
ASTNode.getStartPosition()
,
ASTNode.getLength()
public static CompilationUnit parseCompilationUnit(char[] 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 given source string (the given source string itself
is not remembered with the AST). If a syntax error is detected while
parsing, the relevant node(s) of the tree will be flagged as
MALFORMED
.
source
- the string to be parsed as a Java compilation unitASTNode.getFlags()
,
ASTNode.MALFORMED
,
ASTNode.getStartPosition()
,
ASTNode.getLength()
public ITypeBinding resolveWellKnownType(String name)
Note that bindings are generally unavailable unless requested when the AST is being built.
The following type names are supported:
"boolean"
"char"
"byte"
"short"
"int"
"long"
"float"
"double"
"void"
"java.lang.Object"
"java.lang.String"
"java.lang.StringBuffer"
"java.lang.Throwable"
"java.lang.Exception"
"java.lang.RuntimeException"
"java.lang.Error"
"java.lang.Class"
name
- the name of a well known type
null
if the
named type is not considered well known or if no binding can be found
for itpublic SimpleName newSimpleName(String identifier)
identifier
- the identifier
IllegalArgumentException
- if the identifier is invalidpublic QualifiedName newQualifiedName(Name qualifier, SimpleName name)
qualifier
- the qualifier name nodename
- the simple name being qualified
IllegalArgumentException
- if:
public Name newName(String[] identifiers)
identifiers
- a list of 1 or more name segments, each of which
is a legal Java identifier
IllegalArgumentException
- if:
public SimpleType newSimpleType(Name typeName)
This method can be used to convert a name (Name
) into a
type (Type
) by wrapping it.
typeName
- the name of the class or interface
IllegalArgumentException
- if:
public ArrayType newArrayType(Type componentType)
componentType
- the component type (possibly another array type)
IllegalArgumentException
- if:
public ArrayType newArrayType(Type elementType, int 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.
elementType
- the element type (never an array type)dimensions
- the number of dimensions, a positive number
IllegalArgumentException
- if:
public PrimitiveType newPrimitiveType(PrimitiveType.Code typeCode)
typeCode
- one of the primitive type code constants declared in
PrimitiveType
IllegalArgumentException
- if the primitive type code is invalidpublic CompilationUnit newCompilationUnit()
Note that the new compilation unit is not automatically
made the root node of this AST. This must be done explicitly
by calling setRoot
.
public PackageDeclaration newPackageDeclaration()
IllegalArgumentException
- if:
public ImportDeclaration newImportDeclaration()
IllegalArgumentException
- if:
public TypeDeclaration newTypeDeclaration()
To create an interface, use this method and then call
TypeDeclaration.setInterface(true)
and
public MethodDeclaration newMethodDeclaration()
To create a constructor, use this method and then call
MethodDeclaration.setConstructor(true)
and
MethodDeclaration.setName(className)
.
public SingleVariableDeclaration newSingleVariableDeclaration()
public VariableDeclarationFragment newVariableDeclarationFragment()
public Initializer newInitializer()
public Javadoc newJavadoc()
public VariableDeclarationStatement newVariableDeclarationStatement(VariableDeclarationFragment fragment)
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.
fragment
- the variable declaration fragment
IllegalArgumentException
- if:
public TypeDeclarationStatement newTypeDeclarationStatement(TypeDeclaration decl)
This method can be used to convert a type declaration
(TypeDeclaration
) into a statement
(Statement
) by wrapping it.
decl
- the type declaration
IllegalArgumentException
- if:
public Block newBlock()
public ContinueStatement newContinueStatement()
public BreakStatement newBreakStatement()
public ExpressionStatement newExpressionStatement(Expression 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.
expression
- the expression
IllegalArgumentException
- if:
public IfStatement newIfStatement()
public WhileStatement newWhileStatement()
public DoStatement newDoStatement()
public TryStatement newTryStatement()
public CatchClause newCatchClause()
public ReturnStatement newReturnStatement()
public ThrowStatement newThrowStatement()
public AssertStatement newAssertStatement()
public EmptyStatement newEmptyStatement()
public LabeledStatement newLabeledStatement()
public SwitchStatement newSwitchStatement()
public SwitchCase newSwitchCase()
public SynchronizedStatement newSynchronizedStatement()
public ForStatement newForStatement()
public StringLiteral newStringLiteral()
public CharacterLiteral newCharacterLiteral()
public NumberLiteral newNumberLiteral(String literal)
literal
- the token for the numeric literal as it would
appear in Java source code
public NumberLiteral newNumberLiteral()
"0"
.
public NullLiteral newNullLiteral()
public BooleanLiteral newBooleanLiteral(boolean value)
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));
value
- the boolean value
public Assignment newAssignment()
public MethodInvocation newMethodInvocation()
public SuperMethodInvocation newSuperMethodInvocation()
public ConstructorInvocation newConstructorInvocation()
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.
public SuperConstructorInvocation newSuperConstructorInvocation()
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.
public VariableDeclarationExpression newVariableDeclarationExpression(VariableDeclarationFragment fragment)
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.
fragment
- the first variable declaration fragment
IllegalArgumentException
- if:
public FieldDeclaration newFieldDeclaration(VariableDeclarationFragment fragment)
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.
fragment
- the variable declaration fragment
IllegalArgumentException
- if:
public ThisExpression newThisExpression()
public FieldAccess newFieldAccess()
public SuperFieldAccess newSuperFieldAccess()
public TypeLiteral newTypeLiteral()
public CastExpression newCastExpression()
public ParenthesizedExpression newParenthesizedExpression()
public InfixExpression newInfixExpression()
public InstanceofExpression newInstanceofExpression()
public PostfixExpression newPostfixExpression()
public PrefixExpression newPrefixExpression()
public ArrayAccess newArrayAccess()
public ArrayCreation newArrayCreation()
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"));
public ClassInstanceCreation newClassInstanceCreation()
public AnonymousClassDeclaration newAnonymousClassDeclaration()
public ArrayInitializer newArrayInitializer()
public ConditionalExpression newConditionalExpression()
|
Eclipse JDT 2.0 |
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |