I am trying to evaluate an expression using Mvel java library. The expression is to check if a list contains a given string, but I am getting ClassCastException when ececuting the program. Below is the code snippet.
Any help is appreciated, Thanks!
import org.mvel2.MVEL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by rakesh on 25-Nov-16.
*/
public class Test {
public static void main(String[] args) {
Map<String, Object> evalParams = new HashMap<>();
List<String> list = Arrays.asList("abc", "xyz", "asd");
evalParams.put("subType",list);
// evalParams.put("contentType","Hotfixes");
System.out.println();
// System.out.println(MVEL.evalToBoolean("contentType == \"Hotfixes\" && \"abc\" in subType", evalParams));
System.out.println(MVEL.evalToBoolean("\"abc\" in subType", evalParams));
}
}
Maven dependency details:
<dependency>
<groupId>org.mvel</groupId>
<artifactId>mvel2</artifactId>
<version>2.3.1.Final</version>
</dependency>
Stacktrace:
Exception in thread "main" [Error: syntax error or incompatable types]
[Near : {... "abc" in subType ....}]
^
[Line: 1, Column: 10]
at org.mvel2.compiler.AbstractParser.reduce(AbstractParser.java:2617)
at org.mvel2.compiler.AbstractParser.arithmeticFunctionReduction(AbstractParser.java:2522)
at org.mvel2.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:147)
at org.mvel2.MVELInterpretedRuntime.parse(MVELInterpretedRuntime.java:47)
at org.mvel2.MVEL.eval(MVEL.java:288)
at org.mvel2.MVEL.evalToBoolean(MVEL.java:612)
at org.mvel2.MVEL.evalToBoolean(MVEL.java:657)
at Test.main(Test.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
at org.mvel2.compiler.AbstractParser.reduceNumeric(AbstractParser.java:2643)
at org.mvel2.compiler.AbstractParser.reduce(AbstractParser.java:2613)
... 12 more