How do you use Java 1.6 Annotation Processing to perform compile time weaving?

前端 未结 4 1582
滥情空心
滥情空心 2020-12-18 02:57

I have created an annotation, applied it to a DTO and written a Java 1.6 style annotationProcessor. I can see how to have the annotationProcessor write a new source file, w

4条回答
  •  天命终不由人
    2020-12-18 03:47

    You have to use internal compiler's classes – some inspiration:

    • AOP or APT for overriding methods from super classes
    • RomanNumeralProcessor.java
    • Java Multiline String

    But it is brinkmanship. Your program will compile only on Sun/OpenJDK and there can be problems in future versions (internal API can change). Although once compiled, it is standard bytecode and will run everywhere.

    BTW: if you want use it in Eclipse, you should add some special support for it because Eclipse uses non-standard compiler. Your design should be more complex and you should add a level of abstraction to your processor – like Lombok does.

提交回复
热议问题