Extract variable references in Javac

I am developing a java program that extends TreeScanner from com.sun.source.util. I am looking for sth as visitMethodInvocation, but for a variable.

I was wondering if there is a way to extract variable references when calling visitVariable? Or please let me know what is the best way to extract links using TreeScanner.

A simple example: in the code below, when I visit a method (visitMethod), I retrieve all its operators, and then call VariableTreeScanner. I was wondering if I can extract variable references in visitvariable?

public static class MethodTreeScanner extends TreeScanner<Void, Void> {
    @Override
    public Void visitMethod(MethodTree arg0, Void arg1) {
        List<? extends StatementTree> statements = arg0.getBody().getStatements();
        for(StatementTree statementTree: statements) {

            Kind statementKind = statementTree.getKind();
            if (statementKind.equals(Tree.Kind.VARIABLE)) {
                statementTree.accept(new VariableTreeScanner (compilationUnitTree, sourcePositions, arg0.getName().toString()), null);
        }
    }
}

public static class VariableTreeScanner extends TreeScanner<Void, Void> {
     @Override
     public Void visitVariable(VariableTree arg0, Void arg1) {
           // I like to extract variable references here, or let me know if there is a better way to do this.
     }
 }        
+4
source share

Source: https://habr.com/ru/post/1569870/


All Articles