StringBuilder:ArrayIndexOutOfBoundsException [英] StringBuilder: ArrayIndexOutOfBoundsException
问题描述
我们在WebSphere 7/IBM JDK 6中遇到了一个奇怪的问题,其中一个节点存在一些初始化问题.
We're experiencing a strange issue with WebSphere 7/ IBM JDK 6, where one of the nodes has some initialization issue.
我们有一些调用InitialContext.lookup的代码,在此节点上,有时会出现以下异常:
We have some code which calls InitialContext.lookup and on this node we're getting sometimes the following exception:
Caused by: java.lang.ArrayIndexOutOfBoundsException
at java.lang.String.getChars(String.java:666)
at java.lang.StringBuilder.append(StringBuilder.java:207)
at javax.naming.spi.NamingManager.getURLContext(NamingManager.java:646)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:422)
at javax.naming.InitialContext.lookup(InitialContext.java:436)
[...]
我们看了javax.naming.spi.NamingManager.getURLContext的源代码:
We had a look at the source code of javax.naming.spi.NamingManager.getURLContext:
public static Context getURLContext(String schema, Hashtable<?, ?> envmt)
throws NamingException {
if (null == schema || 0 == schema.length() || null == envmt) {
return null;
}
// obtain pkg prefixes from hashtable
String pkgPrefixes[] = EnvironmentReader
.getFactoryNamesFromEnvironmentAndProviderResource(envmt, null,
Context.URL_PKG_PREFIXES);
for (String element : pkgPrefixes) {
// create factory instance
ObjectFactory factory;
String clsName = element + "." //$NON-NLS-1$
+ schema + "." //$NON-NLS-1$
+ schema + "URLContextFactory"; //$NON-NLS-1$
[...]
第646行是增强的for循环,但是下一条语句是String串联,编译器可能会将其替换为StringBuilder.
Line 646 is the enhanced for-loop, but the next statement is a String concatenation and is probably replaced with a StringBuilder by the compiler.
我们在StringBuilder上进行了一些快速的单元测试,但无法引发ArrayIndexOutOfBoundsException.
We did some quick unit tests on StringBuilder but couldn't provoke an ArrayIndexOutOfBoundsException.
如何在这里抛出ArrayIndexOutOfBoundsException以及如何避免它?
How can an ArrayIndexOutOfBoundsException be thrown here and how can we avoid it?
修改:
我们正在使用以下Java版本:
We are using the following java version:
java version "1.6.0"
Java(TM) SE Runtime Environment (build pxa6460sr9fp2ifix-20110913_02(SR9 FP2+IV03622+IZ99243))
IBM J9 VM (build 2.4, JRE 1.6.0 IBM J9 2.4 Linux amd64-64 jvmxa6460sr9-20110912_90359 (JIT enabled, AOT enabled)
J9VM - 20110912_090359
JIT - r9_20101028_17488ifx31
GC - 20101027_AA)
JCL - 20110727_04
推荐答案
这是IBM JVM的JIT编译器的已知错误.解决方法似乎是在JIT编译中排除了getChars
:
This is a known bug of the JIT compiler of the IBM JVM. The workaround seems to be the exclusion of getChars
from the JIT compilation:
-Xjit:exclude={ProgramClass.callStringGetChars*}
请参见 IZ78413:JIT兼容的字符串.GETCHARS抛出意外的ARRAYINDEXOUTOFBO UNDSEXCEPTION 供参考.
这篇关于StringBuilder:ArrayIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!