创建自定义 AbstractProcessor 并与 Eclipse 集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6686774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:36:40  来源:igfitidea点击:

Creating a custom AbstractProcessor and integrating with Eclipse

javaeclipseannotations

提问by Mark Elliot

I'm trying to create a new annotation with which I'll do some runtime wiring, but, for a number of reasons, I'd like to verify at compile time that my wiring will be successful with some rudimentary checks.

我正在尝试创建一个新的注释,我将用它来进行一些运行时接线,但是,出于多种原因,我想在编译时验证我的接线是否会通过一些基本检查成功。

Suppose I create a new annotation:

假设我创建了一个新注释:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation{
}

Now I want to do some kind of validation at compile time, like check the field that CustomAnnotationannotates is of a particular type: ParticularType. I'm working in Java 6, so I created an AbstractProcessor:

现在我想在编译时进行某种验证,例如检查CustomAnnotation注释的字段是否属于特定类型:ParticularType. 我在 Java 6 中工作,所以我创建了一个AbstractProcessor

@SupportedAnnotationTypes("com.example.CustomAnnotation")
public class CompileTimeAnnotationProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations, 
                           RoundEnvironment roundEnv) {
        Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(CustomAnnotation.class);
        for(Element e : elements){
            if(!e.getClass().equals(ParticularType.class)){
                processingEnv.getMessager().printMessage(Kind.ERROR,
                     "@CustomAnnotation annotated fields must be of type ParticularType");
            }
        }
        return true;
    }

}

Then, based on some instructions I found, I created a folder META-INF/servicesand created a file javax.annotation.processing.Processorwith contents:

然后,根据我发现的一些说明,我创建了一个文件夹META-INF/services并创建了一个javax.annotation.processing.Processor包含内容的文件:

 com.example.CompileTimeAnnotationProcessor

Then, I exported the project as a jar.

然后,我将项目导出为 jar。

In another project, I built a simple test class:

在另一个项目中,我构建了一个简单的测试类:

public class TestClass {
    @CustomAnnotation
    private String bar; // not `ParticularType`
}

I configured the Eclipse project properties as follows:

我按如下方式配置了 Eclipse 项目属性:

  • Set Java Compiler -> Annotation Processing: "Enable annotation processing" and "Enable processing in editor"
  • Set Java Compiler -> Annotation Processing -> Factory Path to include my exported jar and checked under advanced that my fully qualified class shows up.
  • 设置 Java Compiler -> Annotation Processing:“启用注释处理”和“启用编辑器中的处理”
  • 设置 Java Compiler -> Annotation Processing -> Factory Path 以包含我导出的 jar 并在高级下检查我的完全限定类是否显示。

I clicked "apply" and Eclipse prompts to rebuild the project, I hit okay -- but no error is thrown, despite having the annotation processor.

我点击了“应用”,Eclipse 提示重建项目,我点击没问题——但没有抛出错误,尽管有注释处理器。

Where did I go wrong?

我哪里做错了?



I ran this using javacas

我运行这个使用javac作为

javac -classpath "..\bin;path\to\tools.jar" -processorpath ..\bin -processor com.example.CompileTimeAnnotationProcessor com\test\TestClass.java

with output

带输出

@CustomAnnotation annotated fields must be of type ParticularType

@CustomAnnotation 注释字段必须是 ParticularType 类型

采纳答案by Mark Elliot

To have errors show up in the editor, the Elementcausing the error needs to be tagged in the printMessagefunction. For the example above, this means that the compile time check should use:

要在编辑器中显示Element错误,需要在printMessage函数中标记导致错误的原因。对于上面的例子,这意味着编译时检查应该使用:

processingEnv.getMessager().printMessage(Kind.ERROR,
    "@CustomAnnotation annotated fields must be of type ParticularType",
    e); // note we explicitly pass the element "e" as the location of the error