如何在运行时获取方法的JavaDoc? [英] How to get a JavaDoc of a method at run time?

查看:582
本文介绍了如何在运行时获取方法的JavaDoc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行时很容易获得方法名称 Class

BUT

如何在运行时获取方法的 JavaDoc

Its easy to get a method Name of a Class at run time
BUT
How i can get a JavaDoc of a method at run time ?

如以下示例

我们的类包括 JavaDoc 我们的目标方法

Our Class that include JavaDoc of our target method

public class MyClass {
    /**
     * 
     * @param x value of ....
     * @return result of ....
     */
    public String myMethod(int x) {
        return "any value";
    }

}

我们的类有一个主方法

public class TestJava {
    public static void main(String[] args) {
        // get Class method Name at run time
        String methodName = MyClass.class.getMethods()[0].getName();
        System.out.println(methodName); // will print myMethod
        // How to  get a JavaDoc of myMethod `method` at run time
        // MyClass.class.getMethods()[0].????
        // expected to print a JavaDoc of myMethod
    }
}


推荐答案

你不能: class 文件不包含评论。

You can't : the class file doesn't contain the comments.

解决方案是在构建程序时根据HTML生成javadoc,并根据类的名称和方法名称构建URL。您还可以使用 doclet API

A "solution" would be to generate the javadoc as HTML when you build your program and to build an URL from the name of the class and the name of the method. You could also generate the javadoc in a more suitable format than HTML using the doclet API.

这篇关于如何在运行时获取方法的JavaDoc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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