Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.event » [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.event;
   15   
   16   import java.lang.annotation.Annotation;
   17   import java.lang.reflect.Method;
   18   import java.lang.reflect.ParameterizedType;
   19   import java.lang.reflect.Type;
   20   
   21   import javax.enterprise.event.Event;
   22   import javax.enterprise.event.Observes;
   23   import javax.enterprise.event.TransactionPhase;
   24   import javax.enterprise.inject.Disposes;
   25   import javax.enterprise.inject.Produces;
   26   import javax.enterprise.inject.spi.InjectionPoint;
   27   import javax.inject.Inject;
   28   
   29   import org.apache.webbeans.exception.WebBeansConfigurationException;
   30   import org.apache.webbeans.util.AnnotationUtil;
   31   import org.apache.webbeans.util.Asserts;
   32   import org.apache.webbeans.util.ClassUtil;
   33   
   34   public final class EventUtil
   35   {
   36       private EventUtil()
   37       {
   38   
   39       }
   40   
   41       public static void checkEventType(Class<?> eventType)
   42       {
   43           Asserts.assertNotNull(eventType, "eventType parameter can not be null");
   44   
   45           if (ClassUtil.isDefinitionConstainsTypeVariables(eventType))
   46           {
   47               throw new IllegalArgumentException("Event type : " + eventType.getName() + " can not be generic");
   48           }
   49       }
   50   
   51       public static void checkEventBindings(Annotation... annotations)
   52       {
   53           AnnotationUtil.checkQualifierConditions(annotations);
   54       }
   55   
   56       public static TransactionalObserverType getObserverMethodTransactionType(Method observerMethod)
   57       {
   58           Observes observes = AnnotationUtil.getMethodFirstParameterAnnotation(observerMethod, Observes.class);
   59           
   60           if (observes.during().equals(TransactionPhase.AFTER_COMPLETION))
   61           {
   62               return TransactionalObserverType.AFTER_TRANSACTION_COMPLETION;
   63           }
   64           else if (observes.during().equals(TransactionPhase.AFTER_SUCCESS))
   65           {
   66               return TransactionalObserverType.AFTER_TRANSACTION_SUCCESS;
   67           }
   68           else if (observes.during().equals(TransactionPhase.AFTER_FAILURE))
   69           {
   70               return TransactionalObserverType.AFTER_TRANSACTION_FAILURE;
   71           }
   72           else if (observes.during().equals(TransactionPhase.BEFORE_COMPLETION))
   73           {
   74               return TransactionalObserverType.BEFORE_TRANSACTION_COMPLETION;
   75           }
   76           else
   77           {
   78               return TransactionalObserverType.NONE;
   79           }
   80   
   81       }
   82   
   83       public static void checkObserverMethodConditions(Method candidateObserverMethod, Class<?> clazz)
   84       {
   85           Asserts.assertNotNull(candidateObserverMethod, "candidateObserverMethod parameter can not be null");
   86           Asserts.nullCheckForClass(clazz);
   87   
   88           if (AnnotationUtil.isMethodMultipleParameterAnnotationExist(candidateObserverMethod, Observes.class))
   89           {
   90               throw new WebBeansConfigurationException("Observer method : " + candidateObserverMethod.getName() + " in class : " + clazz.getName() + " can not define two parameters with annotated @Observes");
   91           }
   92   
   93           if (AnnotationUtil.isMethodHasAnnotation(candidateObserverMethod, Produces.class) || AnnotationUtil.isMethodHasAnnotation(candidateObserverMethod, Inject.class))
   94           {
   95               throw new WebBeansConfigurationException("Observer method : " + candidateObserverMethod.getName() + " in class : " + clazz.getName() + " can not annotated with annotation in the list {@Produces, @Initializer, @Destructor}");
   96   
   97           }
   98   
   99           if (AnnotationUtil.isMethodParameterAnnotationExist(candidateObserverMethod, Disposes.class))
  100           {
  101               throw new WebBeansConfigurationException("Observer method : " + candidateObserverMethod.getName() + " in class : " + clazz.getName() + " can not annotated with annotation @Disposes");
  102           }
  103   
  104   //        Type type = AnnotationUtil.getMethodFirstParameterWithAnnotation(candidateObserverMethod, Observes.class);
  105   //        
  106   //        Class<?> eventType = null;
  107   //       
  108   //        if (type instanceof ParameterizedType)
  109   //        {
  110   //            eventType = (Class<?>) ((ParameterizedType) type).getRawType();
  111   //        }
  112   //        else
  113   //        {
  114   //            eventType = (Class<?>) type;
  115   //        }
  116   
  117   //        if (ClassUtil.isDefinitionConstainsTypeVariables(eventType))
  118   //        {
  119   //            throw new WebBeansConfigurationException("Observer method : " + candidateObserverMethod.getName() + " in class : " + clazz.getName() + " can not defined as generic");
  120   //        }
  121       }
  122   
  123   
  124       public static boolean checkObservableInjectionPointConditions(InjectionPoint injectionPoint)
  125       {
  126           Type type = injectionPoint.getType();
  127           
  128           Class<?> candidateClazz = null;
  129           if(type instanceof Class)
  130           {
  131               candidateClazz = (Class<?>)type;
  132           }
  133           else if(type instanceof ParameterizedType)
  134           {
  135               ParameterizedType pt = (ParameterizedType)type;
  136               candidateClazz = (Class<?>)pt.getRawType();
  137           }
  138           
  139           if(!candidateClazz.equals(Event.class))
  140           {
  141               return false;
  142           }                
  143   
  144           if (!ClassUtil.isParametrizedType(injectionPoint.getType()))
  145           {
  146               throw new WebBeansConfigurationException("@Observable field injection " + injectionPoint + " must be ParametrizedType with actual type argument");
  147           }
  148           else
  149           {                        
  150               if(ClassUtil.isParametrizedType(injectionPoint.getType()))
  151               {
  152                   ParameterizedType pt = (ParameterizedType)injectionPoint.getType();
  153                   
  154                   Class<?> rawType = (Class<?>) pt.getRawType();
  155                   
  156                   Type[] typeArgs = pt.getActualTypeArguments();
  157                   
  158                   if(!(rawType.equals(Event.class)))
  159                   {
  160                       return false;
  161                   }                
  162                   else
  163                   {                                        
  164                       if(typeArgs.length == 1)
  165                       {
  166                           Type actualArgument = typeArgs[0];
  167                           
  168                           if(ClassUtil.isParametrizedType(actualArgument) || ClassUtil.isWildCardType(actualArgument) || ClassUtil.isTypeVariable(actualArgument))
  169                           {                            
  170                               throw new IllegalArgumentException("@Observable field injection " + injectionPoint.toString() + " actual type argument can not be Parametrized, Wildcard type or Type variable");                            
  171                           }
  172                                                   
  173                           if(ClassUtil.isDefinitionConstainsTypeVariables((Class<?>)actualArgument))
  174                           {
  175                               throw new IllegalArgumentException("@Observable field injection " + injectionPoint.toString() + " must not have TypeVariable or WildCard generic type argument");                            
  176                           }
  177                       }
  178                       else
  179                       {
  180                           throw new IllegalArgumentException("@Observable field injection " + injectionPoint.toString() + " must not have more than one actual type argument");
  181                       }
  182                   }                                
  183               }
  184               else
  185               {
  186                   throw new IllegalArgumentException("@Observable field injection " + injectionPoint.toString() + " must be defined as ParameterizedType with one actual type argument");
  187               }        
  188           }
  189           
  190           return true;
  191   
  192       }
  193   
  194   }

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