我有一个类
public class SomeClass {
@CustomAnnotation1
String stringProperty = "LALALA";
@CustomAnnotation1
int integerProperty;
@CustomAnnotation1
ClassObject classObject;
}
CustomAnnotation1 是我定义的自定义注释,可以放在任何字段上。假设类(class)
ClassObject
就像public class ClassObject {
@CustomAnnotation1
public String someOtherString;
public String log;
}
我想达到什么 - 如果我的注释放在任何不是原始类型的字段上,我想访问该类的所有字段。
我的方法 - 获取所有带有
CustomAnnotation1
注释的字段,迭代所有这些,如果它是非原始的,则获取该类和进程中的所有字段。我试过的 - 我可以使用我的
AbstractProcessor
中的以下代码来获得所有元素的注释。类(class)。Collection<? extends Element> annotatedElements = roundEnvironment.getElementsAnnotatedWith(CustomAnnotation1.class);
List<VariableElement> variableElements = ElementFilter.fieldsIn(annotatedElements);
问题 ——
VariableElement
进行了大量研究类,但无法找到一种方法来检查该字段是否为原始字段。这能做到吗? 最佳答案
VariableElement.asType().getKind().isPrimitive()
关于Java注解处理如何检查VariableElement是原始类型(int,float)还是某个类的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53494703/