/************************************************
 * Copyright (c) Shen Li. All rights reserved.  *
 * http://joyaop.sourceforge.net                *
 * -------------------------------------------- *
 * Distributable under LGPL license.            *
 * See terms of license at gnu.org.             *
 ************************************************/
package net.sf.joyaop.extension.picocontainer;

import net.sf.joyaop.ObjectFactory;
import org.picocontainer.Parameter;
import org.picocontainer.defaults.AssignabilityRegistrationException;
import org.picocontainer.defaults.ConstructorInjectionComponentAdapter;
import org.picocontainer.defaults.NotConcreteRegistrationException;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author Shen Li
 */
public class ConstructorInjectionAspectizedComponentAdapter extends ConstructorInjectionComponentAdapter {
    private final ObjectFactory objectFactory;

    public ConstructorInjectionAspectizedComponentAdapter(ObjectFactory objectFactory, Object componentKey,
                                                             Class componentImplementation, Parameter[] parameters)
            throws AssignabilityRegistrationException, NotConcreteRegistrationException {
        super(componentKey, AbstractClassUtils.wrap(componentImplementation), parameters, false);
        this.objectFactory = objectFactory;
    }

    protected Object newInstance(Constructor constructor, Object[] parameters)
            throws InstantiationException, IllegalAccessException, InvocationTargetException {
        Class clazz = constructor.getDeclaringClass();
        clazz = AbstractClassUtils.unwrap(clazz);
        return objectFactory.newInstance(clazz, constructor.getParameterTypes(), parameters);
    }
}