注释处理器不在普通 Java 中运行 [英] Annotation processor doesn't run in plain Java

查看:30
本文介绍了注释处理器不在普通 Java 中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用普通的 java(不是 android api)制作注释处理器,但是每当我运行我的主函数时,处理器应该因为错误而停止构建过程,但它没有.

I am trying to make annotation processor in plain java (not android api), but anytime I run my main function, processor is supposed to stop build process because of error, but it doesn't.

我的项目结构是:

Root
  |-> core (all features including annotations)
  |-> annotation-processors (just annotation processor with set-up META-INF and processor class)
  |-> example (main void with class that is annotated with @Disable - annotation declared in core, this should stop compiler)

注解处理器类是

@SupportedAnnotationTypes("jacore.support.Disable")
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class Processor extends AbstractProcessor {

    private Filer filer;
    private Messager messager;
    private Elements elements;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnvironment) {
        this.filer = processingEnvironment.getFiler();
        this.messager = processingEnvironment.getMessager();
        this.elements = processingEnvironment.getElementUtils();
    }

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        for (Element element : roundEnvironment.getElementsAnnotatedWith(Disable.class)) {
            if (element.getKind() != ElementKind.CLASS) {
                messager.printMessage(Diagnostic.Kind.ERROR, "@Activity should be on top of classes");
                return false;
            }
        }

        return true;
    }

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        return Collections.singleton(Disable.class.getCanonicalName());
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }
}

我正在使用 InteliJ IDEA,并且在设置中启用了注释处理器.注释处理器类可能看起来很愚蠢,我真的想让它运行,然后我会改进它的功能.

I am using InteliJ IDEA and annotation processors are enabled in settings. Annotation processor class may seem stupid, I really want to make it run, then I will improve features of it.

有'example'模块的build.gradle

There is build.gradle of 'example' module

plugins {
    id 'java'
}

group 'sk.runner'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    implementation project(":core")
    annotationProcessor project(":annotation-processors")
}

推荐答案

您应该完全在 gradle 中配置您的构建过程,而不是使用 Intellij IDEA.这样它将独立于 IDE,并且 IDEA 支持与 gradle 项目自动同步.

Instead of using Intellij IDEA, you should configure your build process entirely in gradle. This way it will be IDE-independent, and IDEA supports auto-sync with the gradle project.

在 gradle 中你可以尝试这样的事情,然后运行 ​​gradle 'build' 任务(或 'classes' 任务只编译源代码):

In gradle you can try something like this, and then run the gradle 'build' task (or 'classes' task to only compile source):

task myCustomAnnotationProcessorTask(type: JavaCompile, group: 'build') {
    source = sourceSets.main.java
    classpath = sourceSets.main.compileClasspath
    options.compilerArgs = ['-proc:only',
                            '-processor', 'jacore.processors.Processor']
}
compileJava.dependsOn myCustomAnnotationProcessorTask

这篇关于注释处理器不在普通 Java 中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