简体   繁体   English

提取Java方法中引用的实例变量

[英]Extract instance variables referenced in a java method

I have a class like below 我有一个像下面这样的课程

class A{
    private var1, var2;

    public void methodA(){
        sout(var1);
    }
    public void methodB(){
        sout(var1);
        sout(var2);
    }
}

Here in that code snippet I have class with two instance variables var1 , var2 and two methods methodA , methodB . 这里在代码片段我有类两个实例变量VAR1,VAR2和两个方法了methodA, 的methodB。 var1 is referenced in both methodA and methodB . VAR1中都和了methodA引用的methodB。 How can I extract this information from a java class? 如何从Java类中提取此信息?

You can use this library: JavaParser Core 您可以使用以下库: JavaParser Core

    JavaAnalyzer jpa = new JavaAnalyzer(this, "A.java");

    AtomicBoolean var1IsReferencedInMethodA = new AtomicBoolean(false);

    jpa.visit((MethodDeclaration methodDeclaration) -> {
        var1IsReferencedInMethodA.setTrueWhen(methodDeclaration.getName().equals("methodA")
                && (methodDeclaration.getBody().getStmts().get(0).toString().equals("var1")));
    });

    System.out.println("var1 is referenced in methodA(): " + var1IsReferencedInMethodA);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM