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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15312238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:12:12  来源:igfitidea点击:

How to get a JavaDoc of a method at run time?

javaoopruntimejavadoc

提问by ahmednabil88

Its easy to get a method Nameof a Classat run time
BUT
How i can get a JavaDocof a method at run time ?

它很容易在运行时获得a method Name, 但是 我如何Class在运行时获得一个方法?

JavaDoc

As the following example

作为下面的例子

Our Class that include JavaDocof our target method

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

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

}

Our Class that has a main method

我们的类有一个主要方法

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

采纳答案by Denys Séguret

You can't : the classfile doesn't contain the comments.

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

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 生成为 HTML,并根据类名和方法名构建 URL。您还可以使用doclet API以比 HTML 更合适的格式生成 javadoc 。

回答by Abhi

The only way to get it at runtime is to use custom annotations.

在运行时获取它的唯一方法是使用自定义注释。

Create a custom annotation class:

创建自定义注解类:

@Retention(RUNTIME)
@Target(value = METHOD)
public @interface ServiceDef {
  /**
   * This provides description when generating docs.
   */
  public String desc() default "";
  /**
   * This provides params when generating docs.
   */
  public String[] params();
}

Use it on a method of a class, e.g.:

在类的方法上使用它,例如:

@ServiceDef(desc = "This is an utility class",
            params = {"name - the name","format - the format"})     
public void read(String name, String format)

Inspect the annotations via reflection:

通过反射检查注释:

for (Method method : Sample.class.getMethods()) {
  if (Modifier.isPublic(method.getModifiers())) {
    ServiceDef serviceDef = method.getAnnotation(ServiceDef.class);
    if (serviceDef != null) {
      String[] params = serviceDef.params();
      String descOfMethod = serviceDef.desc();
    }
  }
}

回答by dnault

Annotation processors have access to the Javadoc comments in the source code. If you have control over the compilation process for the classes whose Javadoc you are interested in, you can use an annotation processor to grab the Javadoc at compile-time and make it available later at runtime.

注释处理器可以访问源代码中的 Javadoc 注释。如果您可以控制您感兴趣的 Javadoc 类的编译过程,您可以使用注释处理器在编译时获取 Javadoc,并使其在运行时可用。

This is the approach used in the therapi-runtime-javadocproject (disclosure: which I authored and am shamelessly plugging).

这是在therapi-runtime-javadoc项目中使用的方法(披露:我创作并无耻地插入)。

回答by Lie Ryan

You can run javadoc programmaticallyand passing options to generate documentation for the class that you want and then parsing the generated document to get the documentation for the method that you want. You will need the source code at runtime because comments are not in the class file.

您可以以编程方式运行 javadoc并传递选项以生成所需类的文档,然后解析生成的文档以获取所需方法的文档。您将在运行时需要源代码,因为注释不在类文件中。

回答by cara

Comments do not have a representation in bytecode, they get stripped out by the compiler and aren't available "at runtime".

注释在字节码中没有表示,它们会被编译器剥离,并且在“运行时”不可用。