Search in sources :

Example 1 with MethodExpression

use of jakarta.el.MethodExpression in project tomcat by apache.

the class AstIdentifier method getMethodExpression.

private final MethodExpression getMethodExpression(EvaluationContext ctx) throws ELException {
    Object obj = null;
    // case A: ValueExpression exists, getValue which must
    // be a MethodExpression
    VariableMapper varMapper = ctx.getVariableMapper();
    ValueExpression ve = null;
    if (varMapper != null) {
        ve = varMapper.resolveVariable(this.image);
        if (ve != null) {
            obj = ve.getValue(ctx);
        }
    }
    // a MethodExpression to be able to invoke
    if (ve == null) {
        ctx.setPropertyResolved(false);
        obj = ctx.getELResolver().getValue(ctx, null, this.image);
    }
    // finally provide helpful hints
    if (obj instanceof MethodExpression) {
        return (MethodExpression) obj;
    } else if (obj == null) {
        throw new MethodNotFoundException(MessageFactory.get("error.identifier.noMethod", this.image));
    } else {
        throw new ELException(MessageFactory.get("error.identifier.notMethodExpression", this.image, obj.getClass().getName()));
    }
}
Also used : VariableMapper(jakarta.el.VariableMapper) ValueExpression(jakarta.el.ValueExpression) ELException(jakarta.el.ELException) MethodExpression(jakarta.el.MethodExpression) MethodNotFoundException(jakarta.el.MethodNotFoundException)

Example 2 with MethodExpression

use of jakarta.el.MethodExpression in project tomcat by apache.

the class TestMethodExpressionImpl method testBug53792d.

@Test
public void testBug53792d() {
    MethodExpression me = factory.createMethodExpression(context, "#{beanB.sayHello().length()}", null, new Class<?>[] {});
    Integer result = (Integer) me.invoke(context, new Object[] { "foo" });
    Assert.assertEquals(beanB.sayHello().length(), result.intValue());
}
Also used : MethodExpression(jakarta.el.MethodExpression) Test(org.junit.Test)

Example 3 with MethodExpression

use of jakarta.el.MethodExpression in project tomcat by apache.

the class TestMethodExpressionImpl method testBug60844.

@Test(expected = IllegalArgumentException.class)
public void testBug60844() {
    MethodExpression me2 = factory.createMethodExpression(context, "${beanC.sayHello}", null, new Class[] { TesterBeanA.class, TesterBeanB.class });
    me2.invoke(context, new Object[] { new Object() });
}
Also used : MethodExpression(jakarta.el.MethodExpression) Test(org.junit.Test)

Example 4 with MethodExpression

use of jakarta.el.MethodExpression in project tomcat by apache.

the class TestMethodExpressionImpl method testBug57855b.

@Test(expected = IllegalArgumentException.class)
public void testBug57855b() {
    MethodExpression me = factory.createMethodExpression(context, "${beanAA.echo2}", null, new Class[] { String.class });
    me.invoke(context, null);
}
Also used : MethodExpression(jakarta.el.MethodExpression) Test(org.junit.Test)

Example 5 with MethodExpression

use of jakarta.el.MethodExpression in project tomcat by apache.

the class TestMethodExpressionImpl method testInvoke.

@Test
public void testInvoke() {
    MethodExpression me1 = factory.createMethodExpression(context, "${beanB.getName}", String.class, new Class<?>[] {});
    MethodExpression me2 = factory.createMethodExpression(context, "${beanB.sayHello('JUnit')}", String.class, new Class<?>[] { String.class });
    MethodExpression me3 = factory.createMethodExpression(context, "${beanB.sayHello}", String.class, new Class<?>[] { String.class });
    Assert.assertEquals("B", me1.invoke(context, null));
    Assert.assertEquals("Hello JUnit from B", me2.invoke(context, null));
    Assert.assertEquals("Hello JUnit from B", me2.invoke(context, new Object[] { "JUnit2" }));
    Assert.assertEquals("Hello JUnit2 from B", me3.invoke(context, new Object[] { "JUnit2" }));
    Assert.assertEquals("Hello JUnit from B", me2.invoke(context, new Object[] { null }));
    Assert.assertEquals("Hello  from B", me3.invoke(context, new Object[] { null }));
}
Also used : MethodExpression(jakarta.el.MethodExpression) Test(org.junit.Test)

Aggregations

MethodExpression (jakarta.el.MethodExpression)45 Test (org.junit.Test)44 ValueExpression (jakarta.el.ValueExpression)7 MethodNotFoundException (jakarta.el.MethodNotFoundException)6 MethodInfo (jakarta.el.MethodInfo)2 ELException (jakarta.el.ELException)1 VariableMapper (jakarta.el.VariableMapper)1