Backend Development 9 min read

A Comprehensive Introduction to Aviator: Lightweight JVM Expression Engine and Script Language

This article introduces Aviator, a high‑performance lightweight JVM‑based expression engine and script language, detailing its features, usage scenarios, code examples for evaluating expressions, variables, custom functions, and script execution, making it suitable for rule engines and dynamic calculations.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
A Comprehensive Introduction to Aviator: Lightweight JVM Expression Engine and Script Language

Earlier posts compared several rule engines; this article focuses on Aviator, a lightweight, high‑performance JVM‑based expression engine that evolved into AviatorScript, a script language that runs on the JVM and Android.

Key Features

Supports basic types (numbers, strings, regex, booleans) and all Java operators with correct precedence.

Functions are first‑class citizens, supporting closures and functional programming.

Built‑in bigint / decimal types for large integer and high‑precision arithmetic, with operator overloading ( +-*/ ).

Full script syntax: multiline data, conditionals, loops, lexical scopes, exception handling.

Functional programming with Sequence abstraction for collection handling.

Lightweight module system.

Easy Java method invocation and complete Java script API.

Customizable sandbox and full‑featured language options.

ASM mode compiles scripts to JVM bytecode for maximum performance; interpreter mode works on Android and other non‑standard Java platforms.

Typical Use Cases

Rule evaluation and rule engines.

Formula calculations.

Dynamic script control.

ELT and collection data processing.

Aviator Basic Usage

Dependency

com.googlecode.aviator
aviator
5.3.3

Evaluating an Expression

// 返回值为16
Long r = (Long) AviatorEvaluator.execute("2 * (3 + 5)");

Compiling for Reuse

Expression expression = AviatorEvaluator.compile("2 * (3 + 5)");
Long r = (Long) expression.execute();

Aviator treats numbers as long or double and supports arithmetic, logical, comparison, bitwise, ternary, and regex operators.

Examples

// 返回 hello world
String r = (String) AviatorEvaluator.execute("'hello' + ' world'");

// 返回 true
Boolean r = (Boolean) AviatorEvaluator.execute("100 > 80 && 30 < 40");

// 三元表达式,返回 30
Long r = (Long) AviatorEvaluator.execute("100 > 80 ? 30 : 40");

// 正则表达式,返回 true
Boolean r = (Boolean) AviatorEvaluator.execute("'hello' =~ /[\\w]+/");

Expression Variables

Long a = 12L;
Boolean r = (Boolean) AviatorEvaluator.exec("a > 10", a);

Lists and objects can also be passed:

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 a = new Person("movee", 25);
Boolean r = (Boolean) AviatorEvaluator.exec("a.age > 10", a);
Map
env = new HashMap<>();
env.put("person", new Person("movee", 25));
env.put("a", 20L);
Object result = AviatorEvaluator.execute("person.name", env);

JSON extraction is straightforward:

String jsonStr = "{\n    \"a\": {\n        \"b\": [\n            {\"x\": 3},\n            {\"x\": 4}\n        ]\n    }\n}";
JSONObject jsonObj = new JSONObject(jsonStr);
Object value = AviatorEvaluator.execute("a.b[0]['x']", jsonObj.toMap()); // returns 3

Using Built‑in Functions

// 返回4
Long r = (Long) AviatorEvaluator.execute("math.round(4.3)");

// 返回5
Long r = (Long) AviatorEvaluator.execute("string.length('hello')");

// 返回 ArrayList [1,2,3]
Object r = AviatorEvaluator.execute("seq.list(1,2,3)");

Custom Functions

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

AviatorScript extends the engine to full scripts.

// 返回1
Object r = AviatorEvaluator.execute("if (true) { return 1; } else { return 2; }");

Script files typically use the .av extension. Example hello.av :

if (a > 10) {
    return 10;
} else {
    return a;
}
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

Javarule engineaviatorexpression-enginescripting
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.