Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2012, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * 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.jboss.weld.injection.producer;
 
 
 
Common functionality for javax.enterprise.inject.spi.Producers backing producer fields and producer methods.

Author(s):
Jozef Hartinger
Marko Luksa
 
 public abstract class AbstractMemberProducer<X, T> extends AbstractProducer<T> {
 
     private final DisposalMethod<?, ?> disposalMethod;
 
     public AbstractMemberProducer(EnhancedAnnotatedMember<T, ? super X, ? extends MemberenhancedMemberDisposalMethod<?, ?> disposalMethod) {
         this. = disposalMethod;
         checkDeclaringBean();
         checkProducerReturnType(enhancedMember);
     }
 
     protected void checkDeclaringBean() {
         if (getDeclaringBean() == null && !getAnnotated().isStatic()) {
             throw ..declaringBeanMissing(getAnnotated());
         }
     }

    
Validates the producer method
 
     protected void checkProducerReturnType(EnhancedAnnotatedMember<T, ? super X, ? extends MemberenhancedMember) {
         checkReturnTypeIsConcrete(enhancedMemberenhancedMember.getBaseType());
         checkReturnTypeForWildcardsAndTypeVariables(enhancedMemberenhancedMember.getBaseType());
     }
 
     private void checkReturnTypeIsConcrete(EnhancedAnnotatedMember<T, ? super X, ? extends MemberenhancedMemberType type) {
         if (type instanceof TypeVariable<?> || type instanceof WildcardType) {
             throw ..returnTypeMustBeConcrete(enhancedMember);
         } else if (type instanceof GenericArrayType) {
             GenericArrayType arrayType = (GenericArrayTypetype;
             checkReturnTypeIsConcrete(enhancedMemberarrayType.getGenericComponentType());
         }
     }
 
     private void checkReturnTypeForWildcardsAndTypeVariables(EnhancedAnnotatedMember<T, ? super X, ? extends MemberenhancedMemberType type) {
         if (type instanceof TypeVariable<?>) {
             if (!isDependent()) {
                 throw producerWithTypeVariableBeanTypeMustBeDependent(enhancedMember);
             }
         } else if (type instanceof WildcardType) {
             throw producerCannotHaveWildcardBeanType(enhancedMember);
         } else if (type instanceof ParameterizedType) {
             ParameterizedType parameterizedType = (ParameterizedTypetype;
             for (Type parameterType : parameterizedType.getActualTypeArguments()) {
                 checkReturnTypeForWildcardsAndTypeVariables(enhancedMemberparameterType);
             }
         } else if (type instanceof GenericArrayType) {
             GenericArrayType arrayType = (GenericArrayTypetype;
             checkReturnTypeForWildcardsAndTypeVariables(enhancedMemberarrayType.getGenericComponentType());
         }
     }
 
     protected abstract DefinitionException producerCannotHaveWildcardBeanType(Object member);
 
    private boolean isDependent() {
        return getBean() != null && Dependent.class.equals(getBean().getScope());
    }

    
Gets the receiver of the product. The two creational contexts need to be separated because the receiver only serves the product creation (it is not a dependent instance of the created instance).

Parameters:
productCreationalContext the creational context of the produced instance
receiverCreationalContext the creational context of the receiver
Returns:
The receiver
    protected Object getReceiver(CreationalContext<?> productCreationalContextCreationalContext<?> receiverCreationalContext) {
        // This is a bit dangerous, as it means that producer methods can end up
        // executing on partially constructed instances. Also, it's not required
        // by the spec...
        if (getAnnotated().isStatic()) {
            return null;
        } else {
            if (productCreationalContext instanceof WeldCreationalContext<?>) {
                WeldCreationalContext<?> creationalContextImpl = (WeldCreationalContext<?>) productCreationalContext;
                final Object incompleteInstance = creationalContextImpl.getIncompleteInstance(getDeclaringBean());
                if (incompleteInstance != null) {
                    ..circularCall(getAnnotated(), getDeclaringBean());
                    return incompleteInstance;
                }
            }
            return getBeanManager().getReference(getDeclaringBean(), nullreceiverCreationalContexttrue);
        }
    }
    public void dispose(T instance) {
        if ( != null) {
            // CreationalContext is only created if we need it to obtain the receiver
            // MethodInvocationStrategy takes care of creating CC for parameters, if needed
            if (getAnnotated().isStatic()) {
                .invokeDisposeMethod(nullinstancenull);
            } else {
                WeldCreationalContext<X> ctx = null;
                try {
                    Object receiver = ContextualInstance.getIfExists(getDeclaringBean(), getBeanManager());
                    if (receiver == null) {
                        ctx = getBeanManager().createCreationalContext(null);
                        // Create child CC so that a dependent reciever may be destroyed after the disposer method completes
                        receiver = ContextualInstance.get(getDeclaringBean(), getBeanManager(), ctx.getCreationalContext(getDeclaringBean()));
                    }
                    if (receiver != null) {
                        .invokeDisposeMethod(receiverinstancectx);
                    }
                } finally {
                    if (ctx != null) {
                        ctx.release();
                    }
                }
            }
        }
    }
    @Override
    public T produce(CreationalContext<T> ctx) {
        CreationalContext<X> receiverCreationalContext = getReceiverCreationalContext(ctx);
        Object receiver = getReceiver(ctxreceiverCreationalContext);
        try {
            return produce(receiverctx);
        } finally {
            receiverCreationalContext.release();
        }
    }
        if(ctx instanceof WeldCreationalContext) {
        } else {
            return getBeanManager().createCreationalContext(getDeclaringBean());
        }
    }
    public DisposalMethod<?, ?> getDisposalMethod() {
        return ;
    }
    protected boolean isTypeSerializable(Object object) {
        return object instanceof Serializable;
    }
    public abstract BeanManagerImpl getBeanManager();
    public abstract Bean<X> getDeclaringBean();
    public abstract Bean<T> getBean();
    public abstract AnnotatedMember<? super X> getAnnotated();
    protected abstract T produce(Object receiverCreationalContext<T> ctx);
    @Override
    public String toString() {
        StringBuilder result = new StringBuilder("Producer for ");
        if (getDeclaringBean() == null) {
            result.append(getAnnotated());
        } else {
            if (getBean() == null) {
                result.append(getAnnotated());
        } else {
                result.append(getBean());
            }
            result.append(" declared on ").append(getDeclaringBean());
        }
        return result.toString();
    }
New to GrepCode? Check out our FAQ X