Search in sources :

Example 1 with ELException

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

the class AstFunction method getValue.

@Override
public Object getValue(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 && this.prefix.length() == 0) {
        // TODO: Do we need to think about precedence of the various ways
        // a lambda expression may be obtained from something that
        // the parser thinks is a function?
        Object obj = null;
        if (ctx.isLambdaArgument(this.localName)) {
            obj = ctx.getLambdaArgument(this.localName);
        }
        if (obj == null) {
            VariableMapper varMapper = ctx.getVariableMapper();
            if (varMapper != null) {
                obj = varMapper.resolveVariable(this.localName);
                if (obj instanceof ValueExpression) {
                    // See if this returns a LambdaExpression
                    obj = ((ValueExpression) obj).getValue(ctx);
                }
            }
        }
        if (obj == null) {
            obj = ctx.getELResolver().getValue(ctx, null, this.localName);
        }
        if (obj instanceof LambdaExpression) {
            // Build arguments
            int i = 0;
            while (obj instanceof LambdaExpression && i < jjtGetNumChildren()) {
                Node args = jjtGetChild(i);
                obj = ((LambdaExpression) obj).invoke(((AstMethodParameters) args).getParameters(ctx));
                i++;
            }
            if (i < jjtGetNumChildren()) {
                // there were too many sets of parameters
                throw new ELException(MessageFactory.get("error.lambda.tooManyMethodParameterSets"));
            }
            return obj;
        }
        // Call to a constructor or a static method
        obj = ctx.getImportHandler().resolveClass(this.localName);
        if (obj != null) {
            return ctx.getELResolver().invoke(ctx, new ELClass((Class<?>) obj), "<init>", null, ((AstMethodParameters) this.children[0]).getParameters(ctx));
        }
        obj = ctx.getImportHandler().resolveStatic(this.localName);
        if (obj != null) {
            return ctx.getELResolver().invoke(ctx, new ELClass((Class<?>) obj), this.localName, null, ((AstMethodParameters) this.children[0]).getParameters(ctx));
        }
    }
    if (m == null) {
        throw new ELException(MessageFactory.get("error.fnMapper.method", this.getOutputName()));
    }
    // single set of method parameters
    if (this.jjtGetNumChildren() != 1) {
        throw new ELException(MessageFactory.get("error.function.tooManyMethodParameterSets", getOutputName()));
    }
    Node parameters = jjtGetChild(0);
    Class<?>[] paramTypes = m.getParameterTypes();
    Object[] params = null;
    Object result = null;
    int inputParameterCount = parameters.jjtGetNumChildren();
    int methodParameterCount = paramTypes.length;
    if (inputParameterCount == 0 && methodParameterCount == 1 && m.isVarArgs()) {
        params = new Object[] { null };
    } else if (inputParameterCount > 0) {
        params = new Object[methodParameterCount];
        try {
            for (int i = 0; i < methodParameterCount; i++) {
                if (m.isVarArgs() && i == methodParameterCount - 1) {
                    if (inputParameterCount < methodParameterCount) {
                        params[i] = new Object[] { null };
                    } else if (inputParameterCount == methodParameterCount && paramTypes[i].isArray()) {
                        params[i] = parameters.jjtGetChild(i).getValue(ctx);
                    } else {
                        Object[] varargs = new Object[inputParameterCount - methodParameterCount + 1];
                        Class<?> target = paramTypes[i].getComponentType();
                        for (int j = i; j < inputParameterCount; j++) {
                            varargs[j - i] = parameters.jjtGetChild(j).getValue(ctx);
                            varargs[j - i] = ELSupport.coerceToType(ctx, varargs[j - i], target);
                        }
                        params[i] = varargs;
                    }
                } else {
                    params[i] = parameters.jjtGetChild(i).getValue(ctx);
                }
                params[i] = ELSupport.coerceToType(ctx, params[i], paramTypes[i]);
            }
        } catch (ELException ele) {
            throw new ELException(MessageFactory.get("error.function", this.getOutputName()), ele);
        }
    }
    try {
        result = m.invoke(null, params);
    } catch (IllegalAccessException iae) {
        throw new ELException(MessageFactory.get("error.function", this.getOutputName()), iae);
    } catch (InvocationTargetException ite) {
        Throwable cause = ite.getCause();
        if (cause instanceof ThreadDeath) {
            throw (ThreadDeath) cause;
        }
        if (cause instanceof VirtualMachineError) {
            throw (VirtualMachineError) cause;
        }
        throw new ELException(MessageFactory.get("error.function", this.getOutputName()), cause);
    }
    return result;
}
Also used : VariableMapper(jakarta.el.VariableMapper) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ELClass(jakarta.el.ELClass) ValueExpression(jakarta.el.ValueExpression) ELClass(jakarta.el.ELClass) ELException(jakarta.el.ELException) LambdaExpression(jakarta.el.LambdaExpression) FunctionMapper(jakarta.el.FunctionMapper)

