title: cast across classloader?
body: <p>How can I do this:</p>
  
  <pre><code>class Foo {
    public static Foo get() throws Exception {
      ClassLoader cl = new URLClassLoader(new URL[]{"foo.jar"}, null); // Foo.class is in foo.jar
      return (Foo)cl.loadClass("Foo").newInstance(); // fails on class cast
    }
  }
  </code></pre>
  
  <p>What I need is for the JVM to consider the Foo instance from cl as if it is an instance of Foo from the classloader of the executing code. </p>
  
  <p>I have seen these approaches, none of them good for me (the above example is a toy example):</p>
  
  <ol>
  <li>Load the class (or a separate interface) by a class loader that is a parent of both the calling code and created classloader</li>
  <li>Serialize and deserialize the object.</li>
  </ol>