Skip to content

Commit 9f527af

Browse files
committed
Merge pull request #75 from scijava/load-delegate-class
Add an API for ModuleInfos to load their delegate class
2 parents 99e5326 + a524ed2 commit 9f527af

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

src/main/java/org/scijava/command/CommandInfo.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,18 @@ public String getDelegateClassName() {
297297
return getClassName();
298298
}
299299

300+
@Override
301+
public Class<?> loadDelegateClass() throws ClassNotFoundException {
302+
try {
303+
return loadClass();
304+
}
305+
catch (final InstantiableException exc) {
306+
final ClassNotFoundException cnfe = new ClassNotFoundException();
307+
cnfe.initCause(exc);
308+
throw cnfe;
309+
}
310+
}
311+
300312
@Override
301313
public Module createModule() throws ModuleException {
302314
// if the command implements Module, return a new instance directly

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,13 @@ public void setValue(final Module module, final T value) {
266266
// -- Internal methods --
267267

268268
protected Class<?> getDelegateClass() {
269-
return ClassUtils.loadClass(info.getDelegateClassName());
269+
try {
270+
return info.loadDelegateClass();
271+
}
272+
catch (final ClassNotFoundException exc) {
273+
// TODO: Consider a better error handling mechanism here.
274+
throw new IllegalStateException(exc);
275+
}
270276
}
271277

272278
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public String getDelegateClassName() {
9595
return getModuleClass().getName();
9696
}
9797

98+
@Override
99+
public Class<?> loadDelegateClass() {
100+
return getModuleClass();
101+
}
102+
98103
@Override
99104
public Module createModule() throws ModuleException {
100105
try {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ public interface ModuleInfo extends UIDetails, Validated {
9494
*/
9595
String getDelegateClassName();
9696

97+
/**
98+
* Loads the class containing the module's actual implementation. The name of
99+
* the loaded class will match the value returned by
100+
* {@link #getDelegateClassName()}.
101+
*
102+
* @see org.scijava.Instantiable#loadClass()
103+
*/
104+
Class<?> loadDelegateClass() throws ClassNotFoundException;
105+
97106
/** Instantiates the module described by this module info. */
98107
Module createModule() throws ModuleException;
99108

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ public String getDelegateClassName() {
236236
return ScriptModule.class.getName();
237237
}
238238

239+
@Override
240+
public Class<?> loadDelegateClass() {
241+
return ScriptModule.class;
242+
}
243+
239244
@Override
240245
public ScriptModule createModule() throws ModuleException {
241246
return new ScriptModule(this);

0 commit comments

Comments
 (0)