Calling all javax.el juel experts :-)
We want to use JUEL to evaluate expressions in a pure Java context. These expressions are typically mathematical ones and are provided by users via a ui. An added aspect is that these expressions are part of configuration, and only get evaluated later as part of another process. Also, variable values used in the expressions are only available later as part of this process. As such we want to provide basic validation to the user at expression creation time, so that the user knows his expression won't fall over at evaluation time.
We used Janino for this before, but we are replacing this with JUEL - it is much faster (and we have a lot of evaluation to get trough , throughput is important, etc). Also JUEL works with exactly the same expression syntax - i.e. we can plug it into the backend and it is backwards compatible. (Just have to add ${..}
around the old Janino expressions.
However we have one issue with JUEL - is there any way that it can validate the expression being used before it is evaluated?
I.e. something like this :
context.getVariableMapper().setVariable("a",expressionFactory.createValueExpression(new Double(1), double.class));
context.getVariableMapper().setVariable("b", expressionFactory.createValueExpression(new Double(2), double.class));
ValueExpression expression = expressionFactory.createExpression("${a+b}", double.class, context);
Object result = expression.getValue(context);
Doing a println(result)
then correctly prints out 3.
With Janino I could cook the expression a+b, and if it was malformed or would return an incorrect return type (i.e. not a double) I would get an exception. I could then use this to validate user input on the fly, and force the user to correct any errors before e.g. saving the configuration.
Is there any way how to do this with JUEL? Or does anyone know of an alternative approach?
Thank you,
Lukas
The expression is parsed (read: validated) when "compiling" with
expressionFactory.createValueExpression("${a+b}", ...)
. Later, evaluation happens with `Expression.getValue(...).I would suggest to use
ELResolver
mechanism to resolve properties (x, y) at evaluation time rather than using variables (use these for constants, e.g. PI, etc) at parse time. To do this, set properties before evaluation usingELContext.setValue(...)
.Note that you do not need to use the same instance of
ELContext
at parse time and evaluation time. Set variables and functions in your parse context and properties in your evaluation context.