Skip to content

Commit d5575ac

Browse files
committed
AbstractConverter: can't convert null inputs
The default converter contract is that inputs can not be null, with the intent that individual converters can otherwise override this behavior. This contract is now consistent between all canConvert signatures, and has been clarified in the javadoc. See imagej/imagej-common#52
1 parent c049aae commit d5575ac

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/main/java/org/scijava/convert/AbstractConverter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@
5555
* implementation would like to suggest candidates for conversion, this method
5656
* can be overridden.
5757
* </p>
58+
* <p>
59+
* NB: by default, the provied {@link #canConvert} methods will return
60+
* {@code false} if the input is {@code null}. This allows {@link Converter}
61+
* implementors to assume any input is non-{@code null} - but this behavior is
62+
* overridden. Casting {@code null Object} inputs is handled by the
63+
* {@link NullConverter}, while {@code null class} inputs are handled by the
64+
* {@link DefaultConverter}.
65+
* </p>
5866
*
5967
* @author Mark Hiner
6068
*/
@@ -99,6 +107,7 @@ public boolean canConvert(final Object src, final Class<?> dest) {
99107

100108
@Override
101109
public boolean canConvert(final Class<?> src, final Class<?> dest) {
110+
if (src == null) return false;
102111
return ConversionUtils.canCast(src, getInputType()) &&
103112
ConversionUtils.canCast(getOutputType(), dest);
104113
}

0 commit comments

Comments
 (0)