Skip to content

Commit 60ef945

Browse files
lnyngctrueden
authored andcommitted
Make Module support Initializable
It might be easier for the delegate class inside the module to implement this interface than to specify the initializer inside the Plugin annotation, especially because annotation could not be partially overridden. Now the Initializable interface has higher priority than the initializer annotation, and at most one of them will be called when a module is initialized. Closes #249. Signed-off-by: Curtis Rueden <ctrueden@wisc.edu>
1 parent 7cb92b9 commit 60ef945

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/main/java/org/scijava/module/AbstractModule.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import java.util.HashSet;
3636
import java.util.Map;
3737

38+
import org.scijava.Initializable;
39+
3840
/**
3941
* Abstract superclass of {@link Module} implementations.
4042
* <p>
@@ -76,11 +78,16 @@ public void cancel() {
7678
public void initialize() throws MethodCallException {
7779
// execute global module initializer
7880
final Object delegateObject = getDelegateObject();
79-
if (initializerRef == null) {
80-
final String initializer = getInfo().getInitializer();
81-
initializerRef = new MethodRef(delegateObject.getClass(), initializer);
81+
if (delegateObject instanceof Initializable) {
82+
((Initializable) delegateObject).initialize();
83+
}
84+
else {
85+
if (initializerRef == null) {
86+
final String initializer = getInfo().getInitializer();
87+
initializerRef = new MethodRef(delegateObject.getClass(), initializer);
88+
}
89+
initializerRef.execute(delegateObject);
8290
}
83-
initializerRef.execute(delegateObject);
8491

8592
// execute individual module item initializers
8693
for (final ModuleItem<?> item : getInfo().inputs()) {

0 commit comments

Comments
 (0)