Modify a method using Annotations

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

    Well, you might see if the following boilerplate code will be useful:

    public void magic(Object bean, String[] args) throws Exception {
        for (Method method : bean.getClass().getDeclaredMethods()) {
            if (method.isAnnotationPresent(Anno2.class)) {
                // Invoke the original method
                method.invoke(bean, args);
                // Invoke your 'z' method
                StaticReference.invokeAll();
            }
        }
    }
    

    As an alternative your might employ aspect oriented programming, for instance you have the AspectJ project.

提交回复
热议问题