// How to use groovy in Java 1.6 with ScriptEngine // just for POC purposes import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import java.util.Arrays; import java.util.List; class Customer { Customer(String name, String code, Integer value) { this.name = name; this.code = code; this.value = value; } String name; String code; Integer value; } public class Main { public static void main(String[] args) throws ScriptException { List customers = Arrays.asList( new Customer[]{ new Customer("A","123",1), new Customer("B","456",2), new Customer("C","789",3), new Customer("D","012",4) }); setVariable(customers, "list"); evaluate("names = list.collect { it.name } "); List names = (List) getVariable("names"); System.out.println("names = " + names); } // Bunch of helper methods, just to make the above code // more readable, not necessarily more efficient private static Object getVariable(String name) { return groovy().getContext().getAttribute(name); } private static void evaluate(String script) throws ScriptException { groovy().eval(script, groovy().getContext()); } private static void setVariable(List customers, String varName) { groovy().getContext().setAttribute(varName, customers, ScriptContext.ENGINE_SCOPE); } private static ScriptEngine groovyEngine; private static ScriptEngine groovy() { if( groovyEngine == null ){ groovyEngine = new ScriptEngineManager().getEngineByName("Groovy"); } return groovyEngine; } }