title: Replace implementation of instantiated class without touching the code (java)
body: <p>I have legacy code I don't want to touch.</p>
  
  <pre><code>public class LegacyCode{
      public LegacyCode() {
          Service s = new ClassA();
          s.getMessage();
      }
  }
  </code></pre>
  
  <p>Where <code>ClassA</code> provides a CORBA service call.</p>
  
  <pre><code>public class ClassA implements Service{
      @Override
      public String getMessage() {
         // Make CORBA service call...
         return "Class A";
      }
  }
  </code></pre>
  
  <p>And the interface <code>Service</code> looks like;</p>
  
  <pre><code>public interface Service {
      String getMessage();
  }
  </code></pre>
  
  <p>For test purposes, I want to replace the implementation of <code>Service</code> (in <code>LegacyCode</code> implemented by <code>ClassA</code>) with a stub.</p>
  
  <pre><code>public class ClassB implements Service {
      @Override
      public String getMessage() {
          return "Stub Class B";
      }
  }
  </code></pre>
  
  <p>So far so good. But is it possible without any modifications at the shown legacy code to load <code>ClassB</code> instead of <code>ClassA</code> at the instantiation of <code>ClassA</code>? </p>
  
  <pre><code>// In my test workbench
  new LegacyCode(); // "Stub Class B"
  </code></pre>
  
  <p>I've tried to write a custom classloader and load it at application start by java vm arguments but only the first class (here <code>LegacyCode</code>) was loaded by that loader.</p>
  
  <p>Thanks in advance</p>