Search in sources :

Example 1 with ELResolver

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

the class TestJasperELResolver method doTestConstructor.

private void doTestConstructor(int count) throws Exception {
    List<ELResolver> list = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        list.add(new ImplicitObjectELResolver());
    }
    int adjustedForGraalCount = JspRuntimeLibrary.GRAAL ? count + 1 : count;
    JasperELResolver resolver = new JasperELResolver(list, new StreamELResolverImpl());
    Assert.assertEquals(Integer.valueOf(count), getField("appResolversSize", resolver));
    Assert.assertEquals(STANDARD_RESOLVERS_COUNT + adjustedForGraalCount, ((ELResolver[]) getField("resolvers", resolver)).length);
    Assert.assertEquals(Integer.valueOf(STANDARD_RESOLVERS_COUNT + adjustedForGraalCount), Integer.valueOf(((AtomicInteger) getField("resolversSize", resolver)).get()));
}
Also used : ELResolver(jakarta.el.ELResolver) ImplicitObjectELResolver(jakarta.servlet.jsp.el.ImplicitObjectELResolver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ImplicitObjectELResolver(jakarta.servlet.jsp.el.ImplicitObjectELResolver) ArrayList(java.util.ArrayList) StreamELResolverImpl(org.apache.el.stream.StreamELResolverImpl)

Example 2 with ELResolver

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

the class AstValue method getValue.

@Override
public Object getValue(EvaluationContext ctx) throws ELException {
    Object base = this.children[0].getValue(ctx);
    int propCount = this.jjtGetNumChildren();
    int i = 1;
    Object suffix = null;
    ELResolver resolver = ctx.getELResolver();
    while (base != null && i < propCount) {
        suffix = this.children[i].getValue(ctx);
        if (i + 1 < propCount && (this.children[i + 1] instanceof AstMethodParameters)) {
            AstMethodParameters mps = (AstMethodParameters) this.children[i + 1];
            if (base instanceof Optional && "orElseGet".equals(suffix) && mps.jjtGetNumChildren() == 1) {
                Node paramFoOptional = mps.jjtGetChild(0);
                if (!(paramFoOptional instanceof AstLambdaExpression || paramFoOptional instanceof LambdaExpression)) {
                    throw new ELException(MessageFactory.get("stream.optional.paramNotLambda", suffix));
                }
            }
            // This is a method
            Object[] paramValues = mps.getParameters(ctx);
            base = resolver.invoke(ctx, base, suffix, getTypesFromValues(paramValues), paramValues);
            i += 2;
        } else {
            // This is a property
            if (suffix == null) {
                return null;
            }
            ctx.setPropertyResolved(false);
            base = resolver.getValue(ctx, base, suffix);
            i++;
        }
    }
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get("error.resolver.unhandled", base, suffix));
    }
    return base;
}
Also used : ELResolver(jakarta.el.ELResolver) PropertyNotFoundException(jakarta.el.PropertyNotFoundException) Optional(org.apache.el.stream.Optional) ELException(jakarta.el.ELException) LambdaExpression(jakarta.el.LambdaExpression)

Example 3 with ELResolver

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

the class AstValue method setValue.

@Override
public void setValue(EvaluationContext ctx, Object value) throws ELException {
    Target t = getTarget(ctx);
    ctx.setPropertyResolved(false);
    ELResolver resolver = ctx.getELResolver();
    // coerce to the expected type
    Class<?> targetClass = resolver.getType(ctx, t.base, t.property);
    resolver.setValue(ctx, t.base, t.property, ELSupport.coerceToType(ctx, value, targetClass));
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get("error.resolver.unhandled", t.base, t.property));
    }
}
Also used : ELResolver(jakarta.el.ELResolver) PropertyNotFoundException(jakarta.el.PropertyNotFoundException)

Example 4 with ELResolver

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

the class AstValue method getTarget.

