Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read Javadoc comments by reflection?

I need to know how to read Javadoc comments at run-time (probably by reflection?)

Say I have the following function:

/** *  function that do some thing */ public void myFunc() {      //... } 

At runtime, I can get much information about this function by reflection.. But cannot read the comments. So the question is, How to read this javadoc comments at runtime.

like image 600
Muhammad Hewedy Avatar asked Dec 14 '11 11:12

Muhammad Hewedy


People also ask

How do I read a 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 you get comments in Java?

Single-line comments start with two forward slashes ( // ). Any text between // and the end of the line is ignored by Java (will not be executed).

Do Javadoc comments go before or after annotations?

If an annotation precedes any of the definitions listed above, then the javadoc comment should be placed before the annotation. If several multiline comments with javadoc identifiers are placed sequentially, only the one closest to the definition, right above it, with the javadoc identifier will be used.

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.


1 Answers

Doclet class:

public class ExtractCommentsDoclet {     public static boolean start(RootDoc root) throws IOException {         for (ClassDoc c : root.classes()) {             print(c.qualifiedName(), c.commentText());             for (FieldDoc f : c.fields(false)) {                 print(f.qualifiedName(), f.commentText());             }             for (MethodDoc m : c.methods(false)) {                 print(m.qualifiedName(), m.commentText());                 if (m.commentText() != null && m.commentText().length() > 0) {                     for (ParamTag p : m.paramTags())                         print(m.qualifiedName() + "@" + p.parameterName(), p.parameterComment());                     for (Tag t : m.tags("return")) {                         if (t.text() != null && t.text().length() > 0)                             print(m.qualifiedName() + "@return", t.text());                     }                 }             }         }         return true;     }      private static void print(String name, String comment) throws IOException {         if (comment != null && comment.length() > 0) {             new FileWriter(name + ".txt").append(comment).close();         }     } } 

And maven execution:

<plugin>     <artifactId>maven-javadoc-plugin</artifactId>     <extensions>true</extensions>     <executions>         <execution>             <phase>compile</phase>             <goals>                 <goal>aggregate</goal>             </goals>         </execution>     </executions>     <configuration>         <doclet>ExtractCommentsDoclet</doclet>         <docletPath>${project.build.directory}/classes</docletPath>         <reportOutputDirectory>${project.build.outputDirectory}/META-INF</reportOutputDirectory>         <useStandardDocletOptions>false</useStandardDocletOptions>     </configuration> </plugin> 

Read docs from classpath:META-INF/apidocs

like image 113
Khayredinov Dmitriy Avatar answered Sep 23 '22 21:09

Khayredinov Dmitriy