Can annotations be used for code injection?

前端 未结 4 1626
南方客
南方客 2021-02-08 18:24

I realise that this might be a question that has been asked and answered, but please bear with me.

I want to know if it is possible to use annotations to inject code int

4条回答
  •  悲&欢浪女
    2021-02-08 18:43

    I went looking for something similar last year. There is no standard way to alter classes using annotation processors or the compiler and the annotation API documentation recommends creating decorators.

    If you are willing to live with the hacks, have a look at Adrian Kuhn's use of the private API where he adds Roman numeral literals to Java. This approach is limited to the Sun javac compiler and you would need to implement something else if you used another (like the Eclipse compiler).


    Edit: anyone interested in this area should check out Project Lombok.

提交回复
热议问题