Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexOutOfBoundsException when saving changes #10

Open
fedejeanne opened this issue Aug 10, 2023 · 3 comments
Open

IndexOutOfBoundsException when saving changes #10

fedejeanne opened this issue Aug 10, 2023 · 3 comments
Labels
Bug A Derivation of Expected Behavior Help Wanted Request for Help JDT Issues for JDT

Comments

@fedejeanne
Copy link

fedejeanne commented Aug 10, 2023

This exception popped up when saving content in a Java class

!ENTRY org.eclipse.jdt.ui 4 10006 2023-08-10 13:47:57.221
!MESSAGE The save participant 'org.eclipse.jdt.ui.postsavelistener.cleanup' caused an exception: java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 570
!STACK 0
java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 570
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:372)
	at java.base/java.util.ArrayList.get(ArrayList.java:459)
	at org.eclipse.jdt.internal.formatter.TokenManager.get(TokenManager.java:77)
	at org.eclipse.jdt.internal.formatter.TokenManager.findIndex(TokenManager.java:170)
	at org.eclipse.jdt.internal.formatter.TokenManager.lastIndexIn(TokenManager.java:196)
	at org.eclipse.jdt.internal.formatter.LineBreaksPreparator.handleBracedCode(LineBreaksPreparator.java:713)
	at org.eclipse.jdt.internal.formatter.LineBreaksPreparator.handleBracedCode(LineBreaksPreparator.java:705)
	at org.eclipse.jdt.internal.formatter.LineBreaksPreparator.visit(LineBreaksPreparator.java:155)
	at org.eclipse.jdt.core.dom.TypeDeclaration.accept0(TypeDeclaration.java:482)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3214)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:3285)
	at org.eclipse.jdt.core.dom.CompilationUnit.accept0(CompilationUnit.java:258)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:3214)
	at org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.prepareLineBreaks(DefaultCodeFormatter.java:417)
	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.corext.util.CodeFormatterUtil.reformat(CodeFormatterUtil.java:396)
	at org.eclipse.jdt.internal.corext.fix.CodeFormatFix.createCleanUp(CodeFormatFix.java:81)
	at org.eclipse.jdt.internal.ui.fix.CodeFormatCleanUp.createFix(CodeFormatCleanUp.java:64)
	at org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring.calculateChange(CleanUpRefactoring.java:768)
	at org.eclipse.jdt.internal.corext.fix.CleanUpPostSaveListener.saved(CleanUpPostSaveListener.java:384)

Image

Expected behavior

The exception shouldn't happen

How to reproduce

The problem happens seldom but a possible way of reproducing it could be:

  • Open Java class
  • Create a syntax problem
  • Save
@fedejeanne fedejeanne added Bug A Derivation of Expected Behavior Help Wanted Request for Help JDT Issues for JDT labels Aug 10, 2023
@sagarrohat
Copy link

sagarrohat commented Sep 24, 2023

On quick google search I found this comment in a bug which has code to reproduce the bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=471825#c11

It fails when we try to Save an syntactically incorrect code.

As far I can understand, it fails because we ignore errors if kind is K_COMPILATION_UNIT: https://github.com/eclipse-jdt/eclipse.jdt.core/blob/master/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java#L317

For me if a code is not syntactically we should not format it at all but I can't find any comment on why ignoreErrors was set to true for K_COMPILATION_UNIT in the original code which was introduced in this commit: eclipse-jdt/eclipse.jdt.core@b0e7530

@sagarrohat
Copy link

Submitted issue in jdt.core repo: eclipse-jdt/eclipse.jdt.core#1437

@HeikoKlare
Copy link
Contributor

Pushed back to ready. Can be picked again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A Derivation of Expected Behavior Help Wanted Request for Help JDT Issues for JDT
Projects
Status: 🔖 Ready: Atomic
Development

Successfully merging a pull request may close this issue.

3 participants