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");
}
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);
});
}
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);
}
}
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());
}
}
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;
}
Aggregations