Bug 40937 - ISourceReference.getSource throws ArrayIndexOutOfBoundsException
Summary: ISourceReference.getSource throws ArrayIndexOutOfBoundsException
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.0 M4   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-07-30 05:29 EDT by Dani Megert CLA
Modified: 2003-10-08 07:27 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 Dani Megert CLA 2003-07-30 05:29:10 EDT
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)
Comment 1 Olivier Thomann CLA 2003-07-30 15:32:45 EDT
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);
	}
}
Comment 2 Dani Megert CLA 2003-07-31 02:25:47 EDT
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.

Comment 3 Olivier Thomann CLA 2003-07-31 10:32:56 EDT
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.
Comment 4 Olivier Thomann CLA 2003-08-28 09:03:48 EDT
Fixed and released in HEAD.
Comment 5 Olivier Thomann CLA 2003-08-28 09:04:01 EDT
Change milestone.
Comment 6 Olivier Thomann CLA 2003-08-28 10:29:50 EDT
In case of an ArrayOutOfBoundsException during getSource(), null will be returned.
Comment 7 David Audel CLA 2003-10-08 07:27:59 EDT
Verified.