Search in sources :

Example 21 with ExpressionFactory

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

the class TestValueExpressionImpl method testBug56522SetNullValue.

@Test
public void testBug56522SetNullValue() {
    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 ve = factory.createValueExpression(context, "${beanB.name}", String.class);
    // First check the basics work
    String result = (String) ve.getValue(context);
    Assert.assertEquals("Tomcat", result);
    // Now set the value to null
    ve.setValue(context, null);
    Assert.assertEquals("", beanB.getName());
}
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 22 with ExpressionFactory

use of jakarta.el.ExpressionFactory 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 23 with ExpressionFactory

use of jakarta.el.ExpressionFactory 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 24 with ExpressionFactory

use of jakarta.el.ExpressionFactory 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 25 with ExpressionFactory

use of jakarta.el.ExpressionFactory 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

ExpressionFactory (jakarta.el.ExpressionFactory)26 ELContext (jakarta.el.ELContext)25 ValueExpression (jakarta.el.ValueExpression)24 Test (org.junit.Test)22 ELContextImpl (org.apache.jasper.el.ELContextImpl)16 ELProcessor (jakarta.el.ELProcessor)7 ELException (jakarta.el.ELException)3 ValueReference (jakarta.el.ValueReference)3 ELManager (jakarta.el.ELManager)1 TesterELContext (jakarta.el.TesterELContext)1 VariableMapper (jakarta.el.VariableMapper)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Nodes (org.apache.jasper.compiler.ELNode.Nodes)1 TextBuilder (org.apache.jasper.compiler.ELParser.TextBuilder)1 ELContextWrapper (org.apache.jasper.runtime.JspContextWrapper.ELContextWrapper)1