我有一节课
public class SomeClass {
@CustomAnnotation1
String stringProperty = "LALALA";
@CustomAnnotation1
int integerProperty;
@CustomAnnotation1
ClassObject classObject;
}
customannotation1是由我定义的自定义注释,可以放在任何字段上。假设类
ClassObject
有点像
public class ClassObject {
@CustomAnnotation1
public String someOtherString;
public String log;
}
我想达到的目标
-如果我的注释放在任何不是基元类型的字段上,我希望访问该类的所有字段。
我的方法
-将所有字段注释为
CustomAnnotation1
,遍历所有字段,如果它是非基元的,则获取该类和进程中的所有字段。
我试过的
-我可以使用下面的代码在我的
AbstractProcessor
班级。
Collection<? extends Element> annotatedElements = roundEnvironment.getElementsAnnotatedWith(CustomAnnotation1.class);
List<VariableElement> variableElements = ElementFilter.fieldsIn(annotatedElements);
问题
-
-
我研究了很多关于
VariableElement
类,但无法找到检查字段是否为基元的方法。能做到吗?
-
有没有更好的方法来实现这个目标?