|
19 | 19 | import org.springframework.core.convert.TypeDescriptor; |
20 | 20 | import org.springframework.expression.EvaluationContext; |
21 | 21 | import org.springframework.expression.EvaluationException; |
| 22 | +import org.springframework.expression.TypeConverter; |
22 | 23 | import org.springframework.expression.TypedValue; |
23 | 24 | import org.springframework.util.ClassUtils; |
24 | 25 |
|
@@ -70,4 +71,68 @@ public static <T> T convertTypedValue(EvaluationContext context, TypedValue type |
70 | 71 | throw new EvaluationException("Cannot convert value '" + value + "' to type '" + targetType.getName() + "'"); |
71 | 72 | } |
72 | 73 |
|
| 74 | + /** |
| 75 | + * Attempt to convert a typed value to an int using the supplied type converter. |
| 76 | + */ |
| 77 | + public static int toInt(TypeConverter typeConverter, TypedValue typedValue) { |
| 78 | + return (Integer) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(), |
| 79 | + TypeDescriptor.valueOf(Integer.class)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Attempt to convert a typed value to a boolean using the supplied type converter. |
| 84 | + */ |
| 85 | + public static boolean toBoolean(TypeConverter typeConverter, TypedValue typedValue) { |
| 86 | + return (Boolean) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(), |
| 87 | + TypeDescriptor.valueOf(Boolean.class)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Attempt to convert a typed value to a double using the supplied type converter. |
| 92 | + */ |
| 93 | + public static double toDouble(TypeConverter typeConverter, TypedValue typedValue) { |
| 94 | + return (Double) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(), |
| 95 | + TypeDescriptor.valueOf(Double.class)); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Attempt to convert a typed value to a long using the supplied type converter. |
| 100 | + */ |
| 101 | + public static long toLong(TypeConverter typeConverter, TypedValue typedValue) { |
| 102 | + return (Long) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(), TypeDescriptor |
| 103 | + .valueOf(Long.class)); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Attempt to convert a typed value to a char using the supplied type converter. |
| 108 | + */ |
| 109 | + public static char toChar(TypeConverter typeConverter, TypedValue typedValue) { |
| 110 | + return (Character) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(), |
| 111 | + TypeDescriptor.valueOf(Character.class)); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Attempt to convert a typed value to a short using the supplied type converter. |
| 116 | + */ |
| 117 | + public static short toShort(TypeConverter typeConverter, TypedValue typedValue) { |
| 118 | + return (Short) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(), TypeDescriptor |
| 119 | + .valueOf(Short.class)); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Attempt to convert a typed value to a float using the supplied type converter. |
| 124 | + */ |
| 125 | + public static float toFloat(TypeConverter typeConverter, TypedValue typedValue) { |
| 126 | + return (Float) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(), TypeDescriptor |
| 127 | + .valueOf(Float.class)); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Attempt to convert a typed value to a byte using the supplied type converter. |
| 132 | + */ |
| 133 | + public static byte toByte(TypeConverter typeConverter, TypedValue typedValue) { |
| 134 | + return (Byte) typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(), TypeDescriptor |
| 135 | + .valueOf(Byte.class)); |
| 136 | + } |
| 137 | + |
73 | 138 | } |
0 commit comments