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 7420471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:14:40+00:00 2026-05-29T08:14:40+00:00

I have created a little program that reads in a Java file and feeds

  • 0

I have created a little program that reads in a Java file and feeds it to the ASTParser from the Eclipse JDT to build an Abstract Syntax Tree (AST). The root node is a CompilationUnit which I am able to access. I then iterate over the Types collection which contains the class(es) from the Java file. In my case, there is only one (the public class). This class is represented as an object that is of the type TypeDeclaration. I know I have successfully accessed this object because I am able to get its SimpleName and print it to the console.

TypeDeclaration has many methods including getFields() and getMethods(). However, when I call these methods, they return collections that are empty. The Java class that I am reading certainly has both Fields and Methods, so I don’t understand why it is coming up as empty. Any ideas what is causing this? Am I misusing this API in some way or have I not initialized something?

Here is a reduced version of my code for accessing the AST:

// char array to store the file in
char[] contents = null;
BufferedReader br = new BufferedReader(new FileReader(this.file));
StringBuffer sb = new StringBuffer();
String line = "";
while((line = br.readLine()) != null) {
    sb.append(line);
}
contents = new char[sb.length()];
sb.getChars(0, sb.length()-1, contents, 0);

// Create the ASTParser
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(contents);
parser.setResolveBindings(true);
CompilationUnit parse = (CompilationUnit) parser.createAST(null);

// look at each of the classes in the compilation unit
for(Object type : parse.types()) {
    TypeDeclaration td = (TypeDeclaration) type;
    // Print out the name of the td
    System.out.println(td.getName().toString()); // this works
    FieldDeclaration[] fds = td.getFields();
    MethodDeclaration[] mds = td.getMethods();
    System.out.println("Fields: " + fds.size()); // returns 0???
    System.out.println("Methods: " + mds.size()); // returns 0???
}

Here is the Java file that I am reading in:

public class Vector {

    // x, the first int value of this vector
    private int x;

    // y, the second int value of this vector
    private int y;

    public Vector(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public String toString() {
        return "Vector( " + this.x + " , " + this.y + " )";
    }

    public int getX() {
        return x;
}

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}

So, as expected, the first print in my AST code results in Vector, but then the subsequent prints result in Fields: 0 and Methods: 0, when I would actually expect Fields: 2 and Methods: 6.

  • 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-05-29T08:14:41+00:00Added an answer on May 29, 2026 at 8:14 am

    The problem with the above code is that the newline characters (\n) are being lost. Each time the contents of the BufferedReader are appended to the StringBuffer (sb), the \n isn’t being included. The result is that from line 3 of the sample program on, everything gets commented out because the program is read in as:

    public class Vector { // x, the first int value of this vector private int x; ...
    

    No worries though because there is a simple solution. Inside of the while loop of your parsing program, simple append \n to the end of each line of input read. Like follows:

    ...
    while((line = br.readLine()) != null) {
        sb.append(line + "\n");
    }
    ...
    

    The program should now be read in correctly and the output should be, as expected, 2 fields and 6 methods!

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

Sidebar

Related Questions

I have created a few little programs that export data to a text file
I have written a program in Java to read in a text file of
I have a little program that I want to make open automatically when my
I have a simple javascript program that's acting a little funny. When I try
As you can see below I have created a little program to concatenate 2
I am using vb.net 2010 and I have created a program that uses sockets
Hi I have created a little application to move some files around and put
I have a page which contains a jQuery-UI horizontal slider, created using a little
I'm a jQuery newbie, and I have trouble with a little script I created.
I have created a PHP-script to update a web server that is live inside

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.