use of cn.taketoday.expression.MethodExpression 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.MethodExpression in project today-framework by TAKETODAY.
the class OverloadedMethodTests method testMethodExprInvokingWithoutArgs.
/**
* JSF may invoke MethodExpression which has parameter declared, but pass in no
* arguments (not null).
*/
@Test
void testMethodExprInvokingWithoutArgs() {
MethodExpression methodExpr = exprFactory.createMethodExpression(elContext, "${foo.methodForMethodExpr}", String.class, new Class<?>[] { String.class });
Object invoke = methodExpr.invoke(elContext, new Object[0]);
System.err.println(invoke);
assertNull(invoke);
}
use of cn.taketoday.expression.MethodExpression in project today-framework by TAKETODAY.
the class AstIdentifier method getMethodExpression.
private MethodExpression getMethodExpression(EvaluationContext ctx) {
// case A: ValueExpression exists, getValue which must be a MethodExpression
Object obj = null;
VariableMapper varMapper = ctx.getVariableMapper();
ValueExpression ve = null;
if (varMapper != null) {
ve = varMapper.resolveVariable(image);
if (ve != null) {
obj = ve.getValue(ctx);
}
}
// case B: evaluate the identity against the ELResolver, again, must be a MethodExpression to be able to invoke
if (ve == null) {
ctx.setPropertyResolved(false);
obj = ctx.getResolver().getValue(ctx, null, image);
}
// finally provide helpful hints
if (obj instanceof MethodExpression) {
return (MethodExpression) obj;
} else if (obj == null) {
throw new MethodNotFoundException("Identity '" + image + "' was null and was unable to invoke");
}
throw new ExpressionException("Identity '" + image + "' does not reference a MethodExpression instance, returned type: " + obj.getClass().getName());
}
use of cn.taketoday.expression.MethodExpression in project today-framework by TAKETODAY.
the class ELProcessorTests method testMethExpr.
@Test
void testMethExpr() {
MethodExpression meth = null;
ExpressionContext ctxt = elm.getContext();
try {
meth = factory.createMethodExpression(ctxt, "#{str.length}", Object.class, null);
} catch (NullPointerException ex) {
// Do nothing
}
assertTrue(meth == null);
meth = factory.createMethodExpression(ctxt, "#{'abc'.length()}", Object.class, null);
Object result = meth.invoke(ctxt, new Object[] { "abcde" });
System.out.println("'abc'.length() called, equals " + result);
assertEquals(result, 3);
}
Aggregations