Eclipse插件:如果Annotation不存在,AbstractAnnotationHover会导致NullPointerException

时间:2016-06-09 21:26:11

标签: java eclipse eclipse-plugin

我有什么:我开发了一个Eclipse编辑器,并使用此方法覆盖SourceViewerConfiguration中的getTextHover方法:

public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
                return new AbstractAnnotationHover(true) {
                    public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
                        IAnnotationModel model = ((SourceViewer) textViewer).getAnnotationModel();
                        @SuppressWarnings("unchecked")
                        Iterator<Annotation> parent = 
                        ((IAnnotationModelExtension2)model).getAnnotationIterator(hoverRegion.getOffset(), 
                                hoverRegion.getLength(), true, true);
                        Iterator<?> iter = new JavaAnnotationIterator(parent, false); 
                        Annotation annotation = null;
                        Position position = null;
                        while (iter.hasNext()) {
                            Annotation a = (Annotation) iter.next();
                            Position p = model.getPosition(a);
                            annotation = a;
                            position = p;
                        }
                        return new AnnotationInfo(annotation, position, textViewer) {
                            public ICompletionProposal[] getCompletionProposals() {
                                ICompletionProposal proposal1 = null;
                                IMarkerResolution [] resolutions = null;
                                ICompletionProposal [] com = null;
                                if (annotation instanceof MarkerAnnotation) {
                                    resolutions = new ErrorResolution().getResolutions(((MarkerAnnotation) annotation).getMarker());
                                    if(resolutions.length != 0){
                                        proposal1 = new MarkerResolutionProposal(resolutions[0], 
                                                ((MarkerAnnotation) annotation).getMarker());
                                        return new ICompletionProposal[] { proposal1 };
                                    }
                                }
                                return com ;
                            }
                        };
                    } 
                };  
  }

当鼠标悬停在代码查看器中的带注释错误上时,它会显示QuickFix。但是,如果我将鼠标悬停在一个没有快速修复或只是正确代码行的错误上,则会导致以下错误:

org.eclipse.swt.SWTException:无法执行runnable(java.lang.NullPointerException)     在org.eclipse.swt.SWT.error(SWT.java:4491)     在org.eclipse.swt.SWT.error(SWT.java:4406)     在org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:138)     在org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4155)     在org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3772)     at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine $ 4.run(PartRenderingEngine.java:1127)     在org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)     在org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1018)     在org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:156)     在org.eclipse.ui.internal.Workbench $ 5.run(Workbench.java:694)     在org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)     在org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:606)     在org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)     在org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:139)     在org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)     在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)     在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)     在org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)     在org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:497)     在org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:669)     在org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)     在org.eclipse.equinox.launcher.Main.run(Main.java:1515)     在org.eclipse.equinox.launcher.Main.main(Main.java:1488) 引起:java.lang.NullPointerException     at org.eclipse.jdt.internal.ui.text.java.hover.AbstractAnnotationHover $ AnnotationInformationControl.createAnnotationInformation(AbstractAnnotationHover.java:329)     at org.eclipse.jdt.internal.ui.text.java.hover.AbstractAnnotationHover $ AnnotationInformationControl.deferredCreateContent(AbstractAnnotationHover.java:282)     at org.eclipse.jdt.internal.ui.text.java.hover.AbstractAnnotationHover $ AnnotationInformationControl.setInput(AbstractAnnotationHover.java:186)     at org.eclipse.jface.text.AbstractInformationControlManager.internalShowInformationControl(AbstractInformationControlManager.java:1181)     at org.eclipse.jface.text.AbstractInformationControlManager.presentInformation(AbstractInformationControlManager.java:1150)     at org.eclipse.jface.text.AbstractHoverInformationControlManager.presentInformation(AbstractHoverInformationControlManager.java:902)     at org.eclipse.jface.text.TextViewerHoverManager.doPresentInformation(TextViewerHoverManager.java:243)     at org.eclipse.jface.text.TextViewerHoverManager $ 5.run(TextViewerHoverManager.java:233)     在org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)     在org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)     ......还有24个

并且未显示注释的消息。

我想要的是什么:我想,如果我在快速修复可用时出现错误,则会显示快速修复,如果没有,则只显示注释消息。

0 个答案:

没有答案