简体   繁体   English

使用Annotation处理器获取annotatted类的声明字段

[英]getting declared field of annotatted class with Annotation processor

I have an annotation processor and I need to get the class associated with an element so I can retrieve its declared fields: 我有一个注释处理器,我需要获取与元素关联的类,以便我可以检索其声明的字段:

@Override
public boolean process(Set<? extends TypeElement> annotations,
        RoundEnvironment roundEnv) {
    String className = null;
    String packageName = null;
    String fqClassName = null;
    List<String> fields = new LinkedList<String>();
    for (Element elem : roundEnv.getElementsAnnotatedWith(FieldConstant.class)) {
        if (elem.getKind() == ElementKind.CLASS) {
            //              Encrypt encrypt = elem.getAnnotation(Encrypt.class);
            //              String message = "annotation found in " + elem.getSimpleName();
            //              processingEnv.getMessager().printMessage(Kind.NOTE, message);
            TypeElement classElement = (TypeElement) elem;
            PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
            className = classElement.getSimpleName().toString();
            for(Field field : classElement.getClass().getDeclaredFields()){
               do something...
            }
      .....

Obviously, className.getClass() returns the TypeElement.class but I want to retrieve the annotated class. 显然, className.getClass()返回TypeElement.class但我想检索带注释的类。 How can I do that? 我怎样才能做到这一点?

You cannot use reflection on code being compiled normally, since the code may have been modified or generated in a previous pass (how would you add them to the classpath???). 您不能对正常编译的代码使用反射,因为代码可能已在上一次传递中被修改或生成(如何将它们添加到类路径中?)。

However the good news is that you can use the packages in javax.lang.model to get the properties of fields. 然而,好消息是您可以使用javax.lang.model的包来获取字段的属性。

The following example prints all field definitions to System.out (perhaps missing a few keywords such as transient ): 以下示例将所有字段定义打印到System.out (可能缺少一些关键字,如transient ):

@Override
public boolean process(Set<? extends TypeElement> annotations,
        RoundEnvironment roundEnv) {
    for (Element elem : roundEnv.getElementsAnnotatedWith(FieldConstant.class)) {
        if (elem.getKind() == ElementKind.CLASS) {
            // print fields
            for (Element enclosedElement : elem.getEnclosedElements()) {
                if (enclosedElement.getKind() == ElementKind.FIELD) {
                    Set<Modifier> modifiers = enclosedElement.getModifiers();
                    StringBuilder sb = new StringBuilder();
                    if (modifiers.contains(Modifier.PRIVATE)) {
                        sb.append("private ");
                    } else if (modifiers.contains(Modifier.PROTECTED)) {
                        sb.append("protected ");
                    } else if (modifiers.contains(Modifier.PUBLIC)) {
                        sb.append("public ");
                    }
                    if (modifiers.contains(Modifier.STATIC))
                        sb.append("static ");
                    if (modifiers.contains(Modifier.FINAL))
                        sb.append("final ");
                    sb.append(enclosedElement.asType()).append(" ").append(enclosedElement.getSimpleName());
                    System.out.println(sb);
                }
            }
        }
    }
    ...

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

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