private final Target getTarget(EvaluationContext ctx) throws ELException {
    // evaluate expr-a to value-a
    Object base = this.children[0].getValue(ctx);
    // if our base is null (we know there are more properties to evaluate)
    if (base == null) {
        throw new PropertyNotFoundException(MessageFactory.get("error.unreachable.base", this.children[0].getImage()));
    }
    // set up our start/end
    Object property = null;
    int propCount = this.jjtGetNumChildren();
    int i = 1;
    // Evaluate any properties or methods before our target
    ELResolver resolver = ctx.getELResolver();
    while (i < propCount) {
        if (i + 2 < propCount && this.children[i + 1] instanceof AstMethodParameters) {
            // Method call not at end of expression
            base = resolver.invoke(ctx, base, this.children[i].getValue(ctx), null, ((AstMethodParameters) this.children[i + 1]).getParameters(ctx));
            i += 2;
        } else if (i + 2 == propCount && this.children[i + 1] instanceof AstMethodParameters) {
            // Method call at end of expression
            ctx.setPropertyResolved(false);
            property = this.children[i].getValue(ctx);
            i += 2;
            if (property == null) {
                throw new PropertyNotFoundException(MessageFactory.get("error.unreachable.property", property));
            }
        } else if (i + 1 < propCount) {
            // Object with property not at end of expression
            property = this.children[i].getValue(ctx);
            ctx.setPropertyResolved(false);
            base = resolver.getValue(ctx, base, property);
            i++;
        } else {
            // Object with property at end of expression
            ctx.setPropertyResolved(false);
            property = this.children[i].getValue(ctx);
            i++;
            if (property == null) {
                throw new PropertyNotFoundException(MessageFactory.get("error.unreachable.property", property));
            }
        }
        if (base == null) {
            throw new PropertyNotFoundException(MessageFactory.get("error.unreachable.property", property));
        }
    }
    Target t = new Target();
    t.base = base;
    t.property = property;
    return t;
}
Also used : PropertyNotFoundException(jakarta.el.PropertyNotFoundException) ELResolver(jakarta.el.ELResolver)

Example 5 with ELResolver

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

the class TestScopedAttributeELResolverPerformance method testGetValuePerformance.

/*
     * With the caching of NotFound responses this test takes ~20ms. Without the
     * caching it takes ~6s.
     */
@Test
public void testGetValuePerformance() throws Exception {
    ELContext context = new StandardELContext(ELManager.getExpressionFactory());
    context.putContext(JspContext.class, new TesterPageContext());
    ELResolver resolver = new ScopedAttributeELResolver();
    for (int i = 0; i < 100000; i++) {
        resolver.getValue(context, null, "unknown");
    }
}
Also used : TesterPageContext(jakarta.servlet.jsp.TesterPageContext) ELContext(jakarta.el.ELContext) StandardELContext(jakarta.el.StandardELContext) ELResolver(jakarta.el.ELResolver) StandardELContext(jakarta.el.StandardELContext) Test(org.junit.Test)

Aggregations

ELResolver (jakarta.el.ELResolver)6 PropertyNotFoundException (jakarta.el.PropertyNotFoundException)3 CompositeELResolver (jakarta.el.CompositeELResolver)1 ELContext (jakarta.el.ELContext)1 ELException (jakarta.el.ELException)1 LambdaExpression (jakarta.el.LambdaExpression)1 StandardELContext (jakarta.el.StandardELContext)1 TesterPageContext (jakarta.servlet.jsp.TesterPageContext)1 ImplicitObjectELResolver (jakarta.servlet.jsp.el.ImplicitObjectELResolver)1 PrivilegedAction (java.security.PrivilegedAction)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Optional (org.apache.el.stream.Optional)1 StreamELResolverImpl (org.apache.el.stream.StreamELResolverImpl)1 ELContextImpl (org.apache.jasper.el.ELContextImpl)1 JasperELResolver (org.apache.jasper.el.JasperELResolver)1 Test (org.junit.Test)1