Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.util » [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.util;
   15   
   16   import java.lang.annotation.Annotation;
   17   import java.lang.reflect.Constructor;
   18   import java.lang.reflect.Field;
   19   import java.lang.reflect.Method;
   20   import java.lang.reflect.ParameterizedType;
   21   import java.lang.reflect.Type;
   22   import java.util.ArrayList;
   23   import java.util.HashSet;
   24   import java.util.List;
   25   import java.util.Set;
   26   import java.util.StringTokenizer;
   27   
   28   import javax.enterprise.inject.Any;
   29   import javax.enterprise.inject.NonBinding;
   30   import javax.enterprise.inject.spi.Bean;
   31   import javax.enterprise.inject.stereotype.Stereotype;
   32   
   33   import javax.inject.Qualifier;
   34   import javax.interceptor.InterceptorBinding;
   35   
   36   import org.apache.webbeans.exception.WebBeansConfigurationException;
   37   import org.apache.webbeans.plugins.OpenWebBeansPlugin;
   38   import org.apache.webbeans.plugins.PluginLoader;
   39   import org.apache.webbeans.xml.XMLAnnotationTypeManager;
   40   
   41   /**
   42    * Utility class related with {@link Annotation} operations.
   43    * 
   44    * @author <a href="mailto:gurkanerdogdu@yahoo.com">Gurkan Erdogdu</a>
   45    * @since 1.0
   46    */
   47   public final class AnnotationUtil
   48   {
   49       // No instantiate
   50       private AnnotationUtil()
   51       {
   52           throw new UnsupportedOperationException();
   53       }
   54   
   55       /**
   56        * Check given annotation exist on the method.
   57        * 
   58        * @param method method
   59        * @param clazz annotation class
   60        * @return true or false
   61        */
   62       public static boolean isMethodHasAnnotation(Method method, Class<? extends Annotation> clazz)
   63       {
   64           Asserts.assertNotNull(method, "Method argument can not be null");
   65           Asserts.assertNotNull(clazz, "Clazz argument can not be null");
   66   
   67           Annotation[] anns = method.getDeclaredAnnotations();
   68           for (Annotation annotation : anns)
   69           {
   70               if (annotation.annotationType().equals(clazz))
   71               {
   72                   return true;
   73               }
   74           }
   75   
   76           return false;
   77   
   78       }
   79   
   80       /**
   81        * Check if a resource annotation exist on the method.
   82        * 
   83        * @param method method
   84        * @return <code>true</code> if any resource annotation exists for the given method
   85        */
   86       public static boolean isMethodHasResourceAnnotation(Method method)
   87       {
   88           Asserts.assertNotNull(method, "Method argument can not be null");
   89           
   90           Annotation[] anns = method.getDeclaredAnnotations();
   91           return hasResourceAnnotation(anns);
   92       }
   93       
   94       /**
   95        * Check given annotation exist in the any parameter of the given method.
   96        * Return true if exist false otherwise.
   97        * 
   98        * @param method method
   99        * @param annotation checking annotation
  100        * @return true or false
  101        */
  102       public static boolean isMethodParameterAnnotationExist(Method method, Class<? extends Annotation> clazz)
  103       {
  104           Asserts.assertNotNull(method, "Method argument can not be null");
  105           Asserts.assertNotNull(clazz, "Clazz argument can not be null");
  106   
  107           Annotation[][] parameterAnns = method.getParameterAnnotations();
  108   
  109           for (Annotation[] parameters : parameterAnns)
  110           {
  111               for (Annotation param : parameters)
  112               {
  113                   Class<? extends Annotation> btype = param.annotationType();
  114                   if (btype.equals(clazz))
  115                   {
  116                       return true;
  117                   }
  118               }
  119   
  120           }
  121           return false;
  122       }
  123   
  124       public static Type[] getMethodParameterGenericTypesWithGivenAnnotation(Method method, Class<? extends Annotation> clazz)
  125       {
  126           Asserts.assertNotNull(method, "Method argument can not be null");
  127           Asserts.assertNotNull(clazz, "Clazz argument can not be null");
  128   
  129           List<Type> list = new ArrayList<Type>();
  130           Type[] result = null;
  131   
  132           Annotation[][] parameterAnns = method.getParameterAnnotations();
  133           Type[] genericTypes = method.getGenericParameterTypes();
  134   
  135           int i = 0;
  136           for (Annotation[] parameters : parameterAnns)
  137           {
  138               for (Annotation param : parameters)
  139               {
  140                   Class<? extends Annotation> btype = param.annotationType();
  141                   if (btype.equals(clazz))
  142                   {
  143                       list.add(genericTypes[i]);
  144                       break;
  145                   }
  146               }
  147   
  148               i++;
  149   
  150           }
  151   
  152           result = new Type[list.size()];
  153           result = list.toArray(result);
  154   
  155           return result;
  156       }
  157   
  158       public static Type[] getConstructorParameterGenericTypesWithGivenAnnotation(Constructor<?> constructor, Class<? extends Annotation> clazz)
  159       {
  160           Asserts.assertNotNull(constructor, "constructor argument can not be null");
  161           Asserts.assertNotNull(clazz, "Clazz argument can not be null");
  162   
  163           List<Type> list = new ArrayList<Type>();
  164           Type[] result = null;
  165   
  166           Annotation[][] parameterAnns = constructor.getParameterAnnotations();
  167           Type[] genericTypes = constructor.getGenericParameterTypes();
  168   
  169           int i = 0;
  170           for (Annotation[] parameters : parameterAnns)
  171           {
  172               for (Annotation param : parameters)
  173               {
  174                   Class<? extends Annotation> btype = param.annotationType();
  175                   if (btype.equals(clazz))
  176                   {
  177                       list.add(genericTypes[i]);
  178                       break;
  179                   }
  180               }
  181   
  182               i++;
  183   
  184           }
  185   
  186           result = new Type[list.size()];
  187           result = list.toArray(result);
  188   
  189           return result;
  190       }
  191   
  192       /**
  193        * Check given annotation exist in the multiple parameter of the given
  194        * method. Return true if exist false otherwise.
  195        * 
  196        * @param method method
  197        * @param annotation checking annotation
  198        * @return true or false
  199        */
  200       public static boolean isMethodMultipleParameterAnnotationExist(Method method, Class<? extends Annotation> clazz)
  201       {
  202           Asserts.assertNotNull(method, "Method argument can not be null");
  203           Asserts.assertNotNull(clazz, "Clazz argument can not be null");
  204   
  205           Annotation[][] parameterAnns = method.getParameterAnnotations();
  206   
  207           boolean found = false;
  208   
  209           for (Annotation[] parameters : parameterAnns)
  210           {
  211               for (Annotation param : parameters)
  212               {
  213   
  214                   if (param.annotationType().equals(clazz))
  215                   {
  216                       if (!found)
  217                       {
  218                           found = true;
  219                       }
  220                       else
  221                       {
  222                           return true;
  223                       }
  224                   }
  225               }
  226   
  227           }
  228           return false;
  229       }
  230   
  231       /**
  232        * Gets the method first found parameter type that is annotated with the
  233        * given annotation.
  234        * 
  235        * @param method method
  236        * @param annotation checking annotation
  237        * @return type
  238        */
  239       public static Type getMethodFirstParameterWithAnnotation(Method method, Class<? extends Annotation> clazz)
  240       {
  241           Asserts.assertNotNull(method, "Method argument can not be null");
  242           Asserts.assertNotNull(clazz, "Clazz argument can not be null");
  243   
  244           Annotation[][] parameterAnns = method.getParameterAnnotations();
  245           Type[] params = method.getGenericParameterTypes();
  246   
  247           int index = 0;
  248           for (Annotation[] parameters : parameterAnns)
  249           {
  250               for (Annotation param : parameters)
  251               {
  252                   Class<? extends Annotation> btype = param.annotationType();
  253                   if (btype.equals(clazz))
  254                   {
  255                       return params[index];
  256                   }
  257               }
  258   
  259               index++;
  260   
  261           }
  262           return null;
  263       }
  264   
  265       public static Class<?> getMethodFirstParameterTypeClazzWithAnnotation(Method method, Class<? extends Annotation> clazz)
  266       {
  267           Type type = getMethodFirstParameterWithAnnotation(method, clazz);
  268   
  269           if (type instanceof ParameterizedType)
  270           {
  271               return (Class<?>) ((ParameterizedType) type).getRawType();
  272           }
  273           else
  274           {
  275               return (Class<?>) type;
  276           }
  277       }
  278   
  279       /**
  280        * Gets the method first found parameter qualifiers.
  281        * 
  282        * @param method method
  283        * @param annotation checking annotation
  284        * @return annotation array
  285        */
  286       public static Annotation[] getMethodFirstParameterQualifierWithGivenAnnotation(Method method, Class<? extends Annotation> clazz)
  287       {
  288           Asserts.assertNotNull(method, "Method argument can not be null");
  289           Asserts.assertNotNull(clazz, "Clazz argument can not be null");
  290   
  291           Annotation[][] parameterAnns = method.getParameterAnnotations();
  292           List<Annotation> list = new ArrayList<Annotation>();
  293           Annotation[] result = null;
  294   
  295           int index = 0;
  296           for (Annotation[] parameters : parameterAnns)
  297           {
  298               boolean found = false;
  299               for (Annotation param : parameters)
  300               {
  301                   Class<? extends Annotation> btype = param.annotationType();
  302                   if (btype.equals(clazz))
  303                   {
  304                       found = true;
  305                       continue;
  306                   }
  307   
  308                   if (AnnotationUtil.isQualifierAnnotation(btype))
  309                   {
  310                       list.add(param);
  311                   }
  312   
  313               }
  314   
  315               if (found)
  316               {
  317                   result = new Annotation[list.size()];
  318                   result = list.toArray(result);
  319                   return result;
  320               }
  321   
  322               index++;
  323   
  324           }
  325           result = new Annotation[0];
  326           return result;
  327       }
  328       
  329       /**
  330        * Gets the method first found parameter annotation with given type.
  331        * 
  332        * @param method method
  333        * @param annotation checking annotation
  334        * @return annotation
  335        */
  336       public static <T extends Annotation> T getMethodFirstParameterAnnotation(Method method, Class<T> clazz)
  337       {
  338           Asserts.assertNotNull(method, "Method argument can not be null");
  339           Asserts.assertNotNull(clazz, "Clazz argument can not be null");
  340   
  341           Annotation[][] parameterAnns = method.getParameterAnnotations();
  342   
  343           for (Annotation[] parameters : parameterAnns)
  344           {
  345               for (Annotation param : parameters)
  346               {
  347                   Class<? extends Annotation> btype = param.annotationType();
  348                   if (btype.equals(clazz))
  349                   {
  350                       return clazz.cast(param);
  351                   }
  352   
  353               }
  354   
  355           }
  356   
  357           return null;
  358       }    
  359   
  360       /**
  361        * Check given annotation cross ref exist in the any parameter of the given
  362        * method. Return true if exist false otherwise.
  363        * 
  364        * @param method method
  365        * @param annotation checking annotation
  366        * @return true or false
  367        */
  368       public static boolean isMethodParameterAnnotationCrossRefExist(Method method, Class<? extends Annotation> clazz)
  369       {
  370           Asserts.assertNotNull(method, "Method argument can not be null");
  371           Asserts.assertNotNull(clazz, "Clazz argument can not be null");
  372   
  373           Annotation[][] parameterAnns = method.getParameterAnnotations();
  374   
  375           for (Annotation[] parameters : parameterAnns)
  376           {
  377               for (Annotation param : parameters)
  378               {
  379                   Annotation[] btype = param.annotationType().getDeclaredAnnotations();
  380   
  381                   for (Annotation b : btype)
  382                   {
  383                       if (b.annotationType().equals(clazz))
  384                       {
  385                           return true;
  386                       }
  387                   }
  388               }
  389   
  390           }
  391           return false;
  392       }
  393   
  394       /**
  395        * Returns true if the injection point binding type and {@link NonBinding}
  396        * member values are equal to the given member annotation.
  397        * 
  398        * @param clazz annotation class
  399        * @param src component binding type annotation
  400        * @param member annotation for querying the binding type
  401        * @return true or false
  402        */
  403       public static boolean isAnnotationMemberExist(Class<? extends Annotation> clazz, Annotation src, Annotation member)
  404       {
  405           Asserts.assertNotNull(clazz, "Clazz argument can not be null");
  406           Asserts.assertNotNull(src, "Src argument can not be null");
  407           Asserts.assertNotNull(member, "Member argument can not be null");
  408   
  409           if (!src.annotationType().equals(member.annotationType()))
  410           {
  411               return false;
  412           }
  413   
  414           Method[] methods = clazz.getDeclaredMethods();
  415   
  416           List<String> list = new ArrayList<String>();
  417   
  418           for (Method method : methods)
  419           {
  420               Annotation[] annots = method.getDeclaredAnnotations();
  421   
  422               if (annots.length > 0)
  423               {
  424                   for (Annotation annot : annots)
  425                   {
  426                       if (!annot.annotationType().equals(NonBinding.class))
  427                       {
  428                           list.add(method.getName());
  429                       }
  430                   }
  431   
  432               }
  433               else
  434               {
  435                   list.add(method.getName());
  436               }
  437           }
  438   
  439           return checkEquality(src.toString(), member.toString(), list);
  440   
  441       }
  442   
  443       /**
  444        * Check that given two annotation values are equal or not.
  445        * 
  446        * @param src annotation toString method
  447        * @param member annotation toString method
  448        * @param arguments annotation member values with {@link NonBinding}
  449        *            annoations.
  450        * @return true or false
  451        */
  452       private static boolean checkEquality(String src, String member, List<String> arguments)
  453       {
  454           if ((checkEquBuffer(src, arguments).toString().trim().equals(checkEquBuffer(member, arguments).toString().trim())))
  455               return true;
  456           return false;
  457       }
  458   
  459       /*
  460        * Private check method
  461        */
  462       private static StringBuffer checkEquBuffer(String src, List<String> arguments)
  463       {
  464           int index = src.indexOf('(');
  465   
  466           String sbstr = src.substring(index + 1, src.length() - 1);
  467   
  468           StringBuffer srcBuf = new StringBuffer();
  469   
  470           StringTokenizer tok = new StringTokenizer(sbstr, ",");
  471           while (tok.hasMoreTokens())
  472           {
  473               String token = tok.nextToken();
  474   
  475               StringTokenizer tok2 = new StringTokenizer(token, "=");
  476               while (tok2.hasMoreElements())
  477               {
  478                   String tt = tok2.nextToken();
  479                   if (arguments.contains(tt.trim()))
  480                   {
  481                       srcBuf.append(tt);
  482                       srcBuf.append("=");
  483   
  484                       if (tok2.hasMoreElements())
  485                           srcBuf.append(tok2.nextToken());
  486                   }
  487               }
  488   
  489           }
  490   
  491           return srcBuf;
  492       }
  493   
  494       /**
  495        * Gets the array of qualifier annotations on the given array.
  496        * 
  497        * @param annotations annotation array
  498        * @return array containing qualifier anns
  499        */
  500       public static Annotation[] getQualifierAnnotations(Annotation... annotations)
  501       {
  502           Asserts.assertNotNull(annotations, "Annotations argument can not be null");
  503   
  504           Set<Annotation> set = new HashSet<Annotation>();
  505   
  506           for (Annotation annot : annotations)
  507           {
  508               if (AnnotationUtil.isQualifierAnnotation(annot.annotationType()))
  509               {
  510                   set.add(annot);
  511               }
  512           }
  513   
  514           Annotation[] a = new Annotation[set.size()];
  515           a = set.toArray(a);
  516   
  517           return a;
  518       }
  519   
  520       /**
  521        * Gets the array of resource annotations on the given array.
  522        * 
  523        * @param annotations annotation array
  524        * @return array containing resource type anns
  525        */
  526       public static Annotation[] getResourceAnnotations(Annotation... annotations)
  527       {
  528           Asserts.assertNotNull(annotations, "Annotations argument can not be null");
  529   
  530           Set<Annotation> set = new HashSet<Annotation>();
  531   
  532           for (Annotation annot : annotations)
  533           {
  534               if (AnnotationUtil.isResourceAnnotation(annot.annotationType()))
  535               {
  536                   set.add(annot);
  537               }
  538           }
  539   
  540           Annotation[] a = new Annotation[set.size()];
  541           a = set.toArray(a);
  542   
  543           return a;
  544   
  545       }
  546       
  547       /**
  548        * Gets array of methods that has parameter with given annotation type.
  549        * 
  550        * @param clazz class for check
  551        * @param annotation for check
  552        * @return array of methods
  553        */
  554       public static Method[] getMethodsWithParameterAnnotation(Class<?> clazz, Class<? extends Annotation> annotation)
  555       {
  556           Asserts.assertNotNull(clazz, "Clazz argument can not be null");
  557           Asserts.assertNotNull(annotation, "Annotation argument can not be null");
  558   
  559           Method[] methods = clazz.getDeclaredMethods();
  560           List<Method> list = new ArrayList<Method>();
  561           Method[] rMethod = null;
  562   
  563           for (Method m : methods)
  564           {
  565               if (isMethodParameterAnnotationExist(m, annotation))
  566               {
  567                   list.add(m);
  568               }
  569           }
  570   
  571           rMethod = new Method[list.size()];
  572           rMethod = list.toArray(rMethod);
  573   
  574           return rMethod;
  575       }
  576   
  577       /**
  578        * Gets array of methods that has given annotation type.
  579        * 
  580        * @param clazz class for check
  581        * @param annotation for check
  582        * @return array of methods
  583        */
  584       public static Method[] getMethodsWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotation)
  585       {
  586           Asserts.assertNotNull(clazz, "Clazz argument can not be null");
  587           Asserts.assertNotNull(annotation, "Annotation argument can not be null");
  588   
  589           Method[] methods = clazz.getDeclaredMethods();
  590           List<Method> list = new ArrayList<Method>();
  591           Method[] rMethod = null;
  592   
  593           for (Method m : methods)
  594           {
  595               if (isMethodHasAnnotation(m, annotation))
  596               {
  597                   list.add(m);
  598               }
  599           }
  600   
  601           rMethod = new Method[list.size()];
  602           rMethod = list.toArray(rMethod);
  603   
  604           return rMethod;
  605       }
  606   
  607       /**
  608        * Check whether or not class contains the given annotation.
  609        * 
  610        * @param clazz class instance
  611        * @param annotation annotation class
  612        * @return return true or false
  613        */
  614       public static boolean isAnnotationExistOnClass(Class<?> clazz, Class<? extends Annotation> annotation)
  615       {
  616           Asserts.assertNotNull(clazz, "Clazz argument can not be null");
  617           Asserts.assertNotNull(annotation, "Annotation argument can not be null");
  618   
  619           Annotation a = clazz.getAnnotation(annotation);
  620   
  621           if (a != null)
  622           {
  623               return true;
  624           }
  625   
  626           return false;
  627       }
  628   
  629       public static boolean isMetaAnnotationExist(Annotation[] anns, Class<? extends Annotation> metaAnnotation)
  630       {
  631           Asserts.assertNotNull(anns, "Anns argument can not be null");
  632           Asserts.assertNotNull(metaAnnotation, "MetaAnnotation argument can not be null");
  633   
  634           for (Annotation annot : anns)
  635           {
  636               if (annot.annotationType().isAnnotationPresent(metaAnnotation))
  637               {
  638                   return true;
  639               }
  640           }
  641   
  642           return false;
  643   
  644       }
  645   
  646       public static boolean isAnnotationExist(Annotation[] anns, Class<? extends Annotation> annotation)
  647       {
  648           return getAnnotation(anns, annotation) != null;
  649       }
  650   
  651       /**
  652        * get the annotation of the given type from the array. 
  653        * @param anns
  654        * @param annotation
  655        * @return the Annotation with the given type or <code>null</code> if no such found.
  656        */
  657       public static Annotation getAnnotation(Annotation[] anns, Class<? extends Annotation> annotation)
  658       {
  659           Asserts.assertNotNull(anns, "anns argument can not be null");
  660           Asserts.assertNotNull(annotation, "annotation argument can not be null");
  661           for (Annotation annot : anns)
  662           {
  663               if (annot.annotationType().equals(annotation))
  664               {
  665                   return annot;
  666               }
  667           }
  668   
  669           return null;
  670       }
  671       
  672       public static Annotation[] getMetaAnnotations(Annotation[] anns, Class<? extends Annotation> metaAnnotation)
  673       {
  674           List<Annotation> annots = new ArrayList<Annotation>();
  675           Annotation[] result = null;
  676           Asserts.assertNotNull(anns, "Anns argument can not be null");
  677           Asserts.assertNotNull(metaAnnotation, "MetaAnnotation argument can not be null");
  678   
  679           for (Annotation annot : anns)
  680           {
  681               if (annot.annotationType().isAnnotationPresent(metaAnnotation))
  682               {
  683                   annots.add(annot);
  684               }
  685           }
  686   
  687           result = new Annotation[annots.size()];
  688           result = annots.toArray(result);
  689   
  690           return result;
  691       }
  692   
  693       public static Field[] getClazzFieldsWithGivenAnnotation(Class<?> clazz, Class<? extends Annotation> annotation)
  694       {
  695           Field[] fields = clazz.getDeclaredFields();
  696           List<Field> list = new ArrayList<Field>();
  697   
  698           if (fields.length != 0)
  699           {
  700               for (Field field : fields)
  701               {
  702                   if (field.isAnnotationPresent(annotation))
  703                   {
  704                       list.add(field);
  705                   }
  706               }
  707           }
  708   
  709           fields = new Field[list.size()];
  710           fields = list.toArray(fields);
  711   
  712           return fields;
  713       }
  714   
  715       public static void checkQualifierConditions(Annotation... qualifierAnnots)
  716       {
  717           Annotation before = null;
  718   
  719           for (Annotation ann : qualifierAnnots)
  720           {
  721               Method[] methods = ann.annotationType().getDeclaredMethods();
  722   
  723               for (Method method : methods)
  724               {
  725                   Class<?> clazz = method.getReturnType();
  726                   if (clazz.isArray() || clazz.isAnnotation())
  727                   {
  728                       if (!AnnotationUtil.isAnnotationExist(method.getDeclaredAnnotations(), NonBinding.class))
  729                       {
  730                           throw new WebBeansConfigurationException("@Qualifier : " + ann.annotationType().getName() + " must have @NonBinding valued members for its array-valued and annotation valued members");
  731                       }
  732                   }
  733               }
  734               
  735               
  736               if (!AnnotationUtil.isQualifierAnnotation(ann.annotationType()))
  737               {
  738                   throw new IllegalArgumentException("Qualifier annotations must be annotated with @Qualifier");
  739               }
  740   
  741               if (before == null)
  742               {
  743                   before = ann;
  744               }
  745               else
  746               {
  747                   if (before.equals(ann))
  748                   {
  749                       throw new IllegalArgumentException("Qualifier annotations can not contain duplicate qualifier : @" + before.annotationType().getName());
  750                   }
  751                   else
  752                   {
  753                       before = ann;
  754                   }
  755               }
  756           }
  757       }
  758   
  759       /**
  760        * Returns true if the annotation is defined in xml or annotated with
  761        * {@link javax.inject.Qualifier} false otherwise.
  762        * 
  763        * @param clazz type of the annotation
  764        * @return true if the annotation is defined in xml or annotated with
  765        *         {@link javax.inject.Qualifier} false otherwise
  766        */
  767       public static boolean isQualifierAnnotation(Class<? extends Annotation> clazz)
  768       {
  769           Asserts.assertNotNull(clazz, "clazz parameter can not be null");
  770           XMLAnnotationTypeManager manager = XMLAnnotationTypeManager.getInstance();
  771           if (manager.isBindingTypeExist(clazz))
  772           {
  773               return true;
  774           }
  775           else if (clazz.isAnnotationPresent(Qualifier.class))
  776           {
  777               return true;
  778           }
  779   
  780           return false;
  781       }
  782       
  783       
  784       /**
  785        * Returns true if any binding exist
  786        * 
  787        * @param bean bean
  788        * @return true if any binding exist
  789        */
  790       public static boolean isAnyQualifierExist(Bean<?> bean)
  791       {
  792       	Asserts.assertNotNull(bean, "bean parameter can not be null");
  793       	Set<Annotation> qualifiers = bean.getQualifiers();
  794       	
  795       	for(Annotation ann : qualifiers)
  796       	{
  797       		if(ann.annotationType().equals(Any.class))
  798       		{
  799       			return true;
  800       		}
  801       	}
  802       	
  803       	return false;
  804       }
  805   
  806       /**
  807        * check if any of the given resources is a resource annotation
  808        * @see AnnotationUtil#isResourceAnnotation(Class)
  809        */
  810       public static boolean hasResourceAnnotation(Annotation[] annotations)
  811       {
  812           for (Annotation anno : annotations)
  813           {
  814               if (AnnotationUtil.isResourceAnnotation(anno.annotationType()))
  815               {
  816                   return true;
  817               }
  818           }        
  819           
  820           return false;
  821       }
  822   
  823       /**
  824        * Returns true if the annotation is a valid WebBeans Resource,
  825        * a resource defined in common annotations JSR-250, a remote EJB
  826        * or a web service.
  827        * The following annotations indicate resources
  828        * <ol>
  829        * <li>&#x0040;CustomerDataservice</li>
  830        * <li>&#x0040;Resource</li>
  831        * <li>&#x0040;PersistenceContext</li>
  832        * <li>&#x0040;PersistenceUnit</li>
  833        * <li>&#x0040;EJB</li>
  834        * <li>&#x0040;WebServiceRef</li>
  835        * <li>&#x0040;</li>
  836        * </ol>
  837        * 
  838        * Please note that &#x0040;PersistenceContext(type=EXTENDED) 
  839        * is not supported for simple beans.
  840        * 
  841        * @param clazz type of the annotation
  842        * @return true if the annotation is defined in xml or annotated with
  843        *         {@link javax.inject.Qualifier} false otherwise
  844        */
  845       public static boolean isResourceAnnotation(Class<? extends Annotation> clazz)
  846       {
  847           Asserts.assertNotNull(clazz, "clazz parameter can not be null");
  848   
  849           List<OpenWebBeansPlugin> plugins = PluginLoader.getInstance().getPlugins();
  850           
  851           for (OpenWebBeansPlugin plugin : plugins)
  852           {
  853               if (plugin.isResourceAnnotation(clazz))
  854               {
  855                   return true;
  856               }
  857           }
  858   
  859           return false;
  860       }
  861   
  862       /**
  863        * Returns true if the annotation is defined in xml or annotated with
  864        * {@link javax.interceptor.InterceptorBinding} false otherwise.
  865        * 
  866        * @param clazz type of the annotation
  867        * @return true if the annotation is defined in xml or annotated with
  868        *         {@link javax.interceptor.InterceptorBinding} false otherwise
  869        */
  870       public static boolean isInterceptorBindingAnnotation(Class<? extends Annotation> clazz)
  871       {
  872           Asserts.assertNotNull(clazz, "clazz parameter can not be null");
  873           XMLAnnotationTypeManager manager = XMLAnnotationTypeManager.getInstance();
  874           if (manager.isInterceptorBindingTypeExist(clazz))
  875           {
  876               return true;
  877           }
  878           else if (clazz.isAnnotationPresent(InterceptorBinding.class))
  879           {
  880               return true;
  881           }
  882   
  883           return false;
  884       }
  885   
  886       /**
  887        * If candidate class has an interceptor binding annotation type then return
  888        * true, false otherwise.
  889        * 
  890        * @param clazz interceptor candidate class
  891        * @return true if candidate class has an interceptor binding annotation
  892        *         type false otherwise
  893        */
  894       public static boolean isInterceptorBindingMetaAnnotationExist(Annotation[] anns)
  895       {
  896           Asserts.assertNotNull(anns, "anns parameter can not be null");
  897   
  898           for (Annotation ann : anns)
  899           {
  900               if (isInterceptorBindingAnnotation(ann.annotationType()))
  901               {
  902                   return true;
  903               }
  904               else
  905               {
  906                   continue;
  907               }
  908           }
  909   
  910           return false;
  911       }
  912   
  913       public static Annotation[] getInterceptorBindingMetaAnnotations(Annotation[] anns)
  914       {
  915           Asserts.assertNotNull(anns, "anns parameter can not be null");
  916           List<Annotation> interAnns = new ArrayList<Annotation>();
  917   
  918           for (Annotation ann : anns)
  919           {
  920               if (isInterceptorBindingAnnotation(ann.annotationType()))
  921               {
  922                   interAnns.add(ann);
  923               }
  924           }
  925   
  926           Annotation[] ret = new Annotation[interAnns.size()];
  927           ret = interAnns.toArray(ret);
  928   
  929           return ret;
  930       }
  931   
  932       public static Annotation[] getStereotypeMetaAnnotations(Annotation[] anns)
  933       {
  934           Asserts.assertNotNull(anns, "anns parameter can not be null");
  935           List<Annotation> interAnns = new ArrayList<Annotation>();
  936   
  937           for (Annotation ann : anns)
  938           {
  939               if (isStereoTypeAnnotation(ann.annotationType()))
  940               {
  941                   interAnns.add(ann);
  942               }
  943           }
  944   
  945           Annotation[] ret = new Annotation[interAnns.size()];
  946           ret = interAnns.toArray(ret);
  947   
  948           return ret;
  949       }
  950   
  951       public static boolean isStereoTypeMetaAnnotationExist(Annotation[] anns)
  952       {
  953           Asserts.assertNotNull(anns, "anns parameter can not be null");
  954   
  955           for (Annotation ann : anns)
  956           {
  957               if (isStereoTypeAnnotation(ann.annotationType()))
  958               {
  959                   return true;
  960               }
  961               else
  962               {
  963                   continue;
  964               }
  965           }
  966   
  967           return false;
  968       }
  969   
  970       /**
  971        * Returns true if the annotation is defined in xml or annotated with
  972        * {@link Stereotype} false otherwise.
  973        * 
  974        * @param clazz type of the annotation
  975        * @return true if the annotation is defined in xml or annotated with
  976        *         {@link Stereotype} false otherwise
  977        */
  978       public static boolean isStereoTypeAnnotation(Class<? extends Annotation> clazz)
  979       {
  980           Asserts.assertNotNull(clazz, "clazz parameter can not be null");
  981           XMLAnnotationTypeManager manager = XMLAnnotationTypeManager.getInstance();
  982           if (manager.isStereoTypeExist(clazz))
  983           {
  984               return true;
  985           }
  986           else if (clazz.isAnnotationPresent(Stereotype.class))
  987           {
  988               return true;
  989           }
  990   
  991           return false;
  992       }
  993       
  994       /**
  995        * If the bean extends generic class via {@link Realizes}
  996        * annotation, realized based producer methods, fields and observer
  997        * methods qualifier is
  998        * 
  999        * <ul>
 1000        *  <li>Qualifiers on the definitions</li>
 1001        *  <li>Plus class qualifiers</li>
 1002        *  <li>Minus generic class qualifiers</li>
 1003        * </ul>
 1004        * 
 1005        * @param clazz realized definition class
 1006        * @param anns binding annotations array
 1007        */
 1008       public static Annotation[] getRealizesGenericAnnotations(Class<?> clazz, Annotation[] anns)
 1009       {
 1010          Set<Annotation> setAnnots = new HashSet<Annotation>();
 1011           
 1012           for(Annotation definedAnn : anns)
 1013           {
 1014               setAnnots.add(definedAnn);
 1015           }
 1016           
 1017           Annotation[] genericReliazesAnns = AnnotationUtil.getQualifierAnnotations(clazz.getSuperclass().getDeclaredAnnotations());
 1018           
 1019           for(Annotation generic : genericReliazesAnns)
 1020           {
 1021               setAnnots.remove(generic);
 1022           }
 1023           
 1024           genericReliazesAnns = AnnotationUtil.getQualifierAnnotations(clazz.getDeclaredAnnotations());
 1025   
 1026           for(Annotation generic : genericReliazesAnns)
 1027           {
 1028               setAnnots.add(generic);
 1029           }            
 1030           
 1031           Annotation[] annots = new Annotation[setAnnots.size()];
 1032           annots = setAnnots.toArray(annots);
 1033           
 1034           return annots;
 1035       }
 1036   
 1037       public static Annotation[] getAnnotationsFromSet(Set<Annotation> set)
 1038       {
 1039           if(set != null)
 1040           {
 1041               Annotation[] anns = new Annotation[set.size()];
 1042               anns = set.toArray(anns);
 1043               
 1044               return anns;
 1045           }
 1046           
 1047           return new Annotation[0];
 1048       }
 1049   }

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