Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

As the following example

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

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     } } 
like image 520
ahmednabil88 Avatar asked Mar 09 '13 15:03

ahmednabil88


People also ask

How do you reference a method in javadoc?

Javadoc provides the @link inline tag for referencing the members in the Java classes. We can think of the @link tag as similar to the anchor tag in HTML, which is used to link one page to another via hyperlinks. Similar to the anchor tag, the path_to_member is the destination, and the label is the display text.

How do I find the generated javadoc?

Accessing the Javadoc from NetbeansSelect the desired package, class or method name, right-click and select Show Javadoc. This will launch your default web browser and navigate to the Javadoc for the selected item.

How do I create an automatic javadoc?

In the Package Explorer view, select a Java project and click Project > Generate Javadoc with Diagrams > Automatically. In the Generate Javadoc wizard, under Javadoc command, select the Javadoc command (an executable file).

How do I generate javadoc for a method in Intellij?

From the main menu, select Tools | Generate JavaDoc. In the dialog that opens, select a scope — a set of files or directories for which you want to generate the reference, and set the output directory where the generated documentation will be placed.


1 Answers

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();     }   } } 
like image 123
Liam Avatar answered Oct 06 '22 01:10

Liam