public static Object evaluateExpression(Expression expr, CompilerConfiguration config) {
        String className = "Expression$" + UUID.randomUUID().toString().replace('-', '$');
        ClassNode node = new ClassNode(className, Opcodes.ACC_PUBLIC, OBJECT_TYPE);
        ReturnStatement code = new ReturnStatement(expr);
        addGeneratedMethod(node, "eval", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code);
        CompilerConfiguration copyConf = new CompilerConfiguration(config);
        CompilationUnit cu = new CompilationUnit(copyConf);
        cu.addClassNode(node);
        cu.compile(Phases.CLASS_GENERATION);
        @SuppressWarnings("unchecked")
        List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses();
        Class aClass = cu.getClassLoader().defineClass(className, classes.get(0).getBytes());
        try {
            return aClass.getMethod("eval").invoke(null);
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw new GroovyBugError(e);
        }
    }
public static Object evaluateExpression ( Expression expr , CompilerConfiguration config ) { String className = "Expression$" + UUID . randomUUID ( ) . toString ( ) . replace ( '-' , '$' ) ; ClassNode node = new ClassNode ( className , Opcodes . ACC_PUBLIC , OBJECT_TYPE ) ; ReturnStatement code = new ReturnStatement ( expr ) ; addGeneratedMethod ( node , "eval" , Opcodes . ACC_PUBLIC + Opcodes . ACC_STATIC , OBJECT_TYPE , Parameter . EMPTY_ARRAY , ClassNode . EMPTY_ARRAY , code ) ; CompilerConfiguration copyConf = new CompilerConfiguration ( config ) ; CompilationUnit cu = new CompilationUnit ( copyConf ) ; cu . addClassNode ( node ) ; cu . compile ( Phases . CLASS_GENERATION ) ; @ SuppressWarnings ( "unchecked" ) List < GroovyClass > classes = ( List < GroovyClass > ) cu . getClasses ( ) ; Class aClass = cu . getClassLoader ( ) . defineClass ( className , classes . get ( 0 ) . getBytes ( ) ) ; try { return aClass . getMethod ( "eval" ) . invoke ( null ) ; } catch ( IllegalAccessException | NoSuchMethodException | InvocationTargetException e ) { throw new GroovyBugError ( e ) ; } }
A helper method that can be used to evaluate expressions as found in annotation
parameters. For example, it will evaluate a constant, be it referenced directly as
an integer or as a reference to a field.
<p>
If this method throws an exception, then the expression cannot be evaluated on its own.

@param expr   the expression to be evaluated
@param config the compiler configuration
@return the result of the expression