Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » [AST] How to create a new Expression Statement?
[AST] How to create a new Expression Statement? [message #167504] Wed, 07 July 2004 15:15 Go to next message
Eclipse UserFriend
Originally posted by: jkee.andrew.cmu.edu

Hi,

I want to be able to add statements into an AST because I want to perform
some sort of source code editing (not using the editors). I'm trying to
figure out how to create a new expression statement out of a string,
e.g. System.out.println("foo");

The AST has method newExpressionStatement which takes in an Expression,
but how to create an Expression with my string? Apparently I need some
sort of parser than can build that portion of AST out of the incomplete
compilation Unit. Any pointers? Thanks in advance.
Re: [AST] How to create a new Expression Statement? [message #167545 is a reply to message #167504] Wed, 07 July 2004 19:46 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: olivier_thomannNO.ca.ibm.comSPAM

Le Wed, 7 Jul 2004 15:15:38 +0000 (UTC), jkee@andrew.cmu.edu (Chow) a
écrit :

>The AST has method newExpressionStatement which takes in an Expression,
>but how to create an Expression with my string? Apparently I need some
>sort of parser than can build that portion of AST out of the incomplete
>compilation Unit. Any pointers? Thanks in advance.
You can parse a statement using the org.eclipse.jdt.core.dom.ASTParser
methods. You can then add this statement inside the rest of the tree.

Or you can create the nodes from scratch using the method on the
org.eclipse.jdt.core.dom.AST.
For System.out.println("foo");, you have to create a method invocation
with a string literal as the parameter.
--
Olivier
Re: [AST] How to create a new Expression Statement? [message #167599 is a reply to message #167504] Wed, 07 July 2004 21:16 Go to previous message
Eclipse UserFriend
Originally posted by: turker.keskinpala.vanderbilt.edu

Hi,

I spent some time to figure out that same issue and thought I would share my
findings. I assume you have a CompilationUnit already but I will write it
down just in case:

//Create parser
parser = ASTParser.newParser( AST.JLS2);
//Set the source of that parser
parser.setSource( source );
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
//This enables the modifications to the cu
cu.recordmodifications

An expression can basically be several things like MethodInvocation,
StringLiteral etc. Let's say you want to create a newIfStatement and set its
expression. You can do this:

IfStatement if_statement = ast.newIfStatement();
if_statement.setExpression(ast.newSimpleName("x"));
//ast.newSimplename("x") is the expression parameter in this case


There is a test code where you can see implementations of AST methods:
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jdt.cor e.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTVisitorT est.java

Hope this helps,
Turker




"Chow" <jkee@andrew.cmu.edu> wrote in message
news:cch42q$b9f$1@eclipse.org...
> Hi,
>
> I want to be able to add statements into an AST because I want to perform
> some sort of source code editing (not using the editors). I'm trying to
> figure out how to create a new expression statement out of a string,
> e.g. System.out.println("foo");
>
> The AST has method newExpressionStatement which takes in an Expression,
> but how to create an Expression with my string? Apparently I need some
> sort of parser than can build that portion of AST out of the incomplete
> compilation Unit. Any pointers? Thanks in advance.
>
>
Previous Topic:Error: "Required library cannot denot external folder"
Next Topic:HOW DO I ? : sharing/overriding default preferences between users in 3.0
Goto Forum:
  


Current Time: Fri Apr 26 17:58:59 GMT 2024

Powered by FUDForum. Page generated in 0.02828 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top