注释处理工具< - 检查有效的注解 [英] Annotation Processing Tool <- checking valid annotation

查看:147
本文介绍了注释处理工具< - 检查有效的注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@ColumnMetadata(index=1)
...
@ColumnMetadata(index=2)
...
@ColumnMetadata(index=3)
...

和我要检查索引号是否使用APT独一无二的。我不知道如何做到这一点。我不明白的教程,一般我有问题在网络上找到的材料。

And I have to check whether index numbers are unique using APT. I have no idea how to do this. I don't understand tutorials, generally I have problem to find materials on the net.

如何做到这一点?关于APT任何教程/什么?

How to do this? Any tutorials/anything about APT?

推荐答案

您可能想使用可插拔注解API中,的工具的继任者。下面就来开始一个简短的教程:的的Java 6.0功能的一部分 - 2:可插拔注释处理API

You probably want to use the pluggable annotation API, the successor of the apt tool. Here's a short tutorial to get started: Java 6.0 Features Part – 2 : Pluggable Annotation Processing API

这大概是你需要做的检查你的注释中的步骤:

This are roughly the steps you need to do to check your annotations:


  1. 创建一个注解处理器,它应该延长 AbstractProcessor ​​

  2. 定义看哪​​些注解,加:结果
    @SupportedAnnotationTypes(价值= {full.name.of.ColumnMetadata})

  3. 重写过程方法。

  4. 使用 RoundEnvironment 参数访问源$ C ​​$ c的元素。你需要什么元素取决于你想要做什么。

    • 自上而下的方法:<一href=\"http://docs.oracle.com/javase/6/docs/api/javax/annotation/processing/RoundEnvironment.html#getRootElements%28%29\"相对=nofollow> 提供所有类,你可以筛选要检查特定元素getRootElements。如果你要分析你周围的注释code结构,此方法非常有用,例如类的方法或属性的注解是在

    • 自底向上的方法:<一href=\"http://docs.oracle.com/javase/6/docs/api/javax/annotation/processing/RoundEnvironment.html#getElementsAnnotatedWith%28java.lang.Class%29\"相对=nofollow> getElementsAnnotatedWith 使用此方法只得到注解元素。可以推断元素的位置,但可能需要保持你的元素的轨道,如果你想(通过映射注解元素的列表一类型为例)对它们进行比较。

  1. Create a annotation processor, it should extend AbstractProcessor.
  2. Define which annotations to look for, add:
    @SupportedAnnotationTypes(value= {"full.name.of.ColumnMetadata"})
  3. Override the process method.
  4. Use the RoundEnvironment parameter to access the elements of the source code. What elements you need depends on what you want to do.
    • Top-down approach: getRootElements provides all classes, which you could filter for particular elements you want to check. This method is useful if you want to analyse the code structure around your annotations, for example the class your method or property annotations are in.
    • Bottom-up approach: getElementsAnnotatedWith Use this method to get only annotated elements. You can infer the position of the elements but may need to keep track of your elements if you want to compare them (for example by mapping a list of annotated elements to a class type).

这篇关于注释处理工具&LT; - 检查有效的注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