title: Code injection via custom annotation
body: <p>Here's my use case:</p>
  
  <p>I need to do some generic operation before and after each method of a given class, which is based on the parameter(s) of the method. For example:</p>
  
  <pre><code>void process(Processable object) {
      LOGGER.log(object.getDesc());
      object.process();
  }
  
  class BaseClass {
  
      String method1(Object o){ //o may or may not be Processable(add process logic only in former case)
          if(o intstanceof Prcessable){
              LOGGER.log(object.getDesc());
              object.process();
          }
          //method logic
      }
  }
  </code></pre>
  
  <p>My <code>BaseClass</code> has a lot of methods and I know for a fact that the same functionality will be added to several similar classes as well in future.
  Is something like the following possible?</p>
  
  <pre><code>@MarkForProcessing
  String method1(@Process Object o){ 
      //method logic
  }
  </code></pre>
  
  <p>PS: Can AspectJ/guice be used? Also want to know how to implement this from scratch for understanding.</p>
  
  <p>Edit: Forgot to mention, what I have tried.(Not complete or working)</p>
  
  <pre><code>public @interface MarkForProcessing {
      String getMetadata();
  }
  
  
      final public class Handler {
          public boolean process(Object instance) throws Exception {
              Class&lt;?&gt; clazz = instance.getClass();
              for(Method m : clazz.getDeclaredMethods()) {
                  if(m.isAnnotationPresent(LocalSource.class)) {
                      LocalSource annotation = m.getAnnotation(MarkForProcessing.class);
  
                      Class&lt;?&gt; returnType = m.getReturnType();
                      Class&lt;?&gt;[] inputParamTypes = m.getParameterTypes();
                      Class&lt;?&gt; inputType = null;
  
                      // We are interested in just 1st param
                      if(inputParamTypes.length != 0) {
                          inputType = inputParamTypes[0];
                      }
                     // But all i have access to here is just the types, I need access to the method param.
                  }
                  return false;
              }
              return false;
          }
  </code></pre>