javax.el.MethodExpression Java Examples

The following examples show how to use javax.el.MethodExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExpressionBuilder.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
public MethodExpression createMethodExpression(Class<?> expectedReturnType,
        Class<?>[] expectedParamTypes) throws ELException {
    Node n = this.build();
    if (!n.isParametersProvided() && expectedParamTypes == null) {
        throw new NullPointerException(MessageFactory
                .get("error.method.nullParms"));
    }
    if (n instanceof AstValue || n instanceof AstIdentifier) {
        return new MethodExpressionImpl(expression, n, this.fnMapper,
                this.varMapper, expectedReturnType, expectedParamTypes);
    } else if (n instanceof AstLiteralExpression) {
        return new MethodExpressionLiteral(expression, expectedReturnType,
                expectedParamTypes);
    } else {
        throw new ELException("Not a Valid Method Expression: "
                + expression);
    }
}
 
Example #2
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug53792a() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);
    me = factory.createMethodExpression(context,
            "${beanB.setName('" + BUG53792 + "')}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);

    ValueExpression ve = factory.createValueExpression(context,
            "#{beanA.getBean().name}", java.lang.String.class);
    String actual = (String) ve.getValue(context);
    assertEquals(BUG53792, actual);
}
 
Example #3
Source File: TestMethodExpressionImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug53792b() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);
    me = factory.createMethodExpression(context,
            "${beanB.setName('" + BUG53792 + "')}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);

    ValueExpression ve = factory.createValueExpression(context,
            "#{beanA.getBean().name.length()}", java.lang.Integer.class);
    Integer actual = (Integer) ve.getValue(context);
    Assert.assertEquals(Integer.valueOf(BUG53792.length()), actual);
}
 
Example #4
Source File: TestMethodExpressionImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug53792a() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);
    me = factory.createMethodExpression(context,
            "${beanB.setName('" + BUG53792 + "')}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);

    ValueExpression ve = factory.createValueExpression(context,
            "#{beanA.getBean().name}", java.lang.String.class);
    String actual = (String) ve.getValue(context);
    Assert.assertEquals(BUG53792, actual);
}
 
Example #5
Source File: TestMethodExpressionImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@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 }));
}
 
Example #6
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug53792b() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);
    me = factory.createMethodExpression(context,
            "${beanB.setName('" + BUG53792 + "')}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);

    ValueExpression ve = factory.createValueExpression(context,
            "#{beanA.getBean().name.length()}", java.lang.Integer.class);
    Integer actual = (Integer) ve.getValue(context);
    assertEquals(Integer.valueOf(BUG53792.length()), actual);
}
 
Example #7
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testIsParametersProvided() {
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    ValueExpression var =
        factory.createValueExpression(beanB, TesterBeanB.class);
    context.getVariableMapper().setVariable("beanB", var);

    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 });

    assertFalse(me1.isParmetersProvided());
    assertTrue(me2.isParmetersProvided());
}
 
Example #8
Source File: ExpressionBuilder.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
public MethodExpression createMethodExpression(Class<?> expectedReturnType,
        Class<?>[] expectedParamTypes) throws ELException {
    Node n = this.build();
    if (!n.isParametersProvided() && expectedParamTypes == null) {
        throw new NullPointerException(MessageFactory
                .get("error.method.nullParms"));
    }
    if (n instanceof AstValue || n instanceof AstIdentifier) {
        return new MethodExpressionImpl(expression, n, this.fnMapper,
                this.varMapper, expectedReturnType, expectedParamTypes);
    } else if (n instanceof AstLiteralExpression) {
        return new MethodExpressionLiteral(expression, expectedReturnType,
                expectedParamTypes);
    } else {
        throw new ELException("Not a Valid Method Expression: "
                + expression);
    }
}
 
Example #9
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeWithSuperABReturnTypeParamTypes() {
    MethodExpression me5 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanA,beanB)}", String.class ,
            new Class<?>[] {TesterBeanA.class, TesterBeanB.class});
    Object r5 = me5.invoke(context, null);
    assertEquals("AB: Hello A from B", r5.toString());
}
 
Example #10
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvoke() {
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("B");

    context.getVariableMapper().setVariable("beanB",
            factory.createValueExpression(beanB, TesterBeanB.class));

    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 });

    assertEquals("B", me1.invoke(context, null));
    assertEquals("Hello JUnit from B", me2.invoke(context, null));
    assertEquals("Hello JUnit from B",
            me2.invoke(context, new Object[] { "JUnit2" }));
    assertEquals("Hello JUnit2 from B",
            me3.invoke(context, new Object[] { "JUnit2" }));
    assertEquals("Hello JUnit from B",
            me2.invoke(context, new Object[] { null }));
    assertEquals("Hello  from B",
            me3.invoke(context, new Object[] { null }));
}
 
Example #11
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeWithSuperABB() {
    MethodExpression me6 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanA,beanBB)}", null , null);
    Object r6 = me6.invoke(context, null);
    assertEquals("ABB: Hello A from BB", r6.toString());
}
 
Example #12
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeWithVarArgsAAABB() throws Exception {
    MethodExpression me8 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAAA,beanBB,beanBB)}", null , null);
    Object r8 = me8.invoke(context, null);
    assertEquals("ABB[]: Hello AAA from BB, BB", r8.toString());
}
 
