views:

179

answers:

1

Given an ANTLR Java Grammar - what java source code would I write to get a list of method names in a java source listing?

eg for the following

public class HelloWorld {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new HelloWorld().printHelloWorld();
    }

    private void printHelloWorld() {
        // TODO Auto-generated method stub
        System.out.println("Hello World");
    }

}

would return

main
printHelloWorld
+1  A: 

Basically you need to edit the grammar file to add that functionality. Find the rule that detects methods, and add some code there that adds the name of the method to a list the parser is collecting. Then, after you instantiate the parser you can access the method names.

You can see a full example here (edited grammar file for Java used in JUnitConverter (shameless plug)), specifically the addMethod method.

abyx