自动委托一个java类的所有方法 [英] Automatically delegating all methods of a java class

查看:20
本文介绍了自动委托一个java类的所有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含许多公共方法的类:

Say I have a class with many of public methods:

public class MyClass {

    public void method1() {}
    public void method2() {}
    (...)
    public void methodN() {}

}

现在我想创建一个 wrapper 类,它将所有方法委托给包装实例(委托):

Now I would like to create a wrapper class which would delegate all the methods to wrapped instance (delegate):

public class WrapperClass extends MyClass  {
    private final MyClass delegate;

    public WrapperClass(MyClass delegate) {
        this.delagate = delegate;
    }

    public void method1() { delegate.method1(); }
    public void method2() { delegate.method2(); }
    (...)
    public void methodN() { delegate.methodN(); }

}

现在,如果 MyClass 有很多方法,我需要覆盖它们中的每一个,这些方法或多或少都是委托"的相同代码.我想知道是否有可能做一些魔术来自动调用 Java 中的方法(所以 Wrapper 类需要说嘿,如果你对我调用一个方法,只需转到 delegate 对象并调用这个方法就可以了).

Now if MyClass has a lot of methods I would need to override each of them which is more or less the same code which just "delegates". I was wondering if it is possible to do some magic to automatically call a method in Java (so the Wrapper class would need to say "Hey if you call a method on me just go to delegate object and call this method on it).

顺便说一句:我不能使用继承,因为委托不在我的控制之下.我只是从其他地方获取它的实例(另一种情况是如果 MyClass 是最终的).

BTW: I can not use inheritance because the delegate is not under my control.I just get its instance from elsewhere (another case would be if MyClass was final).

注意:我不想要 IDE 生成.我知道我可以在 IntelliJ/Eclipse 的帮助下做到这一点,但我很好奇这是否可以在代码中完成.

NOTE: I do not want IDE generation. I know I can do it with help of IntelliJ/Eclipse, but I'm curious if this can be done in code.

有什么建议可以实现这样的目标吗?(注意:我可能可以用一些脚本语言来完成,比如 php,我可以使用 php 魔术函数来拦截调用).

Any suggestions how to achieve something like this? (NOTE: I would probably be able to do it in some scripting languages like php where I could use php magic functions to intercept the call).

推荐答案

也许java的动态Proxy可以帮到你.它仅在您因此使用接口时才有效.在这种情况下,我将调用接口 MyInterface 并设置一个默认实现:

Perhaps the dynamic Proxy of java can help you. It only works if you consequently use interfaces. In this case, I will call the interface MyInterface and set up a default implementation:

public class MyClass implements MyInterface {

    @Override
    public void method1() {
        System.out.println("foo1");
    }

    @Override
    public void method2() {
        System.out.println("foo2");
    }

    @Override
    public void methodN() {
        System.out.println("fooN");
    }

    public static void main(String[] args) {
        MyClass wrapped = new MyClass();
        wrapped.method1();
        wrapped.method2();
        MyInterface wrapper = WrapperClass.wrap(wrapped);
        wrapper.method1();
        wrapper.method2();
    }

}

包装类实现看起来像:

public class WrapperClass extends MyClass implements MyInterface, InvocationHandler {

    private final MyClass delegate;

    public WrapperClass(MyClass delegate) {
        this.delegate = delegate;
    }

    public static MyInterface wrap(MyClass wrapped) {
        return (MyInterface) Proxy.newProxyInstance(MyClass.class.getClassLoader(), new Class[] { MyInterface.class }, new WrapperClass(wrapped));
    }

    //you may skip this definition, it is only for demonstration
    public void method1() {
        System.out.println("bar");
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Method m = findMethod(this.getClass(), method);
        if (m != null) {
            return m.invoke(this, args);
        }
        m = findMethod(delegate.getClass(), method);
        if (m != null) {
            return m.invoke(delegate, args);
        }
        return null;
    }

    private Method findMethod(Class<?> clazz, Method method) throws Throwable {
        try {
            return clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
        } catch (NoSuchMethodException e) {
            return null;
        }
    }

}

注意这个类:

  • 扩展 MyClass,继承默认实现(任何其他实现)
  • 实现Invocationhandler,允许代理进行反射
  • 可选地实现MyInterface(满足装饰器模式)
  • extends MyClass, to inherit a default implementation (any other would do)
  • implements Invocationhandler, to allow the proxy to do reflection
  • optionally implement MyInterface (to satisfy the decorator pattern)

此解决方案允许您覆盖特殊方法,但委托所有其他方法.这甚至适用于 Wrapper 类的子类.

This solution allows you to override special methods, but to delegate all others. This will even work with sub classes of Wrapper class.

请注意,方法 findMethod 尚未捕获特殊情况.

Note that the method findMethod does not yet capture the special cases.

这篇关于自动委托一个java类的所有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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