/*
 * Copyright © 2012 Sebastian Hoß <mail@shoss.de>
 * This work is free. You can redistribute it and/or modify it under the
 * terms of the Do What The Fuck You Want To Public License, Version 2,
 * as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
 */
package com.github.sebhoss.contract.verifier;

import com.github.sebhoss.contract.annotation.Clause;
import com.github.sebhoss.contract.annotation.SpEL;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * SpEL-based implementation of the {@link ContractContextFactory}.
 */
@SpEL
public class SpELBasedContractContextFactory implements ContractContextFactory {

    @Override
    public ContractContext createContext(final Object instance, final Object[] arguments, final String[] parameterNames) {
        final ExpressionParser parser = new SpelExpressionParser();
        final EvaluationContext context = new StandardEvaluationContext();

        for (int index = 0; index < arguments.length; index++) {
            context.setVariable(parameterNames[index], arguments[index]);
        }
        context.setVariable(Clause.THIS, instance);

        return new SpELContractContext(parser, context);
    }

}

@@ -322,62 +322,8 @@
 r;%0A%0A
-import com.github.sebhoss.contract.annotation.Clause;%0A
 impo
@@ -1078,16 +1078,24 @@
 Context(
+instance
 );%0A%0A    
@@ -1242,60 +1242,8 @@
    %7D
-%0A        context.setVariable(Clause.THIS, instance);
 %0A%0A  

Set current instance as root object