How to generate code dynamically with annotations at build time in Java?

前端 未结 3 1441
一个人的身影
一个人的身影 2020-12-13 10:17

I\'m looking for a solution for generating code. I have googled, searched on SO and some blogs but I didn\'t find a good solution.

I\'d like to put an annotation on

3条回答
  •  难免孤独
    2020-12-13 10:38

    The annotation processing tool has been integrated in javac since version 1.6 and is part of the JDK. So there is no need for external tools when using the Pluggable Annotation API. You can generate any code by analysing custom annotations or method/parameter/field/class declarations using the Mirror API.

    The annotation processor API says you shouldn't change existing classes. Instead you should generate subclasses of existing classes and create extension methods on those subclasses.

    It seems to be possible to change classes anyway (e.g. by using bytecode manipulation libraries) though that would in contrast to the specification and could lead to issues with other annotation processors and may not work with all compilers in the same way.

提交回复
热议问题