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

Bug 318877

Summary: [Contributions] IndexOutOfBoundsException by menu org.eclipse.wst.xml.ui.designMenuId after if an plugin tries to insert additional menus at menu location menu:org.eclipse.ui.main.menu?after=additions
Product: [Eclipse Project] Platform Reporter: Stefan Weiße <stefan.weisse>
Component: UIAssignee: Platform UI Triaged <platform-ui-triaged>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: dak2009, edesbalazs, pwebster
Version: 3.6   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard: stalebug

Description Stefan Weiße CLA 2010-07-05 07:19:25 EDT
Build Identifier: 20100617-1415

Eclipse Helios 3.6  20090619-0625  - Eclipse for RCP/Plug-in Developers

Causes a crash , if an additional plug'in tries to insert additional menus at menu location   

menu:org.eclipse.ui.main.menu?after=additions"

by call of org.eclipse.ui.internal.menus.WorkbenchMenuService.addContributionFactory

The crash is cause by ContributionFactory:

val$cache			MenuAdditionCacheEntry  (id=193)	
	additionElement	ConfigurationElementHandle  (id=233)	
	contributeToAllPopups	null	
	hasAdditions		true	
	location		"menu:org.eclipse.wst.xml.ui.designMenuId" (id=237)	
	menuService		WorkbenchMenuService  (id=171)	
	namespace		"org.eclipse.wst.xml.ui" (id=238)	
	subCaches		null	
	visWhenMap		HashMap<K,V>  (id=239)	


Crash stack:

!ENTRY org.eclipse.ui.workbench 4 2 2010-07-05 11:00:58.027
!MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.ui.workbench".
!STACK 0
java.lang.IndexOutOfBoundsException: Index: -1, Size: 0
	at java.util.ArrayList.add(Unknown Source)
	at org.eclipse.jface.action.ContributionManager.insert(ContributionManager.java:316)
	at org.eclipse.ui.internal.menus.WorkbenchMenuService$5.run(WorkbenchMenuService.java:646)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.ui.internal.menus.WorkbenchMenuService.processAdditions(WorkbenchMenuService.java:656)
	at org.eclipse.ui.internal.menus.WorkbenchMenuService.addContributionsToManager(WorkbenchMenuService.java:744)
	at org.eclipse.ui.internal.menus.WorkbenchMenuService.populateContributionManager(WorkbenchMenuService.java:730)
	at org.eclipse.ui.internal.menus.WorkbenchMenuService.addContributionsToManager(WorkbenchMenuService.java:782)
	at org.eclipse.ui.internal.menus.WorkbenchMenuService.addContributionFactory(WorkbenchMenuService.java:496)
	at org.eclipse.ui.internal.menus.SlaveMenuService.addContributionFactory(SlaveMenuService.java:107)

This effect is new by Eclipse 3.6 and will by caised by addition code ( since 3.6 ) in WorkbenchMenuService.processAdditions.run

	// Cache the items for future cleanup
	ManagerPopulationRecord mpr = (ManagerPopulationRecord) populatedManagers.get(mgr);
	ContributionRoot contributions = mpr.getContributions(cache);
	if (contributions != null) {
		// Existing contributions in the mgr will be released.
		// Adjust the insertionIndex
		for (Iterator i = contributions.getItems().iterator(); i.hasNext();) {
			IContributionItem item = (IContributionItem) i.next();
			if (item.equals(mgr.find(item.getId())))
								insertionIndex--;
		}
	}

The exception conditions will always generated under following condition:

•	The factory contributions are already applied  to the appropriate ContributionManager instance
•	The  ContributionManager contributions ArrayList member contains no more items than the factory generated contribution list
•	One of the members of the manager contribution contains an item with id = “addition”
  
Therefore the new code insertion index correction code in Eclipse 3.6 causes , that the insertion index will be counted down below zero and causes the exception at line 646  

						mgr.insert(insertionIndex, ici);

because the ContributionManager.insert throws an exception IndexOutOfBoundsException exception.




Reproducible: Always

Steps to Reproduce:
1. Add in new contribution factory at location menu:org.eclipse.ui.main.menu?after=additions" via  org.eclipse.ui.internal.menus.WorkbenchMenuService.addContributionFactory 
2. the org.eclipse.ui.internal.menus.WorkbenchMenuService.addContributionsToManager function recurse through any sub-menus ( from line 777 of WorkbenchMenuService.java )
3. exception under the described conditions by menu:org.eclipse.wst.xml.ui.designMenuId ( may be that other menu contributions hit the same described conditions )
Comment 1 David Kyle CLA 2011-03-23 16:30:45 EDT
I see the same problem running Eclipse 3.6.1, Centos 5.4, GTK.
Comment 2 Balazs Edes CLA 2014-07-01 11:02:14 EDT
Is there any news about this? We are using 3.7 and recently exactly the same stack trace started showing up.
Comment 3 Paul Webster CLA 2014-07-02 11:13:10 EDT
No one is looking at 3.7 any more.  The latest release out is Luna, 4.4.

Try to reproduce the problem with that, or you can hack into the source code yourself.

PW
Comment 4 Lars Vogel CLA 2019-11-27 07:22:39 EST
This bug hasn't had any activity in quite some time. Maybe the problem got
resolved, was a duplicate of something else, or became less pressing for some
reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it.
The information can be, for example, that the problem still occurs, that you
still want the feature, that more information is needed, or that the bug is
(for whatever reason) no longer relevant.

If the bug is still relevant, please remove the stalebug whiteboard tag.