当我测试eclipse JDT包时,以下代码导致了空指针异常。
parse.findDeclaringNode(method.resolveMethodBinding()).getClass();
奇怪的是以下代码,
System.out.println("Binding start: " + parse.findDeclaringNode(method.resolveMethodBinding()));
打印以下内容:
Binding start: public static int test(){
return 1;
}
很明显,parse.findDeclaringNode(method.resolveMethodBinding())
返回的内容不为空。
然而,尽管打印了一些内容,但是如下所示添加toString()也会产生空指针异常,这表明打印甚至不会通过" toString()":
parse.findDeclaringNode(method.resolveMethodBinding()).toString();
在我解释代码应该做什么之前,这是运行"parse.findDeclaringNode(method.resolveMethodBinding()).getClass();"
的异常的控制台输出:
!ENTRY org.eclipse.ui 4 0 2018-01-23 20:46:17.150
!MESSAGE Unhandled event loop exception
!STACK 0
org.eclipse.e4.core.di.InjectionException: java.lang.NullPointerException
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:65)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:305)
at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:239)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:152)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:494)
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:487)
at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:210)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:431)
at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem.handleWidgetSelection(AbstractContributionItem.java:446)
at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem.lambda$2(AbstractContributionItem.java:472)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4238)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3817)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1150)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1039)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:153)
at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:680)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:594)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:148)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:151)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)
at org.eclipse.equinox.launcher.Main.run(Main.java:1499)
at org.eclipse.equinox.launcher.Main.main(Main.java:1472)
Caused by: java.lang.NullPointerException
at de.vogella.jdt.astsimple.handlers.SampleHandler.createAST(SampleHandler.java:89)
at de.vogella.jdt.astsimple.handlers.SampleHandler.analyseMethods(SampleHandler.java:52)
at de.vogella.jdt.astsimple.handlers.SampleHandler.execute(SampleHandler.java:37)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:291)
at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:55)
... 37 more
让我解释一下parse.findDeclaringNode(method.resolveMethodBinding()))
是什么。
"解析"是CompilationUnit和"方法"是MethodInvocation。 parse.findDeclaringNode(method.resolveMethodBinding())
应返回ASTNode。 Eclipse会识别这一点,所以如果我输入该行并在末尾添加句点,它会建议我使用ASTNode的方法。
此外,输入System.out.println(parse.findDeclaringNode(method.resolveMethodBinding()) instanceof String);
会让Eclipse抱怨Incompatible conditional operand types ASTNode and String
。因此,即使导入的包也建议返回ASTNode。
但是,System.out.println(parse.findDeclaringNode(method.resolveMethodBinding()) instanceof ASTNode);
打印错误。
我不知道parse.findDeclaringNode(method.resolveMethodBinding())
实际上正在返回什么。
=========== UPDATE ===============
当我做ASTNode test = parse.findDeclaringNode(method.resolveMethodBinding());
时发生了奇怪的事情。 Eclipse调试器显示" test"为null,甚至用test == null
进行测试都返回true。
显然parse.findDeclaringNode(method.resolveMethodBinding())
会返回可以打印的内容但是当被指定为ASTNode对象时,它会变为空。