创建自定义 AbstractProcessor 并与 Eclipse 集成
我正在尝试创建一个新的注释,用它进行一些运行时接线,但是,由于多种原因,我想在编译时验证我的接线是否会通过一些基本检查成功。
假设我创建一个新注释:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation{
}
现在我想在编译时进行某种验证,例如检查 CustomAnnotation
注释的字段是否属于特定类型:PspecialType
。我正在使用 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;
}
}
然后,根据我找到的一些说明,我创建了一个文件夹 META-INF/services 并创建了一个文件javax.annotation.processing.Processor
内容:
com.example.CompileTimeAnnotationProcessor
然后,我将项目导出为 jar。
在另一个项目中,我构建了一个简单的测试类:
public class TestClass {
@CustomAnnotation
private String bar; // not `ParticularType`
}
我按如下方式配置了 Eclipse 项目属性:
- 设置 Java 编译器 ->注解处理:“启用注解处理”和“在编辑器中启用处理”
- 设置Java编译器->注释处理->包含我导出的 jar 的工厂路径,并在高级下检查我的完全合格的类是否显示。
我单击“应用”,Eclipse 提示重建项目,我单击“确定”——但没有抛出任何错误,尽管有注释处理器。
我哪里做错了?
我使用 javac
运行它作为
javac -classpath "..\bin;path\to\tools.jar" -processorpath ..\bin -processor com.example.CompileTimeAnnotationProcessor com\test\TestClass.java
输出
@CustomAnnotation 注解字段的类型必须为 PspecialType
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在编辑器中显示错误,需要在
printMessage
函数中标记导致错误的Element
。对于上面的示例,这意味着编译时检查应使用:To have errors show up in the editor, the
Element
causing the error needs to be tagged in theprintMessage
function. For the example above, this means that the compile time check should use: