Reputation: 65
Let's say I have a method:
void method() {
int i = 5;
}
I want to insert a statement at the very beginning of that method, between { and int i = 5;. How do I do that with javaparser? When visiting methods, ASTHelper.addStmt(..) inserts the statement at the end of the method.
Upvotes: 0
Views: 490
Reputation: 2248
You can use the visitor on the MethodDeclaration
Once you have the MethodDeclaration you can get its body (getBody) and then access the list of statements composing the body (getStmts). You can your statement in the position you want by calling the add method which accept an index: add(int, N)
Note: I am referring to the upcoming version of JavaParser, 3.0.0. The API of the current version can be slightly different but you should get the idea
Source: I am a JavaParser contributor
Upvotes: 3