我可以在自定义doclet的帮助下生成Javadoc注释吗? [英] Can I generate Javadoc comments with the help of a custom doclet?

查看:117
本文介绍了我可以在自定义doclet的帮助下生成Javadoc注释吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的评论是什么意思

/**
*My Comment from database
*/

我的问题:

我有一堆完全没有评论的DTO.但是,SQL数据库中有注释.我可以通过发送查询然后检索ResultSet来获得这些注释.

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.

我的任务是使用SQL数据库中的注释创建一个Javadoc-API(如HTML并在Javacode中),以使代码库更易于理解.

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.

我编写了一个小的Java程序,该程序从sql数据库中检索注释. 我使用上面显示的所需javadoc注释格式将找到的注释写入文件. 评论栏中没有文字表示该评论为空(可以!)

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)

这就是现在的样子

这就是之后的样子

如开始所述..我可以在自定义doclet的帮助下生成Javadoc注释(像这样)吗?我被告知,使用简单的Java程序的解决方案很好,但是最好为此任务创建一个doclet.

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.

我尝试过什么:

我阅读了一些有关该主题的Doclet概述,Javadoc FAQ和教程.我发现您可以从com.sun.javadoc.*扩展Doclet类.覆盖start(RootDoc root)方法.

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.

借助此功能,我可以借助此自定义doclet在所需的程序包中打印字段,标签和类的方法.

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

除了这个用例之外,我没有找到有关如何实际编写自己的Doclet的更多详细信息.

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

甚至可以编写一个在您的代码中生成javadoc注释的doclet吗?还是使用我现有的解决方案会更好?

这是我现在的自定义doclet:

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;
    }
}

推荐答案

是否甚至可以编写一个doclet来在您的代码中生成javadoc注释?

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

基本上,不. doclet无法在源代码中添加注释,因为原始源代码无法通过doclet API获得. javadoc命令旨在从源代码中提取 javaem注释 并将其传递给doclet.

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?

可能.

我实际上将从生成HTML的现有doclet开始.我将对其进行修改以查询数据库以提取注释,并在生成HTML时将它们与RootDoc树中的注释进行合并.

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.

这篇关于我可以在自定义doclet的帮助下生成Javadoc注释吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