Eclipse parser fails to parse method body correctly

68 Views Asked by At

This is the code I am using and it reads other method bodies correctly except the test method in the TestExample file. I believe it is due to the predicate (->). Please help me fix this.

public static void testrun2(String str) {
    ASTParser parser = ASTParser.newParser(AST.JLS4);

    parser.setSource(str.toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setResolveBindings(true);

    // apply classpath
    parser.setEnvironment(new String[] {dir}, null, null, true);
    parser.setUnitName("any_name");

    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

    cu.accept(new ASTVisitor() {
        public boolean visit(MethodDeclaration node) { 
            System.out.println("\n");
            System.out.println("Method name:"+node.getName().getIdentifier());
            Block block = node.getBody();
            System.out.println("Here is the problematic output:"+ block.toString());
            return true;
        }
    });
}

Here is the java file I'm reading (and it is being read correctly which means input to testrun2 is correctly read string)

package eclipseparser;

public class TestExample {
    private static String dir="./eclipseparser";

    public void test() throws IOException {
        Files.walk(Paths.get(dir))
             .filter(Files::isRegularFile)
             .filter(p -> p.toString()
                           .toLowerCase()
                           .endsWith(".java"))
                           .forEach(f -> {
                               System.out.println("this statement is skipped");
                            });
        System.out.println("this statement is read");
    }

    public static void main(String[] args) throws IOException {
        TestExample t=new TestExample();
        t.test();
    }
}

Following is my output:

output

0

There are 0 best solutions below