Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the javadoc of all methods with a specific Annotation, with the method name and package/class name?

Tags:

java

javadoc

I'm looking for a way to fetch all the javadoc for all methods with a specific Annotation, at runtime. I'm going to use this javadoc to display details about these methods in a custom made UI.

These methods are later used for testing, they will be invoked in an unknown order, determined by the user - I wish to let the user see the javadoc without having to browse the sources.

Yes, I have the sources and can use them to achieve the goal.

The best way I thought about doing this is maintaining a separate file with descriptions, which will be read on runtime, but that means I have to maintain both javadoc and the external file, and I don't like the idea of maintaining two copies of the pretty similar text.

Cheers for any answers! Thanks.

like image 302
eitama Avatar asked Nov 05 '22 08:11

eitama


1 Answers

This isn't as simple as it sounds because the JavaDoc isn't part of the class file (there is no getJavaDoc() method on java.lang.reflect.Method).

I'd attack the problem like this:

  1. Download the Eclipse JDT. It contains the Eclipse Java compiler (Add org.eclipse.jdt.core_*.jar to your classpath. "Using the batch compiler" might also help).
  2. See here how to get an AST from Java sources using the Eclipse compiler.
  3. The AST contains the source code: You see annotations, parameter names, everything. To collect the JavaDoc, use an AstVisitor
  4. Extract everything you need into one or more XML documents (easy to create and parse) which your UI reads.
  5. Create an Ant task to automate the process and add it to your build.
like image 83
Aaron Digulla Avatar answered Nov 15 '22 01:11

Aaron Digulla