Modify a method using Annotations

后端 未结 5 1956
無奈伤痛
無奈伤痛 2020-12-29 10:21

How can I change what a method is doing in Java ?

I mean, I am trying to use annotations to make the following code

@Anno1(Argument = \"Option1\")
p         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-29 10:50

    It's perfectly possible to do what you ask, although there is a caveat: relying on private compiler APIs. Sounds scary, but it isn't really (compiler implementations tend to be stable).

    There's a paper that explains the procedure: The Hacker's Guide to Javac.

    Notably, this is used by Project Lombok to provide automatic getter/setter generation (amongst other things). The following article explains how it does it, basically re-iterating what is said the aforementioned paper.

提交回复
热议问题