如何在IntelliJ IDEA中运行AnnotationProcessor?

时间:2016-10-17 13:58:17

标签: java annotations javacompiler

我在IntelliJ IDEA中创建了简单的Annotation处理器,添加了注释配置文件,但我不明白如何运行它。 我知道注释处理器在编译时工作,但我没有在输出中看到来自Annotation处理器信使的任何消息。

public class SimpleAnnotationProcessor extends AbstractProcessor {
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {

    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,"hello");
    for (final Element element : roundEnv.getElementsAnnotatedWith(Immutable.class)) {
        if (element instanceof TypeElement) {
            final TypeElement typeElement = (TypeElement) element;


            for (final Element eclosedElement: typeElement.getEnclosedElements()) {
                if (eclosedElement instanceof VariableElement) {
                    final VariableElement variableElement = (VariableElement) eclosedElement;

                    if (!variableElement.getModifiers().contains(Modifier.FINAL)) {
                        **processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
                                String.format("Class '%s' is annotated as @Immutable," +
                                                "but filed '%s' is not declared as final",
                                        typeElement.getSimpleName(), variableElement.getSimpleName()));**



                    }
                }
            }
        }
    }

    return true;
}

}

1 个答案:

答案 0 :(得分:0)

设置 - &gt;构建,执行,部署 - &gt;编译器 - &gt;注释处理器 - &gt;启用注释处理。