Skip to content

Commit fbd6485

Browse files
committed
Context: fix injection of Context parameters
The previous test was: type.isAssignableFrom(getClass()) The idea being that if the @parameter type is FooContext, and our context is a BarContext, then it should not be assigned. But the order was backwards: e.g., if we had an Object @parameter and a BarContext, the test would pass, and the BarContext would be assigned to the Object. However, we do not want contexts to be assigned to just *any* Object, but only to Context @parameters and subtypes thereof. So we update the test as follows: Context.class.isAssignableFrom(type) && type.isInstance(this) The new test first checks whether the type is a Context or subtype, and if so, whether the context is assignment compatible with the type of the @parameter. The test "type.isInstance(this)" is equivalent to, but hopefully more succinct and easier to understand than, the previous "type.isAssignableFrom(getClass())" test.
1 parent ca6905e commit fbd6485

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void inject(final Object o) {
287287
}
288288
ClassUtils.setValue(f, o, service);
289289
}
290-
else if (type.isAssignableFrom(getClass())) {
290+
else if (Context.class.isAssignableFrom(type) && type.isInstance(this)) {
291291
final Context existingContext = (Context) ClassUtils.getValue(f, o);
292292
if (existingContext != null) {
293293
throw new IllegalStateException("Context already injected: " +

0 commit comments

Comments
 (0)