注释处理器不';t在纯Java中运行
我试图用纯java(不是android api)制作注释处理器,但每当我运行我的主函数时,处理器都会因为错误而停止构建过程,但事实并非如此 我的项目结构是:注释处理器不';t在纯Java中运行,java,annotations,Java,Annotations,我试图用纯java(不是android api)制作注释处理器,但每当我运行我的主函数时,处理器都会因为错误而停止构建过程,但事实并非如此 我的项目结构是: Root |-> core (all features including annotations) |-> annotation-processors (just annotation processor with set-up META-INF and processor class) |-> exampl
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();
}
}
您应该完全在gradle中配置构建过程,而不是使用Intellij IDEA。这样它将独立于IDE,IDEA支持与gradle项目的自动同步 在gradle中,您可以尝试以下操作,然后运行gradle“build”任务(或“classes”任务以仅编译源代码):
你在编译器输出中看到错误消息了吗?@dnault没有,但它应该显示error这让我怀疑错误代码路径是否正在实际执行。我会尝试从注释处理器中抛出一个异常(而不仅仅是打印消息)。这不是推荐的方法,但它可能会更好地指示处理器是否正在执行。@dnault我应该在init或process中抛出错误,还是不重要?@dnault添加了
抛出新的非法状态异常()代码>到init()和process()方法以及编译的项目,我将尝试Tommorovi将此代码段添加到build.gradle,现在所有关键字(如依赖项、应用插件等)都变灰“无法解析符号*)。程序编译成功,但批注处理器未运行。@Martin可能生成有语法错误。如果生成文件无效,编译将不起作用。你是如何编译你的程序的?如果您从项目根目录(包含build.gradle的目录)运行“gradlew”,您会得到什么输出?我正在使用Intellij IDEA trought主函数运行程序。我可以尝试运行gradlew。*出了什么问题:无法从“10.0.2”确定java版本。
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")
}
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