Results 1 to 10 of 10
  1. #1
    axenos is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default Parse .java files

    Hello,

    I am using JavaParser (Java Source Reflection) to parse .java files, but what I really want is to get the info from the comments, Javadoc and simple // comments.

    JavaParser parses only the code and not the comments. So:

    Java Code:
    JavaMethod[] methods = js.getMethods();
    for (int i = 0;  i < methods.length;  i++) {
        System.out.println("  " + methods[i].getName());
        System.out.println("    " + methods[i].getComment());
    }
    gives the names of the methods but null for the javadoc comments.

    I am using netbeans. Do you know which library should I use?

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    If you have access to a .java file, why don't you parse it yourself for the comments? Just look for the method declaration, and search within the brackets for any occurrences of "//".

  3. #3
    axenos is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Thanks 4 your answer ra4king.

    ..Because I need to take the comments separately from the methods, then from the description of the class, then from the fields and then any other "//" comments.
    I want this to work with any source file with unknown to me structure, not only this particularly.

    Is there a parser at all? I can't find a parser to work like this.

  4. #4
    axenos is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    I am making my own parser.

    If I know the method's name in the source code, how can I take the previous lines that contain the method Javadoc?

    for the author's name, I have done the following code.

    Java Code:
    FileInputStream fstream = new FileInputStream(pFile.getAbsolutePath());
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                // Read File Line By Line
                while ((strLine = br.readLine()) != null) {
                    // Print the content on the console
                    // System.out.println (strLine);
                    if (strLine.contains("* @author")){
                        author = strLine.substring("* @author".length()+2);
                        System.out.println("Author: " + author);
                        System.out.println();
                    }
    }
    .........e.t.c

  5. #5
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    I wouldn't suggest doing "substring("* @author*.length()+2)" because you may not know how many stars and spaces there might be. try doing:
    Java Code:
    strLine.substring(strLine.indexOf("@author")+8);
    If you want the comments right before a method, I would suggest storing all the lines in an ArrayList first then, after searching and finding the method declaration, parse the previous lines for the comments.

  6. #6
    axenos is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    ok!! I did do that with the ArrayList and it worked!

    I will try the tip you told me..

    Thanks again, good luck at you do!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by axenos View Post
    ok!! I did do that with the ArrayList and it worked!

    I will try the tip you told me..

    Thanks again, good luck at you do!
    Before you start implementing everything yourself, read the API documentation for the Doclet class in the JavaDoc documentation. A Doc instance tells you about the comment part of several details of a Java program (methods, classes, fields etc.)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Where is this Doclet class? I can't seem to find it :/

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Where is this Doclet class?
    Here.

    db

  10. #10
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Hm, I have actually never ventured into the "com" package. Pretty cool classes over there!

Similar Threads

  1. Replies: 10
    Last Post: 04-12-2011, 06:00 PM
  2. Parse Data from Java Applet
    By ssh1091 in forum Java Applets
    Replies: 6
    Last Post: 02-27-2011, 03:28 AM
  3. Parse log file using java
    By mvg in forum Advanced Java
    Replies: 0
    Last Post: 07-03-2009, 11:36 AM
  4. Parse EDI File Using java
    By vaskarbasak in forum Advanced Java
    Replies: 6
    Last Post: 09-24-2008, 02:38 PM
  5. How to parse an xml file using java
    By karthik84 in forum XML
    Replies: 4
    Last Post: 09-12-2008, 09:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •