I want to parse the method check() using java parser. With getStatements() method in MethodDeclaration I can get the method. But I want to traverse the code inside the method. Is there any way to do this. I need to count the number of return statement inside this method.
final JavaParser javaParser = new JavaParser();
final CompilationUnit compilationUnit = pr.getResult().get();
final TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);
MethodDeclaration md = (MethodDeclaration) resourceClass.getMethodsByName("check");
md.getBody().get().getStatements();
private int check(){
if(x=y)
return 10;
else
return 5;
}
You need to define a visitor that counts
ReturnStmtinstances. One way to do this is the following.Notes
countReturnStmtstakes in any type of node that could be in a ASTReturnStmtinstance, you stop and return 1.