Search in sources :

Example 11 with ELException

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

the class ExpressionBuilder method visit.

/*
     * (non-Javadoc)
     *
     * @see com.sun.el.parser.NodeVisitor#visit(com.sun.el.parser.Node)
     */
@Override
public void visit(Node node) throws ELException {
    if (node instanceof AstFunction) {
        AstFunction funcNode = (AstFunction) node;
        Method m = null;
        if (this.fnMapper != null) {
            m = fnMapper.resolveFunction(funcNode.getPrefix(), funcNode.getLocalName());
        }
        // need to treat it as a variable rather than a function.
        if (m == null && this.varMapper != null && funcNode.getPrefix().length() == 0) {
            this.varMapper.resolveVariable(funcNode.getLocalName());
            return;
        }
        if (this.fnMapper == null) {
            throw new ELException(MessageFactory.get("error.fnMapper.null"));
        }
        if (m == null) {
            throw new ELException(MessageFactory.get("error.fnMapper.method", funcNode.getOutputName()));
        }
        int methodParameterCount = m.getParameterTypes().length;
        // AstFunction->MethodParameters->Parameters()
        int inputParameterCount = node.jjtGetChild(0).jjtGetNumChildren();
        if (m.isVarArgs() && inputParameterCount < methodParameterCount - 1 || !m.isVarArgs() && inputParameterCount != methodParameterCount) {
            throw new ELException(MessageFactory.get("error.fnMapper.paramcount", funcNode.getOutputName(), "" + methodParameterCount, "" + node.jjtGetChild(0).jjtGetNumChildren()));
        }
    } else if (node instanceof AstIdentifier && this.varMapper != null) {
        String variable = node.getImage();
        // simply capture it
        this.varMapper.resolveVariable(variable);
    }
}
Also used : AstFunction(org.apache.el.parser.AstFunction) AstIdentifier(org.apache.el.parser.AstIdentifier) Method(java.lang.reflect.Method) ELException(jakarta.el.ELException)

Example 12 with ELException

use of jakarta.el.ELException 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(jakarta.el.ELClass) PropertyNotFoundException(jakarta.el.PropertyNotFoundException) VariableMapper(jakarta.el.VariableMapper) ValueExpression(jakarta.el.ValueExpression) ELException(jakarta.el.ELException)

Example 13 with ELException

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

the class AstFunction method getType.

@Override
public Class<?> getType(EvaluationContext ctx) throws ELException {
    FunctionMapper fnMapper = ctx.getFunctionMapper();
    // quickly validate again for this request
    if (fnMapper == null) {
        throw new ELException(MessageFactory.get("error.fnMapper.null"));
    }
    Method m = fnMapper.resolveFunction(this.prefix, this.localName);
    if (m == null) {
        throw new ELException(MessageFactory.get("error.fnMapper.method", this.getOutputName()));
    }
    return m.getReturnType();
}
Also used : ELException(jakarta.el.ELException) Method(java.lang.reflect.Method) FunctionMapper(jakarta.el.FunctionMapper)

Example 14 with ELException

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

the class AstLambdaExpression method getValue.

@Override
public Object getValue(EvaluationContext ctx) throws ELException {
    // Correct evaluation requires knowledge of the whole set of nested
    // expressions, not just the current expression
    NestedState state = getNestedState();
    // Check that there are not more sets of parameters than there are
    // nested expressions.
    int methodParameterSetCount = jjtGetNumChildren() - 2;
    if (methodParameterSetCount > state.getNestingCount()) {
        throw new ELException(MessageFactory.get("error.lambda.tooManyMethodParameterSets"));
    }
    // First child is always parameters even if there aren't any
    AstLambdaParameters formalParametersNode = (AstLambdaParameters) children[0];
    Node[] formalParamNodes = formalParametersNode.children;
    // Second child is a value expression
    ValueExpressionImpl ve = new ValueExpressionImpl("", children[1], ctx.getFunctionMapper(), ctx.getVariableMapper(), null);
    // Build a LambdaExpression
    List<String> formalParameters = new ArrayList<>();
    if (formalParamNodes != null) {
        for (Node formalParamNode : formalParamNodes) {
            formalParameters.add(formalParamNode.getImage());
        }
    }
    LambdaExpression le = new LambdaExpression(formalParameters, ve);
    le.setELContext(ctx);
    if (jjtGetNumChildren() == 2) {
        // in the nesting declare parameters
        if (state.getHasFormalParameters()) {
            return le;
        } else {
            return le.invoke(ctx, (Object[]) null);
        }
    }
    /*
         * This is a (possibly nested) lambda expression with one or more sets
         * of parameters provided.
         *
         * If there are more nested expressions than sets of parameters this may
         * return a LambdaExpression.
         *
         * If there are more sets of parameters than nested expressions an
         * ELException will have been thrown by the check at the start of this
         * method.
         */
    // Always have to invoke the outer-most expression
    int methodParameterIndex = 2;
    Object result = le.invoke(((AstMethodParameters) children[methodParameterIndex]).getParameters(ctx));
    methodParameterIndex++;
    while (result instanceof LambdaExpression && methodParameterIndex < jjtGetNumChildren()) {
        result = ((LambdaExpression) result).invoke(((AstMethodParameters) children[methodParameterIndex]).getParameters(ctx));
        methodParameterIndex++;
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) ELException(jakarta.el.ELException) ValueExpressionImpl(org.apache.el.ValueExpressionImpl) LambdaExpression(jakarta.el.LambdaExpression)

Example 15 with ELException

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

the class TestELEvaluation method testMixedTypes.

/**
 * Test mixing ${...} and #{...} in the same expression.
 */
@Test
public void testMixedTypes() {
    // Mixing types should throw an error
    Exception e = null;
    try {
        evaluateExpression("${1+1}#{1+1}");
    } catch (ELException el) {
        e = el;
    }
    Assert.assertNotNull(e);
}
Also used : ELException(jakarta.el.ELException) ELException(jakarta.el.ELException) Test(org.junit.Test)

Aggregations

ELException (jakarta.el.ELException)18 ValueExpression (jakarta.el.ValueExpression)6 LambdaExpression (jakarta.el.LambdaExpression)4 PropertyNotFoundException (jakarta.el.PropertyNotFoundException)4 Method (java.lang.reflect.Method)4 Test (org.junit.Test)4 ELContext (jakarta.el.ELContext)3 ExpressionFactory (jakarta.el.ExpressionFactory)3 MethodNotFoundException (jakarta.el.MethodNotFoundException)3 VariableMapper (jakarta.el.VariableMapper)3 ELClass (jakarta.el.ELClass)2 FunctionMapper (jakarta.el.FunctionMapper)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ELContextImpl (org.apache.jasper.el.ELContextImpl)2 ELManager (jakarta.el.ELManager)1 ELResolver (jakarta.el.ELResolver)1 MethodExpression (jakarta.el.MethodExpression)1 MethodInfo (jakarta.el.MethodInfo)1 MethodReference (jakarta.el.MethodReference)1 PropertyEditor (java.beans.PropertyEditor)1