Jexl尝试

一、引入Jar包

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-jexl3</artifactId>
    <version>3.1</version>
</dependency>

二、实例


import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.MapContext;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.HashMap;
import java.util.Map;

/**
 * Jexl尝试
 */
public class JexlUtil {
    private static JexlEngine engine = new JexlBuilder().create();

    private static JexlContext context = new MapContext();

    private static String VALUE = "value";
    private static String VALUES = "values";

    /**
     * 计算表达式的值,看源值和目标值是否匹配
     * @param value 源值
     * @param operator 运算符
     * @param values 目标值
     * @return boolean 返回布尔类型计算结果
     **/
    public static boolean evaluate(Object value, String operator, Object values) {
        String expressionStr = VALUE + " " + operator + " " +  VALUES;
        context.set(VALUE, value);
        context.set(VALUES, values);
        return (Boolean) engine.createExpression(expressionStr).evaluate(context);
    }

    private static ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    private static ScriptEngine javaScriptEngine = scriptEngineManager.getEngineByName("JavaScript");

    /**
     * 执行javaScript脚本
     * @return java.lang.Object
     **/
    public static Object executeFunction() {
        String script = "function add(x, y) { return x + y;}";
        try {
            javaScriptEngine.eval(script);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        Invocable invocable = (Invocable) javaScriptEngine;

        Object result = null;
        try {
            result = invocable.invokeFunction("add", 10, 20);
        } catch (ScriptException | NoSuchMethodException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 执行字符串表达式
     * @return void
     **/
    public static void execute()  {
        JexlContext mapContext = new MapContext();
        String script = "if (company.type == 1) {\n" +
                "  company.name = 'a';\n" +
                "  company.age = 12;\n" +
                "} else if (company.type == 3 && company.num > 100) {\n" +
                "  company.name = 'b';\n" +
                "  company.age = 13;\n" +
                "} else if (company.type == 3 && company.num > 200) {\n" +
                "  company.name = 'c';\n" +
                "  company.age = 14;\n" +
                "} else {\n" +
                "  company.name = 'd';\n" +
                "  company.age = 15;\n" +
                "}\n";

        Map<String, Object> map = new HashMap<>();
        map.put("type", "3");
        map.put("num", 200);
        mapContext.set("company", map);
        engine.createScript(script).execute(mapContext);

        System.out.println(((Map)mapContext.get("company")).get("name"));
        System.out.println(((Map)mapContext.get("company")).get("age"));
    }

    public static void main(String[] args) {
        boolean result = evaluate(1, "!=", 2);
        System.out.println(result);

        System.out.println(executeFunction());

        execute();
    }
}

三、结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值