java.lang.reflect.Proxy的替代方法,用于创建抽象类(而不是接口)的代理 [英] Alternatives to java.lang.reflect.Proxy for creating proxies of abstract classes (rather than interfaces)

查看:306
本文介绍了java.lang.reflect.Proxy的替代方法,用于创建抽象类(而不是接口)的代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档


[ java.lang.reflect。] 代理
提供静态方法,创建动态代理类和
实例,它也是所有动态代理$ b $的
超类b由这些方法创建的类。

[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.

newProxyMethod 方法(负责生成动态代理)具有以下签名:

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

不幸的是,这会阻止生成动态代理 extends 一个特定的抽象类(而不是实现特定的接口)。这是有道理的,考虑到 java.lang.reflect.Proxy 是所有动态代理的超类,从而阻止另一个类成为超类。

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.

因此,是否可以生成 java.lang.reflect.Proxy 的任何替代方法,可以生成继承的动态代理从特定的抽象类,将所有对抽象方法的调用重定向到调用处理程序?

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


推荐答案

它可以使用 Javassist 完成(参见 ProxyFactory )或 CGLIB

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

Adam使用Javassist的示例:

我(Adam Paynter)使用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();

产生此输出:


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

这篇关于java.lang.reflect.Proxy的替代方法,用于创建抽象类(而不是接口)的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