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

前端 未结 4 1565
滥情空心
滥情空心 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:27

    By design, the annotation processing facility does not allow direct modification of the source code being processed. However, one can generate subclasses of the type being processed or the superclass of the type being processed. With some planning, this does allow some of the effect of modifying the type in question. I've written up an example of how this can fit together; see this blog entry for a more detailed explanation and some sample code.

    0 讨论(0)
  • 2020-12-18 03:42

    You are looking for "Instrumentation", which is what frameworks like AspectJ do. In this case you have to specify a jar in the command line with the "-agent" option, and then have the possibility to filter all loaded classes. During this filter step you can check for annotations, and modify the bytecode before it gets loaded in the virtual machine. Libraries for doing the actual bytecode modification include "asm", and maybe the highlevel wrappers "cglib" and "javassist". You could even precompile your classes to generate a list of classes which have to be instrumented by you, to make filtering in the beginning a bit faster.

    See java.lang.instrumentation for more info.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-18 03:48

    You have to extend the javac compiler for this, which means building your program won't be as portable as a regular application. See http://weblogs.java.net/blog/cayhorstmann/archive/2006/06/say_no_to_prope.html for more details on how someone achieved this.

    0 讨论(0)
提交回复
热议问题