📖 English Documentation | 📖 中文文档
Aviator
is a lightweight, high performance expression evaluator for java
.
It compiles expression to byte code and evaluate it on the fly.
- Support almost all operators, including arithmetic operators, relational operators, logical operators, bitwise operators, regular expression matching operators(
=~
), ternary expressions(?:
). - Support operator precedence, or use parentheses to specify precedence explicitly.
- Support assignment.
- Logical operators support short-circuit.
- Support for rich types such as
nil
, integers and floats, strings, regular expressions, dates, variables, etc.; Supports automatic type conversion and promotion. - Support
lambda
anonymous functions and closures. - A powerful set of commonly used function libraries is built-in.
- Support user-customized function, easy to extend, support function call point parameter list capture and import java class methods as custom functions.
- Support operator overload.
- Support big numbers(
BigInteger
) and high precision(BigDecimal
) operations. - Support multi-line expression and customized evaluator instance.
- Lightweight and high performance, offer a wide range of customization options.
- Function missing mechanism just like ruby's method msising.
See home page for details.
- 4.2.5, an usefull feature: function missing.
- 4.2.4, adds annotations for importing java class methods and some new features.
<dependency>
<groupId>com.googlecode.aviator</groupId>
<artifactId>aviator</artifactId>
<version>{version}</version>
</dependency>
Check available versions at search.maven.org.
int[] a = ...;
Map<String, Object> env = new HashMap<String, Object>();
env.put("a", a);
AviatorEvaluator.execute("1 + 2 + 3");
AviatorEvaluator.execute("a[1] + 100", env);
AviatorEvaluator.execute("'a[1]=' + a[1]", env);
// get the length of the array
AviatorEvaluator.execute("count(a)", env);
// compute the sum of the array
AviatorEvaluator.execute("reduce(a, +, 0)", env);
// check each element of array is between 0 <= e < 10.
AviatorEvaluator.execute("seq.every(a, seq.and(seq.ge(0), seq.lt(10)))", env);
// compute the sum by Lambda
AviatorEvaluator.execute("reduce(a, lambda(x,y) -> x + y end, 0)", env);
// import string instance methods
AviatorEvaluator.addInstanceFunctions("s", String.class);
AviatorEvaluator.execute("s.indexOf('hello', 'l')");
AviatorEvaluator.execute("s.replaceAll('hello', 'l', 'x')");
// import static methods
AviatorEvaluator.addStaticFunctions("sutil", StringUtils.class);
AviatorEvaluator.execute("sutil.isBlank('hello')");
// Call any public instance methods by reflection
AviatorEvaluator.setFunctionMissing(JavaMethodReflectionFunctionMissing.getInstance());
// Calling String#indexOf by reflection
System.out.println(AviatorEvaluator.execute("indexOf('hello world', 'w')"));
// Calling Long#floatValue by reflection
System.out.println(AviatorEvaluator.execute("floatValue(3)"));
// Calling BigDecimal#add by reflection
System.out.println(AviatorEvaluator.execute("add(3M, 4M)"));
See user guide for details.
- downloads: https://github.com/killme2008/aviator/releases
- documents: https://github.com/killme2008/aviator/wiki
- javadoc: http://fnil.net/aviator/apidocs/
- author: dennis (killme2008@gmail.com)
- spring boot: https://github.com/mengxiangrui007/spring-boot-rule-jsr94