我正在使用Janino评估脚本,但我不知道如何在脚本中表示值列表
<!-- https://mvnrepository.com/artifact/org.codehaus.janino/janino -->
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.1.0</version>
</dependency>我需要评估containsAll脚本,例如: attributeA.containsAll(3,5)
我尝试过使用[]和数组,但是编译器要么说:"org.codehaus.commons.compiler.CompileException:第1行,第25列:意外的标记"[“in org.codehaus.commons.compiler.CompileException:”,或者说: Arrays.asList第1行,第32列:未知变量或类型“数组”。
import com.my.project.MyCollection;
import org.codehaus.commons.compiler.CompileException;
import org.codehaus.janino.ExpressionEvaluator;
private static void evalExpression(Map<String, MyCollection> fields)
{
String script= "attributeA.containsAll([3,5])";
try {
ExpressionEvaluator ee = new ExpressionEvaluator();
Class[] parameterTypes = new Class[fields.size()];
String[] parameterNames = new String[fields.size()];
Object[] arguments = new Object[fields.size()];
int i = 0;
for (Map.Entry<String, MyCollection> field : fields.entrySet()) {
String fieldName = field.getKey();
MyCollection fieldValues = field.getValue();
parameterNames[i] = fieldName;
parameterTypes[i] = MyCollection.class;
arguments[i]=fieldValues;
i++;
}
ee.setParameters(parameterNames, parameterTypes);
ee.setExpressionType(Boolean.class);
// And now we "cook" (scan, parse, compile and load) the fabulous expression.
ee.cook(script);
// Eventually we evaluate the expression - and that goes super-fast.
Boolean result = (Boolean) ee.evaluate(arguments);
System.out.println(result);
}
catch (Exception ex){
System.out.println(ex.getMessage());
}
}我期望输出false/ true,但却抛出了org.codehaus.commons.compiler.CompileException
发布于 2019-09-15 17:32:01
我在脚本中添加了:“导入静态java.util.Arrays.asList;”
并使用以下语法: attributeA.containsAll(asList(3,5))
https://stackoverflow.com/questions/57941810
复制相似问题