title: Java: How to listen on methods invocation without registering each object explicitely?
body: <p>I want to listen on method calls in order to attach additional behavior dynamically around the call. I've already done it on JUnit methods with a custom annotation and runner. I'm trying to do it on a standard java application.</p>
  
  <p>The main idea is to do:</p>
  
  <pre><code>@Override
  public void beforeInvoke (Object self, Method m, Object[] args){
     Object[] newargs = modifyArgs (args);
     m.invoke (self, newargs);
  }
  </code></pre>
  
  <p>It's just an abstract idea, I don't have any concrete example, but I'm curious if it's possible in java.</p>
  
  <p>I've found some approaches: </p>
  
  <pre><code>java.lang.reflect.Proxy.newProxyInstance(...) 
  </code></pre>
  
  <p>where a proxy is defined for an interface only (but not used to decorate concrete classes). It seems similar to injection pattern and it's a different concern.</p>
  
  <p>Another approach <a href="https://stackoverflow.com/questions/3291637/alternatives-to-java-lang-reflect-proxy-for-creating-proxies-of-abstract-classes">here</a> using a factory pattern with the ProxyFactory class. This other solution requires explicit calls to create() method to produce object proxies listening on method invocations. So, if you bypass it by using natural constructors of your classes, it's not working. It's very constraining if you must explicit a call to a factory each time you have to create an object.</p>
  
  <p>There is a way to do it with transparency ?
  Like Proxy.newProxyInstance() but working also on concrete classes ?</p>
  
  <p>Thanks.</p>