Skip to content

Commit 6121cf8

Browse files
committed
Merge branch 'fix-script-discovery'
See http://forum.imagej.net/t/11888
2 parents 7b8be13 + 65f3d5f commit 6121cf8

File tree

6 files changed

+48
-24
lines changed

6 files changed

+48
-24
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>scijava-common</artifactId>
13-
<version>2.74.4-SNAPSHOT</version>
13+
<version>2.75.0-SNAPSHOT</version>
1414

1515
<name>SciJava Common</name>
1616
<description>SciJava Common is a shared library for SciJava software. It provides a plugin framework, with an extensible mechanism for service discovery, backed by its own annotation processor, so that plugins can be loaded dynamically. It is used by downstream projects in the SciJava ecosystem, such as ImageJ and SCIFIO.</description>

src/main/java/org/scijava/event/SciJavaEvent.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
package org.scijava.event;
3434

3535
import org.scijava.AbstractContextual;
36+
import org.scijava.util.DebugUtils;
3637

3738
/**
3839
* Base class for all SciJava events.
@@ -83,6 +84,14 @@ public StackTraceElement[] getStackTrace() {
8384
return stackTrace;
8485
}
8586

87+
/**
88+
* Gets a stack trace for the calling thread when the event was published.
89+
* This method is useful for debugging what triggered an event.
90+
*/
91+
public String dumpStack() {
92+
return DebugUtils.getStackDump(getCallingThread(), getStackTrace());
93+
}
94+
8695
// Object methods --
8796

8897
@Override

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,22 @@ public ShadowMenu getMenu(final String menuRoot) {
7979
// -- Event handlers --
8080

8181
@EventHandler
82-
protected void onEvent(final ModulesAddedEvent event) {
83-
if (rootMenus == null) {
84-
// add *all* known modules, which includes the ones given here
85-
rootMenus();
86-
return;
87-
}
88-
// data structure already exists; add *these* modules only
82+
protected synchronized void onEvent(final ModulesAddedEvent event) {
83+
if (rootMenus == null) return; // menus not yet initialized
8984
addModules(event.getItems());
9085
}
9186

9287
@EventHandler
93-
protected void onEvent(final ModulesRemovedEvent event) {
88+
protected synchronized void onEvent(final ModulesRemovedEvent event) {
89+
if (rootMenus == null) return; // menus not yet initialized
9490
for (final ShadowMenu menu : rootMenus().values()) {
9591
menu.removeAll(event.getItems());
9692
}
9793
}
9894

9995
@EventHandler
100-
protected void onEvent(final ModulesUpdatedEvent event) {
96+
protected synchronized void onEvent(final ModulesUpdatedEvent event) {
97+
if (rootMenus == null) return; // menus not yet initialized
10198
for (final ShadowMenu menu : rootMenus().values()) {
10299
menu.updateAll(event.getItems());
103100
}
@@ -165,9 +162,7 @@ private synchronized void addModules(final Collection<ModuleInfo> items,
165162
* </p>
166163
*/
167164
private HashMap<String, ShadowMenu> rootMenus() {
168-
if (rootMenus == null) {
169-
initRootMenus();
170-
}
165+
if (rootMenus == null) initRootMenus();
171166
return rootMenus;
172167
}
173168

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
* @author Johannes Schindelin
7676
* @author Curtis Rueden
7777
*/
78-
@Plugin(type = Service.class, priority = Priority.HIGH)
78+
@Plugin(type = Service.class)
7979
public class DefaultScriptService extends
8080
AbstractSingletonService<ScriptLanguage> implements ScriptService
8181
{
@@ -226,14 +226,8 @@ public void initialize() {
226226
super.initialize();
227227

228228
// add scripts to the module index... only when needed!
229-
moduleService.getIndex().addLater(new LazyObjects<ScriptInfo>() {
230-
231-
@Override
232-
public Collection<ScriptInfo> get() {
233-
return scripts().values();
234-
}
235-
236-
});
229+
final LazyObjects<ScriptInfo> lazyScripts = () -> scripts().values();
230+
moduleService.getIndex().addLater(lazyScripts);
237231
}
238232

239233
// -- Helper methods - lazy initialization --
@@ -359,7 +353,7 @@ private synchronized void initAliasMap() {
359353
* are registered with the service.
360354
*/
361355
private ScriptInfo getOrCreate(final File file) {
362-
final ScriptInfo info = scripts().get(file);
356+
final ScriptInfo info = scripts().get(file.getAbsolutePath());
363357
if (info != null) return info;
364358
return new ScriptInfo(getContext(), file);
365359
}

src/main/java/org/scijava/util/DebugUtils.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,32 @@ public static String getStackTrace(final Throwable t) {
6767
}
6868
}
6969

70+
/**
71+
* Provides a stack dump of the given thread.
72+
* <p>
73+
* The output is similar to a subset of that given when Ctrl+\ (or Ctrl+Pause
74+
* on Windows) is pressed from the console.
75+
* </p>
76+
*/
77+
public static String getStackDump(final Thread thread) {
78+
return getStackDump(thread, thread.getStackTrace());
79+
}
80+
81+
/**
82+
* Provides a stack dump of the given thread + call stack.
83+
* <p>
84+
* The output is similar to a subset of that given when Ctrl+\ (or Ctrl+Pause
85+
* on Windows) is pressed from the console.
86+
* </p>
87+
*/
88+
public static String getStackDump(final Thread thread,
89+
final StackTraceElement[] stackTrace)
90+
{
91+
final StringBuilder sb = new StringBuilder();
92+
dumpThread(thread, stackTrace, sb);
93+
return sb.toString();
94+
}
95+
7096
/**
7197
* Provides a complete stack dump of all threads.
7298
* <p>

src/test/java/org/scijava/ContextCreationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public void testNoPlugins() {
8787
public void testFull() {
8888
final Class<?>[] expected =
8989
{ org.scijava.event.DefaultEventService.class,
90-
org.scijava.script.DefaultScriptService.class,
9190
org.scijava.app.DefaultAppService.class,
9291
org.scijava.app.DefaultStatusService.class,
9392
org.scijava.command.DefaultCommandService.class,
@@ -113,6 +112,7 @@ public void testFull() {
113112
org.scijava.prefs.DefaultPrefService.class,
114113
org.scijava.run.DefaultRunService.class,
115114
org.scijava.script.DefaultScriptHeaderService.class,
115+
org.scijava.script.DefaultScriptService.class,
116116
org.scijava.script.process.DefaultScriptProcessorService.class,
117117
org.scijava.startup.DefaultStartupService.class,
118118
org.scijava.task.DefaultTaskService.class,

0 commit comments

Comments
 (0)