get method statements using javaparser

2019-06-01 06:16发布

问题:

Is it possible to get list of method statements without comments, i used method.getBody() and this is the output

/*
set the value of the age integer to 32
*/
int age = 32;

I want to make statements only are the outcome like this

int age = 32;

回答1:

.getBody() method return BlockStmt object which is Statements in between { and } so the following code do what i want

Optional<BlockStmt> block = method.getBody();
NodeList<Statement> statements = block.get().getStatements();

for (Statement tmp : statements) {
    tmp.removeComment();
    System.out.println(tmp.toString());
}