Created
September 3, 2019 19:59
-
-
Save LukeHackett/b9d61bba075c460cebc974f64855ee2a to your computer and use it in GitHub Desktop.
CompilerFactoryFactory example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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