View Javadoc

1   package groovy.util;
2   
3   import org.codehaus.groovy.control.CompilationFailedException;
4   
5   import groovy.lang.Binding;
6   import groovy.lang.GroovyShell;
7   
8   /***
9    * Allow easy integration from Groovy into Java through convenience methods.
10   * @see EvalTest
11   * @author Dierk Koenig
12   */
13  
14  public class Eval {
15      /***
16       * @param expression the Groovy expression to evaluate
17       * @return the result of the expression
18       * @throws CompilationFailedException if expression is no proper Groovy
19       */
20      public static Object me(final String expression) throws CompilationFailedException {
21          return me(null, null, expression);
22      }
23  
24      /***
25       * evaluate expression and make object available inside the expression as 'symbol'
26       * @param expression the Groovy expression to evaluate
27       * @return the result of the expression
28       * @throws CompilationFailedException if expression is no proper Groovy
29       */
30      public static Object me(final String symbol, final Object object, final String expression) throws CompilationFailedException {
31          Binding b = new Binding();
32          b.setVariable(symbol, object);
33          GroovyShell sh = new GroovyShell(b);
34          return sh.evaluate(expression);
35      }
36  
37      /***
38       * evaluate expression and make x available inside the expression as 'x'
39       * @param expression the Groovy expression to evaluate
40       * @return the result of the expression
41       * @throws CompilationFailedException if expression is no proper Groovy
42       */
43      public static Object x(final Object x, final String expression) throws CompilationFailedException {
44          return me("x", x, expression);
45      }
46  
47      /***
48       * evaluate expression and make x and y available inside the expression as 'x' and 'y'
49       * @param expression the Groovy expression to evaluate
50       * @return the result of the expression
51       * @throws CompilationFailedException if expression is no proper Groovy
52       */
53      public static Object xy(final Object x, final Object y, final String expression) throws CompilationFailedException {
54          Binding b = new Binding();
55          b.setVariable("x", x);
56          b.setVariable("y", y);
57          GroovyShell sh = new GroovyShell(b);
58          return sh.evaluate(expression);
59      }
60  
61      /***
62       * evaluate expression and make x,y,z available inside the expression as 'x','y','z'
63       * @param expression the Groovy expression to evaluate
64       * @return the result of the expression
65       * @throws CompilationFailedException if expression is no proper Groovy
66       */
67      public static Object xyz(final Object x, final Object y, final Object z, final String expression) throws CompilationFailedException {
68          Binding b = new Binding();
69          b.setVariable("x", x);
70          b.setVariable("y", y);
71          b.setVariable("z", z);
72          GroovyShell sh = new GroovyShell(b);
73          return sh.evaluate(expression);
74      }
75  }