java - 如何在 IntelliJ IDEA 中运行 AnnotationProcessor?

标签 java annotations javacompiler

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

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;
}

最佳答案

设置 -> 构建、执行、部署 -> 编译器 -> 注释处理器 -> 启用注释处理。

关于java - 如何在 IntelliJ IDEA 中运行 AnnotationProcessor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40088387/

相关文章:

java - ToolProvider.getSystemJavaCompiler() 在 JDK 9 中返回 null

java - 自定义注释的使用,如何

java - org.springframework.beans.factory.annotation.Value 的替代方案,它不是基于 Spring 的

java - 为什么 JavaCompiler 在实例化 Java 类时速度很慢?

java - 如何获取JavaCompiler编译任务生成的类文件列表?

ios - UIViewcontroller 不会旋转

java - Java SWT 或 Swing 的数据透视表

java - 进度对话框不会在 fragment 中消失

java - karaf features-maven-plugin add-features-to-repo "Unable to find suitable bundle for dependency"尽管它就在那里

java - 使用递归反转列表的元素