/*
 * Copyright 2012 Agorava
 *
 * 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 org.agorava.utils.solder.bean.generic;

import org.agorava.utils.solder.bean.Beans;
import org.agorava.utils.solder.reflection.Reflections;

import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Set;

/**
 * A helper class for implementing producer methods and fields on generic beans
 *
 * @author Pete Muir
 * @author Ove Ranheim
 */
abstract class AbstractGenericProducerBean<T> extends AbstractGenericBean<T> {

    private final Type declaringBeanType;
    private final Annotation[] declaringBeanQualifiers;
    private final Class<? extends Annotation> scopeOverride;

    protected AbstractGenericProducerBean(Bean<T> delegate, GenericIdentifier identifier, Set<Annotation> qualifiers, Set<Annotation> declaringBeanQualifiers, Class<? extends Annotation> scopeOverride, String id, boolean alternative, Class<?> beanClass, BeanManager beanManager) {
        super(delegate, qualifiers, identifier, id, alternative, beanClass, beanManager);
        this.declaringBeanType = delegate.getBeanClass();
        this.declaringBeanQualifiers = declaringBeanQualifiers.toArray(Reflections.EMPTY_ANNOTATION_ARRAY);
        this.scopeOverride = scopeOverride;
    }

    protected Annotation[] getDeclaringBeanQualifiers() {
        return declaringBeanQualifiers;
    }

    protected Type getDeclaringBeanType() {
        return declaringBeanType;
    }

    protected abstract T getValue(Object receiver, CreationalContext<T> creationalContext);

    @Override
    public T create(CreationalContext<T> creationalContext) {
        try {
            Object receiver = getReceiver(creationalContext);
            T instance = getValue(receiver, creationalContext);
            Beans.checkReturnValue(instance, this, null, getBeanManager());
            return instance;
        } finally {
            if (getDeclaringBean().getScope().equals(Dependent.class)) {
                creationalContext.release();
            }
        }
    }

    protected Object getReceiver(CreationalContext<T> creationalContext) {
        Bean<?> declaringBean = getDeclaringBean();
        return getBeanManager().getReference(declaringBean, declaringBean.getBeanClass(), creationalContext);
    }

    protected Bean<?> getDeclaringBean() {
        return getBeanManager().resolve(getBeanManager().getBeans(getDeclaringBeanType(), getDeclaringBeanQualifiers()));
    }

    @Override
    public Class<? extends Annotation> getScope() {
        if (scopeOverride == null) {
            return super.getScope();
        } else {
            return scopeOverride;
        }
    }

}

@@ -2935,17 +2935,20 @@
 ngBean, 
-d
+getD
 eclaring
@@ -2951,29 +2951,20 @@
 ringBean
-.getBeanClass
+Type
 (), crea

Bring AbstractGenericProducerBean into line with solder version, fixing an issue with a changed interpretation of javax.enterprise.inject.spi.Bean.getBeanClass()