我正在尝试使用Mvel java库来评估表达式。表达式是检查列表是否包含给定的字符串,但是在执行程序时我得到ClassCastException。以下是代码段。
感谢任何帮助,谢谢!
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>
<groupId>org.mvel</groupId>
<artifactId>mvel2</artifactId>
<version>2.3.1.Final</version>
</dependency>
堆栈跟踪:
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