Community
Participate
Working Groups
I20030729 I got the stack below caused by a bug in my code. However, reading the API I would not expect ISourceReference.getSource to throw an ArrayIndexOutOfBoundsException !ENTRY org.eclipse.ui.workbench 4 2 Jul 29, 2003 15:43:05.868 !MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.ui.workbench". !STACK 0 java.lang.ArrayIndexOutOfBoundsException at org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter.getText(DocumentAdapter.java:383) at org.eclipse.jdt.internal.core.SourceRefElement.getSource(SourceRefElement.java:141) at org.eclipse.jdt.internal.ui.infoviews.SourceView.setInput(SourceView.java:209) at org.eclipse.jdt.internal.ui.infoviews.AbstractInfoView.setInputFrom(AbstractInfoView.java:244) at org.eclipse.jdt.internal.ui.infoviews.AbstractInfoView.selectionChanged(AbstractInfoView.java:168) at org.eclipse.ui.internal.AbstractSelectionService$4.run(AbstractSelectionService.java:178) at org.eclipse.core.internal.runtime.InternalPlatform.run(InternalPlatform.java:1015) at org.eclipse.core.runtime.Platform.run(Platform.java:420) at org.eclipse.ui.internal.AbstractSelectionService.firePostSelection(AbstractSelectionService.java:176) at org.eclipse.ui.internal.AbstractSelectionService$2.selectionChanged(AbstractSelectionService.java:76) at org.eclipse.jface.text.TextViewer.firePostSelectionChanged(TextViewer.java:2099) at org.eclipse.jface.text.TextViewer.firePostSelectionChanged(TextViewer.java:2052) at org.eclipse.jface.text.TextViewer$4.run(TextViewer.java:2031) at org.eclipse.swt.widgets.Display.runTimer(Display.java:2230) at org.eclipse.swt.widgets.Display.messageProc(Display.java:1764) at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method) at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:1338) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:1876) at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:1680) at org.eclipse.ui.internal.Workbench.run(Workbench.java:1663) at org.eclipse.core.internal.boot.InternalBootLoader.run(InternalBootLoader.java:858) 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)
Proposal: /** * @see ISourceReference */ public String getSource() throws JavaModelException { IOpenable openable = getOpenableParent(); IBuffer buffer = openable.getBuffer(); if (buffer == null) { return null; } ISourceRange range = getSourceRange(); int offset = range.getOffset(); int length = range.getLength(); if (offset == -1 || length == 0 ) { return null; } try { return buffer.getText(offset, length); } catch(RuntimeException e) { if (e instanceof IndexOutOfBoundsException) { throw new JavaModelException(e, IJavaModelStatusConstants.INDEX_OUT_OF_BOUNDS); } throw new JavaModelException(e, IStatus.ERROR); } }
Yes, and maybe the Javadoc should explain when/why this exception happens. Another solution could be to mention the ArrayIndexOutOfBoundsException directly in the API, but I think this is not the normal pattern for JDT Core APIs.
No I don't like the idea of having ArrayIndexOutOfBoundsException directly in the API. A runtime exception can occur while executing buffer.getText(...). We simply need to convert this exception to a JavaModelException. This is specified in the doc. The doc says: * @exception JavaModelException if this element does not exist or if an * exception occurs while accessing its corresponding resource I believe this is enough. JavaModelException needs to be handled by callers of getSource(). The proposal fixes the difference between the doc and the implementation.
Fixed and released in HEAD.
Change milestone.
In case of an ArrayOutOfBoundsException during getSource(), null will be returned.
Verified.