Search in sources :

Example 36 with ValueExpression

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

the class TestValueExpressionImpl method testBug51544Bean.

/*
     * Test returning an empty list as a bean property.
     */
@Test
public void testBug51544Bean() throws Exception {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);
    TesterBeanA beanA = new TesterBeanA();
    beanA.setValList(Collections.emptyList());
    ValueExpression var = factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("beanA", var);
    ValueExpression ve = factory.createValueExpression(context, "${beanA.valList.size()}", Integer.class);
    Integer result = (Integer) ve.getValue(context);
    Assert.assertEquals(Integer.valueOf(0), result);
}
Also used : ELContext(jakarta.el.ELContext) ExpressionFactory(jakarta.el.ExpressionFactory) ValueExpression(jakarta.el.ValueExpression) ELContextImpl(org.apache.jasper.el.ELContextImpl) Test(org.junit.Test)

Example 37 with ValueExpression

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

the class TestValueExpressionImpl method testGetValueReferenceVariable.

@Test
public void testGetValueReferenceVariable() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    ValueExpression var = factory.createValueExpression(beanB, TesterBeanB.class);
    context.getVariableMapper().setVariable("beanB", var);
    ValueExpression var2 = factory.createValueExpression(context, "${beanB.name}", String.class);
    context.getVariableMapper().setVariable("foo", var2);
    ValueExpression ve = factory.createValueExpression(context, "${foo}", ValueExpression.class);
    // Now check the value reference
    ValueReference vr = ve.getValueReference(context);
    Assert.assertNotNull(vr);
    Assert.assertEquals(beanB, vr.getBase());
    Assert.assertEquals("name", vr.getProperty());
}
Also used : ELContext(jakarta.el.ELContext) ExpressionFactory(jakarta.el.ExpressionFactory) ValueExpression(jakarta.el.ValueExpression) ELContextImpl(org.apache.jasper.el.ELContextImpl) ValueReference(jakarta.el.ValueReference) Test(org.junit.Test)

Example 38 with ValueExpression

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

the class TestValueExpressionImpl method testBug51177ObjectList.

@Test
public void testBug51177ObjectList() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);
    Object o1 = "String value";
    Object o2 = Integer.valueOf(32);
    List<Object> list = new ArrayList<>();
    list.add(0, o1);
    list.add(1, o2);
    ValueExpression var = factory.createValueExpression(list, List.class);
    context.getVariableMapper().setVariable("list", var);
    ValueExpression ve1 = factory.createValueExpression(context, "${list[0]}", Object.class);
    ve1.setValue(context, o2);
    Assert.assertEquals(o2, ve1.getValue(context));
    ValueExpression ve2 = factory.createValueExpression(context, "${list[1]}", Object.class);
    ve2.setValue(context, o1);
    Assert.assertEquals(o1, ve2.getValue(context));
}
Also used : ELContext(jakarta.el.ELContext) ExpressionFactory(jakarta.el.ExpressionFactory) ValueExpression(jakarta.el.ValueExpression) ArrayList(java.util.ArrayList) ELContextImpl(org.apache.jasper.el.ELContextImpl) Test(org.junit.Test)

Example 39 with ValueExpression

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

the class TestMethodExpressionImpl method testBugPrimitives.

@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);
    Assert.assertEquals("5", ve.getValue(context));
}
Also used : ValueExpression(jakarta.el.ValueExpression) MethodExpression(jakarta.el.MethodExpression) Test(org.junit.Test)

Example 40 with ValueExpression

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

the class PageContextImpl method proprietaryEvaluate.

/**
 * Proprietary method to evaluate EL expressions. XXX - This method should
 * go away once the EL interpreter moves out of JSTL and into its own
 * project. For now, this is necessary because the standard machinery is too
 * slow.
 *
 * @param expression
 *            The expression to be evaluated
 * @param expectedType
 *            The expected resulting type
 * @param pageContext
 *            The page context
 * @param functionMap
 *            Maps prefix and name to Method
 * @return The result of the evaluation
 * @throws ELException If an error occurs during the evaluation
 */
public static Object proprietaryEvaluate(final String expression, final Class<?> expectedType, final PageContext pageContext, final ProtectedFunctionMapper functionMap) throws ELException {
    final ExpressionFactory exprFactory = jspf.getJspApplicationContext(pageContext.getServletContext()).getExpressionFactory();
    ELContext ctx = pageContext.getELContext();
    ELContextImpl ctxImpl;
    if (ctx instanceof ELContextWrapper) {
        ctxImpl = (ELContextImpl) ((ELContextWrapper) ctx).getWrappedELContext();
    } else {
        ctxImpl = (ELContextImpl) ctx;
    }
    ctxImpl.setFunctionMapper(functionMap);
    ValueExpression ve = exprFactory.createValueExpression(ctx, expression, expectedType);
    return ve.getValue(ctx);
}
Also used : ELContext(jakarta.el.ELContext) ExpressionFactory(jakarta.el.ExpressionFactory) ELContextWrapper(org.apache.jasper.runtime.JspContextWrapper.ELContextWrapper) ValueExpression(jakarta.el.ValueExpression) ELContextImpl(org.apache.jasper.el.ELContextImpl)

Aggregations

ValueExpression (jakarta.el.ValueExpression)42 Test (org.junit.Test)28 ELContext (jakarta.el.ELContext)24 ExpressionFactory (jakarta.el.ExpressionFactory)24 ELContextImpl (org.apache.jasper.el.ELContextImpl)17 VariableMapper (jakarta.el.VariableMapper)8 ELProcessor (jakarta.el.ELProcessor)7 MethodExpression (jakarta.el.MethodExpression)7 ELException (jakarta.el.ELException)6 PropertyNotFoundException (jakarta.el.PropertyNotFoundException)4 ValueReference (jakarta.el.ValueReference)3 ELClass (jakarta.el.ELClass)2 ELManager (jakarta.el.ELManager)1 FunctionMapper (jakarta.el.FunctionMapper)1 LambdaExpression (jakarta.el.LambdaExpression)1 MethodNotFoundException (jakarta.el.MethodNotFoundException)1 TesterELContext (jakarta.el.TesterELContext)1 ELParseException (jakarta.servlet.jsp.el.ELParseException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1