如何获取具有特定注释的所有方法的 javadoc,以及方法名称和包/类名称?

发布于 2024-12-23 08:23:20 字数 293 浏览 4 评论 0原文

我正在寻找一种在运行时获取具有特定注释的所有方法的所有 javadoc 的方法。 我将使用此 javadoc 在定制的 UI 中显示有关这些方法的详细信息。

这些方法稍后用于测试,它们将以未知的顺序调用,由用户确定 - 我希望让用户无需浏览源代码即可看到 javadoc。

是的,我有资源并且可以利用它们来实现目标。

我想到的最好方法是维护一个带有描述的单独文件,该文件将在运行时读取,但这意味着我必须维护 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千仐 2024-12-30 08:23:20

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

我会这样解决这个问题:

  1. 下载 Eclipse JDT。它包含 Eclipse Java 编译器(将 org.eclipse.jdt.core_*.jar 添加到您的类路径中。“使用批处理编译器" 也可能有帮助)。
  2. 请参阅此处 如何从 Java 源获取 AST使用 Eclipse 编译器
  3. AST 包含源代码:您可以看到注释、参数名称等一切。要收集 JavaDoc,请使用 AstVisitor
  4. 将您需要的所有内容提取到您的 UI 读取的一个或多个 XML 文档(易于创建和解析)中。
  5. 创建一个 Ant 任务来自动化该过程并将其添加到您的构建中。

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.
江心雾 2024-12-30 08:23:20

如果您打算在 UI 中使用它并且您的代码库相当大,那么您可能需要考虑对 javadoc 建立索引并在索引上创建搜索查询。 Solr、Lucene、Sphinx 是您可以利用的几种技术。

我怀疑直接使用 JavaDocs 阅读是否能够获得可接受的性能。
在这里查看使用 Lucene 是多么容易
http://www.lucenetutorial.com/lucene-in-5-minutes.html

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

如梦初醒的夏天 2024-12-30 08:23:20

这是可能的,但需要一些努力。

通常,创建 javadoc 是通过运行已达到 CLI 的实用程序 javadoc 来实现的。
您可以使用注释处理器 API 在编译时调用它,并与编译的资源一起存储。然后你可以在运行时读取它。

请查看此项目,这是注释预处理器使用的精美示例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文