Skip to content

Commit f5ced9b

Browse files
committed
SPR-7335: support for expression inline lists and array construction
1 parent 88560fd commit f5ced9b

File tree

15 files changed

+991
-160
lines changed

15 files changed

+991
-160
lines changed

org.springframework.expression/src/main/java/org/springframework/expression/common/ExpressionUtils.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.core.convert.TypeDescriptor;
2020
import org.springframework.expression.EvaluationContext;
2121
import org.springframework.expression.EvaluationException;
22+
import org.springframework.expression.TypeConverter;
2223
import org.springframework.expression.TypedValue;
2324
import org.springframework.util.ClassUtils;
2425

@@ -70,4 +71,68 @@ public static <T> T convertTypedValue(EvaluationContext context, TypedValue type
7071
throw new EvaluationException("Cannot convert value '" + value + "' to type '" + targetType.getName() + "'");
7172
}
7273

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+
73138
}

org.springframework.expression/src/main/java/org/springframework/expression/spel/SpelMessage.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ public enum SpelMessage {
9595
NO_BEAN_RESOLVER_REGISTERED(Kind.ERROR,1057,"No bean resolver registered in the context to resolve access to bean ''{0}''"),//
9696
EXCEPTION_DURING_BEAN_RESOLUTION(Kind.ERROR, 1058, "A problem occurred when trying to resolve bean ''{0}'':''{1}''"), //
9797
INVALID_BEAN_REFERENCE(Kind.ERROR,1059,"@ can only be followed by an identifier or a quoted name"),//
98+
TYPE_NAME_EXPECTED_FOR_ARRAY_CONSTRUCTION(Kind.ERROR, 1060,
99+
"Expected the type of the new array to be specified as a String but found ''{0}''"), //
100+
INCORRECT_ELEMENT_TYPE_FOR_ARRAY(Kind.ERROR, 1061,
101+
"The array of type ''{0}'' cannot have an element of type ''{1}'' inserted"), //
102+
MULTIDIM_ARRAY_INITIALIZER_NOT_SUPPORTED(Kind.ERROR, 1062,
103+
"Using an initializer to build a multi-dimensional array is not currently supported"), //
104+
MISSING_ARRAY_DIMENSION(Kind.ERROR, 1063, "A required array dimension has not been specified"), //
105+
INITIALIZER_LENGTH_INCORRECT(
106+
Kind.ERROR, 1064, "array initializer size does not match array dimensions"), //
98107
;
99108

100109
private Kind kind;

0 commit comments

Comments
 (0)