gpt4 book ai didi

java - Stackoverflow 在 Map 上调用 metaClass 方法

转载 作者:太空宇宙 更新时间:2023-11-04 08:41:10 25 4
gpt4 key购买 nike

这段代码导致 stackOverflow 异常:

ISerializer serializer = buildSerializer(TestDataProvider.getAuthor());
ASObject result = (ASObject) serializer.serialize();
assert result.isNotLazyProxy

此行抛出 StackOverflow:assert result.isNotLazyProxy。请注意,isNotLazyProxy 方法实际上从未被调用。

isNotLazyProxy 是一个扩展方法(在 groovy 中这些叫什么?) 定义如下:

/**
* Asserts that this ASObject is not a lazy loaded proxy,
* ie - that all of it's properties' values have been included
*/
ASObject.metaClass.isNotLazyProxy = { ->
assert delegate[HibernateProxyConstants.PROXYINITIALIZED] == true
return true;
}

但是,在该闭包的第一行设置断点表明它永远不会被调用。

相反,抛出了一个 StackOverflow:

java.lang.StackOverflowError
at java.lang.System.arraycopy(Native Method)
at java.lang.String.getChars(String.java:855)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:391)
at java.lang.StringBuffer.append(StringBuffer.java:224)
at java.lang.StringBuffer.<init>(StringBuffer.java:104)
at org.codehaus.groovy.runtime.InvokerHelper.formatMap(InvokerHelper.java:557)
at org.codehaus.groovy.runtime.InvokerHelper.format(InvokerHelper.java:530)
at org.codehaus.groovy.runtime.InvokerHelper.formatList(InvokerHelper.java:602)
at org.codehaus.groovy.runtime.InvokerHelper.format(InvokerHelper.java:527)
at org.codehaus.groovy.runtime.InvokerHelper.formatMap(InvokerHelper.java:575)

我不确定它是否相关,但 ASObjectMap 的子类,并且它的内容可能具有引用其内部其他键的属性。

我本以为这是相关的,但 StackOverflow 似乎表明 groovy 正在遍历 map 的成员。

发生什么事了?为什么会发生这种堆栈溢出?

最佳答案

assert result.isNotLazyProxy 可能没有做你想做的事情。

在 groovy 中,map.somehing 被翻译为 map.get(something)。请参阅http://groovy.codehaus.org/JN1035-Maps :

assert map2.class == null
//field syntax always refers to value of key, even if it doesn't exist
//use getClass() instead of class for maps...
assert map2.getClass() == LinkedHashMap //the kind of Map being used

因此请使用assert result.isNotLazyProxy()

当然,在您的情况下,result.isNotLazyProxy 应该返回 null,并且 assert result.isNotLazyProxy 断言应该失败。当此断言失败时,groovy 将显示断言错误,并显示图。在您的情况下,由于某种原因格式化 map 失败。

失败原因:

这是一个 known bug ,参见example 。据我所知,它与 ASObject 无关。 ,因为它不使用列表。

关于java - Stackoverflow 在 Map 上调用 metaClass 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5273605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com