Top Description Constructors Methods
groovy.util

public Class Eval

extends Object
Class Inheritance
Imports
groovy.lang.Binding, .GroovyShell, org.codehaus.groovy.control.CompilationFailedException

Allow easy integration from Groovy into Java through convenience methods.

This class is a simple helper on top of GroovyShell. You can use it to evaluate small Groovy scripts that don't need large Binding objects. For example, this script executes with no errors:

assert Eval.me(' 2 * 4 + 2') == 10
assert Eval.x(2, ' x * 4 + 2') == 10
See Also
GroovyShell

Constructor Summary

AccessConstructor and Description
public
Eval()

Method Summary

Modifier and TypeMethod and Description
public static Object

Returns:

the result of the expression
me
(final String
the Groovy expression to evaluate
expression
)

Evaluates the specified String expression and returns the result.

public static Object

Returns:

the result of the expression
me
(final String symbol, final Object object, final String
the Groovy expression to evaluate
expression
)

Evaluates the specified String expression and makes the parameter available inside the script, returning the result.

public static Object

Returns:

the result of the expression
x
(final Object x, final String
the Groovy expression to evaluate
expression
)

Evaluates the specified String expression and makes the parameter available inside the script bound to a variable named 'x', returning the result.

public static Object

Returns:

the result of the expression
xy
(final Object x, final Object y, final String
the Groovy expression to evaluate
expression
)

Evaluates the specified String expression and makes the first two parameters available inside the script bound to variables named 'x' and 'y' respectively, returning the result.

public static Object

Returns:

the result of the expression
xyz
(final Object x, final Object y, final Object z, final String
the Groovy expression to evaluate
expression
)

Evaluates the specified String expression and makes the first three parameters available inside the script bound to variables named 'x', 'y', and 'z' respectively, returning the result.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Evalback to summary
public Eval()

Method Detail

meback to summary
public static Object me(final String expression) throws CompilationFailedException

Evaluates the specified String expression and returns the result. For example:

assert Eval.me(' 2 * 4 + 2') == 10
Parameters
expression:String

the Groovy expression to evaluate

Returns:Object

the result of the expression

Exceptions
CompilationFailedException:
if expression is not valid Groovy
meback to summary
public static Object me(final String symbol, final Object object, final String expression) throws CompilationFailedException

Evaluates the specified String expression and makes the parameter available inside the script, returning the result. For example, this code binds the 'x' variable:

assert Eval.me('x', 2, ' x * 4 + 2') == 10
Parameters
expression:String

the Groovy expression to evaluate

Returns:Object

the result of the expression

Exceptions
CompilationFailedException:
if expression is not valid Groovy
xback to summary
public static Object x(final Object x, final String expression) throws CompilationFailedException

Evaluates the specified String expression and makes the parameter available inside the script bound to a variable named 'x', returning the result. For example, this code executes without failure:

assert Eval.x(2, ' x * 4 + 2') == 10
Parameters
expression:String

the Groovy expression to evaluate

Returns:Object

the result of the expression

Exceptions
CompilationFailedException:
if expression is not valid Groovy
xyback to summary
public static Object xy(final Object x, final Object y, final String expression) throws CompilationFailedException

Evaluates the specified String expression and makes the first two parameters available inside the script bound to variables named 'x' and 'y' respectively, returning the result. For example, this code executes without failure:

assert Eval.xy(2, 4, ' x * y + 2') == 10
Parameters
expression:String

the Groovy expression to evaluate

Returns:Object

the result of the expression

Exceptions
CompilationFailedException:
if expression is not valid Groovy
xyzback to summary
public static Object xyz(final Object x, final Object y, final Object z, final String expression) throws CompilationFailedException

Evaluates the specified String expression and makes the first three parameters available inside the script bound to variables named 'x', 'y', and 'z' respectively, returning the result. For example, this code executes without failure:

assert Eval.xyz(2, 4, 2, ' x * y + z') == 10
Parameters
expression:String

the Groovy expression to evaluate

Returns:Object

the result of the expression

Exceptions
CompilationFailedException:
if expression is not valid Groovy