Skip to content

Commit 070f04d

Browse files
committed
Context: refactor injection into helper methods
This will enable subsequent changes to the error handling of those respective methods.
1 parent b46ecbf commit 070f04d

File tree

1 file changed

+46
-35
lines changed

1 file changed

+46
-35
lines changed

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

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -357,46 +357,14 @@ public Service getService(final String className) {
357357
*/
358358
public void inject(final Object o) {
359359
// iterate over all @Parameter annotated fields
360-
final List<Field> fields =
361-
ClassUtils.getAnnotatedFields(o.getClass(), Parameter.class);
360+
final List<Field> fields = getParameterFields(o);
362361
for (final Field f : fields) {
363-
f.setAccessible(true); // expose private fields
364-
365-
final Class<?> type = f.getType();
366-
if (Service.class.isAssignableFrom(type)) {
367-
final Service existingService = (Service) ClassUtils.getValue(f, o);
368-
if (existingService != null) {
369-
throw new IllegalStateException("Context already injected: " +
370-
f.getDeclaringClass().getName() + "#" + f.getName());
371-
}
372-
373-
// populate Service parameter
374-
@SuppressWarnings("unchecked")
375-
final Class<? extends Service> serviceType =
376-
(Class<? extends Service>) type;
377-
final Service service = getService(serviceType);
378-
if (service == null && f.getAnnotation(Parameter.class).required()) {
379-
throw new IllegalArgumentException(
380-
createMissingServiceMessage(serviceType));
381-
}
382-
ClassUtils.setValue(f, o, service);
383-
}
384-
else if (Context.class.isAssignableFrom(type) && type.isInstance(this)) {
385-
final Context existingContext = (Context) ClassUtils.getValue(f, o);
386-
if (existingContext != null) {
387-
throw new IllegalStateException("Context already injected: " +
388-
f.getDeclaringClass().getName() + "#" + f.getName());
389-
}
390-
391-
// populate Context parameter
392-
ClassUtils.setValue(f, o, this);
393-
}
362+
inject(f, o);
394363
}
395364

396365
// NB: Subscribe to all events handled by this object.
397366
// This greatly simplifies event handling.
398-
final EventService eventService = getService(EventService.class);
399-
if (eventService != null) eventService.subscribe(o);
367+
subscribeToEvents(o);
400368
}
401369

402370
// -- Disposable methods --
@@ -431,6 +399,49 @@ public static List<Class<? extends Service>> serviceClassList(
431399

432400
// -- Helper methods --
433401

402+
private List<Field> getParameterFields(Object o) {
403+
return ClassUtils.getAnnotatedFields(o.getClass(), Parameter.class);
404+
}
405+
406+
private void inject(final Field f, final Object o) {
407+
f.setAccessible(true); // expose private fields
408+
409+
final Class<?> type = f.getType();
410+
if (Service.class.isAssignableFrom(type)) {
411+
final Service existingService = (Service) ClassUtils.getValue(f, o);
412+
if (existingService != null) {
413+
throw new IllegalStateException("Context already injected: " +
414+
f.getDeclaringClass().getName() + "#" + f.getName());
415+
}
416+
417+
// populate Service parameter
418+
@SuppressWarnings("unchecked")
419+
final Class<? extends Service> serviceType =
420+
(Class<? extends Service>) type;
421+
final Service service = getService(serviceType);
422+
if (service == null && f.getAnnotation(Parameter.class).required()) {
423+
throw new IllegalArgumentException(
424+
createMissingServiceMessage(serviceType));
425+
}
426+
ClassUtils.setValue(f, o, service);
427+
}
428+
else if (Context.class.isAssignableFrom(type) && type.isInstance(this)) {
429+
final Context existingContext = (Context) ClassUtils.getValue(f, o);
430+
if (existingContext != null) {
431+
throw new IllegalStateException("Context already injected: " +
432+
f.getDeclaringClass().getName() + "#" + f.getName());
433+
}
434+
435+
// populate Context parameter
436+
ClassUtils.setValue(f, o, this);
437+
}
438+
}
439+
440+
private void subscribeToEvents(final Object o) {
441+
final EventService eventService = getService(EventService.class);
442+
if (eventService != null) eventService.subscribe(o);
443+
}
444+
434445
private String createMissingServiceMessage(
435446
final Class<? extends Service> serviceType)
436447
{

0 commit comments

Comments
 (0)