开发者

Alternatives to java.lang.reflect.Proxy for creating proxies of abstract classes (rather than interfaces)

开发者 https://www.devze.com 2023-01-08 06:36 出处:网络
According to the document开发者_开发知识库ation: [java.lang.reflect.]Proxy provides static methods for

According to the document开发者_开发知识库ation:

[java.lang.reflect.]Proxy 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.

The newProxyMethod method (responsible for generating the dynamic proxies) has the following signature:

public static Object newProxyInstance(ClassLoader loader,
                                      Class<?>[] interfaces,
                                      InvocationHandler h)
                             throws IllegalArgumentException

Unfortunately, this prevents one from generating a dynamic proxy that extends a specific abstract class (rather than implementing specific interfaces). This makes sense, considering java.lang.reflect.Proxy is "the superclass of all dynamic proxies", thereby preventing another class from being the superclass.

Therefore, are there any alternatives to java.lang.reflect.Proxy that can generate dynamic proxies that inherit from a specific abstract class, redirecting all calls to the abstract methods to the invocation handler?

For example, suppose I have an abstract class Dog:

public abstract class Dog {

    public void bark() {
        System.out.println("Woof!");
    }

    public abstract void fetch();

}

Is there a class that allows me to do the following?

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


It can be done using Javassist (see ProxyFactory) or CGLIB.

Adam's example using Javassist:

I (Adam Paynter) wrote this code using Javassist:

ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(Dog.class);
factory.setFilter(
    new MethodFilter() {
        @Override
        public boolean isHandled(Method method) {
            return Modifier.isAbstract(method.getModifiers());
        }
    }
);

MethodHandler handler = new MethodHandler() {
    @Override
    public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
        System.out.println("Handling " + thisMethod + " via the method handler");
        return null;
    }
};

Dog dog = (Dog) factory.create(new Class<?>[0], new Object[0], handler);
dog.bark();
dog.fetch();

Which produces this output:

Woof!
Handling public abstract void mock.Dog.fetch() via the method handler


What you can do in such a case is having a proxy handler that will redirect calls to existing methods of your abstract class.

You of course will have to code it, however it's quite simple. For creating your Proxy, you'll have to give him an InvocationHandler. You'll then only have to check the method type in the invoke(..) method of your invocation handler. But beware : you'll have to check the method type against the underlying object associated to your handler, and not against the declared type of your abstract class.

If I take as an example your dog class, your invocation handler's invoke method may look like this (with an existing associated dog subclass called .. well ... dog)

public void invoke(Object proxy, Method method, Object[] args) {
    if(!Modifier.isAbstract(method.getModifiers())) {
        method.invoke(dog, args); // with the correct exception handling
    } else {
        // what can we do with abstract methods ?
    }
}

However, there is something that keep me wondering : I've talked about a dog object. But, as the Dog class is abstract, you can't create instances, so you have existing subclasses. Furthermore, as a rigorous inspection of Proxy source code reveals, you may discover (at Proxy.java:362) that it is not possible to create a Proxy for a Class object that does not represents an interface).

So, apart from the reality, what you want to do is perfectly possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消