Skip to content

Commit f88984c

Browse files
committed
DefaultScriptService: fix service aliases
Previously, if a service depending on the ScriptService called addAlias during its initialization (which is the case for ImageJ's LegacyService), it would trigger the ScriptService to initialize its alias map, including addition of all available services to the map. But at that point, not all services are available yet. Better is to wait to add services to the alias map until it is _certain_ that the service initialization is complete. The ServicesLoadedEvent is perfect for this.
1 parent bf0a385 commit f88984c

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/main/java/org/scijava/script/DefaultScriptService.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.scijava.MenuPath;
5656
import org.scijava.Priority;
5757
import org.scijava.command.CommandService;
58+
import org.scijava.event.EventHandler;
5859
import org.scijava.log.LogService;
5960
import org.scijava.module.Module;
6061
import org.scijava.module.ModuleService;
@@ -65,6 +66,7 @@
6566
import org.scijava.plugin.PluginInfo;
6667
import org.scijava.plugin.PluginService;
6768
import org.scijava.service.Service;
69+
import org.scijava.service.event.ServicesLoadedEvent;
6870
import org.scijava.util.AppUtils;
6971
import org.scijava.util.ClassUtils;
7072
import org.scijava.util.ColorRGB;
@@ -286,6 +288,18 @@ public Collection<ScriptInfo> get() {
286288
});
287289
}
288290

291+
// -- Event handlers --
292+
293+
@EventHandler
294+
private void
295+
onEvent(@SuppressWarnings("unused") final ServicesLoadedEvent evt)
296+
{
297+
// NB: Add service type aliases after all services have joined the context.
298+
for (final Service service : getContext().getServiceIndex()) {
299+
addAliases(aliasMap, service.getClass());
300+
}
301+
}
302+
289303
// -- Helper methods - lazy initialization --
290304

291305
/** Gets {@link #scriptLanguageIndex}, initializing if needed. */
@@ -401,11 +415,6 @@ private synchronized void initAliasMap() {
401415
addAliases(map, Context.class, BigDecimal.class, BigInteger.class,
402416
ColorRGB.class, ColorRGBA.class, File.class, String.class);
403417

404-
// service types
405-
for (final Service service : getContext().getServiceIndex()) {
406-
addAliases(map, service.getClass());
407-
}
408-
409418
// gateway types
410419
final List<PluginInfo<Gateway>> gatewayPlugins =
411420
pluginService.getPluginsOfType(Gateway.class);

0 commit comments

Comments
 (0)