Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8096239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:23:27+00:00 2026-06-05T21:23:27+00:00

I use Eclipse JDT library to parse java source code to visit all methods

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:23:29+00:00Added an answer on June 5, 2026 at 9:23 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Eclipse to write Java code and use DropBox to sync my code
I've trying to use Eclipse JDT AST parsing classes. After including the initial JAR,
I use Eclipse to study some open source projects on Android. I keep seeing
I use Eclipse Java EE IDE for Web Developers for most of my coding,
I use new Eclipse. Create demo test with jUnit (I added default jUnit library
I am working on an Eclipse plugin that modifies Java code in a user's
I use Eclipse to write the code and I get a red underline at
I use eclipse to develop in Java, because it has a lots of useful
Given the following code in Eclipse: import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.CompilationUnit; public class
Some time ago I wrote an Eclipse plugin which makes use of JDT to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.