Search in sources :

Example 1 with ExpressionException

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

the class SpelParserTests method exceptions.

@Test
void exceptions() {
    ExpressionException exprEx = new ExpressionException("test");
    assertThat(exprEx.getSimpleMessage()).isEqualTo("test");
    assertThat(exprEx.toDetailedString()).isEqualTo("test");
    assertThat(exprEx.getMessage()).isEqualTo("test");
    exprEx = new ExpressionException("wibble", "test");
    assertThat(exprEx.getSimpleMessage()).isEqualTo("test");
    assertThat(exprEx.toDetailedString()).isEqualTo("Expression [wibble]: test");
    assertThat(exprEx.getMessage()).isEqualTo("Expression [wibble]: test");
    exprEx = new ExpressionException("wibble", 3, "test");
    assertThat(exprEx.getSimpleMessage()).isEqualTo("test");
    assertThat(exprEx.toDetailedString()).isEqualTo("Expression [wibble] @3: test");
    assertThat(exprEx.getMessage()).isEqualTo("Expression [wibble] @3: test");
}
Also used : ExpressionException(cn.taketoday.expression.ExpressionException) Test(org.junit.jupiter.api.Test)

Example 2 with ExpressionException

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

the class SpelReproTests method checkTemplateParsingError.

private void checkTemplateParsingError(String expression, ParserContext context, String expectedMessage) {
    SpelExpressionParser parser = new SpelExpressionParser();
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> parser.parseExpression(expression, context)).satisfies(ex -> {
        String message = ex.getMessage();
        if (ex instanceof ExpressionException) {
            message = ((ExpressionException) ex).getSimpleMessage();
        }
        assertThat(message).isEqualTo(expectedMessage);
    });
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) ExpressionException(cn.taketoday.expression.ExpressionException)

Example 3 with ExpressionException

use of cn.taketoday.expression.ExpressionException in project today-framework by TAKETODAY.

the class OverloadedMethodTests method testMethodExprInvoking.

@Test
void testMethodExprInvoking() {
    final Class<?>[] classes = { Runnable.class };
    MethodExpression methodExpr = exprFactory.createMethodExpression(elContext, "${foo.methodForMethodExpr2}", String.class, classes);
    assertEquals("Runnable", methodExpr.invoke(elContext, new Object[] { Thread.currentThread() }));
    try {
        Object invoke = methodExpr.invoke(elContext, new Object[] { "foo" });
        System.err.println(invoke);
        fail("testMethodExprInvoking Failed");
    } catch (ExpressionException e) {
        System.out.println("The following is an expected exception:");
        e.printStackTrace(System.out);
    }
}
Also used : MethodExpression(cn.taketoday.expression.MethodExpression) ExpressionException(cn.taketoday.expression.ExpressionException) Test(org.junit.jupiter.api.Test)

Example 4 with ExpressionException

use of cn.taketoday.expression.ExpressionException in project today-framework by TAKETODAY.

the class AstFunction method getValue.

@Override
public Object getValue(EvaluationContext ctx) throws ExpressionException {
    final Node[] children = this.children;
    final String localName = this.localName;
    // can return another Lambda expression.
    if (prefix.isEmpty()) {
        Object val = findValue(ctx, localName);
        // Check the case of repeated lambda invocation, such as f()()()
        if (val instanceof LambdaExpression) {
            for (final Node child : children) {
                Object[] params = ((AstMethodArguments) child).getParameters(ctx);
                if (!(val instanceof LambdaExpression)) {
                    throw new ExpressionException("Syntax error in calling function ''" + getOutputName() + "''");
                }
                val = ((LambdaExpression) val).invoke(ctx, params);
            }
            return val;
        }
    }
    FunctionMapper fnMapper = ctx.getFunctionMapper();
    Method m = null;
    if (fnMapper != null) {
        m = fnMapper.resolveFunction(prefix, localName);
    }
    if (m == null) {
        if (prefix.isEmpty() && ctx.getImportHandler() != null) {
            // Check if this is a constructor call for an imported class
            Class<?> c = ctx.getImportHandler().resolveClass(localName);
            String methodName = null;
            if (c != null) {
                methodName = MethodSignature.CONSTRUCTOR_NAME;
            } else {
                // Check if this is a imported static method
                c = ctx.getImportHandler().resolveStatic(localName);
                methodName = localName;
            }
            if (c != null) {
                // Use StaticFieldELResolver to invoke the constructor or the static method.
                final Object[] params = ((AstMethodArguments) children[0]).getParameters(ctx);
                return ctx.getResolver().invoke(ctx, c, methodName, null, params);
            }
        }
        // quickly validate for this request
        throw new ExpressionException("Function ''" + this.getOutputName() + "'' not found");
    }
    final Class<?>[] paramTypes = m.getParameterTypes();
    final Object[] params = ((AstMethodArguments) children[0]).getParameters(ctx);
    try {
        for (int i = 0; i < params.length; i++) {
            params[i] = ctx.convertToType(params[i], paramTypes[i]);
        }
    } catch (ExpressionException ele) {
        throw new ExpressionException("Problems calling function '" + this.getOutputName() + "'", ele);
    }
    try {
        // static method
        return m.invoke(null, params);
    } catch (IllegalAccessException iae) {
        throw new ExpressionException("Problems calling function '" + this.getOutputName() + "'", iae);
    } catch (InvocationTargetException ite) {
        throw new ExpressionException("Problems calling function '" + this.getOutputName() + "'", ite.getCause());
    }
}
Also used : Method(java.lang.reflect.Method) ExpressionException(cn.taketoday.expression.ExpressionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) LambdaExpression(cn.taketoday.expression.LambdaExpression) FunctionMapper(cn.taketoday.expression.FunctionMapper)

Example 5 with ExpressionException

use of cn.taketoday.expression.ExpressionException in project today-framework by TAKETODAY.

the class CachedExpressionBuilder method getNode.

public static Node getNode(final String expr) throws ExpressionException {
    if (expr == null) {
        throw new ExpressionException("Expression cannot be null");
    }
    Node node = EXPRESSION_CACHE.get(expr);
    if (node == null) {
        node = parse(expr);
        EXPRESSION_CACHE.put(expr, node);
    }
    return node;
}
Also used : Node(cn.taketoday.expression.parser.Node) ExpressionException(cn.taketoday.expression.ExpressionException)

Aggregations

ExpressionException (cn.taketoday.expression.ExpressionException)12 Method (java.lang.reflect.Method)3 Test (org.junit.jupiter.api.Test)3 FunctionMapper (cn.taketoday.expression.FunctionMapper)2 LambdaExpression (cn.taketoday.expression.LambdaExpression)2 MethodExpression (cn.taketoday.expression.MethodExpression)2 ValueExpression (cn.taketoday.expression.ValueExpression)2 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)2 MethodNotFoundException (cn.taketoday.expression.MethodNotFoundException)1 ValueExpressionImpl (cn.taketoday.expression.ValueExpressionImpl)1 VariableMapper (cn.taketoday.expression.VariableMapper)1 Node (cn.taketoday.expression.parser.Node)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1