Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.inject.impl » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
    9    * or agreed to in writing, software distributed under the License is
   10    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   11    * KIND, either express or implied. See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    */
   14   package org.apache.webbeans.inject.impl;
   15   
   16   import java.lang.annotation.Annotation;
   17   import java.lang.reflect.Constructor;
   18   import java.lang.reflect.Field;
   19   import java.lang.reflect.Member;
   20   import java.lang.reflect.Method;
   21   import java.lang.reflect.Type;
   22   import java.util.ArrayList;
   23   import java.util.List;
   24   import java.util.Set;
   25   
   26   import javax.enterprise.event.Observes;
   27   import javax.enterprise.inject.spi.Annotated;
   28   import javax.enterprise.inject.spi.AnnotatedConstructor;
   29   import javax.enterprise.inject.spi.AnnotatedMethod;
   30   import javax.enterprise.inject.spi.AnnotatedParameter;
   31   import javax.enterprise.inject.spi.Bean;
   32   import javax.enterprise.inject.spi.InjectionPoint;
   33   
   34   import org.apache.webbeans.inject.xml.XMLInjectionModelType;
   35   import org.apache.webbeans.inject.xml.XMLInjectionPointModel;
   36   import org.apache.webbeans.portable.AnnotatedElementFactory;
   37   import org.apache.webbeans.util.AnnotationUtil;
   38   import org.apache.webbeans.util.Asserts;
   39   
   40   public class InjectionPointFactory
   41   {
   42       /**
   43        * 
   44        * @param owner
   45        * @param xmlInjectionModel
   46        * @return
   47        * @deprecated
   48        */
   49       public static InjectionPoint getXMLInjectionPointData(Bean<?> owner, XMLInjectionPointModel xmlInjectionModel)
   50       {
   51           Asserts.assertNotNull(owner, "owner parameter can not be null");
   52           Asserts.assertNotNull(xmlInjectionModel, "xmlInjectionModel parameter can not be null");
   53           
   54           InjectionPoint injectionPoint = null;
   55           
   56           Set<Annotation> setAnns = xmlInjectionModel.getAnnotations();
   57           Annotation[] anns = new Annotation[setAnns.size()];
   58           anns = setAnns.toArray(anns);
   59           
   60           boolean available = true;
   61           
   62           if(xmlInjectionModel.getType().equals(XMLInjectionModelType.FIELD))
   63           {
   64               if(checkFieldApplicable(anns))
   65               {
   66                  available = false; 
   67               }
   68           }
   69           else if(xmlInjectionModel.getType().equals(XMLInjectionModelType.METHOD))
   70           {
   71               if(checkMethodApplicable(anns))
   72               {
   73                   available = false;
   74               }
   75           }
   76           
   77           if(available)
   78           {
   79               injectionPoint = getGenericInjectionPoint(owner, anns, xmlInjectionModel.getInjectionGenericType(), xmlInjectionModel.getInjectionMember(),null);
   80           }
   81           
   82           return injectionPoint;
   83       }
   84   
   85       public static InjectionPoint getFieldInjectionPointData(Bean<?> owner, Field member)
   86       {
   87           Asserts.assertNotNull(owner, "owner parameter can not be null");
   88           Asserts.assertNotNull(member, "member parameter can not be null");
   89   
   90           Annotation[] annots = null;
   91           annots = member.getAnnotations();
   92           
   93           if(!checkFieldApplicable(annots))
   94           {
   95               return getGenericInjectionPoint(owner, annots, member.getGenericType(), member, AnnotatedElementFactory.newAnnotatedField(member, member.getDeclaringClass()));   
   96           }        
   97           else
   98           {
   99               return null;
  100           }
  101   
  102       }
  103       
  104       private static boolean checkFieldApplicable(Annotation[] anns)
  105       {
  106   //        if(AnnotationUtil.isAnnotationExist(anns, Fires.class) || AnnotationUtil.isAnnotationExist(anns, Obtains.class))
  107   //        {
  108   //            return true;
  109   //        }
  110        
  111           return false;
  112       }
  113   
  114       private static InjectionPoint getGenericInjectionPoint(Bean<?> owner, Annotation[] annots, Type type, Member member,Annotated annotated)
  115       {
  116           InjectionPointImpl injectionPoint = null;
  117   
  118           Annotation[] bindingAnnots = AnnotationUtil.getQualifierAnnotations(annots);
  119           injectionPoint = new InjectionPointImpl(owner, type, member, annotated);
  120   
  121           addAnnotation(injectionPoint, bindingAnnots, true);
  122   
  123           return injectionPoint;
  124   
  125       }
  126   
  127       @SuppressWarnings("unchecked")
  128       public static List<InjectionPoint> getMethodInjectionPointData(Bean<?> owner, Method member)
  129       {
  130           Asserts.assertNotNull(owner, "owner parameter can not be null");
  131           Asserts.assertNotNull(member, "member parameter can not be null");
  132   
  133           List<InjectionPoint> lists = new ArrayList<InjectionPoint>();
  134   
  135           AnnotatedMethod method = AnnotatedElementFactory.newAnnotatedMethod(member, member.getDeclaringClass());
  136           List<AnnotatedParameter<?>> parameters = method.getParameters();
  137           
  138           InjectionPoint point = null;
  139           
  140           for(AnnotatedParameter<?> parameter : parameters)
  141           {
  142               point = getGenericInjectionPoint(owner, parameter.getAnnotations().toArray(new Annotation[0]), parameter.getBaseType(), member , parameter);
  143               lists.add(point);
  144           }
  145           
  146           return lists;
  147       }
  148       
  149       
  150       private static boolean checkMethodApplicable(Annotation[] annot)
  151       {
  152           for (Annotation observersAnnot : annot)
  153           {
  154               if (observersAnnot.annotationType().equals(Observes.class))
  155               {
  156                   return true;
  157               }
  158           }
  159           
  160           return false;
  161           
  162       }
  163   
  164       public static InjectionPoint getPartialInjectionPoint(Bean<?> owner,Type type, Member member, Annotated annotated, Annotation...bindings)
  165       {
  166           InjectionPointImpl impl = new InjectionPointImpl(owner,type,member,annotated);
  167           
  168           
  169           for(Annotation annot : bindings)
  170           {
  171               impl.addBindingAnnotation(annot);
  172           }
  173           
  174           return impl;
  175           
  176       }
  177       
  178       @SuppressWarnings("unchecked")
  179       public static List<InjectionPoint> getConstructorInjectionPointData(Bean<?> owner, Constructor<?> member)
  180       {
  181           Asserts.assertNotNull(owner, "owner parameter can not be null");
  182           Asserts.assertNotNull(member, "member parameter can not be null");
  183   
  184           List<InjectionPoint> lists = new ArrayList<InjectionPoint>();
  185   
  186           AnnotatedConstructor constructor = AnnotatedElementFactory.newAnnotatedConstructor(member);
  187           List<AnnotatedParameter<?>> parameters = constructor.getParameters();
  188           
  189           InjectionPoint point = null;
  190           
  191           for(AnnotatedParameter<?> parameter : parameters)
  192           {
  193               point = getGenericInjectionPoint(owner, parameter.getAnnotations().toArray(new Annotation[0]), parameter.getBaseType(), member , parameter);
  194               lists.add(point);
  195           }
  196           
  197           return lists;
  198       }
  199   
  200       private static void addAnnotation(InjectionPointImpl impl, Annotation[] annots, boolean isBinding)
  201       {
  202           for (Annotation ann : annots)
  203           {
  204               if (isBinding)
  205               {
  206                   impl.addBindingAnnotation(ann);
  207               }
  208           }
  209       }    
  210   
  211   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.inject.impl » [javadoc | source]