title: How to "proxy" a method in Java
body: <p>First off, I'm not sure how to best word my solution so if I seem to be babbling at times then please consider this.</p>
  
  <p>There is an interface in a library I wish to modify without touching the physical code,</p>
  
  <pre><code>public interface ProxiedPlayer {
      // .. other code
      public void setPermission(String permission, boolean state);
  }
  </code></pre>
  
  <p>I have written a third party library for handling permissions and having to hook into my API to edit permissions may be a step some developers do not want to take. So I ask that when <code>setPermission</code> is called is it possible to have it invoke my invoke the appropriate method in my library that will handle permission setting <strong>whilst</strong> ignoring the pre-programmed code or not?</p>
  
  <p>Here is the full <a href="http://ci.md-5.net/job/BungeeCord/ws/api/target/apidocs/index.html" rel="nofollow">interface</a> I am attempting to proxy.</p>
  
  <p>I have looked into the Java Proxy class <s>but it seems you need an instance of the object you're trying to proxy in the first place.</s> Given that the method can be called any time I do not believe this to be my solution <em>but will happily stand corrected.</em></p>
  
  <p><strong>I do not have control over instantiation of classes implementing the ProxiedPlayer interface.</strong></p>
  
  <p>EDIT: Ignorant me, there several events that I can subscribe to where it is possible to get an instance of the player, would this be the appropriate place to attempt to proxy the method? One of these events is fired when a player joins the server and getting the instance of the player is possible.</p>
  
  <p>Would the Proxy code need to be called for every instance of the ProxiedPlayer interface or is it possible to simply proxy every invocation of the method in an easier way?</p>
  
  <p>My library is a plugin loaded after everything else that is essential has finished loading.</p>
  
  <p><strong>Edit #2:</strong></p>
  
  <pre><code>import net.md_5.bungee.api.connection.ProxiedPlayer;
  
  import java.lang.reflect.InvocationHandler;
  import java.lang.reflect.Method;
  
  public class InvocationProxy implements InvocationHandler {
      @Override
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
          ProxiedPlayer player = (ProxiedPlayer) proxy;
          if(method.getName().equals("setPermission")) {
              // Call my code here?
          }
          return method.invoke(player, args);
      }
  }
  </code></pre>
  
  <p>Would something along the lines of what I have above work or am I barking up the wrong tree entirely?</p>