AviatorScript: High‑Performance JVM Expression Engine – Features, Usage, and Examples
AviatorScript is a lightweight, high‑performance JVM expression engine and full‑featured scripting language that supports Java‑like operators, first‑class functions, closures, high‑precision arithmetic, script syntax, modularity, sandboxing, and can compile to bytecode for fast rule‑engine, formula, and ELT use cases.
Aviator is a lightweight, high‑performance expression engine built on the JVM. Since version 5.0.0 it has been upgraded to AviatorScript, a full‑featured scripting language that runs on the JVM, including Android.
Key features:
Supports basic types (numbers, strings, regex, booleans) and all Java operators with correct precedence.
Functions are first‑class citizens; closures and functional programming are supported.
Built‑in bigint/decimal types with operator overloading for high‑precision arithmetic.
Complete script syntax: multi‑line data, conditionals, loops, lexical scopes, exception handling.
Sequence abstraction for functional collection processing.
Lightweight module system.
Easy Java method invocation and full Java‑script API.
Customizable sandbox or full‑featured language.
ASM mode compiles scripts to JVM bytecode for maximum speed; interpreter mode works on non‑standard JVM platforms.
Typical use cases include rule engines, formula calculations, dynamic script control, and ELT on collections.
Basic usage
Add the Maven dependency:
<dependency>
<groupId>com.googlecode.aviator</groupId>
<artifactId>aviator</artifactId>
<version>5.3.3</version>
</dependency>Evaluate a simple expression:
Long r = (Long) AviatorEvaluator.execute("2 * (3 + 5)"); // r = 16Compile once and reuse:
Expression exp = AviatorEvaluator.compile("2 * (3 + 5)");
Long r = (Long) exp.execute();Expressions support numbers, strings, booleans, arithmetic, logical, comparison, bitwise operators, ternary, and regex:
String s = (String) AviatorEvaluator.execute("'hello' + ' world'"); // "hello world"
Boolean b = (Boolean) AviatorEvaluator.execute("100 > 80 && 30 < 40"); // true
Long t = (Long) AviatorEvaluator.execute("100 > 80 ? 30 : 40"); // 30
Boolean re = (Boolean) AviatorEvaluator.execute("'hello' =~ /[\\w]+/"); // trueVariables
Pass parameters directly:
Long a = 12L;
Boolean r = (Boolean) AviatorEvaluator.exec("a > 10", a);Pass a list or map:
List
a = new ArrayList<>();
a.add(12L);
a.add(20L);
Boolean r = (Boolean) AviatorEvaluator.exec("a[0] > 10", a);Pass a custom object:
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);Or a map for JSON‑like access:
Map
env = new HashMap<>();
env.put("person", new Person("movee", 25));
Object value = AviatorEvaluator.execute("person.name", env);Functions
Built‑in functions such as math.round, string.length, seq.list are available:
Long r1 = (Long) AviatorEvaluator.execute("math.round(4.3)"); // 4
Long r2 = (Long) AviatorEvaluator.execute("string.length('hello')"); // 5
Object list = AviatorEvaluator.execute("seq.list(1,2,3)"); // [1,2,3]Define a custom function 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"; }
}
AviatorEvaluator.addFunction(new AddFunction());
Long sum = (Long) AviatorEvaluator.getInstance().execute("add(3,4)"); // 7AviatorScript
AviatorScript allows full script execution:
Object r = AviatorEvaluator.execute("if (true) { return 1; } else { return 2; }");Scripts are stored in *.av files and can be compiled and executed with parameters:
Map
env = new HashMap<>();
env.put("a", 30);
Expression script = AviatorEvaluator.getInstance().compileScript("./hello.av", true);
Object result = script.execute(env);For more details see the official repository: https://github.com/killme2008/aviatorscript
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
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.