Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright 2003-2007 the original author or authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy.util;
Allow easy integration from Groovy into Java through convenience methods.

Author(s):
Dierk Koenig
public class Eval {
    

Parameters:
expression the Groovy expression to evaluate
Returns:
the result of the expression
Throws:
org.codehaus.groovy.control.CompilationFailedException if expression is no proper Groovy
    public static Object me(final String expressionthrows CompilationFailedException {
        return me(nullnullexpression);
    }

    
evaluate expression and make object available inside the expression as 'symbol'

Parameters:
expression the Groovy expression to evaluate
Returns:
the result of the expression
Throws:
org.codehaus.groovy.control.CompilationFailedException if expression is no proper Groovy
    public static Object me(final String symbolfinal Object objectfinal String expressionthrows CompilationFailedException {
        Binding b = new Binding();
        b.setVariable(symbolobject);
        GroovyShell sh = new GroovyShell(b);
        return sh.evaluate(expression);
    }

    
evaluate expression and make x available inside the expression as 'x'

Parameters:
expression the Groovy expression to evaluate
Returns:
the result of the expression
Throws:
org.codehaus.groovy.control.CompilationFailedException if expression is no proper Groovy
    public static Object x(final Object xfinal String expressionthrows CompilationFailedException {
        return me("x"xexpression);
    }

    
evaluate expression and make x and y available inside the expression as 'x' and 'y'

Parameters:
expression the Groovy expression to evaluate
Returns:
the result of the expression
Throws:
org.codehaus.groovy.control.CompilationFailedException if expression is no proper Groovy
    public static Object xy(final Object xfinal Object yfinal String expressionthrows CompilationFailedException {
        Binding b = new Binding();
        b.setVariable("x"x);
        b.setVariable("y"y);
        GroovyShell sh = new GroovyShell(b);
        return sh.evaluate(expression);
    }

    
evaluate expression and make x,y,z available inside the expression as 'x','y','z'

Parameters:
expression the Groovy expression to evaluate
Returns:
the result of the expression
Throws:
org.codehaus.groovy.control.CompilationFailedException if expression is no proper Groovy
    public static Object xyz(final Object xfinal Object yfinal Object zfinal String expressionthrows CompilationFailedException {
        Binding b = new Binding();
        b.setVariable("x"x);
        b.setVariable("y"y);
        b.setVariable("z"z);
        GroovyShell sh = new GroovyShell(b);
        return sh.evaluate(expression);
    }
New to GrepCode? Check out our FAQ X