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

查看:105
本文介绍了注释处理器不能在纯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.

有示例"模块的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天全站免登陆