Java – modify methods using annotations

How to change methods in Java and what are they doing?

I mean, I tried to use comments to do the following code

@Anno1(Argument = "Option1")
public class TestClass
{       
    @Anno2
    public void test()
    {
    }

}

become

public class TestClass
{
    private static StaticReference z;

    public void test()
    {
           z.invokeToAll();
    }

}

This is a very simple example. I'm trying to do it Anno1 will have many possible combinations, but this is not my problem My question is how to add code to the method test ()

If possible, I am looking for a more general solution For example A method for adding various codes to a method (not just a method. Invoketoall())

So far, I've used import javax annotation. processing.*; I have the following code, but I don't know how to continue from there

private void processMethodAnnotations(RoundEnvironment env)
{
    for (Element e : env.getElementsAnnotatedWith(Anno2.class))
    {
        //If it is a valid annotation over a method
        if (e.getKind() == ElementKind.METHOD) 
        {
            //What to do here :S
        }else
        {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,"Not a method!",e);               
        }           
    }
}

I found something about java reflection, but I didn't find any source to help me

Obviously, I extended abstractprocessor in my code

I have found this tutorial( http://www.zdnetasia.com/writing-and-processing-custom-annotations-part-3-39362483.htm )But it's about creating a new class, not just changing a method And javax lang.model. Elements does not provide any way to edit the element (representing a method in my example)

I hope my question is clear and in line with the rules If not, please comment and I will clarify thank you.

Solution

Annotation handling is an error method from Wikipedia:

People advise you to do the right thing – AOP You can use AspectJ The quick results method is (if using eclipse):

1) Install ajdt (AspectJ development tool) 2) create an AspectJ project and add your classes and comments 3) create aspects:

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 perform the test and you will see how it works;) Ajdt compiles the code automatically, so it doesn't need any manual work. I hope this is what you call "magic"

to update:

If your code in the test () method depends on the anno1 annotation value, internally, you can obtain the class annotation executed by it in 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();
    }

}

Thisjoinpoint is a special reference variable

UPDATE2:

If you want to add system out. Println (this), you need to write system out. Println (thisjoinpoint. Getthis()), has just been tested and works normally thisJoinPoint. Getthis () returns your "this" but not completely; In fact, this is an object variable. If you want to get any properties, you need to cast or use reflection And this joinpoint Getthis () does not provide access to private properties

Well, now it seems that your question has been answered, but if I miss anything, or you get more questions / questions in this way, please feel free to ask;)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>