Note that there are some explanatory texts on larger screens.

plurals
  1. POAlternatives to java.lang.reflect.Proxy for creating proxies of abstract classes (rather than interfaces)
    primarykey
    data
    text
    <p>According to <a href="http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/lang/reflect/Proxy.html" rel="noreferrer">the documentation</a>:</p> <blockquote> <p>[<code>java.lang.reflect.</code>]<code>Proxy</code> provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.</p> </blockquote> <p>The <a href="http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/lang/reflect/Proxy.html#newProxyInstance%28java.lang.ClassLoader,%20java.lang.Class%5b%5d,%20java.lang.reflect.InvocationHandler)" rel="noreferrer"><code>newProxyMethod</code> method</a> (responsible for generating the dynamic proxies) has the following signature:</p> <pre><code>public static Object newProxyInstance(ClassLoader loader, Class&lt;?&gt;[] interfaces, InvocationHandler h) throws IllegalArgumentException </code></pre> <p>Unfortunately, this prevents one from generating a dynamic proxy that <em>extends</em> a specific abstract class (rather than <em>implementing</em> specific interfaces). This makes sense, considering <code>java.lang.reflect.Proxy</code> is "the superclass of all dynamic proxies", thereby preventing another class from being the superclass.</p> <p>Therefore, are there any alternatives to <code>java.lang.reflect.Proxy</code> that can generate dynamic proxies that <em>inherit</em> from a specific abstract class, redirecting all calls to the <strong>abstract</strong> methods to the invocation handler?</p> <p>For example, suppose I have an abstract class <code>Dog</code>:</p> <pre><code>public abstract class Dog { public void bark() { System.out.println("Woof!"); } public abstract void fetch(); } </code></pre> <p>Is there a class that allows me to do the following?</p> <pre><code>Dog dog = SomeOtherProxy.newProxyInstance(classLoader, Dog.class, h); dog.fetch(); // Will be handled by the invocation handler dog.bark(); // Will NOT be handled by the invocation handler </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload