title: Modify a method using Annotations
body: <p>How can I change what a method is doing in Java ? </p>
  
  <p>I mean, I am trying to use annotations to make the following code</p>
  
  <pre><code>@Anno1(Argument = "Option1")
  public class TestClass
  {       
      @Anno2
      public void test()
      {
      }
  
  }
  </code></pre>
  
  <p>Into</p>
  
  <pre><code>public class TestClass
  {
      private static StaticReference z;
  
      public void test()
      {
             z.invokeToAll();
      }
  
  }
  </code></pre>
  
  <p>This is a very simplified example of what I am trying to do. <code>Anno1</code> will have many possible combinations, but this is not my problem so far. My problem is how to add code to method <code>test()</code></p>
  
  <p>I am looking for a more generic solution if possible. Eg. A way to add every kind of code in the method (not just a way to <code>.invokeToAll()</code>)</p>
  
  <p>So far I am using <code>import javax.annotation.processing.*;</code> and I have the following code, but I don't know how to go on from there </p>
  
  <pre><code>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);               
          }           
      }
  }
  </code></pre>
  
  <p>I have found something about Java Reflection but I have not found any source to help me with what I am doing.</p>
  
  <p>Obviously I <code>extends AbstractProcessor</code> in my code</p>
  
  <p>I have found this tutorial (http://www.zdnetasia.com/writing-and-processing-custom-annotations-part-3-39362483.htm) But this concerns creating a new class, not just changing a method. and the <code>javax.lang.model.elements</code> do not provide any way of editing that element (which in my case represents a Method).</p>
  
  <p>I hope my question is clear and inline with the rules. If not please comment and I will clarify. Thanks.</p>