It is possible to use global functions in expressions. A global function is a function which is bridged to a real Java method. Evaluating the function in an expression will evaluate the associated Java method.
FunctionsDefinitions
singleton class manage a global functions repository shared by all expresssions. FunctionsDefinitions globals = FunctionsDefinitions.getInstance(); def.addFunction("method", instance, method);Note that as for variables, global functions are shared by all expressions.
public class MyClass { public int myAdd(int a, int b) { return a + b; } }And now add it to the global functions repository:
Method method = MyClass.class.getMethod("myAdd", Integer.TYPE, Integer.TYPE); MyClass instance = new MyClass(); FunctionsDefinitions globals = FunctionsDefinitions.getInstance(); def.addFunction("add", instance, method);
Equation equation = ExpressionJ.parse("add(2, 3) + 2"); Objet value = equation.eval();The result of value will be 7.
Copyright 2018 Herve Girod. All Rights Reserved