Skip to content

Instantly share code, notes, and snippets.

@LukeHackett
Created September 3, 2019 19:59
Show Gist options
  • Save LukeHackett/b9d61bba075c460cebc974f64855ee2a to your computer and use it in GitHub Desktop.
Save LukeHackett/b9d61bba075c460cebc974f64855ee2a to your computer and use it in GitHub Desktop.
CompilerFactoryFactory example
package com.bskyb.demo.janino;
import org.codehaus.commons.compiler.CompilerFactoryFactory;
import org.codehaus.commons.compiler.IExpressionEvaluator;
import java.util.Arrays;
import java.util.List;
public class Application {
public static void main(String[] args) throws Exception {
// incoming arguments
String region = "GB";
String identifier = "AA123456GB";
// Known devices
List<String> devices = Arrays.asList("foo", "bar", identifier);
// Create "ExpressionEvaluator" object.
IExpressionEvaluator ee = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();
ee.setExpressionType(boolean.class);
ee.setParameters(
new String[] { "r1", "r2" },
new Class[] { String.class, String.class }
);
ee.cook("r1.equals(r2)");
// Evaluate expression with actual parameter values.
boolean result = (Boolean) ee.evaluate(new String[] {
"GB", region
});
// Print expression result.
System.out.println("Result = " + result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment