[备忘]注解相关参数说明

public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
     // 返回所有被注解了@PreferenceEntity的元素的列表
     Set<? extends Element> annotations = roundEnv.getElementsAnnotatedWith(PreferenceEntity.class);
     for (Element element : annotations) {
         //判断元素的类型为Class
         element.getKind() == ElementKind.CLASS
         // 显示转换元素类型
         TypeElement typeElement = (TypeElement) element; 
         typeElement.getSimpleName()  // 输出元素名称
         typeElement.getAnnotation(MyAnnotation.class).value() //输出注解属性值
     }
     return true;
}
package com.example;    // PackageElement
public class Demo {        // TypeElement
    private int a;      // VariableElement
    private Demo other;  // VariableElement
    public Demo () {}    // ExecuteableElement
    public void setA (  // ExecuteableElement
        int newA   // TypeParameterElement
    ) {}
}

Element的子类介绍

ExecutableElement
表示某个类或接口的方法、构造方法或初始化程序(静态或实例),包括注释类型元素。
PackageElement
表示一个包程序元素。
TypeElement
表示一个类或接口程序元素。
TypeParameterElement
表示一般类、接口、方法或构造方法元素的形式类型参数。
VariableElement
表示一个字段、enum 常量、方法或构造方法参数、局部变量或异常参数。

https://download.oracle.com/technetwork/java/javase/6/docs/zh/api/javax/lang/model/element/Element.html

猜你喜欢

转载自blog.csdn.net/weixin_34144450/article/details/90936034