简体   繁体   English

如何使用特定的Annotation获取所有方法的javadoc,方法名称和包/类名?

[英]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. 我正在寻找一种方法来在运行时获取具有特定Annotation的所有方法的所有javadoc。 I'm going to use this javadoc to display details about these methods in a custom made UI. 我将使用此javadoc在自定义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. 这些方法稍后用于测试,它们将以未知顺序调用,由用户确定 - 我希望让用户看到javadoc而不必浏览源代码。

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. 我想到这样做的最好方法是维护一个带有描述的单独文件,这个文件将在运行时读取,但这意味着我必须同时维护javadoc和外部文件,我不喜欢维护两个副本的想法。非常相似的文字。

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 ). 这并不像听起来那么简单,因为JavaDoc不是类文件的一部分getJavaDoc() java.lang.reflect.Method上没有getJavaDoc()方法)。

I'd attack the problem like this: 我会像这样攻击这个问题:

  1. Download the Eclipse JDT. 下载Eclipse JDT。 It contains the Eclipse Java compiler (Add org.eclipse.jdt.core_*.jar to your classpath. " Using the batch compiler " might also help). 它包含Eclipse Java编译器(将org.eclipse.jdt.core_*.jar添加到类路径中。“ 使用批处理编译器 ”也可能有帮助)。
  2. See here how to get an AST from Java sources using the Eclipse compiler . 请参阅此处如何使用Eclipse编译器从Java源代码获取AST
  3. The AST contains the source code: You see annotations, parameter names, everything. AST包含源代码:您可以看到注释,参数名称,所有内容。 To collect the JavaDoc, use an AstVisitor 要收集JavaDoc,请使用AstVisitor
  4. Extract everything you need into one or more XML documents (easy to create and parse) which your UI reads. 将您需要的所有内容提取到UI读取的一个或多个XML文档(易于创建和解析)中。
  5. Create an Ant task to automate the process and add it to your build. 创建Ant任务以自动化该过程并将其添加到您的构建中。

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. 如果要在UI中使用它并且代码库相当大,那么您可能需要考虑索引javadoc并在索引上创建搜索查询。 Solr, Lucene, Sphinx are few of the technologies you can utilize. Solr,Lucene,Sphinx是您可以使用的技术中的一小部分。

I doubt you will be able to gain acceptable performance by using JavaDocs directly to read. 我怀疑你能通过直接使用JavaDocs来获得可接受的性能。 Check out how easy it is to work with Lucene here http://www.lucenetutorial.com/lucene-in-5-minutes.html 看看在这里使用Lucene是多么容易, 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. 通常,通过运行已到达CLI的实用程序javadoc来实现创建javadoc。 You can invoke it at compile time by using Annotation processor API and store together with your compiled resources. 您可以在编译时使用Annotation处理器API调用它,并与编译的资源一起存储。 Then you can read it at runtime. 然后你可以在运行时阅读它。

Please take a look on this project as a beautiful example of annotation preprocessor usage. 请看一下这个项目,作为注释预处理器使用的一个很好的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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