Skip to content

Commit 596813c

Browse files
ctruedengab1one
authored andcommitted
Do runtime type checking in Typed.supports method
There are situations where we cannot solely rely on the compiler. For example, with DataHandle plugins, the generic parameter L is heterogeneous, so we need to actually check the type of the data. I tried to make this the default implementation of the Typed interface itself, but ran into problems with calling super.supports in downstream classes; apparently, you cannot directly reference default interface methods by writing e.g. Typed.super.supports(...)? Strange.
1 parent 7cccfac commit 596813c

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/main/java/org/scijava/plugin/AbstractTypedPlugin.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,15 @@
4343
public abstract class AbstractTypedPlugin<D> extends AbstractRichPlugin
4444
implements TypedPlugin<D>
4545
{
46-
// NB: No implementation needed.
46+
// -- Typed methods --
47+
48+
@Override
49+
public boolean supports(final D data) {
50+
// NB: Even though the compiler will often guarantee that only data
51+
// of type T is provided here, we still need the runtime check
52+
// for cases where the exact type is not known to compiler --
53+
// e.g., if the object was manufactured by reflection.
54+
return getType().isInstance(data);
55+
}
56+
4757
}

0 commit comments

Comments
 (0)