Example 2 with ELException

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

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

the class AstValue method invoke.

@Override
public // Interface el.parser.Node uses a raw type (and is auto-generated)
Object invoke(EvaluationContext ctx, @SuppressWarnings("rawtypes") Class[] paramTypes, Object[] paramValues) throws ELException {
    Target t = getTarget(ctx);
    Method m = null;
    Object[] values = null;
    Class<?>[] types = null;
    if (isParametersProvided()) {
        values = ((AstMethodParameters) this.jjtGetChild(this.jjtGetNumChildren() - 1)).getParameters(ctx);
        types = getTypesFromValues(values);
    } else {
        values = paramValues;
        types = paramTypes;
    }
    m = ReflectionUtil.getMethod(ctx, t.base, t.property, types, values);
    // Handle varArgs and any coercion required
    values = convertArgs(ctx, values, m);
    Object result = null;
    try {
        result = m.invoke(t.base, values);
    } catch (IllegalAccessException | IllegalArgumentException e) {
        throw new ELException(e);
    } catch (InvocationTargetException ite) {
        Throwable cause = ite.getCause();
        if (cause instanceof ThreadDeath) {
            throw (ThreadDeath) cause;
        }
        if (cause instanceof VirtualMachineError) {
            throw (VirtualMachineError) cause;
        }
        throw new ELException(cause);
    }
    return result;
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ELException(jakarta.el.ELException)

Example 4 with ELException

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

the class AstIdentifier method getMethodExpression.

private final MethodExpression getMethodExpression(EvaluationContext ctx) throws ELException {
    Object obj = null;
    // case A: ValueExpression exists, getValue which must
    // be a MethodExpression
    VariableMapper varMapper = ctx.getVariableMapper();
    ValueExpression ve = null;
    if (varMapper != null) {
        ve = varMapper.resolveVariable(this.image);
        if (ve != null) {
            obj = ve.getValue(ctx);
        }
    }
    // a MethodExpression to be able to invoke
    if (ve == null) {
        ctx.setPropertyResolved(false);
        obj = ctx.getELResolver().getValue(ctx, null, this.image);
    }
    // finally provide helpful hints
    if (obj instanceof MethodExpression) {
        return (MethodExpression) obj;
    } else if (obj == null) {
        throw new MethodNotFoundException(MessageFactory.get("error.identifier.noMethod", this.image));
    } else {
        throw new ELException(MessageFactory.get("error.identifier.notMethodExpression", this.image, obj.getClass().getName()));
    }
}
Also used : VariableMapper(jakarta.el.VariableMapper) ValueExpression(jakarta.el.ValueExpression) ELException(jakarta.el.ELException) MethodExpression(jakarta.el.MethodExpression) MethodNotFoundException(jakarta.el.MethodNotFoundException)

Example 5 with ELException

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

the class TestELParser method testJavaKeyWordIdentifier.

@Test
public void testJavaKeyWordIdentifier() {
    ExpressionFactory factory = ExpressionFactory.newInstance();
    ELContext context = new ELContextImpl(factory);
    TesterBeanA beanA = new TesterBeanA();
    beanA.setInt("five");
    ValueExpression var = factory.createValueExpression(beanA, TesterBeanA.class);
    context.getVariableMapper().setVariable("this", var);
    // Should fail
    Exception e = null;
    try {
        factory.createValueExpression(context, "${this}", String.class);
    } catch (ELException ele) {
        e = ele;
    }
    Assert.assertNotNull(e);
}
Also used : ELContext(jakarta.el.ELContext) ExpressionFactory(jakarta.el.ExpressionFactory) ValueExpression(jakarta.el.ValueExpression) ELContextImpl(org.apache.jasper.el.ELContextImpl) 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