可插入注释处理器 API 可以检索源代码注释吗?

发布于 2024-12-12 05:07:45 字数 298 浏览 0 评论 0原文

我正在使用 Java6+ 的可插入注释处理 api 自动创建一些部署 XML 文件。这些 XML 文件的一部分包含对象的描述。描述始终与与类本身关联的 Javadoc 内容相同。我可以强制注释成为 @Block 注释的字段,但这会重复信息。在注释处理期间有什么方法可以获取类/类型注释的内容吗?

在这个例子中,我想在注释处理期间获得“我的块的一个很好的描述”。

/**
* A nice description of my block
**/
@Block
public class CustomBlock {
}

I am using the pluggable annotation processing api withing Java6+ to automatically create some deployment XML files. Part of these XML files contains a description of the object. The description is ALWAYS the same content as the Javadoc associated with the class itself. I could force the comment to be a field of the @Block annotation, but that duplicates the information. Is there any way during annotation processing to get the contents of the class/type comment?

In this example, I want to get "A nice description of my block" during annotation processing.

/**
* A nice description of my block
**/
@Block
public class CustomBlock {
}

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

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

发布评论

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

评论(3

无所的.畏惧 2024-12-19 05:07:45

我似乎总是在发布 SO 后立即找到答案。

供以后参考,这是解决方案

import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.TypeElement;

public class CustomAnnotationProcessor extends AbstractProcessor
{
    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment re)
    {
 
        // use the protected member, processingEnv
        
        String comment = processingEnv.getElementUtils().getDocComment(anyElement);
    }
}

I seem to always find the answer right after I post on SO.

For future reference, here is the solution

import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.TypeElement;

public class CustomAnnotationProcessor extends AbstractProcessor
{
    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment re)
    {
 
        // use the protected member, processingEnv
        
        String comment = processingEnv.getElementUtils().getDocComment(anyElement);
    }
}
生生不灭 2024-12-19 05:07:45

getDocComment 听起来应该返回评论。

更新:它已移至 元素实用程序

There is getDocComment which sounds like it should return the comment.

Update: It got moved to the elements utitlity.

久光 2024-12-19 05:07:45

注释处理 API 使用 javax.lang.model(.*) 包中的类。这些模型语言构造和所述模型必须在编译期间生成。由于编译器旨在忽略注释和文档,因此这些包中似乎没有任何内容(我也不希望有任何内容)可以让您访问注释/文档。

我不确定 javadoc 工具如何执行其工作,也许这会有所帮助。

Kapep 的答案看起来很有趣,但请注意它使用了 com.sun.* 包中的内容,这是特定于实现的。除非您完全确定提供给注释处理器环境的资源是使用这些类实现的,并且您可以安全地从接口向下转换,否则最好不要使用它。这充其量只是一个脆弱的解决方案。

编辑:顺便说一句,我还使用自定义注释+处理器来生成 XML 格式的元数据,用于接线、验证等。而且我还需要描述。因此,我所做的就是保留 JavaDoc 用于编程目的和在代码中直接使用该类的人可能感兴趣的详细信息,同时在注释中包含一些描述键(或者基于类名/其他注释值的默认值,如果没有给出) )可用于从某些资源文件中获取描述。该描述面向“最终用户”,重点关注高级内容,而不是代码细节。它还有促进国际化的额外好处。我不确定这对你有什么用,但这是我的两分钱。

The annotation processing API makes use of classes in the javax.lang.model(.*) packages. These model language constructs and said models must be generated during compilation. Since a compiler is intended to ignore comments and documentation, there doesn't seem to be anything in those packages, nor did I expect there to be, that gives you access to comments/doc.

I'm not certain how the javadoc facility performs its work, maybe that can be of help.

Kapep's answer looks interesting, but do mind that it uses stuff from a com.sun.* package, which is implementation-specific. Unless you're absolutely sure that the resources offered to your annotatation processor environment are implemented using those classes and you can safely downcast from the interfaces, it's best not to use that. It'd be a brittle solution at best.

EDIT: as an aside, I'm also using custom annotations + processor to generate metadata in XML format for wiring, validation etc. And I also need descriptions. So what I do is keep the JavaDoc for programming purposes and details that might be interesting to someone directly using the class in code, while having some description key in the annotation (or a default based on class name/other annotation values if none is given) that can be used to obtain a description from some resource file. The description is intended for the "end user" and focuses on high-level stuff, not code specifics. It has the added benefit of facilitating internationalization. I'm not certain this would be of any use to you, but there's my two cents.

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