eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
142 stars 112 forks source link

java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 0 #2425

Open kohlschuetter opened 1 month ago

kohlschuetter commented 1 month ago

Seen in the Error log

once as "Problems occurred when invoking code from plug-in: "org.eclipse.jdt.ui"." another one as "Error in JDT Core during reconcile"

java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 0
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
    at java.base/java.util.Objects.checkIndex(Objects.java:385)
    at java.base/java.util.ArrayList.get(ArrayList.java:427)
    at org.eclipse.jdt.core.dom.ASTNode$NodeList.get(ASTNode.java:1631)
    at org.eclipse.jdt.core.dom.ASTConverter.checkAndAddMultipleFieldDeclaration(ASTConverter.java:599)
    at org.eclipse.jdt.core.dom.ASTConverter.buildBodyDeclarations(ASTConverter.java:430)
    at org.eclipse.jdt.core.dom.ASTConverter.convertToEnumDeclaration(ASTConverter.java:3846)
    at org.eclipse.jdt.core.dom.ASTConverter.convert(ASTConverter.java:3482)
    at org.eclipse.jdt.core.dom.ASTConverter.buildBodyDeclarations(ASTConverter.java:213)
    at org.eclipse.jdt.core.dom.ASTConverter.convert(ASTConverter.java:3567)
    at org.eclipse.jdt.core.dom.ASTConverter.convert(ASTConverter.java:1595)
    at org.eclipse.jdt.core.dom.AST.convertCompilationUnit(AST.java:644)
    at org.eclipse.jdt.internal.core.CompilationUnit.buildStructure(CompilationUnit.java:194)
    at org.eclipse.jdt.internal.core.Openable.generateInfos(Openable.java:245)
    at org.eclipse.jdt.internal.core.JavaElement.openWhenClosed(JavaElement.java:585)
    at org.eclipse.jdt.internal.core.CompilationUnit.makeConsistent(CompilationUnit.java:1137)
    at org.eclipse.jdt.internal.core.ReconcileWorkingCopyOperation.makeConsistent(ReconcileWorkingCopyOperation.java:166)
    at org.eclipse.jdt.internal.core.ReconcileWorkingCopyOperation.executeOperation(ReconcileWorkingCopyOperation.java:92)
    at org.eclipse.jdt.internal.core.JavaModelOperation.run(JavaModelOperation.java:740)
    at org.eclipse.jdt.internal.core.JavaModelOperation.runOperation(JavaModelOperation.java:805)
    at org.eclipse.jdt.internal.core.CompilationUnit.reconcile(CompilationUnit.java:1311)
    at org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.reconcile(JavaReconcilingStrategy.java:132)
    at org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy$1.run(JavaReconcilingStrategy.java:94)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:47)
    at org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.reconcile(JavaReconcilingStrategy.java:91)
    at org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.reconcile(JavaReconcilingStrategy.java:158)
    at org.eclipse.jdt.internal.ui.text.CompositeReconcilingStrategy.reconcile(CompositeReconcilingStrategy.java:94)
    at org.eclipse.jdt.internal.ui.text.JavaCompositeReconcilingStrategy.reconcile(JavaCompositeReconcilingStrategy.java:107)
    at org.eclipse.jface.text.reconciler.MonoReconciler.process(MonoReconciler.java:78)
    at org.eclipse.jface.text.reconciler.AbstractReconciler$BackgroundThread.run(AbstractReconciler.java:207)
eclipse.buildId=4.32.0.20240502-0721
java.version=21.0.3
java.vendor=Eclipse Adoptium
BootLoader constants: OS=macosx, ARCH=aarch64, WS=cocoa, NL=en_US
Framework arguments:  -product org.eclipse.epp.package.jee.product -keyring /Users/ck/.eclipse_keyring
Command-line arguments:  -os macosx -ws cocoa -arch aarch64 -product org.eclipse.epp.package.jee.product -keyring /Users/ck/.eclipse_keyring
jukzi commented 1 month ago

@kohlschuetter we need a reproducer. According to the stacktrace it is somehow related to a java enum without body declaration. Unclear how to reproduce that.

kohlschuetter commented 1 month ago

I'm pretty sure I was editing an enum class around that time.

The code has evolved since then, but I was just able to get another error message while editing such a class, but not reliably.

I believe these messages are not very helpful because they refer to code that's currently being edited, indicating some kind of race condition.

Of course, while editing code, any syntax error can occur. I think editing code in Eclipse IDE should not cause such drastic error messages being logged.

