简体   繁体   中英

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

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.

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.

If you are going to use it in a UI and your code base is considerably large then you might want to consider indexing your javadocs and create search queries on the indexes. Solr, Lucene, Sphinx are few of the technologies you can utilize.

I doubt you will be able to gain acceptable performance by using JavaDocs directly to read. Check out how easy it is to work with Lucene here http://www.lucenetutorial.com/lucene-in-5-minutes.html

It is possible but requires some efforts.

Generally creating javadoc is achieved by running utility javadoc that has reach CLI. You can invoke it at compile time by using Annotation processor API and store together with your compiled resources. Then you can read it at runtime.

Please take a look on this project as a beautiful example of annotation preprocessor usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM