<i id='yEtqT'><tr id='yEtqT'><dt id='yEtqT'><q id='yEtqT'><span id='yEtqT'><b id='yEtqT'><form id='yEtqT'><ins id='yEtqT'></ins><ul id='yEtqT'></ul><sub id='yEtqT'></sub></form><legend id='yEtqT'></legend><bdo id='yEtqT'><pre id='yEtqT'><center id='yEtqT'></center></pre></bdo></b><th id='yEtqT'></th></span></q></dt></tr></i><div id='yEtqT'><tfoot id='yEtqT'></tfoot><dl id='yEtqT'><fieldset id='yEtqT'></fieldset></dl></div>
    • <bdo id='yEtqT'></bdo><ul id='yEtqT'></ul>
    <legend id='yEtqT'><style id='yEtqT'><dir id='yEtqT'><q id='yEtqT'></q></dir></style></legend>

      <small id='yEtqT'></small><noframes id='yEtqT'>

    1. <tfoot id='yEtqT'></tfoot>

    2. 自动委托 java 类的所有方法

      Automatically delegating all methods of a java class(自动委托 java 类的所有方法)

      <small id='Iyoip'></small><noframes id='Iyoip'>

      <i id='Iyoip'><tr id='Iyoip'><dt id='Iyoip'><q id='Iyoip'><span id='Iyoip'><b id='Iyoip'><form id='Iyoip'><ins id='Iyoip'></ins><ul id='Iyoip'></ul><sub id='Iyoip'></sub></form><legend id='Iyoip'></legend><bdo id='Iyoip'><pre id='Iyoip'><center id='Iyoip'></center></pre></bdo></b><th id='Iyoip'></th></span></q></dt></tr></i><div id='Iyoip'><tfoot id='Iyoip'></tfoot><dl id='Iyoip'><fieldset id='Iyoip'></fieldset></dl></div>
          <tfoot id='Iyoip'></tfoot>
            <tbody id='Iyoip'></tbody>

            • <bdo id='Iyoip'></bdo><ul id='Iyoip'></ul>

                <legend id='Iyoip'><style id='Iyoip'><dir id='Iyoip'><q id='Iyoip'></q></dir></style></legend>
                本文介绍了自动委托 java 类的所有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                Say I have a class with many of public methods:

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

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

                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 类的所有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                  <small id='0LmR9'></small><noframes id='0LmR9'>

                • <legend id='0LmR9'><style id='0LmR9'><dir id='0LmR9'><q id='0LmR9'></q></dir></style></legend>

                • <tfoot id='0LmR9'></tfoot>

                    <tbody id='0LmR9'></tbody>
                    <i id='0LmR9'><tr id='0LmR9'><dt id='0LmR9'><q id='0LmR9'><span id='0LmR9'><b id='0LmR9'><form id='0LmR9'><ins id='0LmR9'></ins><ul id='0LmR9'></ul><sub id='0LmR9'></sub></form><legend id='0LmR9'></legend><bdo id='0LmR9'><pre id='0LmR9'><center id='0LmR9'></center></pre></bdo></b><th id='0LmR9'></th></span></q></dt></tr></i><div id='0LmR9'><tfoot id='0LmR9'></tfoot><dl id='0LmR9'><fieldset id='0LmR9'></fieldset></dl></div>
                          <bdo id='0LmR9'></bdo><ul id='0LmR9'></ul>