5555/**
5656 * Top-level SciJava application context, which initializes and maintains a list
5757 * of services.
58- *
58+ *
5959 * @author Curtis Rueden
6060 * @see Service
6161 */
@@ -100,7 +100,7 @@ public class Context implements Disposable {
100100
101101 /**
102102 * Creates a new SciJava application context with all available services.
103- *
103+ *
104104 * @see #Context(Collection, PluginIndex, boolean)
105105 */
106106 public Context () {
@@ -109,7 +109,7 @@ public Context() {
109109
110110 /**
111111 * Creates a new SciJava application context.
112- *
112+ *
113113 * @param empty If true, the context will be empty of services; otherwise, it
114114 * will be initialized with all available services.
115115 * @see #Context(boolean, boolean)
@@ -120,7 +120,7 @@ public Context(final boolean empty) {
120120
121121 /**
122122 * Creates a new SciJava application context.
123- *
123+ *
124124 * @param noServices If true, the context will contain no services; otherwise,
125125 * it will be initialized with all available services.
126126 * @param noPlugins If true, the context will contain no plugins; otherwise,
@@ -147,11 +147,11 @@ public Context(final boolean noServices, final boolean noPlugins) {
147147 * To avoid this, we have opted to use raw types and suppress the relevant
148148 * warnings here instead.
149149 * </p>
150- *
150+ *
151151 * @param serviceClasses A list of types that implement the {@link Service}
152152 * interface (e.g., {@code DisplayService.class}). Compatible
153- * services will be loaded in the order given,
154- * <em>regardless of their relative priorities</em>.
153+ * services will be loaded in the order given, <em>regardless of
154+ * their relative priorities</em>.
155155 * @see #Context(Collection, PluginIndex, boolean)
156156 * @throws ClassCastException If any of the given arguments do not implement
157157 * the {@link Service} interface.
@@ -163,7 +163,7 @@ public Context(@SuppressWarnings("rawtypes") final Class... serviceClasses) {
163163 /**
164164 * Creates a new SciJava application context with the specified services (and
165165 * any required service dependencies).
166- *
166+ *
167167 * @param serviceClasses A collection of types that implement the
168168 * {@link Service} interface (e.g., {@code DisplayService.class}).
169169 * Compatible services will be loaded according to the order of the
@@ -177,13 +177,13 @@ public Context(final Collection<Class<? extends Service>> serviceClasses) {
177177 /**
178178 * Creates a new SciJava application context with the specified services (and
179179 * any required service dependencies).
180- *
180+ *
181181 * @param serviceClasses A collection of types that implement the
182182 * {@link Service} interface (e.g., {@code DisplayService.class}).
183183 * Compatible services will be loaded according to the order of the
184184 * collection, <em>regardless of their relative priorities</em>.
185- * @param strict Whether context creation will fail fast when there is
186- * an error instantiating a required service.
185+ * @param strict Whether context creation will fail fast when there is an
186+ * error instantiating a required service.
187187 * @see #Context(Collection, PluginIndex, boolean)
188188 */
189189 public Context (final Collection <Class <? extends Service >> serviceClasses ,
@@ -197,7 +197,7 @@ public Context(final Collection<Class<? extends Service>> serviceClasses,
197197 * the specified PluginIndex. This allows a base set of available plugins to
198198 * be defined, and is useful when plugins that would not be returned by the
199199 * {@link PluginIndex}'s {@link org.scijava.plugin.PluginFinder} are desired.
200- *
200+ *
201201 * @param pluginIndex The plugin index to use when discovering and indexing
202202 * plugins. If you wish to completely control how services are
203203 * discovered (i.e., use your own
@@ -215,7 +215,7 @@ public Context(final PluginIndex pluginIndex) {
215215 * any required service dependencies). Service dependency candidates are
216216 * selected from those discovered by the given {@link PluginIndex}'s
217217 * associated {@link org.scijava.plugin.PluginFinder}.
218- *
218+ *
219219 * @param serviceClasses A collection of types that implement the
220220 * {@link Service} interface (e.g., {@code DisplayService.class}).
221221 * Compatible services will be loaded according to the order of the
@@ -248,7 +248,7 @@ public Context(final Collection<Class<? extends Service>> serviceClasses,
248248 * those of lower priority). See {@link ServiceHelper#loadServices()} for more
249249 * information.
250250 * </p>
251- *
251+ *
252252 * @param serviceClasses A collection of types that implement the
253253 * {@link Service} interface (e.g., {@code DisplayService.class}).
254254 * Compatible services will be loaded according to the order of the
@@ -259,8 +259,8 @@ public Context(final Collection<Class<? extends Service>> serviceClasses,
259259 * {@link org.scijava.plugin.PluginFinder} implementation), then you
260260 * can pass a custom {@link PluginIndex} here. Passing null will
261261 * result in a default plugin index being constructed and used.
262- * @param strict Whether context creation will fail fast when there is
263- * an error instantiating a required service.
262+ * @param strict Whether context creation will fail fast when there is an
263+ * error instantiating a required service.
264264 */
265265 public Context (final Collection <Class <? extends Service >> serviceClasses ,
266266 final PluginIndex pluginIndex , final boolean strict )
@@ -272,9 +272,9 @@ public Context(final Collection<Class<? extends Service>> serviceClasses,
272272
273273 setStrict (strict );
274274
275- if (!serviceClasses .isEmpty ()){
276- final ServiceHelper serviceHelper =
277- new ServiceHelper (this , serviceClasses , strict );
275+ if (!serviceClasses .isEmpty ()) {
276+ final ServiceHelper serviceHelper = //
277+ new ServiceHelper (this , serviceClasses , strict );
278278 serviceHelper .loadServices ();
279279 }
280280 }
@@ -299,21 +299,22 @@ public void setStrict(final boolean strict) {
299299
300300 /**
301301 * Gets the service of the given class.
302- *
302+ *
303303 * @throws NoSuchServiceException if the context does not have the requested
304304 * service.
305305 */
306306 public <S extends Service > S service (final Class <S > c ) {
307307 final S service = getService (c );
308308 if (service == null ) {
309- throw new NoSuchServiceException ("Service " + c .getName () + " not found." );
309+ throw new NoSuchServiceException ("Service " + c .getName () +
310+ " not found." );
310311 }
311312 return service ;
312313 }
313314
314315 /**
315316 * Gets the service of the given class name (useful for scripts).
316- *
317+ *
317318 * @throws IllegalArgumentException if the class does not exist, or is not a
318319 * service class.
319320 * @throws NoSuchServiceException if the context does not have the requested
@@ -352,15 +353,16 @@ public Service getService(final String className) {
352353 * distinct things:
353354 * <ul>
354355 * <li>If the given object has any non-final {@link Context} fields annotated
355- * with @{@link Parameter}, sets the value of those fields to this context.</li>
356+ * with @{@link Parameter}, sets the value of those fields to this context.
357+ * </li>
356358 * <li>If the given object has any non-final {@link Service} fields annotated
357359 * with @{@link Parameter}, sets the value of those fields to the
358360 * corresponding service available from this context.</li>
359361 * <li>Calls {@link EventService#subscribe(Object)} with the object to
360- * register any @{@link EventHandler} annotated methods as event subscribers.</li>
361- * .</li>
362+ * register any @{@link EventHandler} annotated methods as event subscribers.
363+ * </li> .</li>
362364 * </ul>
363- *
365+ *
364366 * @param o The object to which the context should be assigned.
365367 * @throws IllegalStateException If the object already has a context.
366368 * @throws IllegalArgumentException If the object has a required
@@ -370,7 +372,7 @@ public Service getService(final String className) {
370372 public void inject (final Object o ) {
371373 // Ensure parameter fields and event handler methods are cached for this
372374 // object.
373- Query query = new Query ();
375+ final Query query = new Query ();
374376 query .put (Parameter .class , Field .class );
375377 query .put (EventHandler .class , Method .class );
376378 ClassUtils .cacheAnnotatedObjects (o .getClass (), query );
@@ -412,13 +414,13 @@ public void dispose() {
412414 public static List <Class <? extends Service >> serviceClassList (
413415 final Class ... serviceClasses )
414416 {
415- return serviceClasses != null ? ( List ) Arrays . asList ( serviceClasses )
416- : Arrays .asList (Service .class );
417+ return serviceClasses != null ? //
418+ Arrays . asList ( serviceClasses ) : Arrays .asList (Service .class );
417419 }
418420
419421 // -- Helper methods --
420422
421- private List <Field > getParameterFields (Object o ) {
423+ private List <Field > getParameterFields (final Object o ) {
422424 try {
423425 return ClassUtils .getAnnotatedFields (o .getClass (), Parameter .class );
424426 }
@@ -436,36 +438,36 @@ private void inject(final Field f, final Object o) {
436438 if (Service .class .isAssignableFrom (type )) {
437439 final Service existingService = (Service ) ClassUtils .getValue (f , o );
438440 if (strict && existingService != null ) {
439- throw new IllegalStateException ("Context already injected: " +
440- f .getDeclaringClass ().getName () + "#" + f .getName ());
441+ throw new IllegalStateException ("Context already injected: " + //
442+ f .getDeclaringClass ().getName () + "#" + f .getName ());
441443 }
442444
443445 // populate Service parameter
444446 @ SuppressWarnings ("unchecked" )
445447 final Class <? extends Service > serviceType =
446- (Class <? extends Service >) type ;
448+ (Class <? extends Service >) type ;
447449 final Service service = getService (serviceType );
448450 if (service == null && f .getAnnotation (Parameter .class ).required ()) {
449- throw new IllegalArgumentException (
451+ throw new IllegalArgumentException (//
450452 createMissingServiceMessage (serviceType ));
451453 }
452454 if (existingService != null && existingService != service ) {
453455 // NB: Can only happen in non-strict mode.
454- throw new IllegalStateException ("Mismatched context: " +
455- f .getDeclaringClass ().getName () + "#" + f .getName ());
456+ throw new IllegalStateException ("Mismatched context: " + //
457+ f .getDeclaringClass ().getName () + "#" + f .getName ());
456458 }
457459 ClassUtils .setValue (f , o , service );
458460 }
459461 else if (Context .class .isAssignableFrom (type ) && type .isInstance (this )) {
460462 final Context existingContext = (Context ) ClassUtils .getValue (f , o );
461463 if (strict && existingContext != null ) {
462- throw new IllegalStateException ("Context already injected: " +
463- f .getDeclaringClass ().getName () + "#" + f .getName ());
464+ throw new IllegalStateException ("Context already injected: " + //
465+ f .getDeclaringClass ().getName () + "#" + f .getName ());
464466 }
465467 if (existingContext != null && existingContext != this ) {
466468 // NB: Can only happen in non-strict mode.
467- throw new IllegalStateException ("Mismatched context: " +
468- f .getDeclaringClass ().getName () + "#" + f .getName ());
469+ throw new IllegalStateException ("Mismatched context: " + //
470+ f .getDeclaringClass ().getName () + "#" + f .getName ());
469471 }
470472
471473 // populate Context parameter
@@ -506,11 +508,10 @@ private String createMissingServiceMessage(
506508 final Class <? extends Service > serviceType )
507509 {
508510 final String nl = System .getProperty ("line.separator" );
509- final ClassLoader classLoader =
511+ final ClassLoader classLoader = //
510512 Thread .currentThread ().getContextClassLoader ();
511- final StringBuilder msg =
512- new StringBuilder ("Required service is missing: " +
513- serviceType .getName () + nl );
513+ final StringBuilder msg = new StringBuilder (
514+ "Required service is missing: " + serviceType .getName () + nl );
514515 msg .append ("Context: " + this + nl );
515516 msg .append ("ClassLoader: " + classLoader + nl );
516517
@@ -528,8 +529,8 @@ private String createMissingServiceMessage(
528529 }
529530 }
530531 else {
531- msg
532- . append ( "ClassLoader was not a URLClassLoader. Could not print classpath." );
532+ msg . append (
533+ "ClassLoader was not a URLClassLoader. Could not print classpath." );
533534 }
534535 return msg .toString ();
535536 }
@@ -539,8 +540,8 @@ private static PluginIndex plugins(final boolean empty) {
539540 }
540541
541542 private static List <Class <? extends Service >> services (final boolean empty ) {
542- if (empty ) return Collections .<Class <? extends Service >> emptyList ();
543- return Arrays .<Class <? extends Service >> asList (Service .class );
543+ if (empty ) return Collections .<Class <? extends Service >>emptyList ();
544+ return Arrays .<Class <? extends Service >>asList (Service .class );
544545 }
545546
546547 private static boolean strict () {
0 commit comments