2

I use Eclipse JDT library to parse java source code to visit all methods defined in classes. When the code contains comment like "//xxxx" in the body of the method, the parser will stop before the comment, and the method main(String[] args) was ignored.

This is the sample case for parsing:

public class HelloWorld {

    private String name;
    private int age; 

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
                //Set the age
        this.age = age;
        System.out.println();
    }

    public static void main(String[] args) {

        HelloWorld hw = new HelloWorld();
        if(true) {
            hw.setAge(10);
        }

    }
}

This is the code I write to parse the above sample case:

public class Parser {

/**
 * Parse java program in given file path
 * @param filePath
 */
public void parseFile(String filePath) {
    System.out.println("Starting to parse " + filePath);
    char[] source = readCharFromFile(filePath);
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(source);
    parser.setResolveBindings(true);
    CompilationUnit cu = (CompilationUnit) parser.createAST(null);

    cu.accept(new ASTVisitor() {

        @Override
        public boolean visit(MethodDeclaration node) {
            return true;
        }

        @Override
        public void endVisit(MethodDeclaration node) {
            System.out.println("Method " + node.getName().getFullyQualifiedName() + " is visited");
        }

    });
}
}

When I use it to parse the code, it can only get the result that method getName(), setName(), getAge() and getAge() have been visited while main() is ignored.

Looking forward to your answers. Thanks.

2 Answers 2

1

There seems to problem with the code you are using to read the source.

Try this code below to read the file :

File javaFile = new File(filePath);
BufferedReader in = new BufferedReader(new FileReader(javaFile));
final StringBuffer buffer = new StringBuffer();
String line = null;
while (null != (line = in.readLine())) {
     buffer.append(line).append("\n");
}

and use this to set your parser source :

parser.setSource(buffer.toString().toCharArray());

Everything else seems to be fine with the code.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that your readCharFromFile(filePath) method removes \n or end-of-line characters from the file. This means that all lines after the comment are actually part of the comment.

1 Comment

It really strips \n characters? Why would that be useful functionality? Pretty funny if true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.