Skip to content

Commit b46ecbf

Browse files
committed
Context: remember whether the context is strict
Also add API for setting the strictness later, if desired. This will be useful so that we can behave strictly (or not) in later situations, particularly during context injection.
1 parent f952611 commit b46ecbf

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/main/java/org/scijava/Context.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ public class Context implements Disposable {
7878
/** Master index of all plugins known to the application context. */
7979
private final PluginIndex pluginIndex;
8080

81+
/**
82+
* Whether context creation and injection should behave strictly, failing fast
83+
* when attempting to instantiate a required service which is invalid or
84+
* missing.
85+
* <ul>
86+
* <li>If the flag is false, then the context creation will attempt to
87+
* continue even when a required service cannot be instantiated. Otherwise,
88+
* the constructor will throw an {@link IllegalArgumentException} in that
89+
* situation.</li>
90+
* <li>If this flag is false, then a call to {@link Context#inject(Object)}
91+
* will attempt to catch any errors that occur during context injection
92+
* (notably: {@link NoClassDefFoundError} when scanning for event handler
93+
* methods), logging them as errors.</li>
94+
* </ul>
95+
*/
96+
private boolean strict;
97+
8198
/**
8299
* Creates a new SciJava application context with all available services.
83100
*
@@ -240,6 +257,8 @@ public Context(final Collection<Class<? extends Service>> serviceClasses,
240257
this.pluginIndex = pluginIndex == null ? new PluginIndex() : pluginIndex;
241258
this.pluginIndex.discover();
242259

260+
setStrict(strict);
261+
243262
final ServiceHelper serviceHelper =
244263
new ServiceHelper(this, serviceClasses, strict);
245264
serviceHelper.loadServices();
@@ -255,6 +274,14 @@ public PluginIndex getPluginIndex() {
255274
return pluginIndex;
256275
}
257276

277+
public boolean isStrict() {
278+
return strict;
279+
}
280+
281+
public void setStrict(final boolean strict) {
282+
this.strict = strict;
283+
}
284+
258285
/**
259286
* Gets the service of the given class.
260287
*

0 commit comments

Comments
 (0)