Skip to content

Commit 70045f6

Browse files
committed
MenuService: avoid infinite menu generation loop
The rootMenus() double-locking menu generation was calling an initialization method that again called rootMenus() - leading to infinite method calls and a StackOverflow exception. The fix is to have a second initialization method that accepts a menu map and then does not need to call back to rootMenus().
1 parent bfdb854 commit 70045f6

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/main/java/org/scijava/menu/DefaultMenuService.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Collection;
3636
import java.util.HashMap;
3737
import java.util.List;
38+
import java.util.Map;
3839

3940
import org.scijava.UIDetails;
4041
import org.scijava.event.EventHandler;
@@ -132,6 +133,16 @@ protected void onEvent(final ModulesUpdatedEvent event) {
132133
* </p>
133134
*/
134135
private synchronized void addModules(final Collection<ModuleInfo> items) {
136+
addModules(items, rootMenus());
137+
}
138+
139+
/**
140+
* As {@link #addModules(Collection)} adding modules to the provided menu
141+
* root.
142+
*/
143+
private synchronized void addModules(final Collection<ModuleInfo> items,
144+
final Map<String, ShadowMenu> rootMenu)
145+
{
135146
// categorize modules by menu root
136147
final HashMap<String, ArrayList<ModuleInfo>> modulesByMenuRoot =
137148
new HashMap<String, ArrayList<ModuleInfo>>();
@@ -148,11 +159,11 @@ private synchronized void addModules(final Collection<ModuleInfo> items) {
148159
// process each menu root separately
149160
for (final String menuRoot : modulesByMenuRoot.keySet()) {
150161
final ArrayList<ModuleInfo> modules = modulesByMenuRoot.get(menuRoot);
151-
ShadowMenu menu = rootMenus().get(menuRoot);
162+
ShadowMenu menu = rootMenu.get(menuRoot);
152163
if (menu == null) {
153164
// new menu root: create new menu structure
154165
menu = new ShadowMenu(getContext(), modules);
155-
rootMenus().put(menuRoot, menu);
166+
rootMenu.put(menuRoot, menu);
156167
}
157168
else {
158169
// existing menu root: add to menu structure
@@ -184,7 +195,7 @@ private synchronized void initRootMenus() {
184195
final HashMap<String, ShadowMenu> map = new HashMap<String, ShadowMenu>();
185196

186197
final List<ModuleInfo> allModules = moduleService.getModules();
187-
addModules(allModules);
198+
addModules(allModules, map);
188199
rootMenus = map;
189200
}
190201

0 commit comments

Comments
 (0)