groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ravi Kapoor <ravikapoor...@gmail.com>
Subject how to dynamically evaluate expression in java code running inside groovy
Date Wed, 08 Jul 2015 19:11:17 GMT
I am trying to run my whole java project in Groovy.
I have run into an issue i.e. anytime I want to do anything dynamically, I
have to bind all possible things that may exist inside the expression.
This is very inefficient.

Instead I want to be able to pass my existing context to groovy shell. How
do I do this?

I am attaching a sample program highlighting the issue. This seems such an
easy and obvious feature, but I am so stuck here. Thanks for helping.

Ravi


--------------------------------------------------------------------------------
package com.test;

import java.util.HashMap;
import java.util.Map;

import groovy.lang.*;

public class Test {

public static void main(String[] args) {
GroovyClassLoader classLoader = new GroovyClassLoader();
    Binding binding = new Binding();
    Map<Object, Object> attributes = new HashMap<Object, Object>();

    GroovyShell shell = new GroovyShell(classLoader, binding);
    binding.setVariable("shell", shell);
shell.evaluate("new com.test.Test().runInsideGroovy(shell)");

}

public void runInsideGroovy(GroovyShell shell) {
System.out.println("inside groovy");

String printme = "secret";
shell.evaluate("System.err.println(printme)");
}
}

Mime
View raw message