Modify a method using Annotations

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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-29 10:56

    Annotation processing is wrong way to go for you, from Wikipedia:

    When Java source code is compiled, annotations can be processed by compiler plug-ins called annotation processors. Processors can produce informational messages or create additional Java source files or resources, which in turn may be compiled and processed, but annotation processors cannot modify the annotated code itself.

    People suggested you right way - AOP. Specifically you can use AspectJ. "Quick result" way is (if you use Eclipse):

    1) Install AJDT (AspectJ Development Tools)
    2) Create AspectJ project and add there your classes and annotations
    3) Create Aspect:

    public aspect Processor {
    
        private StaticReference z;
    
        pointcut generic()
                // intercept execution of method named test, annotated with @Anno1
                // from any class type, annotated with @Anno2
            : execution(@Anno2 * (@Anno1 *).test())
                // method takes no arguments
            && args ();
    
        // here you have write what you want method actually does
        void around () : generic()  {
            z.invokeToAll();
        }
    
    
    }
    

    now you can execute a test and you will see that it works ;) AJDT compiles code for you automatically, so do not need any manual work to do, hope that's what you called "magic" ;)

    UPDATE:

    if your code in test() method depends on Anno1 annotation value, then inside aspect you can get class annotation for which it is executed this way:

    void around () : generic()  {
    
        Annotation[] classAnnotations = thisJoinPoint.getThis().getClass().getAnnotations();
    
        String ArgumentValue = null;
        for ( Annotation annotation : classAnnotations ) {
            if ( annotation instanceof Anno1 ) {
                ArgumentValue = ((Anno1) annotation).Argument(); 
                break;
            }
        }
    
        if ( ArgumentValue != null && ArgumentValue.equals("Option1")) {
            z.invokeToAll();
        }
    
    }
    

    where thisJoinPoint is a special reference variable.

    UPDATE2:

    if you want to add System.out.println( this ) in your aspect, you need to write there System.out.println( thisJoinPoint.getThis() ), just tested and it works. thisJoinPoint.getThis() returns you "this" but not exactly; in fact this is Object variable and if you want to get any propery you need either to cast or to use reflection. And thisJoinPoint.getThis() does not provide access to private properties.

    Well, now seems that your question is answered, but if I missed anything, or you get additional question/problems with this way - feel free to ask ;)

    0 讨论(0)
  • 2020-12-29 10:58

    If your class extends a suitable interface, you could wrap it in a DynamicProxy, which delegates all calls to the original methods, except the call to test.

    0 讨论(0)
提交回复
热议问题