Estoy evaluando una expresión Javascript en línea usando ScriptEngine de Java 8.
Expresión de ejemplo
(_tranxAmount > 100) && (profile.received('daily', 'avg', false) < 100) Donde profile es un objeto de clase Java de vinculación de alcance global y received() es su función local. Evalué con éxito esta expresión usando scriptingEngine.eval(script) y está llamando a profile.received('daily', 'avg', false) Quiero decir functionsManager.received('daily', 'avg', false) también .
El motor de secuencias de comandos no pudo evaluar la expresión de JavaScript si contiene más de una función de Java. En ese caso, solo se llama a la primera función de Java. Cómo resolver este problema?
Por ejemplo
(_tranxAmount > 100) && (profile.received('daily', 'avg', false) < 100) && (profile.sent('monthly', 'count', false) < 5)Motor de secuencias de comandos:
private static ScriptEngine scriptingEngine;Constructor interno de la clase principal
scriptingEngine = new ScriptEngineManager().getEngineByName("JavaScript"); scriptingEngine.getBindings(ScriptContext.GLOBAL_SCOPE).put(PROFILING_FUNCTIONS_PREFIX, functionsManager);Evaluación de la expresión
Bindings bindings = new SimpleBindings(customMapOfVaraibles); CompiledScript compiled = ((Compilable) scriptingEngine).compile(" (_tranxAmount > 100) && (profile.received('daily', 'avg', false) < 100) && (profile.sent('monthly', 'count', false) < 5)"); compiled .eval(bindings) Los métodos de functionsManager se ven así
public BigDecimal received(String intervalType, String statisticsType, boolean considerTranxType) throws InvalidRequirementsException { //Do calculations return calculatedValue; } public BigDecimal sent(String intervalType, String statisticsType, boolean considerTranxType) throws InvalidRequirementsException { //Do calculations return calculatedValue; }