根据Java.doc,Messager
有4个printMessage足迹:
printMessage(Diagnostic.Kind kind, CharSequence msg)
printMessage(Diagnostic.Kind kind, CharSequence msg, Element e)
printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a)
printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v)
注释的接口为:
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface AddBuilder {
public boolean lazyBuild() default false;
}
我希望它仅与非抽象类一起使用,并检查Processor
中的抽象性。原因是:我想知道如何在处理器中进行检查。
第二个变种有效:
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Set<? extends Element> classesForBuilder = roundEnv.getElementsAnnotatedWith(AddBuilder.class);
for(Element classElement : classesForBuilder){
if (classElement.getModifiers().contains(Modifier.ABSTRACT) ) {
messager.printMessage(Kind.ERROR, "AnnoBuilder cannot be applied to an abstract class.", classElement);
return true;
}
...
在带注释的元素和Problems
视图中看到错误
,但在错误日志中看不到。
如果我使用第一种形式,希望在“问题”或“错误日志”中找到错误:
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
messager.printMessage(Kind.ERROR, "Some problem with annotations.");
return true;