Using Groovy in Java for custom expression resolution

Source: Internet
Author: User


As a jvm-based language, groovy is increasing in popularity. This article demonstrates how the Groovy.lang.Script class, inherited by GdK, supports custom expression parsing in a Java class.

Input:

Represents a map structure for a row of data. In practical applications, the most common scenario in which this structure is generated may be access to the database through JDBC, a result set of rows obtained by invoking the WebService service, and so on.

Target setting:

Suppose we want to perform an operation on the input data. In the example here, we simulate the most commonly used NVL function in Oracle.


Processing process:
First, define your own expression resolution class by inheriting Groovy.lang.Script:

public class Mybasicscript extends Script

  

Implement specific parsing methods in this class:

public static object Nvl (Object Str,object val) {  return Str==null | | "". Equals (str)? Val:str;}

Second, instantiate a Compilerconfiguration object based on the above custom class.

Compilerconfiguration cfg = new compilerconfiguration (); Cfg.setscriptbaseclass (MyBasicScript.class.getName ());

  

Instantiate a Groovyshell object with this compilerconfiguration instance as a parameter

Shell = new Groovyshell (CFG);

  

Parses and runs an expression through a Shell object. Before running, you can bind script runtime context data through the Bingding object:

Binding binding = new binding (map); Script script = Shell.parse (expr); script.setbinding (binding); Script.run ();

  

Complete code example (two classes, custom script implementation classes, calls, and test classes, respectively)

Package Jg.groovy;import Groovy.lang.script;import Java.lang.reflect.method;public class Mybasicscript extends Script  

  

Package Jg.groovy;import Groovy.lang.binding;import Groovy.lang.groovyshell;import groovy.lang.script;import Java.util.hashmap;import Java.util.hashtable;import Java.util.map;import Org.codehaus.groovy.control.compilerconfiguration;public class Exprsupport {private static final Object lock = new Object ();p rivate static final Groovyshell shell;private static hashtable<string, script> cache = new Hashtable<s Tring, script> (); static {compilerconfiguration cfg = new compilerconfiguration (); Cfg.setscriptbaseclass ( MyBasicScript.class.getName ()); Shell = new Groovyshell (CFG);} public static Object parseexpr (String expr) {Script s = getscriptfromcache (expr); return S.run ();} public static Object parseexpr (String expr, map<?,? > map) {Binding binding = new binding (map); Script script = Getscriptfromcache (expr); script.setbinding (binding); return Script.run ();} private static Script Getscriptfromcache (String expr) {if (Cache.contains (expr)) {return cache.get (expr);} Synchronized (lock){if (Cache.contains (expr)) {return cache.get (expr);} Script script = Shell.parse (expr); Cache.put (expr, script); return script;}} /** * @param args */public static void main (string[] args) {//eg. get one row from dbmap<string, object> row = new Hashmap<string, Object> (), Row.put ("id", "" "), Row.put (" name "," ");//Call mode with bound data parameter System.out.println ( Exprsupport.parseexpr ("NVL (id,0)", Row)); System.out.println (exprsupport.parseexpr ("NVL (name, ' Anonymous ')", row)), or the call method without the binding data parameter, This is groovy's built-in capability System.out.println (exprsupport.parseexpr ("1+2"));}}

  

Output:

42anonymous3

Summary: Combined with Groovy's built-in support for expressions and custom scripting capabilities, you can implement powerful expression parsing capabilities.

Using Groovy in Java for custom expression resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.