Example #13
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeWithVarArgsAAB() throws Exception {
    MethodExpression me4 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAA,beanB,beanB)}", null , null);
    Exception e = null;
    try {
        me4.invoke(context, null);
    } catch (Exception e1) {
        e = e1;
    }
    // Expected to fail
    assertNotNull(e);
}
 
Example #14
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug52445a() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanBB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);

    MethodExpression me1 = factory.createMethodExpression(context,
            "${beanA.bean.sayHello()}", null, null);
    String actual = (String) me1.invoke(context, null);
    assertEquals("Hello from BB", actual);
}
 
Example #15
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeWithVarArgsAABBB() throws Exception {
    MethodExpression me6 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAA,beanBBB,beanBBB)}", null , null);
    Object r6 = me6.invoke(context, null);
    assertEquals("ABB[]: Hello AA from BBB, BBB", r6.toString());
}
 
Example #16
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeWithVarArgsAAAB() throws Exception {
    MethodExpression me7 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAAA,beanB,beanB)}", null , null);
    Exception e = null;
    try {
        me7.invoke(context, null);
    } catch (Exception e1) {
        e = e1;
    }
    // Expected to fail
    assertNotNull(e);
}
 
Example #17
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug49655() throws Exception {
    // This is the call the failed
    MethodExpression me = factory.createMethodExpression(context,
            "#{beanA.setName('New value')}", null, null);
    // The rest is to check it worked correctly
    me.invoke(context, null);
    ValueExpression ve = factory.createValueExpression(context,
            "#{beanA.name}", java.lang.String.class);
    assertEquals("New value", ve.getValue(context));
}
 
Example #18
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBugPrimitives() throws Exception {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setValLong(5)}", null, null);
    me.invoke(context, null);
    ValueExpression ve = factory.createValueExpression(context,
            "#{beanA.valLong}", java.lang.String.class);
    assertEquals("5", ve.getValue(context));
}
 
Example #19
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeWithVarArgsABBB() throws Exception {
    MethodExpression me3 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanA,beanBBB,beanBBB)}", null , null);
    Object r3 = me3.invoke(context, null);
    assertEquals("ABB[]: Hello A from BBB, BBB", r3.toString());
}
 
Example #20
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeWithSuperABNoReturnTypeParamTypes() {
    MethodExpression me4 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanA,beanB)}", null ,
            new Class<?>[] {TesterBeanA.class, TesterBeanB.class});
    Object r4 = me4.invoke(context, null);
    assertEquals("AB: Hello A from B", r4.toString());
}
 
Example #21
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeWithSuperABReturnTypeNoParamTypes() {
    MethodExpression me3 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanA,beanB)}", String.class , null);
    Object r3 = me3.invoke(context, null);
    assertEquals("AB: Hello A from B", r3.toString());
}
 
Example #22
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeWithSuperABNoReturnTypeNoParamTypes() {
    MethodExpression me2 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanA,beanB)}", null , null);
    Object r2 = me2.invoke(context, null);
    assertEquals("AB: Hello A from B", r2.toString());
}
 
Example #23
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeWithSuper() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanA.setBean(beanBB)}", null ,
            new Class<?>[] { TesterBeanB.class });
    me.invoke(context, null);
    ValueExpression ve = factory.createValueExpression(context,
            "${beanA.bean.name}", String.class);
    Object r = ve.getValue(context);
    assertEquals("BB", r);
}
 
Example #24
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeWithSuperAAB() {
    MethodExpression me8 = factory.createMethodExpression(context,
            "${beanC.sayHello(beanAA,beanB)}", null , null);
    Object r8 = me8.invoke(context, null);
    assertEquals("AAB: Hello AA from B", r8.toString());
}
 
Example #25
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug56797b() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanAA.echo2('Hello World!')}", null , null);
    Object r = me.invoke(context, null);
    assertEquals("AA2Hello World!", r.toString());
}
 
Example #26
Source File: ExpressionFactoryImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public MethodExpression createMethodExpression(ELContext context,
        String expression, Class<?> expectedReturnType,
        Class<?>[] expectedParamTypes) {
    ExpressionBuilder builder = new ExpressionBuilder(expression, context);
    return builder.createMethodExpression(expectedReturnType,
            expectedParamTypes);
}
 
Example #27
Source File: AstIdentifier.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
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);
        }
    }

    // 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.getELResolver().getValue(ctx, null, this.image);
    }

    // finally provide helpful hints
    if (obj instanceof MethodExpression) {
        return (MethodExpression) obj;
    } else if (obj == null) {
        throw new MethodNotFoundException("Identity '" + this.image
                + "' was null and was unable to invoke");
    } else {
        throw new ELException(
                "Identity '"
                        + this.image
                        + "' does not reference a MethodExpression instance, returned type: "
                        + obj.getClass().getName());
    }
}
 
Example #28
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug50449a() throws Exception {
    MethodExpression me1 = factory.createMethodExpression(context,
            "${beanB.sayHello()}", null, null);
    String actual = (String) me1.invoke(context, null);
    assertEquals("Hello from B", actual);
}
 
Example #29
Source File: TestMethodExpressionImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug56797a() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanAA.echo1('Hello World!')}", null , null);
    Object r = me.invoke(context, null);
    assertEquals("AA1Hello World!", r.toString());
}
 
Example #30
Source File: TestMethodExpressionImpl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testBug56797b() {
    MethodExpression me = factory.createMethodExpression(context,
            "${beanAA.echo2('Hello World!')}", null , null);
    Object r = me.invoke(context, null);
    Assert.assertEquals("AA2Hello World!", r.toString());
}