Features • Limitations • Installation • Usage
- Zero dependencies
- Alternative to Scripting for the Java Platform JSR-223
- Different scripting language implementations
- Compile scripts inline, load from the classpath or roll your own
ScriptNameResolver
- Expose your utility classes into the scripting language
- No handling of script engine/language mismatch
- Only one script engine implementation from the classpath is picked up
Add JCenter as a repository to your Maven POM:
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>jcenter</id>
<name>JCenter</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
Add the following dependency:
<dependency>
<groupId>de.dmeiners.mapping</groupId>
<artifactId>mapping-api</artifactId>
<version>1.0.0</version>
</dependency>
Important: Adding the Mapping API dependency alone will result in a no-op fallback implementation. See the next section for a list of implementation modules.
- Apache Commons JEXL
- MVFLEX Expression Language (experimental)
- Java-on-Java (experimental)
PostProcessor processor = PostProcessorFactory.builder().build(); // <1>
String someObject = "Hello"; // <2>
Script script = processor.compileInline("target += ' World!'"); // <3>
String result = script.execute(someObject, Collections.emptyMap()); // <4>
assertThat(result).isEqualTo("Hello World!");
- Create a new post processor
- Create a very simple target object
- Compile a simple script (scripting language dependent on the implementation available on the classpath, here it is Apache Commons JEXL)
- Execute the script, result type is guaranteed to the original type
PostProcessor processor = PostProcessorFactory.builder().build();
Map<String, Object> user = new HashMap<>(); // <1>
user.put("firstName", "John");
user.put("lastName", "Doe");
Map<String, Object> context = new HashMap<>(); // <2>
context.put("user", user);
String someObject = "Hello";
String scriptText = "target += ` ${user.firstName} ${user.lastName}`"; // <3>
Script script = processor.compileInline(scriptText);
String result = script.execute(someObject, context);
assertThat(result).isEqualTo("Hello John Doe");
- Target object is a Map this time
- We create another map to use as script context
- Beware that the script text is dependent on the available implementation on the classpath, here it is Apache Commons JEXL
PostProcessor processor = PostProcessorFactory.builder()
.extension("StringUtils", StringUtils.class) // <1>
.extension("tools", new Tools()) // <2>
.build();
Map<String, Object> user = new HashMap<>();
user.put("firstName", "John");
user.put("lastName", "Doe");
Map<String, Object> context = new HashMap<>();
context.put("user", user);
String someObject = "Hello";
String scriptText = "if (StringUtils.isNotBlank(user.firstName)) { target = 'First name is not blank and reversed ' + tools.reverse(user.firstName); }"; // <2>
Script script = processor.compileInline(scriptText);
String result = script.execute(someObject, context);
assertThat(result).isEqualTo("First name is not blank and reversed nhoJ");
- We can register classes to expose static methods to the scripting language
- We can register objects to expose instance methods to the scripting language