Skip to content

Commit 6db9fe0

Browse files
committed
Create CastingConverter
Extract trivial casting logic from DefaultConverter to a new dedicated (high priority) CastingConverter. Fixes issues when trying to convert to, say, Object.
1 parent 584b538 commit 6db9fe0

File tree

2 files changed

+90
-11
lines changed

2 files changed

+90
-11
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
package org.scijava.convert;
32+
33+
import org.scijava.Priority;
34+
import org.scijava.plugin.Plugin;
35+
import org.scijava.util.ClassUtils;
36+
import org.scijava.util.ConversionUtils;
37+
import org.scijava.util.GenericUtils;
38+
39+
/**
40+
* Minimal {@link Converter} implementation to do direct casting.
41+
*
42+
* @author Mark Hiner hinerm at gmail.com
43+
*/
44+
@Plugin(type = Converter.class, priority = Priority.FIRST_PRIORITY - 1)
45+
public class CastingConverter extends AbstractConverter<Object, Object> {
46+
47+
@SuppressWarnings("deprecation")
48+
@Override
49+
public boolean canConvert(final Object src, final Class<?> dest) {
50+
return ClassUtils.canCast(src, dest);
51+
}
52+
53+
@Override
54+
public boolean canConvert(final Class<?> src, final Class<?> dest) {
55+
// OK if the existing object can be casted
56+
if (ConversionUtils.canCast(src, dest))
57+
return true;
58+
59+
return false;
60+
}
61+
62+
@SuppressWarnings("unchecked")
63+
@Override
64+
public <T> T convert(final Object src, final Class<T> dest) {
65+
// NB: Regardless of whether the destination type is an array or
66+
// collection,
67+
// we still want to cast directly if doing so is possible. But note that
68+
// in
69+
// general, this check does not detect cases of incompatible generic
70+
// parameter types. If this limitation becomes a problem in the future
71+
// we
72+
// can extend the logic here to provide additional signatures of canCast
73+
// which operate on Types in general rather than only Classes. However,
74+
// the
75+
// logic could become complex very quickly in various subclassing cases,
76+
// generic parameters resolved vs. propagated, etc.
77+
final Class<?> c = GenericUtils.getClass(dest);
78+
return (T) ConversionUtils.cast(src, c);
79+
}
80+
81+
@Override
82+
public Class<Object> getOutputType() {
83+
return Object.class;
84+
}
85+
86+
@Override
87+
public Class<Object> getInputType() {
88+
return Object.class;
89+
}
90+
}

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,6 @@ public class DefaultConverter extends AbstractConverter<Object, Object> {
6161

6262
@Override
6363
public Object convert(final Object src, final Type dest) {
64-
// NB: Regardless of whether the destination type is an array or collection,
65-
// we still want to cast directly if doing so is possible. But note that in
66-
// general, this check does not detect cases of incompatible generic
67-
// parameter types. If this limitation becomes a problem in the future we
68-
// can extend the logic here to provide additional signatures of canCast
69-
// which operate on Types in general rather than only Classes. However, the
70-
// logic could become complex very quickly in various subclassing cases,
71-
// generic parameters resolved vs. propagated, etc.
72-
final Class<?> c = GenericUtils.getClass(dest);
73-
if (c != null && ConversionUtils.canCast(src, c)) return ConversionUtils
74-
.cast(src, c);
7564

7665
// Handle array types, including generic array types.
7766
if (isArray(dest)) {

0 commit comments

Comments
 (0)