Search in sources :

Example 21 with EvaluationException

use of cn.taketoday.expression.EvaluationException in project today-infrastructure by TAKETODAY.

the class ExpressionLanguageScenarioTests method testScenario_UsingStandardInfrastructure.

/**
 * Scenario: using the standard infrastructure and running simple expression evaluation.
 */
@Test
public void testScenario_UsingStandardInfrastructure() {
    try {
        // Create a parser
        SpelExpressionParser parser = new SpelExpressionParser();
        // Parse an expression
        Expression expr = parser.parseRaw("new String('hello world')");
        // Evaluate it using a 'standard' context
        Object value = expr.getValue();
        // They are reusable
        value = expr.getValue();
        assertThat(value).isEqualTo("hello world");
        assertThat(value.getClass()).isEqualTo(String.class);
    } catch (EvaluationException | ParseException ex) {
        throw new AssertionError(ex.getMessage(), ex);
    }
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) EvaluationException(cn.taketoday.expression.EvaluationException) ParseException(cn.taketoday.expression.ParseException) Test(org.junit.jupiter.api.Test)

Example 22 with EvaluationException

use of cn.taketoday.expression.EvaluationException in project today-infrastructure by TAKETODAY.

the class FunctionReference method executeFunctionJLRMethod.

/**
 * Execute a function represented as a {@code java.lang.reflect.Method}.
 *
 * @param state the expression evaluation state
 * @param method the method to invoke
 * @return the return value of the invoked Java method
 * @throws EvaluationException if there is any problem invoking the method
 */
private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException {
    Object[] functionArgs = getArguments(state);
    if (!method.isVarArgs()) {
        int declaredParamCount = method.getParameterCount();
        if (declaredParamCount != functionArgs.length) {
            throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, functionArgs.length, declaredParamCount);
        }
    }
    if (!Modifier.isStatic(method.getModifiers())) {
        throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_MUST_BE_STATIC, ClassUtils.getQualifiedMethodName(method), this.name);
    }
    // Convert arguments if necessary and remap them for varargs if required
    TypeConverter converter = state.getEvaluationContext().getTypeConverter();
    boolean argumentConversionOccurred = ReflectionHelper.convertAllArguments(converter, functionArgs, method);
    if (method.isVarArgs()) {
        functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(method.getParameterTypes(), functionArgs);
    }
    boolean compilable = false;
    try {
        ReflectionUtils.makeAccessible(method);
        Object result = method.invoke(method.getClass(), functionArgs);
        compilable = !argumentConversionOccurred;
        return new TypedValue(result, new TypeDescriptor(new MethodParameter(method, -1)).narrow(result));
    } catch (Exception ex) {
        throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL, this.name, ex.getMessage());
    } finally {
        if (compilable) {
            this.exitTypeDescriptor = CodeFlow.toDescriptor(method.getReturnType());
            this.method = method;
        } else {
            this.exitTypeDescriptor = null;
            this.method = null;
        }
    }
}
Also used : TypeConverter(cn.taketoday.expression.TypeConverter) SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) TypeDescriptor(cn.taketoday.core.TypeDescriptor) MethodParameter(cn.taketoday.core.MethodParameter) EvaluationException(cn.taketoday.expression.EvaluationException) SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) TypedValue(cn.taketoday.expression.TypedValue)

Aggregations

EvaluationException (cn.taketoday.expression.EvaluationException)22 Expression (cn.taketoday.expression.Expression)14 Test (org.junit.jupiter.api.Test)14 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)12 ArrayList (java.util.ArrayList)10 MethodParameter (cn.taketoday.core.MethodParameter)6 TypeDescriptor (cn.taketoday.core.TypeDescriptor)6 ParseException (cn.taketoday.expression.ParseException)6 TypeConverter (cn.taketoday.expression.TypeConverter)6 AccessException (cn.taketoday.expression.AccessException)4 SpelEvaluationException (cn.taketoday.expression.spel.SpelEvaluationException)4 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)4 Nullable (cn.taketoday.lang.Nullable)4 MethodFilter (cn.taketoday.expression.MethodFilter)2 OperatorOverloader (cn.taketoday.expression.OperatorOverloader)2 TypedValue (cn.taketoday.expression.TypedValue)2 Constructor (java.lang.reflect.Constructor)2 Method (java.lang.reflect.Method)2 LinkedHashSet (java.util.LinkedHashSet)2