User will enter some formula/expression.
I want to check whether the formula/expression which is a String input to my function is correct(as per MVEL standard) or not.
Following is a valid expression,
String validFormula = "if(dueDate > "2015-12-12") {a*b} else {a+b}";
Following is incorrect expression,
String invalidFormula = "if(dueDate > 2015-12-12) {a*b} else {a+b}";
//Quotes are missing for date
Following is code snippet,
public Formula save(String formula)
{
// want to call MVEL api to check if formula/expression is valid or not
...
}
Is there any api provided by MVEL which accepts expression String and return boolean/throw exception if expression is incorrect?
you can validate the expression using
Serializable compiledFormula = MVEL.compileExpression(validFormula);
Serializable compiledFormula = MVEL.compileExpression(invalidFormula);
This results in org.mvel2.CompileException
if invalid.
This works with mvel 2.2.6, cannot guarantee the API compatibility or the functionality with older versions of library.
Also please note, in your case both expressions will be valid since MVEL does not know the data type of duedate
variable, which will be known only at the run time, when you actually execute the expression. Hope this helps...