Skip to content

Commit 01e4295

Browse files
committed
Deprecate (Class, ...) conversion methods
We would like to move away from these signatures as the process of conversion implies there is an actual source Object to convert from, which makes these signatures confusing. They should be unnecessary, and thus removed in SJC 3.0. This allows us to focus the functionality of the NullConverter, such that its canConvert signatures always return true for a null Object, and false for anything else. This requires a temporary stop-gap of restoring some null class support in the DefaultConverter, but only in deprecated methods.
1 parent 0c7956a commit 01e4295

File tree

5 files changed

+130
-93
lines changed

5 files changed

+130
-93
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public abstract class AbstractConverter<I, O> extends
6161

6262
// -- ConversionHandler methods --
6363

64+
@SuppressWarnings("deprecation")
6465
@Override
6566
public boolean canConvert(final ConversionRequest request) {
6667
Object src = request.sourceObject();
@@ -74,19 +75,14 @@ public boolean canConvert(final ConversionRequest request) {
7475
return canConvert(src, request.destClass());
7576
}
7677

77-
@Override
78-
public boolean canConvert(final Class<?> src, final Type dest) {
79-
final Class<?> destClass = GenericUtils.getClass(dest);
80-
return canConvert(src, destClass);
81-
}
82-
8378
@Override
8479
public boolean canConvert(final Object src, final Type dest) {
8580
if (src == null) return false;
8681
final Class<?> srcClass = src.getClass();
8782
return canConvert(srcClass, dest);
8883
}
8984

85+
@SuppressWarnings("deprecation")
9086
@Override
9187
public boolean canConvert(final Object src, final Class<?> dest) {
9288
if (src == null) return false;
@@ -126,4 +122,13 @@ public boolean supports(final ConversionRequest request) {
126122
public Class<ConversionRequest> getType() {
127123
return ConversionRequest.class;
128124
}
125+
126+
// -- Deprecated API --
127+
128+
@Override
129+
@Deprecated
130+
public boolean canConvert(final Class<?> src, final Type dest) {
131+
final Class<?> destClass = GenericUtils.getClass(dest);
132+
return canConvert(src, destClass);
133+
}
129134
}

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,36 +71,46 @@ public interface ConvertService extends
7171
/**
7272
* @see #getHandler(ConversionRequest)
7373
*/
74-
Converter<?, ?> getHandler(Class<?> src, Class<?> dest);
74+
Converter<?, ?> getHandler(Object src, Type dest);
7575

7676
/**
77-
* @see #getHandler(ConversionRequest)
77+
* @see #supports(ConversionRequest)
7878
*/
79-
Converter<?, ?> getHandler(Object src, Type dest);
79+
boolean supports(Object src, Class<?> dest);
8080

8181
/**
82-
* @see #getHandler(ConversionRequest)
82+
* @see #supports(ConversionRequest)
8383
*/
84-
Converter<?, ?> getHandler(Class<?> src, Type dest);
84+
boolean supports(Object src, Type dest);
85+
86+
// -- Deprecated API --
8587

8688
/**
87-
* @see #supports(ConversionRequest)
89+
* @see #getHandler(ConversionRequest)
90+
* @deprecated Use {@link #getHandler(Object, Class)}
8891
*/
89-
boolean supports(Object src, Class<?> dest);
92+
@Deprecated
93+
Converter<?, ?> getHandler(Class<?> src, Class<?> dest);
9094

9195
/**
92-
* @see #supports(ConversionRequest)
96+
* @see #getHandler(ConversionRequest)
97+
* @deprecated Use {@link #getHandler(Object, Type)}
9398
*/
94-
boolean supports(Class<?> src, Class<?> dest);
99+
@Deprecated
100+
Converter<?, ?> getHandler(Class<?> src, Type dest);
95101

96102
/**
97103
* @see #supports(ConversionRequest)
104+
* @deprecated Use {@link #supports(Object, Class)}
98105
*/
99-
boolean supports(Object src, Type dest);
106+
@Deprecated
107+
boolean supports(Class<?> src, Class<?> dest);
100108

101109
/**
102110
* @see #supports(ConversionRequest)
111+
* @deprecated Use {@link #supports(Object, Type)}
103112
*/
113+
@Deprecated
104114
boolean supports(Class<?> src, Type dest);
105115

106116
Collection<Object> getCompatibleInputs(Class<?> dest);

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,6 @@ public interface Converter<I, O> extends HandlerPlugin<ConversionRequest>
6161
*/
6262
boolean canConvert(ConversionRequest request);
6363

64-
/**
65-
* Checks whether objects of the given class can be converted to the specified
66-
* type.
67-
*
68-
* @see #convert(Object, Type)
69-
*/
70-
boolean canConvert(Class<?> src, Type dest);
71-
72-
/**
73-
* Checks whether objects of the given class can be converted to the specified
74-
* type.
75-
*
76-
* @see #convert(Object, Class)
77-
*/
78-
boolean canConvert(Class<?> src, Class<?> dest);
79-
8064
/**
8165
* Checks whether the given object's type can be converted to the specified
8266
* type.
@@ -189,4 +173,26 @@ public interface Converter<I, O> extends HandlerPlugin<ConversionRequest>
189173
* @return The base {@code Class} this {@code Converter} accepts as input.
190174
*/
191175
Class<I> getInputType();
176+
177+
// -- Deprecated API --
178+
179+
/**
180+
* Checks whether objects of the given class can be converted to the specified
181+
* type.
182+
*
183+
* @see #convert(Object, Type)
184+
* @deprecated Use {@link #canConvert(Object, Type)}
185+
*/
186+
@Deprecated
187+
boolean canConvert(Class<?> src, Type dest);
188+
189+
/**
190+
* Checks whether objects of the given class can be converted to the specified
191+
* type.
192+
*
193+
* @see #convert(Object, Class)
194+
* @deprecated Use {@link #canConvert(Object, Class)}
195+
*/
196+
@Deprecated
197+
boolean canConvert(Class<?> src, Class<?> dest);
192198
}

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

Lines changed: 64 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -59,61 +59,6 @@ public class DefaultConverter extends AbstractConverter<Object, Object> {
5959

6060
// -- ConversionHandler methods --
6161

62-
@Override
63-
public boolean canConvert(final Class<?> src, final Type dest) {
64-
65-
// Handle array types, including generic array types.
66-
if (isArray(dest)) return true;
67-
68-
// Handle parameterized collection types.
69-
if (dest instanceof ParameterizedType && isCollection(dest)) {
70-
return createCollection(GenericUtils.getClass(dest)) != null;
71-
}
72-
73-
return super.canConvert(src, dest);
74-
}
75-
76-
@Override
77-
public boolean canConvert(final Class<?> src, final Class<?> dest) {
78-
// ensure type is well-behaved, rather than a primitive type
79-
final Class<?> saneDest = ConversionUtils.getNonprimitiveType(dest);
80-
81-
// OK if the existing object can be casted
82-
if (ConversionUtils.canCast(src, saneDest)) return true;
83-
84-
// OK for numerical conversions
85-
if (ConversionUtils.canCast(ConversionUtils.getNonprimitiveType(src),
86-
Number.class) &&
87-
(ClassUtils.isByte(dest) || ClassUtils.isDouble(dest) ||
88-
ClassUtils.isFloat(dest) || ClassUtils.isInteger(dest) ||
89-
ClassUtils.isLong(dest) || ClassUtils.isShort(dest)))
90-
{
91-
return true;
92-
}
93-
94-
// OK if string
95-
if (saneDest == String.class) return true;
96-
97-
if (ConversionUtils.canCast(src, String.class)) {
98-
// OK if source type is string and destination type is character
99-
// (in this case, the first character of the string would be used)
100-
if (saneDest == Character.class) return true;
101-
102-
// OK if source type is string and destination type is an enum
103-
if (dest.isEnum()) return true;
104-
}
105-
106-
// OK if appropriate wrapper constructor exists
107-
try {
108-
return getConstructor(saneDest, src) != null;
109-
}
110-
catch (final Exception exc) {
111-
// TODO: Best not to catch blanket Exceptions here.
112-
// no known way to convert
113-
return false;
114-
}
115-
}
116-
11762
@Override
11863
public Object convert(final Object src, final Type dest) {
11964
// NB: Regardless of whether the destination type is an array or collection,
@@ -144,6 +89,8 @@ public Object convert(final Object src, final Type dest) {
14489

14590
@Override
14691
public <T> T convert(final Object src, final Class<T> dest) {
92+
if (dest == null) return null;
93+
if (src == null) return ConversionUtils.getNullValue(dest);
14794

14895
// ensure type is well-behaved, rather than a primitive type
14996
final Class<T> saneDest = ConversionUtils.getNonprimitiveType(dest);
@@ -334,4 +281,66 @@ private Collection<Object> createCollection(final Class<?> type) {
334281
return null;
335282
}
336283
}
284+
285+
// -- Deprecated API --
286+
287+
@Override
288+
@Deprecated
289+
public boolean canConvert(final Class<?> src, final Type dest) {
290+
291+
// Handle array types, including generic array types.
292+
if (isArray(dest)) return true;
293+
294+
// Handle parameterized collection types.
295+
if (dest instanceof ParameterizedType && isCollection(dest)) {
296+
return createCollection(GenericUtils.getClass(dest)) != null;
297+
}
298+
299+
return super.canConvert(src, dest);
300+
}
301+
302+
@Override
303+
@Deprecated
304+
public boolean canConvert(final Class<?> src, final Class<?> dest) {
305+
306+
if (src == null || dest == null) return true;
307+
308+
// ensure type is well-behaved, rather than a primitive type
309+
final Class<?> saneDest = ConversionUtils.getNonprimitiveType(dest);
310+
311+
// OK if the existing object can be casted
312+
if (ConversionUtils.canCast(src, saneDest)) return true;
313+
314+
// OK for numerical conversions
315+
if (ConversionUtils.canCast(ConversionUtils.getNonprimitiveType(src),
316+
Number.class) &&
317+
(ClassUtils.isByte(dest) || ClassUtils.isDouble(dest) ||
318+
ClassUtils.isFloat(dest) || ClassUtils.isInteger(dest) ||
319+
ClassUtils.isLong(dest) || ClassUtils.isShort(dest)))
320+
{
321+
return true;
322+
}
323+
324+
// OK if string
325+
if (saneDest == String.class) return true;
326+
327+
if (ConversionUtils.canCast(src, String.class)) {
328+
// OK if source type is string and destination type is character
329+
// (in this case, the first character of the string would be used)
330+
if (saneDest == Character.class) return true;
331+
332+
// OK if source type is string and destination type is an enum
333+
if (dest.isEnum()) return true;
334+
}
335+
336+
// OK if appropriate wrapper constructor exists
337+
try {
338+
return getConstructor(saneDest, src) != null;
339+
}
340+
catch (final Exception exc) {
341+
// TODO: Best not to catch blanket Exceptions here.
342+
// no known way to convert
343+
return false;
344+
}
345+
}
337346
}

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
* By running at {@link Priority#FIRST_PRIORITY}, other converters should
4444
* not need to worry about {@code null} source or destination parameters.
4545
* </p>
46+
* <p>
47+
* NB: if a {@link Class} source is queried for the {@link #canConvert},
48+
* this converter will always return false (as there is no way of knowing
49+
* if the source object will be null or not).
50+
* </p>
4651
*
4752
* @author Mark Hiner
4853
*/
@@ -51,12 +56,7 @@ public class NullConverter extends AbstractConverter<Object, Object> {
5156

5257
@Override
5358
public boolean canConvert(final Object src, final Class<?> dest) {
54-
return src == null || super.canConvert(src, dest);
55-
}
56-
57-
@Override
58-
public boolean canConvert(final Class<?> src, final Class<?> dest) {
59-
return src == null || dest == null;
59+
return src == null;
6060
}
6161

6262
@Override
@@ -78,4 +78,11 @@ public Class<Object> getInputType() {
7878
return Object.class;
7979
}
8080

81+
// -- Deprecated API --
82+
83+
@Override
84+
@Deprecated
85+
public boolean canConvert(final Class<?> src, final Class<?> dest) {
86+
return false;
87+
}
8188
}

0 commit comments

Comments
 (0)