Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 392871

Summary: ArrayIndexOutOfBoundsException in org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.fields
Product: [Eclipse Project] JDT Reporter: Terry Parker <tparker>
Component: CoreAssignee: Jay Arthanareeswaran <jarthana>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: amj87.iitr, james.steadman
Version: 3.8   
Target Milestone: ---   
Hardware: PC   
OS: Linux-GTK   
Whiteboard: stalebug

Description Terry Parker CLA 2012-10-25 16:55:20 EDT
We are seeing the following ArrayIndexOutOfBoundsException when the Eclipse Java compiler is compiling some of our Java files. The problem relates to the org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.fields() being reentered for the same file, as shown in this stack trace (instrumented code from the 3.8.0 release, so line numbers in SourceTypeBinding may be off):

java.lang.ArrayIndexOutOfBoundsException: 45
	at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.fields(SourceTypeBinding.java:711)
	at org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding.fields(ParameterizedTypeBinding.java:371)
	at org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding.getField(ParameterizedTypeBinding.java:556)
	at org.eclipse.jdt.internal.compiler.lookup.Scope.findField(Scope.java:1071)
	at org.eclipse.jdt.internal.compiler.lookup.Scope.findField(Scope.java:1017)
	at org.eclipse.jdt.internal.compiler.lookup.Scope.getBinding(Scope.java:1713)
	at org.eclipse.jdt.internal.compiler.ast.SingleNameReference.resolveType(SingleNameReference.java:934)
	at org.eclipse.jdt.internal.compiler.ast.MemberValuePair.resolveTypeExpecting(MemberValuePair.java:71)
	at org.eclipse.jdt.internal.compiler.ast.Annotation.resolveType(Annotation.java:354)
	at org.eclipse.jdt.internal.compiler.ast.ASTNode.resolveAnnotations(ASTNode.java:677)
	at org.eclipse.jdt.internal.compiler.lookup.FieldBinding.getAnnotationTagBits(FieldBinding.java:268)
	at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.resolveTypeFor(SourceTypeBinding.java:1415)
	at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.fields(SourceTypeBinding.java:719)
	at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.faultInTypesForFieldsAndMethods(SourceTypeBinding.java:689)
	at org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope.faultInTypes(CompilationUnitScope.java:420)
	at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java:1193)
	at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java:685)
	at org.eclipse.jdt.core.dom.ASTParser.internalCreateAST(ASTParser.java:1181)
	at org.eclipse.jdt.core.dom.ASTParser.createAST(ASTParser.java:807)
	at org.eclipse.jdt.internal.ui.javaeditor.ASTProvider$1.run(ASTProvider.java:544)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.jdt.internal.ui.javaeditor.ASTProvider.createAST(ASTProvider.java:537)
	at org.eclipse.jdt.internal.ui.javaeditor.ASTProvider.getAST(ASTProvider.java:480)
	at org.eclipse.jdt.ui.SharedASTProvider.getAST(SharedASTProvider.java:128)
	at org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$PartListenerGroup.calculateASTandInform(SelectionListenerWithASTManager.java:170)
	at org.eclipse.jdt.internal.ui.viewsupport.SelectionListenerWithASTManager$PartListenerGroup$3.run(SelectionListenerWithASTManager.java:155)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)

If the nested call to SourceTypeBinding.fields() fails to resolve some of the fields' types, it reduces the size of the "fields" array. Since the outer call recorded that array's length and is looping over that initially count, it generates an ArrayIndexOutOfBoundsException.

This problem exists since in the 3.7.2 release as well.

Changing the line "for (int i = 0, length = this.fields.length; i < length; i++)" to be "for (int i = 0, length = this.fields.length; i < this.fields.length; i++)" works around the AIOOBE, but I'm not sure if that is the best solution.
Comment 1 Ayushman Jain CLA 2012-11-07 04:50:13 EST
I couldn't reproduce this but the suggested fix looks ok to me. 
Jay, potential candidate for M4?
Comment 2 Jay Arthanareeswaran CLA 2012-11-07 04:54:51 EST
(In reply to comment #1)
> I couldn't reproduce this but the suggested fix looks ok to me. 
> Jay, potential candidate for M4?

I had a quick look at it too and couldn't think of a better approach. If you have have a good look at the fix suggested, I can push this for M4.

Terry, would you be able to post a patch? One with a test would be nice. with the little time I spent, I couldn't force the error.
Comment 3 Jay Arthanareeswaran CLA 2013-04-24 04:14:11 EDT
The last I tried, I wasn't able to reproduce this. It would be nice to have a test case for this bug.
Comment 4 James Steadman CLA 2014-03-18 07:21:51 EDT
I've just created an account to say that this seems to be present also in Eclipse 4.4M6. I was also having it in the current stable release that can be downloaded from eclipse.org.

I'm not sure exactly what I did that caused the error to occur.

Here is an error flagged in the editor pane:

Internal compiler error: java.lang.ArrayIndexOutOfBoundsException: 3 at 
 org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.fields(SourceTypeBinding.java:721)

It seems that even if I delete the Java file that it's flagging the error simply moves up a package or so and flags a different file with the same error.

Popup dialog gives error:

Errors occurred during the build.
Errors running builder 'Java Builder' on project 'xxxxx'.
3

Any fix for this would be great, or a way that I can resolve the issue myself, because currently I cannot build my project and I'm not able to get rid of this error even through deleting my .metadata folder and re-importing all my projects.

I'm running Win 8.1 with Java 1.7.0_21
If you'd like any more info please let me know and I'll see what I can do.
Comment 5 Terry Parker CLA 2014-03-18 09:53:40 EDT
(In reply to James Steadman from comment #4)

> Any fix for this would be great, or a way that I can resolve the issue
> myself, because currently I cannot build my project and I'm not able to get
> rid of this error even through deleting my .metadata folder and re-importing
> all my projects.
> 
James, the fix for this looks fairly straightforward, but I think we would all like to have a reproducible test case that can be attached to this bug, so that we can create a regression test and verify that the proposed fix is appropriate. Could you try to create such a reproducible test case?

IIRC, I failed in my first attempt to create a reproducible test, but I'm pretty sure it involves having an unresolved symbol in a class that contains annotations (the reentry into SourceTypeBinding.fields() is causes by the ASTNode.resolveAnnotations() call). I think the annotation itself is an unresolved class. In terms of working around the bug, you can look to see that all of the annotations in your project(s) are resolvable.
Comment 6 Eclipse Genie CLA 2020-05-04 02:27:21 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.