0

I want to collect methods in a class that set a certain field e.g. int i = 2;. I know how to get the Statements of a method:

CompilationUnit cu = StaticJavaParser.parse("class A {private int i; public int SetI(int i){this.i=i;}}");
List<MethodDeclaration> methods = cu.findAll(MethodDeclaration.class);
Statement statements = md.getBody().get().getStatements().get(0);
if (statement.isReturnStmt())
    var r = statement.asReturnStmt();

but assignments seem to be expressions and I have no Idea how to get to them. How can I get the (assignment) expressions in a method?

peer
  • 4,171
  • 8
  • 42
  • 73

1 Answers1

0
if (statemet.isExpressionStmt()) {
    Expression e = statement.asExpressionStmt().getExpression();

does the trick.

peer
  • 4,171
  • 8
  • 42
  • 73