java.lang.IllegalArgumentException
    at org.eclipse.jdt.core.dom.ASTNode.setSourceRange(ASTNode.java:3483)
    at org.eclipse.jdt.core.dom.ASTConverter.convert(ASTConverter.java:1875)
    at org.eclipse.jdt.core.dom.ASTConverter.buildBodyDeclarations(ASTConverter.java:428)
    at org.eclipse.jdt.core.dom.ASTConverter.convertToEnumDeclaration(ASTConverter.java:3846)
    at org.eclipse.jdt.core.dom.ASTConverter.convert(ASTConverter.java:3482)
    at org.eclipse.jdt.core.dom.ASTConverter.buildBodyDeclarations(ASTConverter.java:213)
    at org.eclipse.jdt.core.dom.ASTConverter.convert(ASTConverter.java:3567)
    at org.eclipse.jdt.core.dom.ASTConverter.convert(ASTConverter.java:1595)
    at org.eclipse.jdt.core.dom.AST.convertCompilationUnit(AST.java:644)
    at org.eclipse.jdt.internal.core.CompilationUnit.buildStructure(CompilationUnit.java:194)
    at org.eclipse.jdt.internal.core.Openable.generateInfos(Openable.java:245)
    at org.eclipse.jdt.internal.core.JavaElement.openWhenClosed(JavaElement.java:585)
    at org.eclipse.jdt.internal.core.CompilationUnit.makeConsistent(CompilationUnit.java:1137)
    at org.eclipse.jdt.internal.core.ReconcileWorkingCopyOperation.makeConsistent(ReconcileWorkingCopyOperation.java:166)
    at org.eclipse.jdt.internal.core.ReconcileWorkingCopyOperation.executeOperation(ReconcileWorkingCopyOperation.java:92)
    at org.eclipse.jdt.internal.core.JavaModelOperation.run(JavaModelOperation.java:740)
    at org.eclipse.jdt.internal.core.JavaModelOperation.runOperation(JavaModelOperation.java:805)
    at org.eclipse.jdt.internal.core.CompilationUnit.reconcile(CompilationUnit.java:1311)
    at org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.reconcile(JavaReconcilingStrategy.java:132)
    at org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy$1.run(JavaReconcilingStrategy.java:94)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:47)
    at org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.reconcile(JavaReconcilingStrategy.java:91)
    at org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.reconcile(JavaReconcilingStrategy.java:158)
    at org.eclipse.jdt.internal.ui.text.CompositeReconcilingStrategy.reconcile(CompositeReconcilingStrategy.java:94)
    at org.eclipse.jdt.internal.ui.text.JavaCompositeReconcilingStrategy.reconcile(JavaCompositeReconcilingStrategy.java:107)
    at org.eclipse.jface.text.reconciler.MonoReconciler.process(MonoReconciler.java:78)
    at org.eclipse.jface.text.reconciler.AbstractReconciler$BackgroundThread.run(AbstractReconciler.java:207)
Exception occurred during compilation unit conversion:
----------------------------------- SOURCE BEGIN -------------------------------------
package com.kohlschutter.jacline.lib.coding;

import java.util.Arrays;

import com.kohlschutter.annotations.compiletime.SuppressFBWarnings;

import jsinterop.annotations.JsType;

/**
 * An information message, warning or error that may be occur upon coding or decoding.
 * 
 * @author Christian Kohlschütter
 */
@JsType
public class CodingAdvisory {
  private final CodingAdvisoryLevel level;
  private final String[] keys;
  private final String reason;

  private static final String[] NO_KEYS = new String[0];

  public CodingAdvisory(CodingAdvisoryLevel level, String reason, String... keys) {
    this.level = level;
    this.keys = keys.length == 0 ? NO_KEYS : Arrays.copyOf(keys, keys.length);
    this.reason = reason;
  }

  public CodingAdvisoryLevel getLevel() {
    return level;
  }

  @SuppressFBWarnings("EI_EXPOSE_REP")
  public String[] getKeys() {
    return keys;
  }

  public String getReason() {
    return reason;
  }

  @JsType
  public enum CodingAdvisoryLevel {
    INFO("yo"), WARNING, ERROR;
("")
    CodingAdvisoryLevel(String string) {
      // TODO Auto-generated constructor stub
    }
  }
}

----------------------------------- SOURCE END -------------------------------------

(Note the stray ("") line near the end, this was Eclipse's doing; it should actually be after WARNING). I just used cmd-1 to create a corresponding constructor after specifying the parameter (yo).

Maven/m2e project: https://github.com/kohlschutter/jacline/tree/main/jacline-lib-common

jukzi commented 1 month ago

Even on code that has syntax errors the parser should be able to clearly state the syntax error instead of throwing internal exception. Can you please boil down the example to a minimal self-contained reproducer?

kohlschuetter commented 1 month ago

@jukzi I'm having a hard time doing that, as this doesn't even always reproduce with the code above.