Search in sources :

Example 6 with PropertyNotFoundException

use of javax.el.PropertyNotFoundException in project tomcat by apache.

the class AstIdentifier method getValue.

@Override
public Object getValue(EvaluationContext ctx) throws ELException {
    // Lambda parameters
    if (ctx.isLambdaArgument(this.image)) {
        return ctx.getLambdaArgument(this.image);
    }
    // Variable mapper
    VariableMapper varMapper = ctx.getVariableMapper();
    if (varMapper != null) {
        ValueExpression expr = varMapper.resolveVariable(this.image);
        if (expr != null) {
            return expr.getValue(ctx.getELContext());
        }
    }
    // EL Resolvers
    ctx.setPropertyResolved(false);
    Object result;
    /* Putting the Boolean into the ELContext is part of a performance
         * optimisation for ScopedAttributeELResolver. When looking up "foo",
         * the resolver can't differentiate between ${ foo } and ${ foo.bar }.
         * This is important because the expensive class lookup only needs to
         * be performed in the later case. This flag tells the resolver if the
         * lookup can be skipped.
         */
    if (parent instanceof AstValue) {
        ctx.putContext(this.getClass(), Boolean.FALSE);
    } else {
        ctx.putContext(this.getClass(), Boolean.TRUE);
    }
    try {
        result = ctx.getELResolver().getValue(ctx, null, this.image);
    } finally {
        // Always reset the flag to false so the optimisation is not applied
        // inappropriately
        ctx.putContext(this.getClass(), Boolean.FALSE);
    }
    if (ctx.isPropertyResolved()) {
        return result;
    }
    // Import
    result = ctx.getImportHandler().resolveClass(this.image);
    if (result != null) {
        return new ELClass((Class<?>) result);
    }
    result = ctx.getImportHandler().resolveStatic(this.image);
    if (result != null) {
        try {
            return ((Class<?>) result).getField(this.image).get(null);
        } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
            throw new ELException(e);
        }
    }
    throw new PropertyNotFoundException(MessageFactory.get("error.resolver.unhandled.null", this.image));
}
Also used : ELClass(javax.el.ELClass) PropertyNotFoundException(javax.el.PropertyNotFoundException) VariableMapper(javax.el.VariableMapper) ValueExpression(javax.el.ValueExpression) ELException(javax.el.ELException)

Example 7 with PropertyNotFoundException

use of javax.el.PropertyNotFoundException 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(javax.el.ELResolver) PropertyNotFoundException(javax.el.PropertyNotFoundException)

Example 8 with PropertyNotFoundException

use of javax.el.PropertyNotFoundException 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(javax.el.PropertyNotFoundException) ELResolver(javax.el.ELResolver)

Aggregations

PropertyNotFoundException (javax.el.PropertyNotFoundException)8 ValueExpression (javax.el.ValueExpression)4 VariableMapper (javax.el.VariableMapper)4 ELException (javax.el.ELException)3 ELResolver (javax.el.ELResolver)3 ELClass (javax.el.ELClass)1 LambdaExpression (javax.el.LambdaExpression)1 MethodInfo (javax.el.MethodInfo)1 MethodNotFoundException (javax.el.MethodNotFoundException)1 Optional (org.apache.el.stream.Optional)1