Modify a method using Annotations

后端 未结 5 1947
無奈伤痛
無奈伤痛 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:41

    I'm not sure at all if it is even possible to change the source or byte code via annotations. From what your describing it looks as if aspect oriented programming could provide a solution to your problem.

    Your annotations are pretty similiar to the pointcut concept (they mark a location where code needs to be inserted) and the inserted code is close the advice concept.

    Another approach would be parsing the java source file into an abstract syntax tree, modify this AST and serialize to a java compiler input.

提交回复
热议问题