All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.sebhoss.contract.verifier.UelBasedContractContextFactory Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 2012 Sebastian Hoß 
 * 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 javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.StandardELContext;
import javax.inject.Inject;

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

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

    final ExpressionFactory expressionFactory;

    /**
     * @param expressionFactory
     *            The expression factory to use.
     */
    @Inject
    public UelBasedContractContextFactory(final ExpressionFactory expressionFactory) {
        this.expressionFactory = expressionFactory;
    }

    @Override
    public ContractContext createContext(final Object instance, final Object[] arguments, final String[] parameterNames) {
        final ELContext elContext = new StandardELContext(expressionFactory);

        for (int index = 0; index < arguments.length; index++) {
            elContext.getVariableMapper().setVariable(parameterNames[index],
                    expressionFactory.createValueExpression(arguments[index], arguments[index].getClass()));
        }
        elContext.getVariableMapper().setVariable(Clause.THIS,
                expressionFactory.createValueExpression(instance, instance.getClass()));

        return new ELContractContext(elContext, expressionFactory);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy