Search in sources :

Example 1 with MethodExpression

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);
    }
}
Also used : MethodExpression(cn.taketoday.expression.MethodExpression) ExpressionException(cn.taketoday.expression.ExpressionException) Test(org.junit.jupiter.api.Test)

Example 2 with MethodExpression

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);
}
Also used : MethodExpression(cn.taketoday.expression.MethodExpression) Test(org.junit.jupiter.api.Test)

Example 3 with MethodExpression

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());
}
Also used : VariableMapper(cn.taketoday.expression.VariableMapper) ValueExpression(cn.taketoday.expression.ValueExpression) MethodExpression(cn.taketoday.expression.MethodExpression) MethodNotFoundException(cn.taketoday.expression.MethodNotFoundException) ExpressionException(cn.taketoday.expression.ExpressionException)

Example 4 with MethodExpression

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);
}
Also used : ExpressionContext(cn.taketoday.expression.ExpressionContext) MethodExpression(cn.taketoday.expression.MethodExpression) Test(org.junit.jupiter.api.Test)

Aggregations

MethodExpression (cn.taketoday.expression.MethodExpression)4 Test (org.junit.jupiter.api.Test)3 ExpressionException (cn.taketoday.expression.ExpressionException)2 ExpressionContext (cn.taketoday.expression.ExpressionContext)1 MethodNotFoundException (cn.taketoday.expression.MethodNotFoundException)1 ValueExpression (cn.taketoday.expression.ValueExpression)1 VariableMapper (cn.taketoday.expression.VariableMapper)1