Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2008, 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;
 
 import java.util.List;
 
 

Author(s):
Pete Muir
Jozef Hartinger
 
 public class BeanInjectionTarget<T> extends BasicInjectionTarget<T> {
 
     public static <T> BeanInjectionTarget<T> createDefault(EnhancedAnnotatedType<T> typeBean<T> beanBeanManagerImpl beanManager) {
         return new BeanInjectionTarget<T>(typebeanbeanManager);
     }
 
     public static <T> BeanInjectionTarget<T> forCdiInterceptor(EnhancedAnnotatedType<T> typeBean<T> beanBeanManagerImpl manager) {
         return new BeanInjectionTarget<T>(typebeanmanager, ResourceInjector.of(typebeanmanager), NoopLifecycleCallbackInvoker.<T>getInstance());
     }
 
     private final Bean<T> bean;
 
 
     public BeanInjectionTarget(EnhancedAnnotatedType<T> typeBean<T> beanBeanManagerImpl beanManagerInjector<T> injectorLifecycleCallbackInvoker<T> invoker) {
         super(typebeanbeanManagerinjectorinvoker);
         this. = bean;
     }
 
     public BeanInjectionTarget(EnhancedAnnotatedType<T> typeBean<T> beanBeanManagerImpl beanManager) {
         this(typebeanbeanManager, ResourceInjector.of(typebeanbeanManager), DefaultLifecycleCallbackInvoker.of(type));
     }
 
     @Override
     public void dispose(T instance) {
         // No-op
     }
 
     protected boolean isInterceptor() {
         return (getBean() instanceof Interceptor<?>) || getType().isAnnotationPresent(javax.interceptor.Interceptor.class);
     }
 
     protected boolean isDecorator() {
         return (getBean() instanceof Decorator<?>) || getType().isAnnotationPresent(javax.decorator.Decorator.class);
     }
 
     protected boolean isInterceptionCandidate() {
         return !isInterceptor() && !isDecorator() && !Modifier.isAbstract(getType().getJavaClass().getModifiers());
     }
 
     protected void initializeInterceptionModel(EnhancedAnnotatedType<T> annotatedType) {
         AbstractInstantiator<T> instantiator = (AbstractInstantiator<T>) getInstantiator();
         if (instantiator.getConstructorInjectionPoint() == null) {
             return// this is a non-producible InjectionTarget (only created to inject existing instances)
         }
             buildInterceptionModel(annotatedTypeinstantiator);
         }
     }
 
     protected void buildInterceptionModel(EnhancedAnnotatedType<T> annotatedTypeAbstractInstantiator<T> instantiator) {
         new InterceptionModelInitializer<T>(annotatedTypeinstantiator.getConstructorInjectionPoint().getAnnotated(), getBean()).init();
     }
 
     @Override
     public void initializeAfterBeanDiscovery(EnhancedAnnotatedType<T> annotatedType) {
         initializeInterceptionModel(annotatedType);
 
         InterceptionModel interceptionModel = null;
        if (isInterceptionCandidate()) {
            interceptionModel = .getInterceptorModelRegistry().get(getType());
        }
        boolean hasNonConstructorInterceptors = interceptionModel != null && (interceptionModel.hasExternalNonConstructorInterceptors() || interceptionModel.hasTargetClassInterceptors());
        List<Decorator<?>> decorators = null;
        if (getBean() != null && isInterceptionCandidate()) {
            decorators = .resolveDecorators(getBean().getTypes(), getBean().getQualifiers());
        }
        boolean hasDecorators = decorators != null && !decorators.isEmpty();
        if (hasDecorators) {
            checkDecoratedMethods(annotatedTypedecorators);
        }
        if (hasNonConstructorInterceptors || hasDecorators) {
            if (!(getInstantiator() instanceof DefaultInstantiator<?>)) {
                throw new java.lang.IllegalStateException("Unexpected instantiator " + getInstantiator());
            }
            DefaultInstantiator<T> delegate = (DefaultInstantiator<T>) getInstantiator();
            setInstantiator(SubclassedComponentInstantiator.forInterceptedDecoratedBean(annotatedTypegetBean(), delegate));
            if (hasDecorators) {
                setInstantiator(new SubclassDecoratorApplyingInstantiator<T>(getBeanManager().getContextId(), getInstantiator(), getBean(), decorators));
            }
            if (hasNonConstructorInterceptors) {
                setInstantiator(new InterceptorApplyingInstantiator<T>(getInstantiator(), interceptionModelgetType()));
            }
        }
        if (isInterceptionCandidate()) {
            setupConstructorInterceptionInstantiator(interceptionModel);
        }
    }
    protected void setupConstructorInterceptionInstantiator(InterceptionModel interceptionModel) {
        if (interceptionModel != null && interceptionModel.hasExternalConstructorInterceptors()) {
            setInstantiator(new ConstructorInterceptionInstantiator<T>(getInstantiator(), interceptionModelgetType()));
        }
    }
    protected void checkNoArgsConstructor(EnhancedAnnotatedType<T> type) {
        EnhancedAnnotatedConstructor<T> constructor = type.getNoArgsEnhancedConstructor();
        if (constructor == null) {
            throw ..decoratedHasNoNoargsConstructor(this);
        } else if (constructor.isPrivate()) {
            throw ..decoratedNoargsConstructorIsPrivate(this);
        }
    }
    protected void checkDecoratedMethods(EnhancedAnnotatedType<T> typeList<Decorator<?>> decorators) {
        if (type.isFinal()) {
            throw ..finalBeanClassWithDecoratorsNotAllowed(this);
        }
        checkNoArgsConstructor(type);
        for (Decorator<?> decorator : decorators) {
            EnhancedAnnotatedType<?> decoratorClass;
            if (decorator instanceof DecoratorImpl<?>) {
                DecoratorImpl<?> decoratorBean = (DecoratorImpl<?>) decorator;
                decoratorClass = decoratorBean.getBeanManager().getServices().get(ClassTransformer.class).getEnhancedAnnotatedType(decoratorBean.getAnnotated());
            } else if (decorator instanceof CustomDecoratorWrapper<?>) {
                decoratorClass = ((CustomDecoratorWrapper<?>) decorator).getEnhancedAnnotated();
            } else {
                throw ..nonContainerDecorator(decorator);
            }
            for (EnhancedAnnotatedMethod<?, ?> decoratorMethod : decoratorClass.getEnhancedMethods()) {
                EnhancedAnnotatedMethod<?, ?> method = type.getEnhancedMethod(decoratorMethod.getSignature());
                if (method != null && !method.isStatic() && !method.isPrivate() && method.isFinal()) {
                    throw ..finalBeanClassWithInterceptorsNotAllowed(this);
                }
            }
        }
    }
    @Override
    public T produce(CreationalContext<T> ctx) {
        T instance = super.produce(ctx);
        if ( != null && !.getScope().equals(Dependent.class) && !getInstantiator().hasDecoratorSupport()) {
            // This should be safe, but needs verification PLM
            // Without this, the chaining of decorators will fail as the
            // incomplete instance will be resolved
            ctx.push(instance);
        }
        return instance;
    }
    @Override
    public Bean<T> getBean() {
        return ;
    }
New to GrepCode? Check out our FAQ X