Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2010, Red Hat Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.as.weld.injection;
 
 import java.util.List;
 import java.util.Set;
 
 
 import  org.jboss.weld.bean.AbstractClassBean;
 import  org.jboss.weld.exceptions.DefinitionException;
 import  org.jboss.weld.logging.messages.ValidatorMessage;
 import  org.jboss.weld.manager.BeanManagerImpl;
 import  org.jboss.weld.resources.ClassTransformer;

Class that knows how to create and inject a class that requires CDI injection

Author(s):
Stuart Douglas
 
 class WeldEEInjection {
 
     private final Class<?> componentClass;
     private final BeanManagerImpl beanManager;
     private final List<InjectableFieldinjectableFields;
     private final List<InjectableMethodinjectableMethods;
     private final InjectableConstructor constructor;
     private final InjectionTarget injectionTarget;
 
     public WeldEEInjection(Class<?> componentClass, BeanManagerImpl beanManagerList<InjectableFieldinjectableFieldsList<InjectableMethodinjectableMethodsInjectableConstructor constructorfinal InjectionTarget injectionTarget) {
         this. = componentClass;
         this. = beanManager;
         this. = injectableFields;
         this. = injectableMethods;
         this. = constructor;
         this. = injectionTarget;
     }

    
Run field and method injections. Resource injections should be performed before this method is called

Parameters:
instance The instance to inject
ctx The creational context that was used to create the instance
 
     public void inject(Object instanceCreationalContext<?> ctx) {
         if ( != null) {
             .inject(instancectx);
         }
         for (InjectableField field : ) {
             field.inject(instancectx);
         }
         for (InjectableMethod method : ) {
             method.inject(instancectx);
         }
     }

    
Create an instance of the class by calling the bean constructor

Parameters:
ctx The creational context to use
Returns:
A new instance of the object
 
     public Object produce(CreationalContext<?> ctx) {
 
         try {
             if ( != null) {
                return .createInstance(ctx);
            } else {
                return .newInstance();
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        }
    }
    public static WeldEEInjection createWeldEEInjection(Class<?> componentClassBean<?> beanfinal BeanManagerImpl beanManager) {
        final AnnotatedType<?> type = beanManager.getServices().get(ClassTransformer.class).getAnnotatedType(componentClass);
        List<InjectableFieldinjectableFields = new ArrayList<InjectableField>();
        List<InjectableMethodinjectableMethods = new ArrayList<InjectableMethod>();
        AnnotatedConstructor<?> injectConstructor = null;
        for (AnnotatedConstructor<?> constructor : type.getConstructors()) {
            if (constructor.isAnnotationPresent(Inject.class)) {
                if (injectConstructor != null) {
                    throw ..moreThanOneBeanConstructor(componentClass);
                }
                injectConstructor = constructor;
            }
        }
        InjectableConstructor constructor = null;
        if (injectConstructor != null) {
            constructor = new InjectableConstructor(injectConstructorbeanManagerbean);
        }
        final InjectionTarget injectionTarget;
        if (bean instanceof AbstractClassBean) {
            //if we have the bean object we just use the CDI injectionTarget
            injectionTarget = ((AbstractClassBean) bean).getInjectionTarget();
        } else {
            injectionTarget = null;
            //if there is no bean we need to create all the injectable fields / methods ourselves
            //we do not use createInjectionTarget as that will result in EE injections being performed
            //twice
            //look for field injection points
            for (AnnotatedField<?> field : type.getFields()) {
                if (field.isAnnotationPresent(Inject.class)) {
                    if (InjectionPoint.class.isAssignableFrom(field.getJavaMember().getType())) {
                        throw new DefinitionException(ValidatorMessage.INJECTION_INTO_NON_BEAN, field);
                    }
                    final Set<Annotationqualifiers = new HashSet<Annotation>();
                    for (Annotation annotation : field.getAnnotations()) {
                        if (beanManager.isQualifier(annotation.annotationType())) {
                            qualifiers.add(annotation);
                        }
                    }
                    FieldInjectionPoint ip = new FieldInjectionPoint(fieldqualifiersbean);
                    Set<Bean<?>> beans = beanManager.getBeans(ip);
                    Bean<?> ipBean = beanManager.resolve(beans);
                    if (ipBean == null) {
                        throw ..couldNotResolveInjectionPoint(field.getJavaMember().toGenericString(), qualifiers);
                    }
                    injectableFields.add(new InjectableField(field.getJavaMember(), ipBeanip));
                }
            }
            //now look for @Inject methods
            for (AnnotatedMethod<?> method : type.getMethods()) {
                if (method.isAnnotationPresent(Inject.class)) {
                    final List<Bean<?>> parameterBeans = new ArrayList<Bean<?>>();
                    final List<InjectionPointips = new ArrayList<InjectionPoint>();
                    for (AnnotatedParameter<?> param : method.getParameters()) {
                        final Set<Annotationqualifiers = new HashSet<Annotation>();
                        for (Annotation annotation : param.getAnnotations()) {
                            if (beanManager.isQualifier(annotation.annotationType())) {
                                qualifiers.add(annotation);
                            }
                        }
                        final Class<?> parameterType = method.getJavaMember().getParameterTypes()[param.getPosition()];
                        if (InjectionPoint.class.isAssignableFrom(parameterType)) {
                            throw ..attemptingToInjectInjectionPointIntoNonBean(componentClassmethod.getJavaMember());
                        }
                        ParameterInjectionPoint ip = new ParameterInjectionPoint(paramqualifiersbean);
                        Set<Bean<?>> beans = beanManager.getBeans(ip);
                        Bean<?> ipBean = beanManager.resolve(beans);
                        if (ipBean == null) {
                            throw ..couldNotResolveInjectionPoint(param.toString(), qualifiers);
                        }
                        parameterBeans.add(ipBean);
                        ips.add(ip);
                    }
                    injectableMethods.add(new InjectableMethod(method.getJavaMember(), parameterBeansips));
                }
            }
        }
        return new WeldEEInjection(componentClassbeanManagerinjectableFieldsinjectableMethodsconstructorinjectionTarget);
    }
New to GrepCode? Check out our FAQ X