创建自定义AbstractProcessor并与Eclipse集成 [英] Creating a custom AbstractProcessor and integrating with Eclipse
问题描述
我正在尝试创建一个新的注释,我将在其中进行一些运行时布线,但是,由于多种原因,我想在编译时验证我的布线是否会成功进行一些基本检查。
假设我创建了一个新的注释:
@Target(ElementType) .FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation {
}
现在我想在编译时进行某种验证,比如检查 CustomAnnotation
annotates属于特定类型的字段: 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(元素e:元素){
if(!e.getClass()。equals(ParticularType.class)){
processingEnv.getMessager()。printMessage(Kind.ERROR,
@CustomAnnotation带注释的字段必须是ParticularType类型);
}
}
返回true;
}
}
然后,根据一些指示我发现,我创建了一个文件夹 META-INF / services
并创建了一个文件 javax.annotation.processing.Processor
包含内容:
com.example.CompileTimeAnnotationProcessor
然后,我将项目导出为jar。
在另一个项目中,我构建了一个简单的测试类:
public class TestClass {
@CustomAnnotation
private String bar; //不是'ParticularType`
}
我按如下方式配置了Eclipse项目属性: / p>
- 设置Java编译器 - >注释处理:启用注释处理和在编辑器中启用处理
- 设置Java编译器 - >注释处理 - >工厂路径以包含我导出的jar并在高级下检查我的完全限定类显示。
我单击apply并且Eclipse提示重建项目,我点击了 - 但是没有错误,尽管有注释处理器。
哪里做了我错了?
我使用 javac
作为
javac -classpath.. \ bin; path \to\tools.jar-processorpath .. \ bin -processor com.example.CompileTimeAnnotationProcessor com\test\TestClass.java
带输出
@Cust omAnnotation带注释的字段必须是ParticularType类型
要在编辑器中显示错误, 元素
导致错误需要在 printMessage
函数中标记。对于上面的示例,这意味着编译时检查应该使用:
processingEnv.getMessager()。printMessage(Kind.ERROR) ,
@CustomAnnotation带注释的字段必须是ParticularType类型,
e); //注意我们明确地传递元素e作为错误的位置
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 CustomAnnotation
annotates is of a particular type: ParticularType
. I'm working in Java 6, so I created an 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/services
and created a file javax.annotation.processing.Processor
with contents:
com.example.CompileTimeAnnotationProcessor
Then, I exported the project as a 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:
- 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.
I clicked "apply" and Eclipse prompts to rebuild the project, I hit okay -- but no error is thrown, despite having the annotation processor.
Where did I go wrong?
I ran this using javac
as
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
To have errors show up in the editor, the Element
causing the error needs to be tagged in the printMessage
function. For the example above, this means that the compile time check should use:
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
这篇关于创建自定义AbstractProcessor并与Eclipse集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!