Can I generate Javadoc comments with the help of a custom doclet?

北城以北 提交于 2019-12-11 16:19:33

问题


What I mean by comment:

/**
*My Comment from database
*/

My Problem:

I got a bunch of DTO's which are not commented at all. However, there are comments in the SQL-Database. I can get these comments by sending a query and then retrieving the ResultSet.

My task is to create a javadoc-API (as HTML and inside the javacode) with the comments from the SQL-Database in order to make the codebase better understandable.

I have written a small java program that retrieves the comments from the sql-database. I write the found comment into the file with the desired javadoc comment format shown above. No text in the comment column means null for the comment (which is ok!)

For clarification here are pictures before and after. (dont worry about the text being german)

This is what it looks like right now

This is how it should look like after

As mentioned at the start.. Can I generate Javadoc comments (like this) with the help of a custom doclet? I got told that my solution with a simple java programm was good, but it would be better to make a doclet for this task.

What have I tried:

I read up on a couple of Doclet Overviews, Javadoc FAQ's and Tutorials regarding the topic. I have found out that you can extend the Doclet class from com.sun.javadoc.*; to override the start(RootDoc root) method.

With this I could print fields, tags and methods of classes in a desired package with the help of this custom doclet.

Other than this usecase, I have found no further details on how to actually write your own Doclets.

Is it even possible to write a doclet which generates javadoc comments in your code? Or would it just be better to use my existing solution for the problem?

This is my custom doclet right now:

package myPackage;

import com.sun.javadoc.*;

public class ListClass extends Doclet{

    public static void main(String[] args) {
        String[] blarg = new String[] {
                "-private", //shows private fields and methods
                "-doclet",
                "myPackage.ListClass", //Doclet to use
                "-sourcepath",
                "C:/DEV/workspace_jss/MyTestProject/src", //path to Package
                "myPackage" //package name
        };
        com.sun.tools.javadoc.Main.execute(blarg);
    }

    public static boolean start(RootDoc root) {
        ClassDoc[] classes = root.classes();

        for(ClassDoc cDoc : classes) {
            System.out.println(cDoc);

            FieldDoc fields[] = cDoc.fields();
            for(FieldDoc field : fields) {
                System.out.println("  field: " + field);
                System.out.println("     field name: " + field.name());
                System.out.println("     field commentText: " + field.commentText());
                System.out.println("     field type: " + field.type());

                Tag[] tags = field.tags();
                for(Tag tag : tags) {
                    System.out.println("  tag: " + tag);
                    System.out.println("     tag name: " + tag.name());
                    System.out.println("     tag text: " + tag.text());   
                }
            }

            MethodDoc methods[] = cDoc.methods();
            for(MethodDoc method : methods) {
                System.out.println("  method: " + method);
            }
        }

        return true;
    }
}

回答1:


Is it even possible to write a doclet which generates javadoc comments in your code?

Basically, no. A doclet can't add comments to your source code because the original source code is not available to it via the doclet API. The javadoc command is designed to extract the javadoc comments from the source code and pass them to the doclet.

Or would it just be better to use my existing solution for the problem?

Possibly.

I would actually start from an existing doclet that generates HTML. I would modify it to query the database to extract the comments, and merge them with the comments in the RootDoc tree when generating the HTML.

I would not try to generate source code with extra comments added to it. But it you did want to take that approach, you would need to start with a different framework.



来源:https://stackoverflow.com/questions/48204100/can-i-generate-javadoc-comments-with-the-help-of-a-custom-doclet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!