From 0c0cda9815645f0864a87cd854158438e1e78117 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 27 Mar 2023 19:18:41 +0200 Subject: [PATCH] Polish contribution See gh-30189 --- .../spel/ast/ConstructorReference.java | 108 +++++++++--------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java index e044f54ea8c2..7b3a73596881 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java @@ -59,7 +59,7 @@ * @author Andy Clement * @author Juergen Hoeller * @author Sam Brannen - * @author Harry Yang + * @author Harry Yang * @since 3.0 */ public class ConstructorReference extends SpelNodeImpl { @@ -332,17 +332,17 @@ private TypedValue createArray(ExpressionState state) throws EvaluationException throw new SpelEvaluationException(getStartPosition(), SpelMessage.INITIALIZER_LENGTH_INCORRECT); } } - newArray = switch (arrayTypeCode) { case OBJECT -> createReferenceTypeArray(state, typeConverter, initializer.children, componentType); case BOOLEAN -> createBooleanArray(state, typeConverter, initializer.children); - case BYTE -> createByteArray(state, typeConverter, initializer.children); case CHAR -> createCharArray(state, typeConverter, initializer.children); - case DOUBLE -> createDoubleArray(state, typeConverter, initializer.children); - case FLOAT -> createFloatArray(state, typeConverter, initializer.children); + case BYTE -> createByteArray(state, typeConverter, initializer.children); + case SHORT -> createShortArray(state, typeConverter, initializer.children); case INT -> createIntArray(state, typeConverter, initializer.children); case LONG -> createLongArray(state, typeConverter, initializer.children); - case SHORT -> createShortArray(state, typeConverter, initializer.children); + case FLOAT -> createFloatArray(state, typeConverter, initializer.children); + case DOUBLE -> createDoubleArray(state, typeConverter, initializer.children); + default -> throw new IllegalStateException("Unsupported TypeCode: " + arrayTypeCode); }; } return new TypedValue(newArray); @@ -355,88 +355,88 @@ private void checkNumElements(long numElements) { } } - private Object createReferenceTypeArray(ExpressionState state, - TypeConverter typeConverter, SpelNodeImpl[] children, Class componentType) { - Object[] newArray = (Object[]) Array.newInstance(componentType, children.length); - TypeDescriptor toTypeDescriptor = TypeDescriptor.valueOf(componentType); - for (int i = 0; i < newArray.length; i++) { - Object arrayEntry = children[i].getValue(state); - newArray[i] = typeConverter.convertValue(arrayEntry, - TypeDescriptor.forObject(arrayEntry), toTypeDescriptor); + private Object createReferenceTypeArray(ExpressionState state, TypeConverter typeConverter, SpelNodeImpl[] children, + Class componentType) { + + Object[] array = (Object[]) Array.newInstance(componentType, children.length); + TypeDescriptor targetType = TypeDescriptor.valueOf(componentType); + for (int i = 0; i < array.length; i++) { + Object value = children[i].getValue(state); + array[i] = typeConverter.convertValue(value, TypeDescriptor.forObject(value), targetType); } - return newArray; + return array; } - private byte[] createByteArray(ExpressionState state, TypeConverter converter, SpelNodeImpl[] children) { - byte[] byteArray = new byte[children.length]; - for (int i = 0; i < byteArray.length; i++) { + private boolean[] createBooleanArray(ExpressionState state, TypeConverter typeConverter, SpelNodeImpl[] children) { + boolean[] array = new boolean[children.length]; + for (int i = 0; i < array.length; i++) { TypedValue typedValue = children[i].getTypedValue(state); - byteArray[i] = ExpressionUtils.toByte(converter, typedValue); + array[i] = ExpressionUtils.toBoolean(typeConverter, typedValue); } - return byteArray; + return array; } - private float[] createFloatArray(ExpressionState state, TypeConverter typeConverter, SpelNodeImpl[] children) { - float[] floatArray = new float[children.length]; - for (int i = 0; i < floatArray.length; i++) { + private char[] createCharArray(ExpressionState state, TypeConverter typeConverter, SpelNodeImpl[] children) { + char[] array = new char[children.length]; + for (int i = 0; i < array.length; i++) { TypedValue typedValue = children[i].getTypedValue(state); - floatArray[i] = ExpressionUtils.toFloat(typeConverter, typedValue); + array[i] = ExpressionUtils.toChar(typeConverter, typedValue); } - return floatArray; + return array; } - private double[] createDoubleArray(ExpressionState state, TypeConverter typeConverter, SpelNodeImpl[] children) { - double[] doubleArray = new double[children.length]; - for (int i = 0; i < doubleArray.length; i++) { + private byte[] createByteArray(ExpressionState state, TypeConverter converter, SpelNodeImpl[] children) { + byte[] array = new byte[children.length]; + for (int i = 0; i < array.length; i++) { TypedValue typedValue = children[i].getTypedValue(state); - doubleArray[i] = ExpressionUtils.toDouble(typeConverter, typedValue); + array[i] = ExpressionUtils.toByte(converter, typedValue); } - return doubleArray; + return array; } private short[] createShortArray(ExpressionState state, TypeConverter typeConverter, SpelNodeImpl[] children) { - short[] shortArray = new short[children.length]; - for (int i = 0; i < shortArray.length; i++) { + short[] array = new short[children.length]; + for (int i = 0; i < array.length; i++) { TypedValue typedValue = children[i].getTypedValue(state); - shortArray[i] = ExpressionUtils.toShort(typeConverter, typedValue); + array[i] = ExpressionUtils.toShort(typeConverter, typedValue); } - return shortArray; + return array; } - private long[] createLongArray(ExpressionState state, TypeConverter converter, SpelNodeImpl[] children) { - long[] longArray = new long[children.length]; - for (int i = 0; i < longArray.length; i++) { + private int[] createIntArray(ExpressionState state, TypeConverter typeConverter, SpelNodeImpl[] children) { + int[] array = new int[children.length]; + for (int i = 0; i < array.length; i++) { TypedValue typedValue = children[i].getTypedValue(state); - longArray[i] = ExpressionUtils.toLong(converter, typedValue); + array[i] = ExpressionUtils.toInt(typeConverter, typedValue); } - return longArray; + return array; } - private char[] createCharArray(ExpressionState state, TypeConverter typeConverter, SpelNodeImpl[] children) { - char[] newCharArray = new char[children.length]; - for (int i = 0; i < newCharArray.length; i++) { + private long[] createLongArray(ExpressionState state, TypeConverter converter, SpelNodeImpl[] children) { + long[] array = new long[children.length]; + for (int i = 0; i < array.length; i++) { TypedValue typedValue = children[i].getTypedValue(state); - newCharArray[i] = ExpressionUtils.toChar(typeConverter, typedValue); + array[i] = ExpressionUtils.toLong(converter, typedValue); } - return newCharArray; + return array; } - private boolean[] createBooleanArray(ExpressionState state, TypeConverter typeConverter, SpelNodeImpl[] children) { - boolean[] newBooleanArray = new boolean[children.length]; - for (int i = 0; i < newBooleanArray.length; i++) { + private float[] createFloatArray(ExpressionState state, TypeConverter typeConverter, SpelNodeImpl[] children) { + float[] array = new float[children.length]; + for (int i = 0; i < array.length; i++) { TypedValue typedValue = children[i].getTypedValue(state); - newBooleanArray[i] = ExpressionUtils.toBoolean(typeConverter, typedValue); + array[i] = ExpressionUtils.toFloat(typeConverter, typedValue); } - return newBooleanArray; + return array; } - private int[] createIntArray(ExpressionState state, TypeConverter typeConverter, SpelNodeImpl[] children) { - int[] intArray = new int[children.length]; - for (int i = 0; i < intArray.length; i++) { + private double[] createDoubleArray(ExpressionState state, TypeConverter typeConverter, SpelNodeImpl[] children) { + double[] array = new double[children.length]; + for (int i = 0; i < array.length; i++) { TypedValue typedValue = children[i].getTypedValue(state); - intArray[i] = ExpressionUtils.toInt(typeConverter, typedValue); + array[i] = ExpressionUtils.toDouble(typeConverter, typedValue); } - return intArray; + return array; } private boolean hasInitializer() {