title: Is it possible to implement an interface at runtime in Java?
body: <p>I am working on a project where there are a lot of objects that are created by a library, and I have no access to the creation process of these objects.</p>
  
  <p>The following snippets serve as a good example to illustrate my problem.</p>
  
  <p>Code:</p>
  
  <pre><code>public class Clazz {
      //The contents of Clazz are irrelevant
      //Clazz is NOT my class. I have no access to its internal structure.
      //However, I do have access to Clazz objects that were created elsewhere.
  }
  </code></pre>
  
  <p><code>ExampleInterface</code> is an interface that Clazz may or may not implement at compile time.</p>
  
  <p>Code:</p>
  
  <pre><code>public interface ExampleInterface {
      public void run();
  }
  </code></pre>
  
  <p>The following code is the problem that I am running into. Take note of the following:</p>
  
  <ol>
  <li><code>run()</code> is only called when c is an instance of <code>ExampleInterface</code>.</li>
  <li><code>getRunConditions(Clazz c)</code> and <code>executeClazz(Clazz c)</code> are both private methods in a class that I do not have access to.</li>
  <li>At compile time, <code>Clazz</code> will <em>not</em> contain a method named <code>run()</code>.</li>
  <li>ExampleExecutor is <em>not</em> my class. I have no access to it in any
  way (I can't even get an instance of the class).</li>
  </ol>
  
  <p>Code:</p>
  
  <pre><code>public class ExampleExecutor {
      public void executeClazz(Clazz c) {
          if ((c instanceof ExampleInterface) &amp;&amp; getRunConditions(c)) {
              ExampleInterface ex = (ExampleInterface) c;
              ex.run();
          }
      }
  }
  </code></pre>
  
  <p>Obviously <strong>the following method isn't syntactically possible</strong>, but it is what I am trying to achieve. Basically, if c doesn't already implement <code>ExampleInterface</code>, set c to implement <code>ExampleInterface</code>, and then provide the methods that must be overridden.</p>
  
  <p>Take note of the following:</p>
  
  <ol>
  <li><code>extendInterface(</code><em><code>Name of Interface</code></em><code>)</code> is <strong>made-up syntax</strong> I
  created in an attempt to illustrate my goal.</li>
  <li><code>run()</code> <em>must</em> be defined here (at runtime).</li>
  <li>I cannot use a wrapper or proxy class as a solution. IE, the <code>Clazz</code> object must wind up implementing <code>ExampleInterface</code>, and I can't use a workaround. (refer to <strong><em><a href="https://stackoverflow.com/questions/23767885/how-to-check-when-a-method-is-run-in-another-class-in-java-method-call-listener">this link</a></em></strong> if you want to know why).</li>
  </ol>
  
  <p>Code:</p>
  
  <pre><code>public void implementInterface(Clazz c) {
      if (!(c instanceof ExampleInterface)) {
          c.extendInterface(ExampleInterface {
              @Override
              public void run() {
                  //code
              }
          });
      }
  }
  </code></pre>
  
  <p>To clarify, the issue that I am running into is that I need to <em>always</em> know when <code>run()</code> is called in <code>Clazz</code>. If <code>Clazz</code> ever doesn't implement <code>ExampleInterface</code>, I can't know when <code>run()</code> should be called.</p>
  
  <p>At the same time, I would also like to occasionally add support for <code>run()</code> when it by default isn't supported. Because I have no access to the creation of <code>Clazz</code> objects, I can't do this by implementing the interface myself.</p>
  
  <p><strong>Question: Put simply, is it possible to implement an interface (and provide the required methods) at runtime?</strong></p>
  
  <p><strong>NOTE:</strong> While the only solution may require reflection (and if so please post it below), the library I am using has a security manager that blocks the use of all reflection. IE, a reflective solution may be of use to others in the future, but will be of no use to me.</p>
  
  <p>Also, I don't mean just using a library in my own program. An already running host application (which is what the library I am using was made for) complies and then runs code that I write for it. If that application doesn't like any of the code that I provide (IE, conflicts with its security manager), the code is never even compiled.</p>
  
  <p><strong>Why I need to do this:</strong></p>
  
  <p>It has to do with the library that I am using. Because <code>ExampleExecutor</code> is a method that I don't have access to, and I can't control the creation of Clazz, I can't determine when <code>run()</code> is executed.</p>
  
  <p>The reason why I need to know when <code>run()</code> is executed is because in actuality, <code>run()</code> is an event handler that is part of the library I am using.</p>
  
  <p>For example: <code>mouseClicked(CustomMouseEvent evt)</code> might be a method that is part of the interface <code>CustomMouseListener</code>. Sometimes the instance of <code>Clazz</code> I am working with cares when the mouse is clicked (and therefore inherits <code>CustomMouseListener</code>), while other times it doesn't.</p>
  
  <p>Unlike the <code>Clazz</code> instance, I always care if the mouse is clicked, and always need the event to be triggered.</p>
  
  <p>In reality, the <code>ExampleInterface</code> would actually be the following:</p>
  
  <pre><code>public interface CustomMouseListener {
      public void mouseClicked(CustomMouseEvent evt);
      public void mousePressed(CustomMouseEvent evt);
      public void mouseReleased(CustomMouseEvent evt);
      //etc
  }
  </code></pre>