Mastering Aviator: Advanced Java Expression Engine Examples
This guide demonstrates how to use the Aviator 5.2.5 expression engine on Java 8 with practical examples covering ternary operators, regex matching, variable shortcuts, nil handling, date comparisons, big number calculations, the powerful seq library, execution modes, and debugging techniques.
Environment: Java 8 + Aviator 5.2.5. Please read the previous article "Java Expression Evaluation Engine Aviator (Part 1)" first.
Example 8: Ternary Operator
<code>public class TernaryOperatorExample {
public static void main(String[] args) {
Map<String, Object> env = new HashMap<String, Object>();
env.put("num", 100);
String result = (String) AviatorEvaluator.execute("num > 998 ? 'yes':'no'", env);
System.out.println(result);
}
}</code>Aviator's ternary expression does not require the two branches to have the same result type; they can be of any type, unlike Java.
Example 9: Regular Expression Matching
<code>public class RegularExpressionExample {
public static void main(String[] args) {
String email = "3487929**@qq.com";
Map<String, Object> env = new HashMap<String, Object>();
env.put("email", email);
String username = (String) AviatorEvaluator.execute("email=~/([\\w0-8]+)@\\w+[\\.\\w+]+/ ? $1:'unknow'", env);
System.out.println(username);
}
}</code>The =~ operator matches the string against the regex; the result is Boolean, which can be used in a ternary expression. If the match succeeds, $1 returns the first capture group (the username), otherwise "unknown" is returned. This example prints the username part of the email.
Aviator supports regex literals enclosed in //, which can be used for matching, comparison, and only work with strings. Successful matches populate $0 with the whole match, $1 with the first group, and so on. Internally Aviator uses java.util.regex.Pattern, so the syntax is identical to Java.
Example 10: Variable Sugar
Aviator provides a convenient syntax sugar for accessing object properties:
a.baccesses property
bof variable
a, and
a.b.caccesses nested properties.
<code>public class Person {
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
}
public class VariableExample {
public static void main(String[] args) {
Person person = new Person("张三", 20);
Map<string, object=""> env = new HashMap<string, object="">();
env.put("person", person);
String result = (String) AviatorEvaluator.execute("'[Person name = ' + person.name + ' age = ' + person.age + ']'",</string,></string,></code>Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.