Bug 143259 - [1.5][compiler] NullPointerException in ReferenceBinding.binarySearch , Eclipse 3.2RC4
Summary: [1.5][compiler] NullPointerException in ReferenceBinding.binarySearch , Eclip...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.2.1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 146858 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-05-23 12:49 EDT by Karen Butzke CLA
Modified: 2006-09-11 10:25 EDT (History)
1 user (show)

See Also:


Attachments
Test case project (2.03 KB, application/x-zip-compressed)
2006-05-23 12:50 EDT, Karen Butzke CLA
no flags Details
Proposed fix (1.29 KB, patch)
2006-05-23 14:50 EDT, Olivier Thomann CLA
no flags Details | Diff
Patch to clean up field array (2.31 KB, patch)
2006-05-23 14:55 EDT, Kent Johnson CLA
no flags Details | Diff
Proposed patch (10.80 KB, patch)
2006-05-30 07:53 EDT, Philipe Mulet CLA
no flags Details | Diff
Better patch (12.29 KB, patch)
2006-05-30 09:35 EDT, Philipe Mulet CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Karen Butzke CLA 2006-05-23 12:49:04 EDT
I will attach a project that causes the following exception when opened in eclipse 3.2RC4 with jdk 1.5.  If you open the model.Project class, you will get this exception in the error log.  If you add the java.lang.Collection import, the exception no longer occurs.  It's somehow related to the annotation being there and having a compiler error elsewhere in the class. I can also repeat the exception with this as the Project class body (notice the lower case string):
{
	private string asdf;
	
	@GeneratedValue(strategy=GenerationType.TABLE, generator = "jkljljk")
	private int projectId;
}


java.lang.NullPointerException
at org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding.binarySearch(ReferenceBinding.java:76)
at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.getField(SourceTypeBinding.java:845)
at org.eclipse.jdt.internal.compiler.lookup.Scope.findField(Scope.java:826)
at org.eclipse.jdt.internal.compiler.lookup.Scope.getBinding(Scope.java:1412)
at org.eclipse.jdt.internal.compiler.lookup.BlockScope.getBinding(BlockScope.java:424)
at org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference.resolveType(QualifiedNameReference.java:972)
at org.eclipse.jdt.internal.compiler.ast.MemberValuePair.resolveTypeExpecting(MemberValuePair.java:83)
at org.eclipse.jdt.internal.compiler.ast.Annotation.resolveType(Annotation.java:266)
at org.eclipse.jdt.internal.compiler.ast.ASTNode.resolveAnnotations(ASTNode.java:513)
at org.eclipse.jdt.internal.compiler.lookup.FieldBinding.getAnnotationTagBits(FieldBinding.java:252)
at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.resolveTypeFor(SourceTypeBinding.java:1226)
at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.fields(SourceTypeBinding.java:603)
at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.faultInTypesForFieldsAndMethods(SourceTypeBinding.java:582)
at org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope.faultInTypes(CompilationUnitScope.java:427)
at org.eclipse.jdt.internal.compiler.Compiler.process(Compiler.java:587)
at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:411)
at org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.compile(AbstractImageBuilder.java:300)
Comment 1 Karen Butzke CLA 2006-05-23 12:50:11 EDT
Created attachment 42281 [details]
Test case project
Comment 2 Kent Johnson CLA 2006-05-23 14:31:09 EDT
Reduced testcase:

class X {
	@Ann(m=Object)
	private int foo;
	private NonExisting bar;
}
@interface Ann {
	String m();
}
Comment 3 Olivier Thomann CLA 2006-05-23 14:50:58 EDT
Created attachment 42299 [details]
Proposed fix

Simple fix that reverts to a linear search if one of the field binding is null.
Comment 4 Kent Johnson CLA 2006-05-23 14:55:13 EDT
Created attachment 42301 [details]
Patch to clean up field array

The problem is in the method SourceTypeBinding.fields().

It nulls out a field when it detects that its type cannot be resolved, but continues to resolve the other fields, which cause an Annotation type to be resolved. We end up in getField where the null'ed out field is detected.

The proposed patch uses a separate set to keep track of the failed fields and builds the new field array but skipping the failed ones.
Comment 5 Kent Johnson CLA 2006-05-24 11:06:34 EDT
We should make a similar change to SourceTypeBinding.methods() so failed methods are not null'ed out immediately.
Comment 6 Philipe Mulet CLA 2006-05-30 06:55:15 EDT
Problem comes from annotation resolution (looking for @Deprecated) which is reentering field resolution (while fields are being resolved) to resolve 'Object' reference. 

Here is a scenario with methods, which doesn't cause any NPE:
public class X {
	@Ann(m=Object())
	private void foo(){}
	private NonExisting bar(){}
}
@interface Ann {
    String m();
}
Comment 7 Philipe Mulet CLA 2006-05-30 07:14:26 EDT
Previous case doesn't fail, since unresolved return type is not directly nulling out the entry in method array.
Here is another case which causes a NPE with methods:
public class X {
	@Ann(m=bar())
	private void foo(){}
	private NonExisting bar(NonExisting ne){}
}
@interface Ann {
    String m();
}
Comment 8 Philipe Mulet CLA 2006-05-30 07:52:35 EDT
My fix is along the same line as Kent's proposed one, except using an array which makes the logic closer to original one (especially on method scenario).
Added AnnotationTest#test202-207
Comment 9 Philipe Mulet CLA 2006-05-30 07:53:45 EDT
Created attachment 42942 [details]
Proposed patch
Comment 10 Philipe Mulet CLA 2006-05-30 07:56:45 EDT
The scenario is rather rare, it arises through annotation resolution and in presence of unresolved types. This is why it is safer to queue this one for 3.2.1, as opposed to RC7 where we want to be really conservative.
Comment 11 Philipe Mulet CLA 2006-05-30 09:35:37 EDT
Created attachment 42948 [details]
Better patch

Had forgotten to use resolvedMethod in methods() when finding collisions
Comment 12 Philipe Mulet CLA 2006-05-31 14:45:04 EDT
Added AnnotationTest#test202-207.
Released fix for 3.2.1
Comment 13 Frederic Fusier CLA 2006-06-12 05:25:49 EDT
Released for 3.2.1
Released for 3.3 M1 while merging TARGET_321 in HEAD
Comment 14 Olivier Thomann CLA 2006-06-13 13:35:41 EDT
*** Bug 146858 has been marked as a duplicate of this bug. ***
Comment 15 Frederic Fusier CLA 2006-08-08 06:50:47 EDT
Verified for 3.3 M1 using build I20060807-2000.
Comment 16 David Audel CLA 2006-09-11 10:25:58 EDT
Verified for 3.2.1 using build M20060908-1655