Bug 40804 - NPE in MethodBinding.sourceMethod()
Summary: NPE in MethodBinding.sourceMethod()
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: 3.0 M3   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-07-25 19:13 EDT by Ryan Wannop CLA
Modified: 2003-08-28 06:22 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ryan Wannop CLA 2003-07-25 19:13:22 EDT
My plugin crashed from an NPE when I called binding.getDeclaredMethods() on 
an anonymous type binding.  The binding originates from a compilation unit with 
the following error:  
"This compilation unit indirectly references the missing type 
org.eclipse.swt.events.SelectionEvent 
 (typically some required class file is referencing a type outside the 
classpath)"

The NPE occurs at line 467 of MethodBinding because "scope" is null: 
AbstractMethodDeclaration[] methods = sourceType.scope.referenceContext.methods;

Stack trace:  
java.lang.NullPointerException
at org.eclipse.jdt.internal.compiler.lookup.MethodBinding.sourceMethod
(MethodBinding.java:467)
at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.resolveTypesFor
(SourceTypeBinding.java:793)
at org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.methods
(SourceTypeBinding.java:687)
at org.eclipse.jdt.core.dom.TypeBinding.getDeclaredMethods(TypeBinding.java:324)
at ca.ubc.jquery.engine.context.TypeContext.exitContext(TypeContext.java:117)
at ca.ubc.jquery.engine.FactsGenerator.endVisit(FactsGenerator.java:50)
at org.eclipse.jdt.core.dom.TypeDeclaration.accept0(TypeDeclaration.java:163)
at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:1353)
at ca.ubc.jquery.engine.FactsGenerator.visit(FactsGenerator.java:42)
at org.eclipse.jdt.core.dom.TypeDeclaration.accept0(TypeDeclaration.java:154)
at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:1353)
at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:1423)
at org.eclipse.jdt.core.dom.CompilationUnit.accept0(CompilationUnit.java:159)
at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:1353)
at ca.ubc.jquery.engine.FactsGenerator.generate(FactsGenerator.java:417)
at ca.ubc.jquery.engine.CodeFactBucket.update(CodeFactBucket.java:543)
at tyRuBa.engine.RuleBaseBucket.doUpdate(RuleBaseBucket.java:95)
at tyRuBa.engine.FrontEnd.updateBuckets(FrontEnd.java:272)
at tyRuBa.engine.FrontEnd.autoUpdateBuckets(FrontEnd.java:295)
at tyRuBa.engine.QueryEngine.frameQuery(QueryEngine.java:89)
at ca.ubc.jquery.gui.QueryControl.addAvailableTopQueries(QueryControl.java:333)
at ca.ubc.jquery.gui.QueryControl.addTopQueryItems(QueryControl.java:500)
at ca.ubc.jquery.gui.QueryControl.fillContextMenu(QueryControl.java:494)
at ca.ubc.jquery.gui.QueryControl$9.menuAboutToShow(QueryControl.java:566)
at org.eclipse.jface.action.MenuManager.fireAboutToShow(MenuManager.java:255)
at org.eclipse.jface.action.MenuManager.handleAboutToShow(MenuManager.java:334)
at org.eclipse.jface.action.MenuManager.access$0(MenuManager.java:331)
at org.eclipse.jface.action.MenuManager$2.menuShown(MenuManager.java:343)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:116)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:81)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:840)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:865)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:845)
at org.eclipse.swt.widgets.Control.WM_INITMENUPOPUP(Control.java:3141)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:2785)
at org.eclipse.swt.widgets.Decorations.windowProc(Decorations.java:1229)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:2361)
at org.eclipse.swt.internal.win32.OS.TrackPopupMenu(Native Method)
at org.eclipse.swt.widgets.Menu._setVisible(Menu.java:218)
at org.eclipse.swt.widgets.Display.runPopups(Display.java:1863)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:1538)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:1402)
at org.eclipse.ui.internal.Workbench.run(Workbench.java:1385)
at org.eclipse.core.internal.boot.InternalBootLoader.run
(InternalBootLoader.java:845)
at org.eclipse.core.boot.BootLoader.run(BootLoader.java:461)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.eclipse.core.launcher.Main.basicRun(Main.java:291)
at org.eclipse.core.launcher.Main.run(Main.java:747)
at org.eclipse.core.launcher.Main.main(Main.java:583)
Comment 1 Olivier Thomann CLA 2003-07-28 10:54:43 EDT
Please provide a test case. I will investigate.
Comment 2 Olivier Thomann CLA 2003-07-28 13:10:34 EDT
I tried the following test case:
package test0489;

public class A {
	Object foo () {
		return new Y() {
			public void bar() {
			}
		};
	}
}
Where Y implements Zork and I remove the .class file for Zork. I get a similar
error, but I can successfully get the declared methods.
Could you please provide your test case for further investigation?
Comment 3 Olivier Thomann CLA 2003-07-29 14:05:34 EDT
Could you please provide your test case or let me know where I can get your
plugin? What build are you using?
Comment 4 Ryan Wannop CLA 2003-07-29 16:12:05 EDT
I am using Eclipse: Version: 2.1.0, Build id: 200303272130

I zipped the problematic project and emailed it to you directly.
Comment 5 Olivier Thomann CLA 2003-07-30 10:46:56 EDT
I reproduced it. Thanks for the test case.
The problem is that an abortCompilation exception is thrown during the import
resolution because of the missing library (swt.jar). Then the types are not
resolved and the method declarations have no resolved bindings. Because the
scope has been nullified during the compilation unit cleanup, it is not possible
to resolve the bindings afterwards.
So in this case, I think it is acceptable that no binding is returned for
unresolved code. The classpath needs to be fixed in order to resolve properly
all bindings.
The specs say that if a binding cannot be resolved, null can be returned. In
your case, the bug was that we didn't detect early enough that the bindings
could not be resolved because of a failure during the import resolution.
Is this ok for you?
Comment 6 Ryan Wannop CLA 2003-07-30 11:04:17 EDT
Yes.  I would definately prefer a null return value over a null pointer
exception.
Comment 7 Olivier Thomann CLA 2003-07-30 12:23:45 EDT
The fix will be to nullify all bindings that are not resolved. In this case you 
won't get a binding for the type declaration.
Your test case is a case where there is a problem with the import declarations. 
Therefore all declarations underneath are not resolvable. According to the 
specs of the DOM/AST, it is acceptable in this case to return null for all 
bindings.
I am testing my fix and I will release it shortly in HEAD.
Comment 8 Olivier Thomann CLA 2003-07-30 12:53:34 EDT
Fixed and released in HEAD.
Regression tests added.
Comment 9 David Audel CLA 2003-08-28 06:22:56 EDT
Verified.