1 Reply Latest reply: Jul 25, 2008 10:52 AM by thomas.behr RSS

    How to count number of lines inside methods() using the Doclet API

      Wrote a custom doclet using the [Doclet API|http://java.sun.com/j2se/1.3/docs/tooldocs/javadoc/doclet/index.html ].

      The purpose for the doclet is to load Java source files and create stubs (which are identical Java source files but do not contain any method implementation details).

      Instead, the method implementation details need to be replaced with blank lines...
      public class MyDoclet {
           private static String TAB = "\t";
           public static boolean start(RootDoc root) {
                ClassDoc[] classes = root.classes();
                // Parse through class or interface
                for (ClassDoc clazz : classes) {
                     Type superClass = clazz.superclassType();     
                     // Print Methods
                     MethodDoc[] methods = clazz.methods(); 
                     for (MethodDoc method : methods) {
                          Parameter[] parameters = method.parameters();
                          if (!method.isPrivate()) {
                               print(TAB + method.modifiers() + " " 
                                                      + method.returnType().simpleTypeName() + " " + method.name());
                               for (int i=0; i < parameters.length; i++) {
                                    Parameter parameter = (Parameter) parameters;
                                    print(parameter.type().simpleTypeName() + " " + parameter.name());

                                    if (i != parameters.length - 1) {
                                         print(", ");
                               println(" {");
                               println(TAB + "}");
                return true;
      As one can see, I am just creating the method and placing the opening and closing curly braces (along with a new \n line escape sequence, in between).
      Am not really that familiar with the Doclet API...
      (1) What is the best way to figure out how many lines of code are inside each method and then use a for loop to insert the exact same number of blank lines inside the methods?
      (2) Is there a way to do it using the com.sun.javadoc.SourcePosition.line() method? 
      Would really appreciate it if someone could help me because this is an important requirement (hence the 10 Duke Stars).
      Happy coding to all,
        • 1. Re: How to count number of lines inside methods() using the Doclet API
          This is not possible using the Doclet API, because JavaDoc does not store any information regarding implementation detail. Although MethodDoc#position will give you the line where the method is declared, there is no way to determine where the method body starts and ends.

          If you need that much information, maybe you would be better of using a tool such as Eclipse's Abstract Syntax Tree parser. AST will provide you with line numbers for each code expression, hence it is relatively easy to compute the first and last line in a method body.