Search in sources :

Example 6 with ELException

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

the class TestELParser method testJavaKeyWordSuffix.

@Test
public void testJavaKeyWordSuffix() {
    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("beanA", var);
    // Should fail
    Exception e = null;
    try {
        factory.createValueExpression(context, "${beanA.int}", 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)

Example 7 with ELException

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

the class TestELEvaluation method testParserStringLiteral.

@Test
public void testParserStringLiteral() {
    // Inspired by work on bug 45451, comments from kkolinko on the dev
    // list and looking at the spec to find some edge cases
    // The only characters that can be escaped inside a String literal
    // are \ " and '. # and $ are not escaped inside a String literal.
    Assert.assertEquals("\\", evaluateExpression("${'\\\\'}"));
    Assert.assertEquals("\\", evaluateExpression("${\"\\\\\"}"));
    Assert.assertEquals("\\\"'$#", evaluateExpression("${'\\\\\\\"\\'$#'}"));
    Assert.assertEquals("\\\"'$#", evaluateExpression("${\"\\\\\\\"\\'$#\"}"));
    // Trying to quote # or $ should throw an error
    Exception e = null;
    try {
        evaluateExpression("${'\\$'}");
    } catch (ELException el) {
        e = el;
    }
    Assert.assertNotNull(e);
    Assert.assertEquals("\\$", evaluateExpression("${'\\\\$'}"));
    Assert.assertEquals("\\\\$", evaluateExpression("${'\\\\\\\\$'}"));
    // Can use ''' inside '"' when quoting with '"' and vice versa without
    // escaping
    Assert.assertEquals("\\\"", evaluateExpression("${'\\\\\"'}"));
    Assert.assertEquals("\"\\", evaluateExpression("${'\"\\\\'}"));
    Assert.assertEquals("\\'", evaluateExpression("${'\\\\\\''}"));
    Assert.assertEquals("'\\", evaluateExpression("${'\\'\\\\'}"));
    Assert.assertEquals("\\'", evaluateExpression("${\"\\\\'\"}"));
    Assert.assertEquals("'\\", evaluateExpression("${\"'\\\\\"}"));
    Assert.assertEquals("\\\"", evaluateExpression("${\"\\\\\\\"\"}"));
    Assert.assertEquals("\"\\", evaluateExpression("${\"\\\"\\\\\"}"));
}
Also used : ELException(jakarta.el.ELException) ELException(jakarta.el.ELException) Test(org.junit.Test)

Example 8 with ELException

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

the class ELSupport method compare.

/**
 * Compare two objects, after coercing to the same type if appropriate.
 *
 * If the objects are identical, or they are equal according to
 * {@link #equals(ELContext, Object, Object)} then return 0.
 *
 * If either object is a BigDecimal, then coerce both to BigDecimal first.
 * Similarly for Double(Float), BigInteger, and Long(Integer, Char, Short, Byte).
 *
 * Otherwise, check that the first object is an instance of Comparable, and compare
 * against the second object. If that is null, return 1, otherwise
 * return the result of comparing against the second object.
 *
 * Similarly, if the second object is Comparable, if the first is null, return -1,
 * else return the result of comparing against the first object.
 *
 * A null object is considered as:
 * <ul>
 * <li>ZERO when compared with Numbers</li>
 * <li>the empty string for String compares</li>
 * <li>Otherwise null is considered to be lower than anything else.</li>
 * </ul>
 *
 * @param ctx the context in which this comparison is taking place
 * @param obj0 first object
 * @param obj1 second object
 * @return -1, 0, or 1 if this object is less than, equal to, or greater than val.
 * @throws ELException if neither object is Comparable
 * @throws ClassCastException if the objects are not mutually comparable
 */
public static final int compare(final ELContext ctx, final Object obj0, final Object obj1) throws ELException {
    if (obj0 == obj1 || equals(ctx, obj0, obj1)) {
        return 0;
    }
    if (isBigDecimalOp(obj0, obj1)) {
        BigDecimal bd0 = (BigDecimal) coerceToNumber(ctx, obj0, BigDecimal.class);
        BigDecimal bd1 = (BigDecimal) coerceToNumber(ctx, obj1, BigDecimal.class);
        return bd0.compareTo(bd1);
    }
    if (isDoubleOp(obj0, obj1)) {
        Double d0 = (Double) coerceToNumber(ctx, obj0, Double.class);
        Double d1 = (Double) coerceToNumber(ctx, obj1, Double.class);
        return d0.compareTo(d1);
    }
    if (isBigIntegerOp(obj0, obj1)) {
        BigInteger bi0 = (BigInteger) coerceToNumber(ctx, obj0, BigInteger.class);
        BigInteger bi1 = (BigInteger) coerceToNumber(ctx, obj1, BigInteger.class);
        return bi0.compareTo(bi1);
    }
    if (isLongOp(obj0, obj1)) {
        Long l0 = (Long) coerceToNumber(ctx, obj0, Long.class);
        Long l1 = (Long) coerceToNumber(ctx, obj1, Long.class);
        return l0.compareTo(l1);
    }
    if (obj0 instanceof String || obj1 instanceof String) {
        return coerceToString(ctx, obj0).compareTo(coerceToString(ctx, obj1));
    }
    if (obj0 instanceof Comparable<?>) {
        // checked above
        @SuppressWarnings("unchecked") final Comparable<Object> comparable = (Comparable<Object>) obj0;
        return (obj1 != null) ? comparable.compareTo(obj1) : 1;
    }
    if (obj1 instanceof Comparable<?>) {
        // checked above
        @SuppressWarnings("unchecked") final Comparable<Object> comparable = (Comparable<Object>) obj1;
        return (obj0 != null) ? -comparable.compareTo(obj0) : -1;
    }
    throw new ELException(MessageFactory.get("error.compare", obj0, obj1));
}
Also used : BigInteger(java.math.BigInteger) ELException(jakarta.el.ELException) BigDecimal(java.math.BigDecimal)

Example 9 with ELException

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

the class ELSupport method coerceToType.

public static final <T> T coerceToType(final ELContext ctx, final Object obj, final Class<T> type) throws ELException {
    if (ctx != null) {
        boolean originalIsPropertyResolved = ctx.isPropertyResolved();
        try {
            T result = ctx.getELResolver().convertToType(ctx, obj, type);
            if (ctx.isPropertyResolved()) {
                return result;
            }
        } finally {
            ctx.setPropertyResolved(originalIsPropertyResolved);
        }
    }
    if (type == null || Object.class.equals(type) || (obj != null && type.isAssignableFrom(obj.getClass()))) {
        @SuppressWarnings("unchecked") T result = (T) obj;
        return result;
    }
    if (!COERCE_TO_ZERO) {
        if (obj == null && !type.isPrimitive() && !String.class.isAssignableFrom(type)) {
            return null;
        }
    }
    if (String.class.equals(type)) {
        @SuppressWarnings("unchecked") T result = (T) coerceToString(ctx, obj);
        return result;
    }
    if (ELArithmetic.isNumberType(type)) {
        @SuppressWarnings("unchecked") T result = (T) coerceToNumber(ctx, obj, type);
        return result;
    }
    if (Character.class.equals(type) || Character.TYPE == type) {
        @SuppressWarnings("unchecked") T result = (T) coerceToCharacter(ctx, obj);
        return result;
    }
    if (Boolean.class.equals(type) || Boolean.TYPE == type) {
        @SuppressWarnings("unchecked") T result = (T) coerceToBoolean(ctx, obj, Boolean.TYPE == type);
        return result;
    }
    if (type.isEnum()) {
        @SuppressWarnings("unchecked") T result = (T) coerceToEnum(ctx, obj, type);
        return result;
    }
    // new to spec
    if (obj == null) {
        return null;
    }
    if (obj instanceof String) {
        String str = (String) obj;
        PropertyEditor editor = PropertyEditorManager.findEditor(type);
        if (editor == null) {
            if (str.isEmpty()) {
                return null;
            }
            throw new ELException(MessageFactory.get("error.convert", obj, obj.getClass(), type));
        } else {
            try {
                editor.setAsText(str);
                @SuppressWarnings("unchecked") T result = (T) editor.getValue();
                return result;
            } catch (RuntimeException e) {
                if (str.isEmpty()) {
                    return null;
                }
                throw new ELException(MessageFactory.get("error.convert", obj, obj.getClass(), type), e);
            }
        }
    }
    // for an empty map. The parser will always parse {} as an empty set.
    if (obj instanceof Set && type == Map.class && ((Set<?>) obj).isEmpty()) {
        @SuppressWarnings("unchecked") T result = (T) Collections.EMPTY_MAP;
        return result;
    }
    // Handle arrays
    if (type.isArray() && obj.getClass().isArray()) {
        @SuppressWarnings("unchecked") T result = (T) coerceToArray(ctx, obj, type);
        return result;
    }
    if (obj instanceof LambdaExpression && isFunctionalInterface(type)) {
        T result = coerceToFunctionalInterface(ctx, (LambdaExpression) obj, type);
        return result;
    }
    throw new ELException(MessageFactory.get("error.convert", obj, obj.getClass(), type));
}
Also used : Set(java.util.Set) PropertyEditor(java.beans.PropertyEditor) ELException(jakarta.el.ELException) LambdaExpression(jakarta.el.LambdaExpression)

Example 10 with ELException

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

the class ExpressionBuilder method createNodeInternal.

private static final Node createNodeInternal(String expr) throws ELException {
    if (expr == null) {
        throw new ELException(MessageFactory.get("error.null"));
    }
    Node n = expressionCache.get(expr);
    if (n == null) {
        ELParser parser = parserCache.pop();
        try {
            if (parser == null) {
                parser = new ELParser(new StringReader(expr));
            } else {
                parser.ReInit(new StringReader(expr));
            }
            n = parser.CompositeExpression();
            // validate composite expression
            int numChildren = n.jjtGetNumChildren();
            if (numChildren == 1) {
                n = n.jjtGetChild(0);
            } else {
                Class<?> type = null;
                Node child = null;
                for (int i = 0; i < numChildren; i++) {
                    child = n.jjtGetChild(i);
                    if (child instanceof AstLiteralExpression) {
                        continue;
                    }
                    if (type == null) {
                        type = child.getClass();
                    } else {
                        if (!type.equals(child.getClass())) {
                            throw new ELException(MessageFactory.get("error.mixed", expr));
                        }
                    }
                }
            }
            if (n instanceof AstDeferredExpression || n instanceof AstDynamicExpression) {
                n = n.jjtGetChild(0);
            }
            expressionCache.put(expr, n);
        } catch (Exception e) {
            throw new ELException(MessageFactory.get("error.parseFail", expr), e);
        } finally {
            if (parser != null) {
                parserCache.push(parser);
            }
        }
    }
    return n;
}
Also used : AstDeferredExpression(org.apache.el.parser.AstDeferredExpression) Node(org.apache.el.parser.Node) AstDynamicExpression(org.apache.el.parser.AstDynamicExpression) StringReader(java.io.StringReader) ELException(jakarta.el.ELException) ELParser(org.apache.el.parser.ELParser) AstLiteralExpression(org.apache.el.parser.AstLiteralExpression) ELException(jakarta.el.ELException)

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