Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/main/java/org/scijava/Initializable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2014 - 2016 Board of Regents of the University of
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This copyright header should be updated to match the rest of SJC.

* Wisconsin-Madison, University of Konstanz and Brian Northan.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package org.scijava;

/**
* Interface for objects which can be initialized.
*
* @author Curtis Rueden
*/
public interface Initializable {

/** Initializes the object. */
default void initialize() {
// NB: Do nothing by default.
}

}
15 changes: 11 additions & 4 deletions src/main/java/org/scijava/module/AbstractModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.util.HashSet;
import java.util.Map;

import org.scijava.Initializable;

/**
* Abstract superclass of {@link Module} implementations.
* <p>
Expand Down Expand Up @@ -76,11 +78,16 @@ public void cancel() {
public void initialize() throws MethodCallException {
// execute global module initializer
final Object delegateObject = getDelegateObject();
if (initializerRef == null) {
final String initializer = getInfo().getInitializer();
initializerRef = new MethodRef(delegateObject.getClass(), initializer);
if (delegateObject instanceof Initializable) {
((Initializable) delegateObject).initialize();
}
else {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to do either/or here? If so, we should issue a warning when the module implements Initializable and declares an initializer method. Or simpler: why not just run both initializers if both are available/specified?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also a question of handling the case where the Module itself is Initializable. This can happen e.g. for modules which extend AbstractModule directly. I think we should check that instead, and then have CommandModule implement Initializable, working analogously to how support works for Cancelable.

if (initializerRef == null) {
final String initializer = getInfo().getInitializer();
initializerRef = new MethodRef(delegateObject.getClass(), initializer);
}
initializerRef.execute(delegateObject);
}
initializerRef.execute(delegateObject);

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