Bug 306196 - [search] NPE while searching for annotation references in rt.jar of JRE 6.0
Summary: [search] NPE while searching for annotation references in rt.jar of JRE 6.0
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.6   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.6 M7   Edit
Assignee: Satyam Kandula CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-03-17 10:11 EDT by Frederic Fusier CLA
Modified: 2010-04-26 14:12 EDT (History)
2 users (show)

See Also:
frederic_fusier: review+


Attachments
JDT/UI patch to get access to all the constants in the Java Search page (16.43 KB, patch)
2010-03-17 11:20 EDT, Frederic Fusier CLA
no flags Details | Diff
Proposed patch (8.21 KB, patch)
2010-03-23 01:25 EDT, Satyam Kandula CLA
frederic_fusier: iplog+
frederic_fusier: review+
Details | Diff
Jar for the added test (3.82 KB, application/x-java-archive)
2010-03-23 01:53 EDT, Satyam Kandula CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Frederic Fusier CLA 2010-03-17 10:11:34 EDT
Using 3.6M6 but also happened with 3.4.2

While investigating bug 305870, I got a NPE while searching for all annotation references in the rt.jar of JRE 6.0. Note that sources must be attached to the jar to get the exception...

Here's the stack trace:
java.lang.NullPointerException
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.newTypeReferenceMatch(MatchLocator.java:1537)
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.newTypeReferenceMatch(MatchLocator.java:1546)
	at org.eclipse.jdt.internal.core.search.matching.TypeReferenceLocator.matchReportReference(TypeReferenceLocator.java:327)
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.reportMatching(MatchLocator.java:2228)
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.reportMatching(MatchLocator.java:2161)
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.reportMatching(MatchLocator.java:2657)
	at org.eclipse.jdt.internal.core.search.matching.MemberDeclarationVisitor.visit(MemberDeclarationVisitor.java:274)
	at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.traverse(TypeDeclaration.java:1304)
	at org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression.traverse(QualifiedAllocationExpression.java:484)
	at org.eclipse.jdt.internal.compiler.ast.FieldDeclaration.traverse(FieldDeclaration.java:288)
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.reportMatching(MatchLocator.java:2424)
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.reportMatching(MatchLocator.java:2644)
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.reportMatching(MatchLocator.java:2669)
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.reportMatching(MatchLocator.java:2384)
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.process(MatchLocator.java:1645)
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.locateMatches(MatchLocator.java:1050)
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.locateMatches(MatchLocator.java:1096)
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.locateMatches(MatchLocator.java:1228)
	at org.eclipse.jdt.internal.core.search.JavaSearchParticipant.locateMatches(JavaSearchParticipant.java:94)
	at org.eclipse.jdt.internal.core.search.BasicSearchEngine.findMatches(BasicSearchEngine.java:239)
	at org.eclipse.jdt.internal.core.search.BasicSearchEngine.search(BasicSearchEngine.java:523)
	at org.eclipse.jdt.core.search.SearchEngine.search(SearchEngine.java:613)
Comment 1 Frederic Fusier CLA 2010-03-17 11:20:30 EDT
Created attachment 162301 [details]
JDT/UI patch to get access to all the constants in the Java Search page

This patch, applied on the org.eclipse.jdt.ui project (v20100316-0800), let user to access and modify all existing Java Search constants in the Java Search page.
Comment 2 Frederic Fusier CLA 2010-03-17 11:28:04 EDT
To reproduce:
1) use the attached JDT/UI patch,
2) launch a brand new runtime workspace using JRE 6.0,
3) create a Java project
4) click on the 'Search' button in the toolbar
5) set-up the Java Search page as follows:
  a) Search string: *
  b) Search for: Annotation
  c) Limit To: References
  d) Match mode: Exact
  e) Search in: all check-boxes selected
  f) Scope: Workspace
6) Click on Search
   => you should get an Error dialog 'Problem Occurred' saying that an internal
      error occurred during 'Java Search'. NPE

In the Error log, you should retrieve the stack trace posted in comment 0...
Comment 3 Frederic Fusier CLA 2010-03-18 12:22:14 EDT
Satyam,

After having a look on the offending match with Olivier, we agreed on the solution to add a null check to avoid the NPE. However, Olivier think that as createHandle(AbstractMethodDeclaration, IJavaElement) can return null in certain circumstances, we must ensure that similar null checks are done for all other places where this method is called.

Please prepare a patch implementing these null checks and I'll review it.

Thanks
Comment 4 Olivier Thomann CLA 2010-03-18 12:27:39 EDT
In fact, this applies to all createHandle(..) method calls.
Since they can return null, we should handle it in the caller.
Comment 5 Satyam Kandula CLA 2010-03-23 01:16:26 EDT
When the search logic doesn't find the required method in the known classes, other related classes are looked for. When there are no methods in that class file, the other related classes are not being looked at and hence the method handle is not getting created -- As, they are no null checks around the annotations, we are getting this NPE.  

Javac is generating an inner class with the name className$1 without any methods when there is an inner enum. This is causing the search logic to break!
Comment 6 Satyam Kandula CLA 2010-03-23 01:25:46 EDT
Created attachment 162740 [details]
Proposed patch

In this patch, I have made the changes to check for the inner classes even if the first class does not have methods. I have also put a couple of null checks at two places that I found appropriate - I could manage to get the test case by adding a new method in the source code after the class files are generated. 

I did not put null checks for the other createHandle()'s as I don't think we will run into an NPE. I could not manage to get the testcase even by modifying the source code after the class files are generated.
Comment 7 Satyam Kandula CLA 2010-03-23 01:53:17 EDT
Created attachment 162741 [details]
Jar for the added test
Comment 8 Frederic Fusier CLA 2010-03-25 06:33:28 EDT
(In reply to comment #6)
> Created an attachment (id=162740) [details]
> Proposed patch
> 
Very good job Satyam! Having a test case for this is a real good thing :-)
Comment 9 Satyam Kandula CLA 2010-03-29 01:54:10 EDT
Frederic has released the patch on HEAD
Comment 10 Olivier Thomann CLA 2010-04-26 14:12:52 EDT
Verified for 3.6M7 using I20100425-2000