Search in sources :

Example 1 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class ConstructorReference method populateLongArray.

private void populateLongArray(ExpressionState state, Object newArray, TypeConverter typeConverter, InlineList initializer) {
    long[] newLongArray = (long[]) newArray;
    for (int i = 0; i < newLongArray.length; i++) {
        TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
        newLongArray[i] = ExpressionUtils.toLong(typeConverter, typedValue);
    }
}
Also used : TypedValue(org.springframework.expression.TypedValue)

Example 2 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class ConstructorReference method createNewInstance.

/**
	 * Create a new ordinary object and return it.
	 * @param state the expression state within which this expression is being evaluated
	 * @return the new object
	 * @throws EvaluationException if there is a problem creating the object
	 */
private TypedValue createNewInstance(ExpressionState state) throws EvaluationException {
    Object[] arguments = new Object[getChildCount() - 1];
    List<TypeDescriptor> argumentTypes = new ArrayList<>(getChildCount() - 1);
    for (int i = 0; i < arguments.length; i++) {
        TypedValue childValue = this.children[i + 1].getValueInternal(state);
        Object value = childValue.getValue();
        arguments[i] = value;
        argumentTypes.add(TypeDescriptor.forObject(value));
    }
    ConstructorExecutor executorToUse = this.cachedExecutor;
    if (executorToUse != null) {
        try {
            return executorToUse.execute(state.getEvaluationContext(), arguments);
        } catch (AccessException ex) {
            // Otherwise the constructor could not be invoked.
            if (ex.getCause() instanceof InvocationTargetException) {
                // User exception was the root cause - exit now
                Throwable rootCause = ex.getCause().getCause();
                if (rootCause instanceof RuntimeException) {
                    throw (RuntimeException) rootCause;
                } else {
                    String typeName = (String) this.children[0].getValueInternal(state).getValue();
                    throw new SpelEvaluationException(getStartPosition(), rootCause, SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typeName, FormatHelper.formatMethodForMessage("", argumentTypes));
                }
            }
            // At this point we know it wasn't a user problem so worth a retry if a better candidate can be found
            this.cachedExecutor = null;
        }
    }
    // Either there was no accessor or it no longer exists
    String typeName = (String) this.children[0].getValueInternal(state).getValue();
    executorToUse = findExecutorForConstructor(typeName, argumentTypes, state);
    try {
        this.cachedExecutor = executorToUse;
        if (this.cachedExecutor instanceof ReflectiveConstructorExecutor) {
            this.exitTypeDescriptor = CodeFlow.toDescriptor(((ReflectiveConstructorExecutor) this.cachedExecutor).getConstructor().getDeclaringClass());
        }
        return executorToUse.execute(state.getEvaluationContext(), arguments);
    } catch (AccessException ex) {
        throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typeName, FormatHelper.formatMethodForMessage("", argumentTypes));
    }
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) ReflectiveConstructorExecutor(org.springframework.expression.spel.support.ReflectiveConstructorExecutor) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) AccessException(org.springframework.expression.AccessException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ReflectiveConstructorExecutor(org.springframework.expression.spel.support.ReflectiveConstructorExecutor) ConstructorExecutor(org.springframework.expression.ConstructorExecutor) TypedValue(org.springframework.expression.TypedValue)

Example 3 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class ConstructorReference method populateByteArray.

private void populateByteArray(ExpressionState state, Object newArray, TypeConverter typeConverter, InlineList initializer) {
    byte[] newByteArray = (byte[]) newArray;
    for (int i = 0; i < newByteArray.length; i++) {
        TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
        newByteArray[i] = ExpressionUtils.toByte(typeConverter, typedValue);
    }
}
Also used : TypedValue(org.springframework.expression.TypedValue)

Example 4 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class ConstructorReference method populateIntArray.

private void populateIntArray(ExpressionState state, Object newArray, TypeConverter typeConverter, InlineList initializer) {
    int[] newIntArray = (int[]) newArray;
    for (int i = 0; i < newIntArray.length; i++) {
        TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
        newIntArray[i] = ExpressionUtils.toInt(typeConverter, typedValue);
    }
}
Also used : TypedValue(org.springframework.expression.TypedValue)

Example 5 with TypedValue

use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.

the class ConstructorReference method populateBooleanArray.

private void populateBooleanArray(ExpressionState state, Object newArray, TypeConverter typeConverter, InlineList initializer) {
    boolean[] newBooleanArray = (boolean[]) newArray;
    for (int i = 0; i < newBooleanArray.length; i++) {
        TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
        newBooleanArray[i] = ExpressionUtils.toBoolean(typeConverter, typedValue);
    }
}
Also used : TypedValue(org.springframework.expression.TypedValue)

Aggregations

TypedValue (org.springframework.expression.TypedValue)65 Test (org.junit.jupiter.api.Test)18 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)16 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)14 ExpressionState (org.springframework.expression.spel.ExpressionState)9 BigDecimal (java.math.BigDecimal)8 BigInteger (java.math.BigInteger)8 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)8 EvaluationContext (org.springframework.expression.EvaluationContext)7 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 AccessException (org.springframework.expression.AccessException)5 Expression (org.springframework.expression.Expression)4 CompiledExpression (org.springframework.expression.spel.CompiledExpression)4 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)4 MethodParameter (org.springframework.core.MethodParameter)3 EvaluationException (org.springframework.expression.EvaluationException)3 SpelNode (org.springframework.expression.spel.SpelNode)3 ReflectivePropertyAccessor (org.springframework.expression.spel.support.ReflectivePropertyAccessor)3 Nullable (org.springframework.lang.Nullable)3