Skip to content

Commit 133142a

Browse files
committed
Typed: do runtime type checking in default method
See 596813c.
1 parent b431d27 commit 133142a

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/main/java/org/scijava/Typed.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@ public interface Typed<T> {
4242
/**
4343
* Gets whether this object is compatible with the given data object.
4444
* <p>
45-
* By default, this method will return {@code true} always, since the type is
46-
* known to be compatible. But individual implementations may have other
47-
* requirements beyond class assignability.
45+
* By default, this method will return {@code true} iff the data is assignable
46+
* to the associated type given by {@link #getType()}. But individual
47+
* implementations may have other requirements beyond class assignability.
4848
* </p>
4949
*/
50-
default boolean supports(@SuppressWarnings("unused") T data) {
51-
return true;
50+
default boolean supports(final T data) {
51+
// NB: Even though the compiler will often guarantee that only data
52+
// of type T is provided here, we still need the runtime check
53+
// for cases where the exact type is not known to compiler --
54+
// e.g., if the object was manufactured by reflection.
55+
return getType().isInstance(data);
5256
}
5357

5458
/** Gets the type associated with the object. */

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ public abstract class AbstractTypedPlugin<D> extends AbstractRichPlugin
4747

4848
@Override
4949
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);
50+
// NB: Overridden just for backwards compatibility, so that
51+
// downstream classes which call super.supports continue to work.
52+
return TypedPlugin.super.supports(data);
5553
}
56-
5754
}

0 commit comments

Comments
 (0)