Aviator: A Lightweight High‑Performance JVM Expression Engine and Script Language
This article introduces Aviator, a lightweight high‑performance JVM‑based expression engine that evolved into AviatorScript, detailing its core features, typical use cases, basic syntax, variable handling, built‑in and custom functions, and how to run scripts and compile expressions.
Today we introduce Aviator, a lightweight rule engine that started as a high‑performance JVM expression engine and, since version 5.0.0, has become AviatorScript—a lightweight scripting language running on the JVM (including Android).
Key features of Aviator include support for basic types (numbers, strings, regex, booleans), full Java operator support, first‑class functions with closures, built‑in bigint/decimal types with operator overloading ( +-*/ ), complete script syntax (multiline data, conditionals, loops, lexical scope, exception handling), functional programming with Sequence, a lightweight module system, seamless Java method invocation, extensive customization for sandboxing or full‑language use, and high performance via ASM bytecode generation or an interpreter mode for non‑standard JVM platforms.
Typical scenarios are rule evaluation, formula calculation, dynamic script control, and ELT on collection data.
Aviator Basic Usage
Basic Expression
To use Aviator, add the Maven dependency:
<dependency>
<groupId>com.googlecode.aviator</groupId>
<artifactId>aviator</artifactId>
<version>5.3.3</version>
</dependency>Then evaluate an expression:
// returns 16
Long r = (Long) AviatorEvaluator.execute("2 * (3 + 5)");For better performance, compile the expression first:
Expression expression = AviatorEvaluator.compile("2 * (3 + 5)");
Long r = (Long) expression.execute();Aviator handles numbers, strings, booleans, and supports most arithmetic, logical, comparison, bitwise, ternary, and regex operators.
Expression Variables
Variables can be passed as parameters:
Long a = 12L;
Boolean r = (Boolean) AviatorEvaluator.exec("a > 10", a);Lists and objects can also be used as the environment:
List<Long> a = new ArrayList<>();
a.add(12L);
a.add(20L);
Boolean r = (Boolean) AviatorEvaluator.exec("a[0] > 10", a);
public static class Person {
private String name;
private Integer age;
}
Person p = new Person("movee", 25);
Boolean r2 = (Boolean) AviatorEvaluator.exec("a.age > 10", p);
Map
env = new HashMap<>();
env.put("person", new Person("movee", 25));
env.put("a", 20L);
Object result = AviatorEvaluator.execute("person.name", env);Using Functions
Aviator provides many built‑in functions:
// round
Long r1 = (Long) AviatorEvaluator.execute("math.round(4.3)");
// string length
Long r2 = (Long) AviatorEvaluator.execute("string.length('hello')");
// sequence list
Object r3 = AviatorEvaluator.execute("seq.list(1,2,3)");Custom functions can be created by extending AbstractFunction and registering them:
public class AddFunction extends AbstractFunction {
@Override
public AviatorObject call(Map
env, AviatorObject arg1, AviatorObject arg2) {
long num1 = FunctionUtils.getNumberValue(arg1, env).longValue();
long num2 = FunctionUtils.getNumberValue(arg2, env).longValue();
return AviatorLong.valueOf(num1 + num2);
}
@Override
public String getName() { return "add"; }
}
AviatorEvaluator.addFunction(new AddFunction());
long sum = (Long) AviatorEvaluator.getInstance().execute("add(3,4)");AviatorScript
Since the upgrade, Aviator can run full scripts:
// returns 1
Object r = AviatorEvaluator.execute("if (true) { return 1; } else { return 2; }");Scripts are stored in files with the .av extension, e.g., hello.av :
if (a > 10) {
return 10;
} else {
return a;
}Execute the script with parameters:
Map
env = new HashMap<>();
env.put("a", 30);
Expression exp = AviatorEvaluator.getInstance().compileScript("./hello.av", true);
Object result = exp.execute(env);Official documentation: https://github.com/killme2008/aviatorscript
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.