简体   繁体   English

替换java.lang.reflect.Proxy中的InvocationHandler

[英]Replace InvocationHandler in java.lang.reflect.Proxy

I need to replace InvocationHandler in a Proxy object. 我需要替换Proxy对象中的InvocationHandler。 But there is only getter for it and no setter. 但是,只有吸气剂而不是吸气剂。 Why is this so and is there any workaround? 为什么会这样,并且有任何解决方法?

Thanks 谢谢

I think you'll need to keep the same invocation handler but have it change its behaviour. 我认为您需要保留相同的调用处理程序,但要更改其行为。 This will be closer to what you're looking for if you use the State pattern in the invocation handler: 如果在调用处理程序中使用State模式,则将更接近您要查找的内容:

public class Handler implements InvocationHandler
{
    HandlerState state = new NormalState();

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        // Make all method calls to the state object, not this object.
        this.state.doStuff();

        return this.state.doOtherStuff();
    }

    public void switchToErrorState(){
        this.state = new ErrorState();
    }
}

public interface HandlerState
{
    public void doStuff();
    public String doOtherStuff();
}

public class ErrorState implements HandlerState
{
    @Override
    public void doStuff() {
        // Do stuff we do in error mode
    }

    @Override
    public String doOtherStuff() {
        // Do other error mode stuff.
        return null;
    }
}

public class NormalState implements HandlerState
{
    @Override
    public void doStuff() {
        // Do normal stuff
    }

    @Override
    public String doOtherStuff() {
        // Do normal other stuff
        return null;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.reflect.Proxy和java.lang.reflect.InvocationHandler - java.lang.reflect.Proxy and java.lang.reflect.InvocationHandler java.lang.reflect.Proxy:巨大的异常堆栈跟踪 - java.lang.reflect.Proxy: Huge exception stack trace 通过网络传输java.lang.reflect.Proxy - transmit a java.lang.reflect.Proxy over a network java.lang.reflect.Proxy实例是否专门用于最终化处理? - Are java.lang.reflect.Proxy instances treated specially for finalization? 在 java.lang.reflect.Proxy 上实现 equals、hashCode 和 toString - Implementing equals, hashCode, and toString on java.lang.reflect.Proxy 了解java.lang.reflect.InvocationHandler的invoke方法的“代理”参数 - Understanding “proxy” arguments of the invoke method of java.lang.reflect.InvocationHandler 从调用返回另一个代理的Java.lang.reflect.Proxy在赋值时导致ClassCastException - Java.lang.reflect.Proxy returning another proxy from invocation results in ClassCastException on assignment 在java.lang.reflect.Proxy对象上调用扩展函数时Kotlin奇怪的行为 - Kotlin strange behaviour when calling extension function on java.lang.reflect.Proxy object 如何从两个单独的类加载器制作java.lang.reflect.Proxy? - How can I make a java.lang.reflect.Proxy from two separate classloaders? java.lang.reflect.Proxy的替代方法,用于创建抽象类(而不是接口)的代理 - Alternatives to java.lang.reflect.Proxy for creating proxies of abstract classes (rather than interfaces)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM