Search in sources :

Example 1 with ValueExpression

use of javax.el.ValueExpression in project camel by apache.

the class JuelExpression method setVariable.

protected void setVariable(ELContext context, String name, Object value, Class<?> type) {
    ValueExpression valueExpression = getExpressionFactory().createValueExpression(value, type);
    SimpleContext simpleContext = (SimpleContext) context;
    simpleContext.setVariable(name, valueExpression);
}
Also used : SimpleContext(de.odysseus.el.util.SimpleContext) ValueExpression(javax.el.ValueExpression)

Example 2 with ValueExpression

use of javax.el.ValueExpression in project camel by apache.

the class JuelTest method testJuel.

@Test
public void testJuel() throws Exception {
    ExpressionFactory factory = new ExpressionFactoryImpl();
    ELContext context = new SimpleContext();
    ValueExpression valueExpression = factory.createValueExpression(context, "${123 * 2}", Object.class);
    Object value = valueExpression.getValue(context);
    assertEquals("Result is a Long object", 246L, value);
}
Also used : ELContext(javax.el.ELContext) SimpleContext(de.odysseus.el.util.SimpleContext) ExpressionFactory(javax.el.ExpressionFactory) ExpressionFactoryImpl(de.odysseus.el.ExpressionFactoryImpl) ValueExpression(javax.el.ValueExpression) Test(org.junit.Test)

Example 3 with ValueExpression

use of javax.el.ValueExpression 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("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 4 with ValueExpression

use of javax.el.ValueExpression 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.funciton.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] = coerceToType(ctx, varargs[j - i], target);
                        }
                        params[i] = varargs;
                    }
                } else {
                    params[i] = parameters.jjtGetChild(i).getValue(ctx);
                }
                params[i] = 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(javax.el.VariableMapper) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ELClass(javax.el.ELClass) ValueExpression(javax.el.ValueExpression) ELClass(javax.el.ELClass) ELException(javax.el.ELException) LambdaExpression(javax.el.LambdaExpression) FunctionMapper(javax.el.FunctionMapper)

Example 5 with ValueExpression

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

the class AstIdentifier method isReadOnly.

@Override
public boolean isReadOnly(EvaluationContext ctx) throws ELException {
    VariableMapper varMapper = ctx.getVariableMapper();
    if (varMapper != null) {
        ValueExpression expr = varMapper.resolveVariable(this.image);
        if (expr != null) {
            return expr.isReadOnly(ctx.getELContext());
        }
    }
    ctx.setPropertyResolved(false);
    boolean result = ctx.getELResolver().isReadOnly(ctx, null, this.image);
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get("error.resolver.unhandled.null", this.image));
    }
    return result;
}
Also used : PropertyNotFoundException(javax.el.PropertyNotFoundException) VariableMapper(javax.el.VariableMapper) ValueExpression(javax.el.ValueExpression)

Aggregations

ValueExpression (javax.el.ValueExpression)59 Test (org.junit.Test)36 ExpressionFactory (javax.el.ExpressionFactory)33 ELContext (javax.el.ELContext)27 ELContextImpl (org.apache.jasper.el.ELContextImpl)17 VariableMapper (javax.el.VariableMapper)9 ELException (javax.el.ELException)7 ELProcessor (javax.el.ELProcessor)7 MethodExpression (javax.el.MethodExpression)7 PropertyNotFoundException (javax.el.PropertyNotFoundException)4 ValueReference (javax.el.ValueReference)4 SimpleContext (de.odysseus.el.util.SimpleContext)2 HashMap (java.util.HashMap)2 ELClass (javax.el.ELClass)2 FunctionMapper (javax.el.FunctionMapper)2 ELParseException (javax.servlet.jsp.el.ELParseException)2 ExpressionFactoryImpl (de.odysseus.el.ExpressionFactoryImpl)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1