1

我正在使用 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])

我尝试过使用 [] 和 Arrays.asList,但编译器要么说:“org.codehaus.commons.compiler.CompileException:第 1 行,第 25 列:意外的令牌“[”在主目录中”,要么说:org.codehaus。 commons.compiler.CompileException:第 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

4

1 回答 1

0

我在脚本中添加了:“import static java.util.Arrays.asList;”

并使用语法:attributeA.containsAll(asList(3,5))

于 2019-09-15T09:32:00.753 回答