Search code examples
javaparsingjuniteclemmajavaparser

How to extract the boolean value of the branch decision?


I am currently working on a project in which I need to run JUnit test cases and then extract information from the Target Class (class being tested). To extract the statements covered I have used eclEmma, but using this tool I am only able to extract the line numbers which were executed in the Target after running the test class.

I want to extract the Boolean values of branch decisions in the if-statements to derive which conditions are verified when covering a specific true/false branch of the Target Class.

I know that I can parse the Java file using JavaParser and then extract the following information, but I am not sure how to extract the boolean value of the branch decision.

public void visit(IfStmt stmt, Void arg) 
    {
        System.out.println(stmt.getCondition());
        System.out.println(stmt.getElseStmt());
        System.out.println(stmt.getThenStmt());
        super.visit(stmt, null);
    }

Solution

  • EclEmma is based on JaCoCo code coverage library and simply provides its integration into Eclipse, the actual work (instrumentation and analysis) is performed by JaCoCo.

    JaCoCo performs analysis of bytecode, not the source code. Source code is used only to visualize results of bytecode analysis (decorate by highlighting). Bytecode contains only information about lines, so unfortunately mapping bytecode branches back to the source code is not at all a trivial task and doesn't have much in common with parsing of source code. That's why this is not provided out-of-the-box by JaCoCo. However some experimental works were done on this subject. Have a look on the following links for more details:

    Contributions are welcomed to JaCoCo, so if someone is able to reliable implement such mapping - don't hesitate to contribute back.