Search code examples
javajavaparser

get method statements using javaparser


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;

Solution

  • .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());
    }