Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

AviatorScript: Lightweight JVM Expression Engine & Scripting Language Guide

This tutorial introduces AviatorScript, a high-performance JVM expression engine that evolved into a full scripting language, covering Maven setup, expression evaluation, variable binding, built-in functions, custom function creation, and script file execution with code examples.

Overview

Aviator began as a lightweight, high-performance expression engine for the JVM. Starting with version 5.0.0, it evolved into AviatorScript — a full scripting language that runs on the JVM (including Android) with both interpreted and ASM bytecode-compilation modes.

Key Features

Supports primitive types: numbers, strings, booleans, regular expressions

Full Java operator support with correct precedence

Functions as first-class citizens with closure and functional programming support

Built-in bigint / decimal types for arbitrary-precision arithmetic with operator overloading ( +-*/)

Complete script syntax: multi-line statements, conditionals, loops, lexical scoping, exception handling

Functional programming combined with Sequence abstraction for collection processing

Lightweight module system

Multiple ways to call Java methods; full Java Scripting API support

Rich customization options for secure sandbox or full-featured language use

High performance: ASM mode compiles scripts directly to JVM bytecode; interpreted mode runs on non-standard Java platforms like Android

Use Cases

Rule evaluation and rule engines

Formula calculation

Dynamic script control

Collection data ETL

Basic Usage

Maven Dependency

<dependency>
  <groupId>com.googlecode.aviator</groupId>
  <artifactId>aviator</artifactId>
  <version>5.3.3</version>
</dependency>

Expression Evaluation

Direct evaluation:

Long r = (Long) AviatorEvaluator.execute("2 * (3 + 5)"); // returns 16

Compile once, execute repeatedly for performance:

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

Supported Operators

Arithmetic ( + - * / %), logical ( && || !), comparison ( > >= == != < <=), bitwise ( & | ^ << >>), ternary ( ?:), regex ( =~).

Examples

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

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

// Ternary
Long r = (Long) AviatorEvaluator.execute("100 > 80 ? 30 : 40"); // 30

// Regex
Boolean r = (Boolean) AviatorEvaluator.execute("'hello' =~ /[\\w]+/"); // true

Variables

Pass single values:

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

Pass a list:

List<Long> a = new ArrayList<>();
a.add(12L);
a.add(20L);
Boolean r = (Boolean) AviatorEvaluator.exec("a[0] > 10", a);

Pass an object (property access):

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);

Pass a map for multiple variables:

Map<String, Object> env = new HashMap<>();
env.put("person", new Person("movee", 25));
env.put("a", 20L);
Object result = AviatorEvaluator.execute("person.name", env);

JSON extraction example:

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

Built-in Functions

Long r = (Long) AviatorEvaluator.execute("math.round(4.3)"); // 4
Long r = (Long) AviatorEvaluator.execute("string.length('hello')"); // 5
Object r = AviatorEvaluator.execute("seq.list(1,2,3)"); // [1,2,3]

Full function list: https://github.com/killme2008/aviatorscript

Custom Functions

Extend AbstractFunction and implement call and getName:

public class AddFunction extends AbstractFunction {
  @Override
  public AviatorObject call(Map<String, Object> 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:

AviatorEvaluator.addFunction(new AddFunction());
long sum = (Long) AviatorEvaluator.getInstance().execute("add(3,4)"); // 7

AviatorScript Scripts

Scripts use .av extension. Example hello.av:

if (a > 10) {
  return 10;
} else {
  return a;
}

Compile and execute:

Map<String, Object> env = new HashMap<>();
env.put("a", 30);
Expression exp = AviatorEvaluator.getInstance().compileScript("./hello.av", true);
Object result = exp.execute(env); // returns 10

Official Documentation

GitHub repository: https://github.com/killme2008/aviatorscript

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMRule EngineDSLExpression EngineScripting LanguageAviatorScriptBytecode Compilation

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion