b)......}Supposing that ifnode is an IF_STATEMENT node, I can do something simila" name="description" /> b)......}Supposing that ifnode is an IF_STATEMENT node, I can do something simila" />
48047

Eclipse Java AST parser: insert statement before if/for/while

Question:

I'm using the org.eclipse.jdt parser.

I want to rewrite this code:

public void foo(){ ... ... if(a>b) ... ... }

into this:

public void foo(){ ... ... System.out.println("hello"); if(a>b) ... ... }

Supposing that ifnode is an IF_STATEMENT node, I can do something similar to this:

Block block = ast.newBlock(); TextElement siso = ast.newTextElement(); siso.setText("System.out.println(\"hello\");"); ListRewrite listRewrite = rewriter.getListRewrite(block, Block.STATEMENTS_PROPERTY); listRewrite.insertFirst(ifnode, null); listRewrite.insertFirst(siso, null); rewriter.replace(ifnode, block, null);

but this will insert the syso statement at the beginning of the method, while I want it right before the if.

Is there a way to achieve it?

Answer1:

You can use the below code to achieve this (this will add the sysout just before the first IfStatement) :

Block block = ast.newBlock(); TextElement siso = ast.newTextElement(); siso.setText("System.out.println(\"hello\");"); ListRewrite listRewrite = rewriter.getListRewrite(block, CompilationUnit.IF_STATEMENT); listRewrite.insertFirst(siso, null); TextEdit edits = rewriter.rewriteAST(document, null);

Also you can limit the scope of rewrite to the IfStatement:

ASTRewrite rewriter = ASTRewrite.create(ifNode.getAST());

Note: code not tested. Do let me know if you find any issues.

Recommend

  • Flipping the order of subrules inside a rule in a boost::spirit grammar results in segfault
  • Using functions as arguments in hibernate aggregation functions
  • SQLServerException: The index 1 is out of range when executiong Spring Data findAll(Pageable)
  • How to generate an asynchronous reset verilog always blocks with chisel
  • Do I have to inject the service in the unit test if I cal testBed.get() previously?
  • How to align an image side by side with a heading element?
  • How can I emulate a recursive type definition in C++?
  • How can I print out a large table of symbols from MATLAB to form a cross-stitch chart?
  • findObjectsInBackgroundWithBlock block signature not correct
  • Are there “Dynamic Playlists” of unit tests in Visual Studio?
  • Ionic Slide Up Slide Down Animation for only one view in sidebar
  • Select inner HTML item in CSS
  • Want to understand iframe breakout code
  • How gzip file gets stored in HDFS
  • Changing references to deprecated methods C++
  • TFS - how do I sum child task hours to parent
  • react split panel resize
  • Responsive left sidebar open close
  • Prevent page break in text block with iText, XMLWorker
  • Why people use prototype in javascript when it is easy to inherit using apply () and call () methods
  • Is it possible to get the word under the mouse cursor in a ``?
  • c++ regex_replace not doing intended substitution
  • How to render a pixel array most efficiently to a window in c++?
  • C++ friend class std::vector
  • abstracting over a collection
  • SyntaxError: (irb):26: both block arg and actual block given
  • Find group of records that match multiple values
  • Calculate time difference in hh:mm:ss with simple javascript/jquery
  • TFS 2015 - Waiting for an agent to be requested
  • Redirect STDERR in OPEN pipe comand. Perl Linux
  • Remove changes from one element when event occurs on another element?
  • RxJava debounce by arbitrary value
  • Android Google Maps API v2 start navigation
  • CakePHP ACL tutorial initDB function warnings
  • how to avoid repetitive constructor in children
  • Spark fat jar to run multiple versions on YARN
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • ActionScript 2 vs ActionScript 3 performance
  • json Serialization in asp
  • How can i traverse a binary tree from right to left in java?