Update 2: thanks again to @deepak-azad, I managed to solve my problem : here is a link to the main code : https://gist.github.com/1714641
Update : thanks to @deepak-azad, I complemented the code, but still not working.
Im trying to instrument a source file in Java Using EclipseJDT. My main goal is to put some statement “x();” below every variable declaration.
for example:
From this:
int a = 10;
to this :
int a = 10;
method();
I was able to create the ast and made a class tha extend the visit method to get all the variable declaration inside the code :
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
public class VDS extends ASTVisitor {
List<VariableDeclarationStatement> vds = new ArrayList<VariableDeclarationStatement>();
@Override
public boolean visit(VariableDeclarationStatement node) {
vds.add(node);
return super.visit(node);
}
public List<VariableDeclarationStatement> getVDS() {
return vds;
}
}
I tested it and it works fine the capture of each variable, but not the insertion of a new node, using something like this:
VDS vds = new VDS();
unit2.accept(vds); // unit2 is the compilation unit
System.out.println("Variables :" + vds.getVDS().size());
for(VariableDeclarationStatement vds_p : vds.getVDS()){
System.out.println(vds_p.toString())
List<Statement> a = ((Block) vds_.getParent()).statements();
a.add(e);//e is the artificial statement
ListRewrite lrw = astRewrite.getListRewrite(vds_p.getParent().BLock.STATEMENTS_PROPERTY);
lrw.insertAfter(e,vds_p,null);
}
And the code to actually write down is (this part works I tested with a arbitrary annotation)
IDocument document2;
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); // get the buffer manager
IPath path = unit2.getJavaElement().getPath(); // unit: instance of CompilationUnit
try {
bufferManager.connect(path, null);
ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path);
// retrieve the buffer
document2 = textFileBuffer.getDocument();
textFileBuffer.commit(null /* ProgressMonitor */, false /* Overwrite */);
} finally{ bufferManager.disconnect(path, null);}
TextEdit edits = unit2.rewrite(document2, null);
edits.apply(document2);
write2File(document2);
Im a bit confused on how does the hierarchy works, and I came to an idea based on the fact that each variable statement belong to a block , and each block belong to a method, but still dont know how to handle this in the ListRewrite element.
I’ve been reading about ast in eclipse, but all question are related with the creation of code but not the process of edition
When I executed this, I get an illegal argument exception like this :
java.lang.IllegalArgumentException
at org.eclipse.jdt.core.dom.ASTNode.checkNewChild(ASTNode.java:1905)
at org.eclipse.jdt.core.dom.ASTNode$NodeList.add(ASTNode.java:1269)
at java.util.AbstractList.add(AbstractList.java:91)
at plugin.astsimple.handlers.SampleHandler.execute(SampleHandler.java:109)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:293)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:476)
...
where line 109 of my code is the one that adds e to a
a.add(e);
Thanks!
Each ‘Block’ consists of a ‘list’ of statements, hence you need to use “org.eclipse.jdt.core.dom.rewrite.ListRewrite.insertAfter(ASTNode, ASTNode, TextEditGroup)”. You can look for examples of this method’s usage in org.eclipse.jdt.ui plug-in.