无限循环和代理导致堆栈溢出

huangapple go评论78阅读模式
英文:

Endless loop and StackOverflow with a proxy

问题

Sure, here's the translated code portion:

我正在尝试做一个棘手的事情我有一个方法 `ObjectFactory.getObject()`,它返回一个需要实现 `Serializable` 接口的 `Object`。我试图创建一个已经存在的 `ObjectFactory` 实例的代理拦截它的 `getObject()` 方法然后将返回的对象包装到一个实现 `Serializable` 接口的代理中

@Override
public Object get(String name, ObjectFactory<?> objectFactory) {

    ...

    ObjectFactory<?> serializableObjectFactory = (ObjectFactory<?>) Proxy.newProxyInstance(CurrentClass.class.getClassLoader(), new Class[]{ObjectFactory.class}, new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            //在这里我遇到了 StackOverflow 异常。我猜,它只是递归地调用了自身。不知道为什么会这样。
            Object result = ((ObjectFactory<?>) proxy).getObject();

            //在这里让对象实现 Serializable 接口
            return Mixin.create(new Class[]{Serializable.class, result.getClass()}, new Object[]{result});
        }
    });

    ...

    return super.get(name, serializableObjectFactory);
}

当代码执行到 Object result = ((ObjectFactory<?>) proxy).getObject() 时,似乎会递归地调用自身(我遇到了 StackOverflow 异常),而我不知道为什么会这样。

我有两个关于这个问题的猜测,但不能确定:

  1. 我可能错误地使用了代理。但是我已经检查过,似乎是正确的。
  2. invoke() 方法中的对象代理可能是一个代理,因此它可能在无限循环中调用自身。除了 Advise 之外,我没有找到如何将这个代理解包装成一个真实的对象,但它并不适用于这个代理。

您有关于问题原因的猜测吗?

英文:

I'm trying to make the tricky thing. I have method ObjectFactory.getObject() which returns an Object that needs to implement Serializable. I'm trying to make a proxy of an already existing instance of ObjectFactory, intercept its method getObject() and then wrap returned object to a proxy, which implements Serializable.

@Override
public Object get(String name, ObjectFactory&lt;?&gt; objectFactory) {

    ...

    ObjectFactory&lt;?&gt; serializableObjectFactory = (ObjectFactory&lt;?&gt;)Proxy.newProxyInstance(CurrentClass.class.getClassLoader(), new Class[]{ObjectFactory.class}, new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            //Here I&#39;m getting StackOverflow exception. I guess, it just invokes itself recursively. Don&#39;t know why
            Object result = ((ObjectFactory&lt;?&gt;) proxy).getObject();

            //Here I make object implement Serializable
            return Mixin.create(new Class[]{Serializable.class, result.getClass()}, new Object[] {result});
        }
    });

    ...

    return super.get(name, serializableObjectFactory);
}

When code comes to Object result = ((ObjectFactory&lt;?&gt;) proxy).getObject() it seems just invokes itself recursively(I'm getting StackOverflow exception) and I have no idea why.

I have two guesses about which I can't be sure:

  1. I use proxy in the wrong way. But I checked and it seems to be fine.
  2. Object proxy in method invoke() comes as a proxy so it might invoke itself in an endless loop. I didn't find how to unwrap this proxy to a real object besides Advise, but it doesn't suit this proxy.

Do you have any guesses what's wrong?

答案1

得分: 1

让我们看一下这一行:

Object result = ((ObjectFactory&lt;?&gt;) proxy).getObject();

你认为会发生什么?

  1. 它调用了代理。
  2. 代理调用了您的 InvocationHandler。
  3. 回到步骤1。

也许尝试在 objectFactory 上调用方法?

英文:

Let's take a look at this line:

Object result = ((ObjectFactory&lt;?&gt;) proxy).getObject();

What do you think is going to happen?

  1. It invokes the proxy.
  2. The proxy invokes your InvocationHandler.
  3. Go back to step 1.

Maybe call the method on objectFactory instead?

huangapple
  • 本文由 发表于 2020年4月7日 20:20:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61079904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定