Using Java 6 Processors in Eclipse

Java 6 APT 实战
本文介绍如何使用 Java 6 的 Annotation Processing Tool (APT) 在 Eclipse 中创建一个简单的处理器,该处理器能为每个编译类生成新的类,并为特定命名的类添加警告。

JDK5 introduced the APT (Annotation Processing Tool). It was part of the SDK but the classes were part of the unofficial com.sun.* namespace, and you had to use the apt tool to process the source code. 

JDK6 cleaned up the API and integrated this stuff it into javac itself so you didn’t need to use the separate apt tool anymore. 

Apparently built for processing source code with annotations before they are compiled into classes, it can also be used for all kinds of fun like code generation and code analyzers which are IDE independent; and you don’t even need to use annotations necessarily. The JPA2 criteria api meta-model is generated using this.

I made one very contrived example of java6 processor usage with Eclipse. All of this is possible to integrate into a maven build but I’m leaving that out and focusing on how I got this processing to work within Eclipse.

So we’re creating a processor which will generate a new class for each class in projects compiled using this processor. Additionally we’ll create a Warning for each class which starts with a T. Yes it’s silly.

Step 1:  Create the processor project

SillyProcessor.java:

01. @SupportedAnnotationTypes(value= {"*"})
02. @SupportedSourceVersion(SourceVersion.RELEASE_6)
03. public class SillyProcessor extends AbstractProcessor {
04.  
05. private Filer filer;
06. private Messager messager;
07.  
08. @Override
09. public void init(ProcessingEnvironment env) {
10. filer = env.getFiler();
11. messager = env.getMessager();
12. }
13.  
14. @Override
15. public boolean process(Set elements, RoundEnvironment env) {
16.  
17. for (Element element : env.getRootElements()) {
18.  
19. if (element.getSimpleName().toString().startsWith("Silly")) {
20. // We don't want generate new silly classes
21. // for auto-generated silly classes
22. continue;
23. }
24.  
25. if (element.getSimpleName().toString().startsWith("T")) {
26. messager.printMessage(Kind.WARNING,
27. "This class name starts with a T!",
28. element);
29. }
30.  
31. String sillyClassName = "Silly" + element.getSimpleName();
32. String sillyClassContent =
33. "package silly;\n"
34. +   "public class " + sillyClassName + " {\n"
35. +   "   public String foobar;\n"
36. +   "}";
37.  
38. JavaFileObject file = null;
39.  
40. try {
41. file = filer.createSourceFile(
42. "silly/" + sillyClassName,
43. element);
44. file.openWriter()
45. .append(sillyClassContent)
46. .close();
47. catch (IOException e) {
48. e.printStackTrace();
49. }
50.  
51. }
52.  
53. return true;
54. }
55. }

Without creating this META-INF entry I couldn’t get the processor to register in Eclipse.

META-INF/services/javax.annotation.processing.Processor:

1. com.kerebus.annotation.processor.SillyProcessor

Its only contents is the name of the Processor implementation. I guess you might be able to list several processors here, although I’m not sure.

That’s it. Now export it as a jar and use that jar in other projects where you wish to use the processor.

STEP 2:  Create a project which uses your processor.

In the properties for your new project go to Java Compiler -> Annotation Processing
Check the “Enable Project Specific Settings” and make sure “Enable annotation processing” is checked. I also changed the generated source directory to a name which didn’t start with a dot so it wouldn’t be hidden in the package explorer (files or directories which start with a dot are by default filtered away in eclipse).

Next off go to Java Compiler -> Annotation Processing -> Factory Path
Here you should add the jar of your processor project. You cannot use project references.
Press the “Advanced” button and you’ll be presented with a dialog which contains the processor you defined in your META-INF/services/javax.annotation.processing.Processor file. Select it and press ok.

Step 3:  Build!

We’re practically done. Here’s what it looks like for me in my project:

So we get a warning for the Thing class because its class name start with a “T” and for each class in the project we get corresponding “Silly” classes generated. These are compiled and usable just like any other normal class.

For more info check out the eclipse jdt/apt docsthis bit about creating a code analyzer or the offical docs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值