eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Error reported while formatting code with compiler errors

Open
#3,890 4 comments 0 reactions 1 assignee Claimed by @srikanth-sankaran View on GitHub
bug
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

Stumbled upon while trying to fix test fails during https://github.com/eclipse-jdt/eclipse.jdt.core/pull/3883 work.

There is a test written for https://bugs.eclipse.org/bugs/show_bug.cgi?id=280061 (`org.eclipse.jdt.core.tests.formatter.FormatterCommentsBugsTest._testBug280061()`) that tries to format code written for Java 5 with "source" option set to Java 3.

The intent of the test is to make sure the code doesn't run into AIOOBE in that case.

Well, it still does, however with an updated example that tries to format (not compilable) Java 14 code with Java 8 source. I believe the formatter shouldn't fail **that** badly, if it detects inappropriate Java version it could simply do nothing or stop formatting at the place where it can't continue.

So setup a project with Java 8 source level, and try to Ctrl+Shift+F the code below:
```
public class X {
// enum COLOR {
// BLACK, WHITE, RED, GREEN
// }
COLOR color = COLOR.RED;
Object result = switch (color) {
case RED, WHITE -> 1;
case GREEN -> 2;
case BLACK -> {
int number = color.toString().length();
yield number * 4;
}
default -> 0;
};
}
```

Error thrown:
```
Caused by: java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 64
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.internal.formatter.TokenManager.get(TokenManager.java:78)
at org.eclipse.jdt.internal.formatter.TokenManager.findIndex(TokenManager.java:172)
at org.eclipse.jdt.internal.formatter.TokenManager.lastIndexIn(TokenManager.java:205)
at org.eclipse.jdt.internal.formatter.LineBreaksPreparator.handleBracedCode(LineBreaksPreparator.java:664)
at org.eclipse.jdt.internal.formatter.LineBreaksPreparator.handleBracedCode(LineBreaksPreparator.java:656)
at org.eclipse.jdt.internal.formatter.LineBreaksPreparator.visit(LineBreaksPreparator.java:99)
at org.eclipse.jdt.core.dom.TypeDeclaration.accept0(TypeDeclaration.java:480)
at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3312)
at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:3383)
at org.eclipse.jdt.core.dom.CompilationUnit.accept0(CompilationUnit.java:257)
at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3312)
at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.prepareLineBreaks(DefaultCodeFormatter.java:418)
at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.prepareFormattedCode(DefaultCodeFormatter.java:229)
at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.format(DefaultCodeFormatter.java:185)
at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.format(DefaultCodeFormatter.java:169)
at org.eclipse.jdt.internal.corext.util.CodeFormatterUtil.reformat(CodeFormatterUtil.java:361)
at org.eclipse.jdt.internal.ui.text.java.JavaFormattingStrategy.format(JavaFormattingStrategy.java:75)
at org.eclipse.jface.text.formatter.MultiPassContentFormatter.formatMaster(MultiPassContentFormatter.java:197)
at org.eclipse.jface.text.formatter.MultiPassContentFormatter.format(MultiPassContentFormatter.java:136)
at org.eclipse.jface.text.source.SourceViewer.doOperation(SourceViewer.java:1046)
at org.eclipse.jface.text.source.projection.ProjectionViewer.doOperation(ProjectionViewer.java:1468)
at org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer.doOperation(JavaSourceViewer.java:164)
at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor$AdaptedSourceViewer.doOperation(CompilationUnitEditor.java:206)
at org.eclipse.ui.texteditor.TextOperationAction.lambda$0(TextOperationAction.java:130)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:67)
at org.eclipse.ui.texteditor.TextOperationAction.run(TextOperationAction.java:130)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:474)
at org.eclipse.jface.commands.ActionHandler.execute(ActionHandler.java:124)
... 52 more
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.