@@ -41,7 +41,7 @@ public abstract class ExpressionUtils {
4141 * @param targetType the type to attempt conversion to
4242 * @return the converted value
4343 * @throws EvaluationException if there is a problem during conversion or conversion
44- * of the value to the specified type is not supported
44+ * of the value to the specified type is not supported
4545 * @deprecated use {@link #convertTypedValue(EvaluationContext, TypedValue, Class)}
4646 */
4747 @ Deprecated
@@ -58,16 +58,20 @@ public static <T> T convert(EvaluationContext context, Object value, Class<T> ta
5858 * @param targetType the type to attempt conversion to
5959 * @return the converted value
6060 * @throws EvaluationException if there is a problem during conversion or conversion
61- * of the value to the specified type is not supported
61+ * of the value to the specified type is not supported
6262 */
6363 @ SuppressWarnings ("unchecked" )
6464 public static <T > T convertTypedValue (EvaluationContext context , TypedValue typedValue , Class <T > targetType ) {
6565 Object value = typedValue .getValue ();
66- if (( targetType == null ) || ( value != null && ClassUtils . isAssignableValue ( targetType , value )) ) {
66+ if (targetType == null ) {
6767 return (T ) value ;
6868 }
6969 if (context != null ) {
70- return (T ) context .getTypeConverter ().convertValue (value , typedValue .getTypeDescriptor (), TypeDescriptor .valueOf (targetType ));
70+ return (T ) context .getTypeConverter ().convertValue (
71+ value , typedValue .getTypeDescriptor (), TypeDescriptor .valueOf (targetType ));
72+ }
73+ if (ClassUtils .isAssignableValue (targetType , value )) {
74+ return (T ) value ;
7175 }
7276 throw new EvaluationException ("Cannot convert value '" + value + "' to type '" + targetType .getName () + "'" );
7377 }
@@ -100,8 +104,8 @@ public static double toDouble(TypeConverter typeConverter, TypedValue typedValue
100104 * Attempt to convert a typed value to a long using the supplied type converter.
101105 */
102106 public static long toLong (TypeConverter typeConverter , TypedValue typedValue ) {
103- return (Long ) typeConverter .convertValue (typedValue .getValue (), typedValue .getTypeDescriptor (), TypeDescriptor
104- .valueOf (Long .class ));
107+ return (Long ) typeConverter .convertValue (typedValue .getValue (), typedValue .getTypeDescriptor (),
108+ TypeDescriptor .valueOf (Long .class ));
105109 }
106110
107111 /**
@@ -116,24 +120,24 @@ public static char toChar(TypeConverter typeConverter, TypedValue typedValue) {
116120 * Attempt to convert a typed value to a short using the supplied type converter.
117121 */
118122 public static short toShort (TypeConverter typeConverter , TypedValue typedValue ) {
119- return (Short ) typeConverter .convertValue (typedValue .getValue (), typedValue .getTypeDescriptor (), TypeDescriptor
120- .valueOf (Short .class ));
123+ return (Short ) typeConverter .convertValue (typedValue .getValue (), typedValue .getTypeDescriptor (),
124+ TypeDescriptor .valueOf (Short .class ));
121125 }
122126
123127 /**
124128 * Attempt to convert a typed value to a float using the supplied type converter.
125129 */
126130 public static float toFloat (TypeConverter typeConverter , TypedValue typedValue ) {
127- return (Float ) typeConverter .convertValue (typedValue .getValue (), typedValue .getTypeDescriptor (), TypeDescriptor
128- .valueOf (Float .class ));
131+ return (Float ) typeConverter .convertValue (typedValue .getValue (), typedValue .getTypeDescriptor (),
132+ TypeDescriptor .valueOf (Float .class ));
129133 }
130134
131135 /**
132136 * Attempt to convert a typed value to a byte using the supplied type converter.
133137 */
134138 public static byte toByte (TypeConverter typeConverter , TypedValue typedValue ) {
135- return (Byte ) typeConverter .convertValue (typedValue .getValue (), typedValue .getTypeDescriptor (), TypeDescriptor
136- .valueOf (Byte .class ));
139+ return (Byte ) typeConverter .convertValue (typedValue .getValue (), typedValue .getTypeDescriptor (),
140+ TypeDescriptor .valueOf (Byte .class ));
137141 }
138142
139143}
0 commit comments