Search in sources :

Example 6 with ELException

use of javax.el.ELException in project tomcat70 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(javax.el.ELException) Method(java.lang.reflect.Method) FunctionMapper(javax.el.FunctionMapper)

Example 7 with ELException

use of javax.el.ELException in project tomcat70 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) {
        throw new ELException(MessageFactory.get("error.fnMapper.method", this.getOutputName()));
    }
    Class<?>[] paramTypes = m.getParameterTypes();
    Object[] params = null;
    Object result = null;
    int inputParameterCount = this.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] = this.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] = this.jjtGetChild(j).getValue(ctx);
                            varargs[j - i] = coerceToType(varargs[j - i], target);
                        }
                        params[i] = varargs;
                    }
                } else {
                    params[i] = this.jjtGetChild(i).getValue(ctx);
                }
                params[i] = coerceToType(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 : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ELException(javax.el.ELException) FunctionMapper(javax.el.FunctionMapper)

Example 8 with ELException

use of javax.el.ELException in project tomcat70 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("Identity '" + this.image + "' was null and was unable to invoke");
    } else {
        throw new ELException("Identity '" + this.image + "' does not reference a MethodExpression instance, returned type: " + obj.getClass().getName());
    }
}
Also used : VariableMapper(javax.el.VariableMapper) ValueExpression(javax.el.ValueExpression) ELException(javax.el.ELException) MethodExpression(javax.el.MethodExpression) MethodNotFoundException(javax.el.MethodNotFoundException)

Example 9 with ELException

use of javax.el.ELException in project tomcat70 by apache.

the class TestELParser method doTestParser.

private void doTestParser(String input, String expectedResult, String expectedBuilderOutput) throws JasperException {
    ELException elException = null;
    String elResult = null;
    // Don't try and evaluate expressions that depend on variables or functions
    if (expectedResult != null) {
        try {
            ExpressionFactory factory = ExpressionFactory.newInstance();
            ELContext context = new ELContextImpl();
            ValueExpression ve = factory.createValueExpression(context, input, String.class);
            elResult = ve.getValue(context).toString();
            Assert.assertEquals(expectedResult, elResult);
        } catch (ELException ele) {
            elException = ele;
        }
    }
    Nodes nodes = null;
    try {
        nodes = ELParser.parse(input, false);
        Assert.assertNull(elException);
    } catch (IllegalArgumentException iae) {
        Assert.assertNotNull(elResult, elException);
        // Not strictly true but enables us to report both
        iae.initCause(elException);
        throw iae;
    }
    TextBuilder textBuilder = new TextBuilder(false);
    nodes.visit(textBuilder);
    Assert.assertEquals(expectedBuilderOutput, textBuilder.getText());
}
Also used : ELContext(javax.el.ELContext) TextBuilder(org.apache.jasper.compiler.ELParser.TextBuilder) ExpressionFactory(javax.el.ExpressionFactory) ValueExpression(javax.el.ValueExpression) ELContextImpl(org.apache.jasper.el.ELContextImpl) ELException(javax.el.ELException) Nodes(org.apache.jasper.compiler.ELNode.Nodes)

Example 10 with ELException

use of javax.el.ELException in project tomcat70 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(javax.el.ELException) ELException(javax.el.ELException) Test(org.junit.Test)

Aggregations

ELException (javax.el.ELException)23 ValueExpression (javax.el.ValueExpression)6 Method (java.lang.reflect.Method)5 ELContext (javax.el.ELContext)4 Test (org.junit.Test)4 ExpressionFactory (javax.el.ExpressionFactory)3 ELContextImpl (org.apache.jasper.el.ELContextImpl)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 FunctionMapper (javax.el.FunctionMapper)2 ExpressionBuilder (org.jboss.el.lang.ExpressionBuilder)2 BeansException (org.springframework.beans.BeansException)2 WebApplicationContext (org.springframework.web.context.WebApplicationContext)2 ExpressionFactoryImpl (de.odysseus.el.ExpressionFactoryImpl)1 AuthorizationException (fi.otavanopisto.security.AuthorizationException)1 FileNotFoundException (java.io.FileNotFoundException)1 StringReader (java.io.StringReader)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Properties (java.util.Properties)1 EJBException (javax.ejb.EJBException)1