检查变量似乎正在改变运行的行为

时间:2015-04-01 09:58:39

标签: java intellij-idea

我正在调试以下代码。

ArrayList<ArrayList<LinkedList<FileInfo>>> cache = emptyCache();

private void addToCache(final FileInfo fileInfo) {
  final int first = 0xFF & Byte.valueOf(fileInfo.getFirstByteFromMiddle());
  final int second = 0xFF & Byte.valueOf(fileInfo.getSecondByteFromMiddle());

  if (first < 0 || second < 0)
    System.err.println("error");

  cache.get(first).get(second).add(fileInfo);
}

如果没有在System.err.println("error")上设置断点,这就是我在控制台上观察到的内容。

/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:58469,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/tools.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Users/petur/Development/Einfalda/einfalda/target/classes:/Users/petur/.m2/repository/org/apache/commons/commons-collections4/4.0/commons-collections4-4.0.jar:/Users/petur/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar:/Applications/IntelliJ IDEA 14.app/Contents/lib/idea_rt.jar" eu.petur.einfalda.Einfalda /Users/petur/Documents
Connected to the target VM, address: '127.0.0.1:58469', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:58469', transport: 'socket'
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -125
    at java.util.ArrayList.elementData(ArrayList.java:418)
    at java.util.ArrayList.get(ArrayList.java:431)
    at eu.petur.einfalda.DupFinder.addToCache(DupFinder.java:94)
    at eu.petur.einfalda.DupFinder.findDuplicates(DupFinder.java:30)
    at eu.petur.einfalda.Einfalda.main(Einfalda.java:23)

Process finished with exit code 1

但是,如果我在System.err.println("error")的行上设置断点,程序运行正常,并以Process finished with exit code 0退出。 同样,如果我使用System.out.println来显示这两个变量的值,就在它们被设置之后。

为什么在使用变量之前检查变量时不能重现该错误?

1 个答案:

答案 0 :(得分:0)

我通过使用-Djava.compiler=NONE作为VM选项禁用JIT来解决问题。我只能想象这个问题与优化有关。