首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Janino脚本中表示值列表

如何在Janino脚本中表示值列表
EN

Stack Overflow用户
提问于 2019-09-15 14:52:28
回答 1查看 327关注 0票数 1

我正在使用Janino评估脚本,但我不知道如何在脚本中表示值列表

代码语言:javascript
复制
<!-- 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列:未知变量或类型“数组”。

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2019-09-15 17:32:01

我在脚本中添加了:“导入静态java.util.Arrays.asList;”

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57941810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档