AviatorScript: High‑Performance JVM‑Based Expression Engine and Scripting Language – Features and Usage Guide
This article introduces AviatorScript, a lightweight high‑performance expression engine and scripting language for the JVM (including Android), outlines its core features, demonstrates basic expression evaluation, variable handling, built‑in and custom functions, and shows how to run full scripts with code examples.
Aviator was originally a lightweight, high‑performance expression engine for the JVM; since version 5.0.0 it has evolved into AviatorScript, a full‑featured scripting language that runs on the JVM and Android platforms.
Key features include support for basic types (numbers, strings, regex, booleans), full Java operator set, first‑class functions with closures, built‑in bigint / decimal types with operator overloading, complete script syntax (multiline data, conditionals, loops, lexical scopes, exception handling), functional programming via the Sequence abstraction, a lightweight module system, seamless Java method invocation, sandboxing options, and an ASM‑based compilation mode that translates scripts directly to JVM bytecode for maximum performance.
Typical use cases are rule engines, formula calculations, dynamic script control, and ELT‑style collection processing.
Basic Usage
Adding the Dependency
<dependency>
<groupId>com.googlecode.aviator</groupId>
<artifactId>aviator</artifactId>
<version>5.3.3</version>
</dependency>After adding the dependency, evaluate an expression:
// returns 16
Long r = (Long) AviatorEvaluator.execute("2 * (3 + 5)");For repeated execution, compile the expression first:
Expression expression = AviatorEvaluator.compile("2 * (3 + 5)");
Long r = (Long) expression.execute();Aviator treats numeric literals as long or double , so the result above is a Long .
The engine supports arithmetic operators ( + - * / % ), logical operators ( && || ! ), comparison operators ( > >= == != < <= ), bitwise operators ( & | ^ << >> ), the ternary operator ( ?: ), and regex matching ( =~ ).
Expression Variables
Pass parameters to an expression via a map or individual arguments:
Long a = 12L;
Boolean r = (Boolean) AviatorEvaluator.exec("a > 10", a);Lists and objects can also be accessed:
List
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 r = (Boolean) AviatorEvaluator.exec("p.age > 10", p);All parameters are placed into a Map<String, Object> which can also be used to extract values from JSON structures.
Using Functions
Aviator provides many built‑in functions such as:
// round a number
Long r = (Long) AviatorEvaluator.execute("math.round(4.3)");
// string length
Long r = (Long) AviatorEvaluator.execute("string.length('hello')");
// create a list
Object r = AviatorEvaluator.execute("seq.list(1,2,3)");Custom functions can be created by extending AbstractFunction :
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"; }
}Register and use the custom function like any built‑in one:
// registration
AviatorEvaluator.addFunction(new AddFunction());
// usage
long sum = (Long) AviatorEvaluator.getInstance().execute("add(3,4)");AviatorScript Scripts
Beyond single expressions, AviatorScript can execute full scripts stored in .av files:
// 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 repository: https://github.com/killme2008/aviatorscript
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.