我有一个注释处理器,我需要获取与元素关联的类,以便检索其声明的字段:
@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...
}
.....
显然,className.getClass()
返回TypeElement.class
但我想检索带注释的类。我怎样才能做到这一点?