groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ravi Kapoor <ravikapoor...@gmail.com>
Subject Re: how to dynamically evaluate expression in java code running inside groovy
Date Mon, 13 Jul 2015 18:12:02 GMT
I tried to write a program again to demonstrate what I mean, but this time
it works :)
Essentially I was able to run code that was not visible. If you see code
below, the method Calculator.hypotenuse is private, so should not be
visible outside class.
However I was able to execute it from outside using closures.

This solves all my problems, now I am good.
Thanks so much


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

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

import groovy.lang.*;

public class Test2 {

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);
    Test2 test = new Test2();
    binding.setVariable("test", test);
shell.evaluate("test.runInsideGroovy(shell)");
}

public void runInsideGroovy(GroovyShell shell) {
Calculator calculator = new Calculator();
System.out.println("inside groovy");
System.out.println(runInContext(calculator, "hypotenuse(3.0, 4.0)", shell));
}

public Object runInContext(Object context, String script, GroovyShell
shell) {
    Closure cl = (Closure) shell.evaluate("{->" + script + "}");
    cl.setDelegate(context);
    cl.setResolveStrategy(Closure.DELEGATE_FIRST);
    return cl.call();
}
}

class Calculator {
    private double hypotenuse(double width, double height) {
        return Math.sqrt(width * width + height * height);
    }
}


On Mon, Jul 13, 2015 at 5:42 AM, Winnebeck, Jason <
Jason.Winnebeck@windstream.com> wrote:

> Not sure what you mean by "in Java". Obviously the DSL script has to be in
> Groovy. If you mean the "Calculator" class, I would expect that article to
> work whether or not the Calculator class is written in Java or Groovy.
>
> Jason
>
> -----Original Message-----
> From: Ravi [mailto:ravikapoor101@gmail.com]
> Sent: Sunday, July 12, 2015 10:40 PM
> To: users@groovy.incubator.apache.org
> Subject: Re: how to dynamically evaluate expression in java code running
> inside groovy
>
>
> Thanks for your explanation Jason. This helped me understand the core
> issue and I can easily find many workarounds.
>
> Another followup question. Is it possible to execute the code within
> context? I found this article, tried to replicate in java but it wouldn't
> work. Is it possible to do something like this in Java?
>
> http://www.sdidit.nl/2012/12/groovy-dsl-executing-scripts-within.html
>
>
>
>
> On 7/8/2015 12:24 PM, Winnebeck, Jason wrote:
> > When you run a GroovyShell execute it is like you are creating a class
> > with that code in it. The “printme” variable is a local variable to
> > the method, so it cannot be seen outside of that method. Using Groovy
> > doesn’t allow you do violate the scope of Java – a class can’t know
> > the local variables of the method that created/called it.
> >
> > You have to do what you say, that is put into the shell’s binding all
> > of the variables that you might possibly use, which is the common
> > strategy for this problem. A class to hold all of the data can be used
> > if you want to limit the number of bindings (i.e. some kind of state
> class).
> > The closest that you can get to what you are doing is to transform all
> > of the local variables you want to access into instance fields and
> > then set “this” into the binding.
> >
> > Jason
> >
> > *From:*Ravi Kapoor [mailto:ravikapoor101@gmail.com]
> > *Sent:* Wednesday, July 08, 2015 3:11 PM
> > *To:* users@groovy.incubator.apache.org
> > *Subject:* how to dynamically evaluate expression in java code running
> > inside groovy
> >
> > 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)");
> >
> >              }
> >
> > }
> >
> > ----------------------------------------------------------------------
> > -- This email message and any attachments are for the sole use of the
> > intended recipient(s). Any unauthorized review, use, disclosure or
> > distribution is prohibited. If you are not the intended recipient,
> > please contact the sender by reply email and destroy all copies of the
> > original message and any attachments.
>
>
> ----------------------------------------------------------------------
> This email message and any attachments are for the sole use of the
> intended recipient(s). Any unauthorized review, use, disclosure or
> distribution is prohibited. If you are not the intended recipient, please
> contact the sender by reply email and destroy all copies of the original
> message and any attachments.
>

Mime
View raw message