Skip to content

Commit de72621

Browse files
committed
ModuleInfo: add a loadDelegateClass() method
This makes it easier to more consistently and safely obtain the Class<?> object associated with the meat of the module being described. Previously, you could call getDelegateClassName() but then there was no safe way, in general, to load that class in all situations Previously, you could call getDelegateClassName() but then there was no safe way, in general, to load that class in all situations.
1 parent 99e5326 commit de72621

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
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/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)