Skip to content

Commit 2b458fa

Browse files
committed
Context: add isInjectable(Class) method
Now you can ask the context itself whether a particular field type would have its value assigned by an injection operation, or not. Previously, you simply had to know (assume, really) that Service and Context fields would be injected, and others would not.
1 parent 0f3227d commit 2b458fa

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,25 @@ public void inject(final Object o) {
388388
subscribeToEvents(o);
389389
}
390390

391+
/**
392+
* Reports whether a parameter of the given type would be assigned a value as
393+
* a consequence of calling {@link #inject(Object)}.
394+
* <p>
395+
* This method is notably useful for downstream code to discern between
396+
* {@link Parameter} fields whose values would be injected, versus those whose
397+
* values would not, without needing to hardcode type comparison checks
398+
* against the {@link Service} and {@link Context} types.
399+
* </p>
400+
*
401+
* @param type The type of the @{@link Parameter}-annotated field.
402+
* @return True iff a member field of the given type would have its value
403+
* assigned.
404+
*/
405+
public boolean isInjectable(final Class<?> type) {
406+
if (Service.class.isAssignableFrom(type)) return true;
407+
return Context.class.isAssignableFrom(type) && type.isInstance(this);
408+
}
409+
391410
// -- Disposable methods --
392411

393412
@Override

0 commit comments

Comments
 (0)