Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 237489

Summary: GraphViewer#findGraphItem throws ArrayIndexOutOfBoundsException if it can't find passed element
Product: [Tools] GEF Reporter: Stefan Tucker <stefan.tucker>
Component: GEF-Legacy ZestAssignee: Ian Bull <irbull>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: gerald, steeg, zoltan.ujhelyi
Version: 3.4   
Target Milestone: ---   
Hardware: PC   
OS: Windows Server 2003   
Whiteboard:

Description Stefan Tucker CLA 2008-06-17 12:18:22 EDT
Build ID: I20080523-0100

Steps To Reproduce:
Call GraphViewer#findGraphItem(new Integer(5)).

It should return null, but it throws ArrayIndexOutOfBoundsException instead.

More information:
If you call GraphViewer#findGraphItem() and pass an object it can't find in the graph, the findItems() method returns NO_WIDGETS, which is defined as "new Widget[0]."

public GraphItem findGraphItem(Object element) {
	Widget[] result = findItems(element);
	return (result.length == 0 && result[0] instanceof GraphItem) ? null : (GraphItem) result[0];
}

The problem is that if the returned array is of length 0, findGraphItem() accesses the first element. There's no first element (because the array's length is 0), so it throws an exception.

The return statement should probably be:

if ((result.length > 0) && (result[0] instanceof GraphItem))
	return (GraphItem) result[0];
else
	return null;
Comment 1 Zoltan Ujhelyi CLA 2011-04-04 17:26:35 EDT
I tested the issue right now and it is fixed in the current Git version of Zest 2.0.
Comment 2 Fabian Steeg CLA 2011-04-04 23:41:04 EDT
Thanks, verified this works in Zest 2.0 master with a unit test:

http://git.eclipse.org/c/gef/org.eclipse.zest.git/commit/?id=00b35e623a6a102cade9a52afaddf62b1db29b32