Skip to content

Commit 8c3b9d0

Browse files
committed
Context: improve javadoc for explicit service list
When instantiating a Context with an explicit list of services, compatible services will be loaded in the order given, regardless of their relative priorities. For example, if you write: new Context(UsageService.class, EventService.class) Then the DefaultUsageService will be loaded before the DefaultEventService, resulting in the DefaultUsageService's event handlers not being registered properly, since at the time of its initialization the context's EventService will still be null. So for the EventService in particular, it should almost always be the first service given in the explicit list. Presumably, there are similar problems when doing things like: new Context(SCIFIOService.class, SciJavaService.class) Since the EventService matches SciJavaService but not SCIFIOService, although I did not test this. We will either need to: A) Change such constructions to list the core SciJava services first; or B) Change the behavior of context creation such that all compatible services are _always_ loaded in priority order, regardless of the order of the specified service template types. Otherwise, any services which register event handlers, but do not depend on the EventService explicitly or transitively, will not work as expected in such scenarios.
1 parent 8c2b8f6 commit 8c3b9d0

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ public Context(final boolean empty) {
117117
* </p>
118118
*
119119
* @param serviceClasses A list of types that implement the {@link Service}
120-
* interface (e.g., {@code DisplayService.class}).
120+
* interface (e.g., {@code DisplayService.class}). Compatible
121+
* services will be loaded in the order given,
122+
* <em>regardless of their relative priorities</em>. In particular,
123+
* take care to specify the {@link EventService} first if you need
124+
* proper event handling for the other services.
121125
* @see #Context(Collection, PluginIndex, boolean)
122126
* @throws ClassCastException If any of the given arguments do not implement
123127
* the {@link Service} interface.
@@ -132,6 +136,10 @@ public Context(@SuppressWarnings("rawtypes") final Class... serviceClasses) {
132136
*
133137
* @param serviceClasses A collection of types that implement the
134138
* {@link Service} interface (e.g., {@code DisplayService.class}).
139+
* Compatible services will be loaded according to the order of the
140+
* collection, <em>regardless of their relative priorities</em>. In
141+
* particular, take care to specify the {@link EventService} first if
142+
* you need proper event handling for the other services.
135143
* @see #Context(Collection, PluginIndex, boolean)
136144
*/
137145
public Context(final Collection<Class<? extends Service>> serviceClasses) {
@@ -144,8 +152,12 @@ public Context(final Collection<Class<? extends Service>> serviceClasses) {
144152
*
145153
* @param serviceClasses A collection of types that implement the
146154
* {@link Service} interface (e.g., {@code DisplayService.class}).
155+
* Compatible services will be loaded according to the order of the
156+
* collection, <em>regardless of their relative priorities</em>. In
157+
* particular, take care to specify the {@link EventService} first if
158+
* you need proper event handling for the other services.
147159
* @param strict Whether context creation will fail fast when there is
148-
* is an error instantiating a required service.
160+
* an error instantiating a required service.
149161
* @see #Context(Collection, PluginIndex, boolean)
150162
*/
151163
public Context(final Collection<Class<? extends Service>> serviceClasses,
@@ -155,10 +167,10 @@ public Context(final Collection<Class<? extends Service>> serviceClasses,
155167
}
156168

157169
/**
158-
* Creates a new SciJava application with the specified PluginIndex. This
159-
* allows a base set of available plugins to be defined, and is useful when
160-
* plugins that would not be returned by the {@link PluginIndex}'s
161-
* {@link org.scijava.plugin.PluginFinder} are desired.
170+
* Creates a new SciJava application context with all available services from
171+
* the specified PluginIndex. This allows a base set of available plugins to
172+
* be defined, and is useful when plugins that would not be returned by the
173+
* {@link PluginIndex}'s {@link org.scijava.plugin.PluginFinder} are desired.
162174
*
163175
* @param pluginIndex The plugin index to use when discovering and indexing
164176
* plugins. If you wish to completely control how services are
@@ -181,6 +193,10 @@ public Context(final PluginIndex pluginIndex) {
181193
*
182194
* @param serviceClasses A collection of types that implement the
183195
* {@link Service} interface (e.g., {@code DisplayService.class}).
196+
* Compatible services will be loaded according to the order of the
197+
* collection, <em>regardless of their relative priorities</em>. In
198+
* particular, take care to specify the {@link EventService} first if
199+
* you need proper event handling for the other services.
184200
* @param pluginIndex The plugin index to use when discovering and indexing
185201
* plugins. If you wish to completely control how services are
186202
* discovered (i.e., use your own
@@ -212,14 +228,18 @@ public Context(final Collection<Class<? extends Service>> serviceClasses,
212228
*
213229
* @param serviceClasses A collection of types that implement the
214230
* {@link Service} interface (e.g., {@code DisplayService.class}).
231+
* Compatible services will be loaded according to the order of the
232+
* collection, <em>regardless of their relative priorities</em>. In
233+
* particular, take care to specify the {@link EventService} first if
234+
* you need proper event handling for the other services.
215235
* @param pluginIndex The plugin index to use when discovering and indexing
216236
* plugins. If you wish to completely control how services are
217237
* discovered (i.e., use your own
218238
* {@link org.scijava.plugin.PluginFinder} implementation), then you
219239
* can pass a custom {@link PluginIndex} here. Passing null will
220240
* result in a default plugin index being constructed and used.
221241
* @param strict Whether context creation will fail fast when there is
222-
* is an error instantiating a required service.
242+
* an error instantiating a required service.
223243
*/
224244
public Context(final Collection<Class<? extends Service>> serviceClasses,
225245
final PluginIndex pluginIndex, final boolean strict)

0 commit comments

Comments
 (0)