简体   繁体   English

Pluggable Annotation Processor API可以检索源代码注释吗?

[英]Can the Pluggable Annotation Processor API retrieve source code comments?

I am using the pluggable annotation processing api withing Java6+ to automatically create some deployment XML files. 我正在使用带有Java6 +的可插入注释处理api来自动创建一些部署XML文件。 Part of these XML files contains a description of the object. 这些XML文件的一部分包含对象的描述。 The description is ALWAYS the same content as the Javadoc associated with the class itself. 描述始终与与类本身关联的Javadoc具有相同的内容。 I could force the comment to be a field of the @Block annotation, but that duplicates the information. 我可以强制注释成为@Block注释的一个字段,但这会复制信息。 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 {
}

I seem to always find the answer right after I post on SO. 在我发布SO之后,我似乎总能找到答案。

For future reference, here is the solution 为了将来参考,这是解决方案

public class CustomAnnotationProcessor extends AbstractAnnotationProcessor
{
    public boolean process(...)
    {

        // use the protected member, processingEnv

        String comment = processingEnv.getElementUtils().getDocComment(anyElement);


    }
}

There is getDocComment which sounds like it should return the comment. getDocComment听起来应该返回评论。

Update: It got moved to the elements utitlity . 更新:它被移动到元素utitlity

The annotation processing API makes use of classes in the javax.lang.model(.*) packages. 注释处理API使用javax.lang.model(.*)包中的类。 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. 由于编译器旨在忽略注释和文档,因此这些包中似乎没有任何内容,我也没有预期,这使您可以访问comments / doc。

I'm not certain how the javadoc facility performs its work, maybe that can be of help. 我不确定javadoc工具如何执行其工作,也许这可能有所帮助。

Kapep's answer looks interesting, but do mind that it uses stuff from a com.sun.* package, which is implementation-specific. Kapep的答案看起来很有趣,但请注意它使用来自com.sun.*包的东西,这是特定于实现的。 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. 除非您完全确定为您的annotatation处理器环境提供的资源是使用这些类实现的,并且您可以安全地从接口转发,所以最好不要使用它。 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. 编辑:除此之外,我还使用自定义注释+处理器生成XML格式的元数据,用于布线,验证等。我还需要描述。 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. 所以我所做的就是保留JavaDoc用于编程目的和可能对直接在代码中使用类的人感兴趣的细节,同时在注释中有一些描述键(或者如果没有给出,则默认基于类名/其他注释值) )可用于从某个资源文件中获取描述。 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. 我不确定这对你有什么用处,但是我的两分钱。

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

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