2

I have some interesting exercise - to read .java file with Java, to find every code part in this .java file(like methods and etc.) and to say how many line numbers we have in every method without whitespaces and comments and also to find the count of items in every method.

Here is some example that I use - so could Java Parser. With the code I can say how many line numbers I have in method, but cannot remove the count of whitespaces and comments. Can someone help me?

The code:

public static void main(String[] args) throws ParseException, IOException {
    File f = new File(".").getAbsoluteFile();
    File srcRoot = new File(f, "src");
    String srcFilename = TestMethodLineNumber.class.getName().replaceAll("\\.", "/") + ".java";
    File src = new File(srcRoot, srcFilename);
    System.out.println(f);
    System.out.println(srcRoot);
    System.out.println(src);
    getMethodLineNumbers(src);
}

private static void getMethodLineNumbers(File src) throws ParseException, IOException {
    CompilationUnit cu = JavaParser.parse(src);
    new MethodVisitor().visit(cu, null);
}

/**
 * Simple visitor implementation for visiting MethodDeclaration nodes.
 */
private static class MethodVisitor extends VoidVisitorAdapter {
    @Override
    public void visit(MethodDeclaration m, Object arg) {
        System.out.println("From [" + m.getBeginLine() + "," + m.getBeginColumn() + "] to [" + m.getEndLine() + ","
                + m.getEndColumn() + "] is method:");
        System.out.println(m);
    }
}

}

  • Welcome to SO. Please read [ask]. Your question is of the 'why doesn't thins code work?' type, and you haven't met the quality standards for this so I'm voting to close your question. – Software Engineer Jan 22 '15 at 21:46
  • @EngineerDollery: He didn't ask why his code didn't work; he showed code that computed an answer which he hopes to improve. He wants some advice on how to improve his answer. – Ira Baxter Jan 22 '15 at 21:51
  • @akyurkchiev: Your code computes the size using the left- and right-most nodes of the method. What it does not do is descend *into* the tree for the method to find comments or whitespace and adjust the answer; in essence, you need to walk the subtree and check for position gaps across code leaves. While I have done exactly this with another parser/AST system, I'm not an AST Java Parser expert, so I can't help you much with the details; I'm sure some other enterprising SO person likely can. – Ira Baxter Jan 22 '15 at 21:55
  • @IraBaxter -- I had formed a different opinion, but I'll bow to your greater wisdom, thanks for the heads up. – Software Engineer Jan 22 '15 at 22:04
  • I am very sorry if I didn't post my question correctly. @ IraBaxter is right that I just asking for some advice to improve my code - to count only the code and exclude comments and blank spaces.I will appreciate any kind of help or advice :) – akyurkchiev Jan 23 '15 at 04:40

1 Answers1

0

It looks to me like JavaParser already removes blank lines, I would use a technique from this thread Removing all types of comments in java file to remove all the comments and then use JavaParser to get your results.

In general questions like these aren't always interesting unless you're actually going after executable code. Consider:

if (a)
{
    runA();
}
else
{
    runB();
}

You're count will be 8, what exactly does that tell you? Is it more interesting to know there is one if statement and/or two method calls? You'll really want better constraints to know what you're going after:

a ? runA() : b ? runB(): runError("nothing true");

Is only one line, but you have 3 potential execution branches. You can also run into some pretty crazy statements that span multiple lines.

MyFancyObject myFancyObject = new MyFancyObject("This is my really long name",
                                                123113212,
                                                "2014-11-20 18:43:12",
                                                anotherArg, yetAnother,
                                                moar);

Should this be 5 lines or 1?

Community
  • 1
  • 1
JimW
  • 186
  • 8
  • I don't think you answered his question. – Ira Baxter Jan 23 '15 at 17:54
  • His question was "can someone help me?" The more general questions were about removing whitespace (unneeded and in my answer) and removing comments (in my answer). – JimW Jan 23 '15 at 20:46
  • He doesn't want to remove whitespace. He wants to *count* whitespace in the method so he can subtract that count from the apparant line count of the entire method. – Ira Baxter Jan 23 '15 at 21:37