33

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.

Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219
  • Since the 2 answers here says they cant be done with the compiled code, you should be able to do with the source code. And for doing that try searching for "extract javadoc from source" and then get some open source tool and modify it according to your need. – Naveen Babu Dec 14 '11 at 12:06
  • I think it is much easier to go with @Puce since I am the owner of the source code! – Muhammad Hewedy Dec 14 '11 at 12:41
  • 1
    The actual question is: what do you need those JavaDocs for? What's the use case? – Thomas Dec 14 '11 at 12:41
  • 2
    Of course, you could write a doclet (and appropriate build scripts) to include the JavaDocs in the jar in your own format. – Tom Hawtin - tackline Dec 14 '11 at 13:32

4 Answers4

35

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

gherson
  • 169
  • 2
  • 9
  • 1
    nice one, how do you include the tools.jar from jdk in maven? – TecHunter Mar 31 '15 at 15:17
  • Heh, you wanna read javadoc at runtime? — No problem, captain! I'll write a solution for you! :-) But I think you missed one question at the end of your answer: — Why do you need to do that? It is possible to hammer nails using microscope but it is not designed to do such things) – 4ndrew May 12 '15 at 18:25
  • 1
    @4ndrew For example, I'm generating a different kind of documentation by running different "main" and using reflection to get what I need (an annotation processor would be nicer but much more work). Obviously, I need to include some Javadoc, too. – maaartinus May 20 '18 at 23:54
  • 1
    @TecHunter ` com.sun tools LATEST ${java.home}/../lib/tools.jar system ` – 3Qn Nov 17 '20 at 13:48
21

You can't. They're removed from the compiled code.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • Code and sources aren't compiled in the same archive, that's true. But still : all the required information (packages, classes, methods, documentation) is written somewhere in a javadoc.zip files. – Eric Duminil Nov 20 '19 at 19:27
  • @EricDuminil well that's true and if you have access to the sources you could get the JavaDoc from there as well. However, that all depends on whether the javadoc.zip is generated in the first place and accessible/available to the runtime - not to speak of the lengths you'd have to go to to pull it off (see here for some possibilities: https://stackoverflow.com/questions/15312238/how-to-get-a-javadoc-of-a-method-at-run-time) – Thomas Nov 21 '19 at 08:51
16

Consider to use annotations instead of Javadoc and write an annotation processor.

Puce
  • 37,247
  • 13
  • 80
  • 152
8

You can't do that, cause javadoc isn't compiled into final classes.

4ndrew
  • 15,354
  • 2
  • 27
  • 29