VariableReadExtractor extends BaseTreeVisitor { public Set<Symbol> usedVariables() { return used; } VariableReadExtractor(MethodSymbol methodSymbol, boolean includeFields); Set<Symbol> usedVariables(); @Override void visitAssignmentExpression(AssignmentExpressionTree tree); @Override void visitVariable(VariableTree tree); @Override void visitClass(ClassTree tree); @Override void visitLambdaExpression(LambdaExpressionTree lambdaExpressionTree); @Override void visitIdentifier(IdentifierTree tree);  }
@Test void should_not_extract_local_vars_written() throws Exception { MethodTree methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { new A().field1 = 0; a = false;} }; }"); StatementTree statementTree = methodTree.block().body().get(0); VariableReadExtractor extractor = new VariableReadExtractor(methodTree.symbol(), false); statementTree.accept(extractor); assertThat(extractor.usedVariables()).isEmpty(); methodTree = buildMethodTree("void foo(boolean a) { new Object() { void foo() { a = !a;} }; }"); statementTree = methodTree.block().body().get(0); extractor = new VariableReadExtractor(methodTree.symbol(), false); statementTree.accept(extractor); assertThat(extractor.usedVariables()).hasSize(1); }