标签:sde highlight 反射 小明 http mod hello 静态 attr
举个例子来看一看注解定义类在语法树中用哪些语法节点来表示以及如何组织的。
@Retention(RetentionPolicy.RUNTIME) // 注解会在class中存在,运行时可通过反射获取
@Target(ElementType.METHOD) // 目标是方法
@Documented // 文档生成时,该注解将被包含在javadoc中,可去掉
public @interface TestAnnotation {
public String name() default "helloworld";
}
查看JCClassDecl语法节点(注解类也被当作一个类来表示)的jcModifiers属性,截图如下。

jcModifiers中的flags属性值为8705,必写为这样的表示:
(1<<13)|(1<<0)|(1<<9)
查看Flags中关于各个Modifiers指定的常量,可知是public和interface且还有个专门表示annotation的静态常量,如下:
/** Flag that marks attribute interfaces 标记属性接口, added in classfile v49.0.
* 应该只能修饰注解定义类
*/
public static final int ANNOTATION = 1<<13;
然后看一下JCMethodDecl属性,截图如下:

使用如上的注解,如下:
public class C {
@TestAnnotation(name = "小明")
public Integer test(int a, String name, Object c) {
return 1;
}
}
截图如下:
标签:sde highlight 反射 小明 http mod hello 静态 attr
原文地址:http://www.cnblogs.com/extjs4/p/6817064.html