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.GenericArrayType;
   20   import java.lang.reflect.Method;
   21   import java.lang.reflect.Modifier;
   22   import java.lang.reflect.ParameterizedType;
   23   import java.lang.reflect.Type;
   24   import java.lang.reflect.TypeVariable;
   25   import java.lang.reflect.WildcardType;
   26   import java.math.BigDecimal;
   27   import java.math.BigInteger;
   28   import java.sql.Time;
   29   import java.sql.Timestamp;
   30   import java.text.DateFormat;
   31   import java.text.ParseException;
   32   import java.text.SimpleDateFormat;
   33   import java.util.ArrayList;
   34   import java.util.Arrays;
   35   import java.util.Calendar;
   36   import java.util.Date;
   37   import java.util.HashMap;
   38   import java.util.HashSet;
   39   import java.util.Iterator;
   40   import java.util.List;
   41   import java.util.Map;
   42   import java.util.Set;
   43   
   44   import javax.enterprise.inject.spi.InjectionPoint;
   45   
   46   import org.apache.webbeans.exception.WebBeansException;
   47   
   48   /**
   49    * Utility classes with respect to the class operations.
   50    * 
   51    * @author <a href="mailto:gurkanerdogdu@yahoo.com">Gurkan Erdogdu</a>
   52    * @since 1.0
   53    */
   54   @SuppressWarnings("unchecked")
   55   public final class ClassUtil
   56   {
   57       public static final Map<Class<?>, Object> PRIMITIVE_CLASS_DEFAULT_VALUES = new HashMap<Class<?>, Object>();;
   58   
   59       public static final Set<Class<?>> VALUE_TYPES = new HashSet<Class<?>>();
   60   
   61       public static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPERS_MAP = new HashMap<Class<?>, Class<?>>();
   62   
   63       public static final String WEBBEANS_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
   64   
   65       static
   66       { 
   67           PRIMITIVE_CLASS_DEFAULT_VALUES.put(Integer.class, Integer.MIN_VALUE);
   68           PRIMITIVE_CLASS_DEFAULT_VALUES.put(Float.class, Float.MIN_VALUE);
   69           PRIMITIVE_CLASS_DEFAULT_VALUES.put(Double.class, Double.MIN_VALUE);
   70           PRIMITIVE_CLASS_DEFAULT_VALUES.put(Character.class, Character.MIN_VALUE);
   71           PRIMITIVE_CLASS_DEFAULT_VALUES.put(String.class, new String());
   72           PRIMITIVE_CLASS_DEFAULT_VALUES.put(BigDecimal.class, BigDecimal.ZERO);
   73           PRIMITIVE_CLASS_DEFAULT_VALUES.put(BigInteger.class, BigInteger.ZERO);
   74           PRIMITIVE_CLASS_DEFAULT_VALUES.put(Long.class, Long.MIN_VALUE);
   75           PRIMITIVE_CLASS_DEFAULT_VALUES.put(Byte.class, Byte.MIN_VALUE);
   76           PRIMITIVE_CLASS_DEFAULT_VALUES.put(Short.class, Short.MIN_VALUE);
   77           PRIMITIVE_CLASS_DEFAULT_VALUES.put(Boolean.class, Boolean.FALSE);
   78   
   79           VALUE_TYPES.add(String.class);
   80           VALUE_TYPES.add(Date.class);
   81           VALUE_TYPES.add(Calendar.class);
   82           VALUE_TYPES.add(Class.class);
   83           VALUE_TYPES.add(List.class);
   84           VALUE_TYPES.add(Enum.class);
   85           VALUE_TYPES.add(java.sql.Date.class);
   86           VALUE_TYPES.add(Time.class);
   87           VALUE_TYPES.add(Timestamp.class);
   88           VALUE_TYPES.add(BigDecimal.class);
   89           VALUE_TYPES.add(BigInteger.class);
   90           
   91           PRIMITIVE_TO_WRAPPERS_MAP.put(Integer.TYPE,Integer.class);
   92           PRIMITIVE_TO_WRAPPERS_MAP.put(Float.TYPE,Float.class);
   93           PRIMITIVE_TO_WRAPPERS_MAP.put(Double.TYPE,Double.class);
   94           PRIMITIVE_TO_WRAPPERS_MAP.put(Character.TYPE,Character.class);
   95           PRIMITIVE_TO_WRAPPERS_MAP.put(Long.TYPE,Long.class);
   96           PRIMITIVE_TO_WRAPPERS_MAP.put(Byte.TYPE,Byte.class);
   97           PRIMITIVE_TO_WRAPPERS_MAP.put(Short.TYPE,Short.class);
   98           PRIMITIVE_TO_WRAPPERS_MAP.put(Boolean.TYPE,Boolean.class);
   99           PRIMITIVE_TO_WRAPPERS_MAP.put(Void.TYPE,Void.class);
  100       }
  101   
  102       /*
  103        * Private constructor
  104        */
  105       private ClassUtil()
  106       {
  107           throw new UnsupportedOperationException();
  108       }
  109   
  110       /**
  111        * Check the parametrized type actual arguments equals with the class type
  112        * variables at the injection point.
  113        * 
  114        * @param variables type variable
  115        * @param types type
  116        * @return
  117        */
  118       public static boolean checkEqual(TypeVariable<?>[] variables, Type[] types)
  119       {
  120           Asserts.assertNotNull(variables, "variables parameter can not be null");
  121           Asserts.assertNotNull(types, "types parameter can not be null");
  122   
  123           for (TypeVariable<?> variable : variables)
  124           {
  125               for (Type type : types)
  126               {
  127                   if (type instanceof TypeVariable)
  128                   {
  129                       TypeVariable<?> t = ((TypeVariable<?>) type);
  130                       if (t.getGenericDeclaration().equals(variable.getGenericDeclaration()))
  131                       {
  132                           if (t.getName().equals(variable.getName()))
  133                           {
  134                               continue;
  135                           }
  136                           else
  137                           {
  138                               return false;
  139                           }
  140   
  141                       }
  142                       else
  143                       {
  144                           return false;
  145                       }
  146                   }
  147               }
  148           }
  149   
  150           return true;
  151   
  152       }
  153   
  154       public static Class<?> getClassFromName(String name)
  155       {
  156           Class<?> clazz = null;
  157   
  158           try
  159           {
  160               ClassLoader loader = WebBeansUtil.getCurrentClassLoader();
  161               clazz = loader.loadClass(name);
  162   
  163               return clazz;
  164   
  165           }
  166           catch (ClassNotFoundException e)
  167           {
  168               try
  169               {
  170                   clazz = ClassUtil.class.getClassLoader().loadClass(name);
  171   
  172                   return clazz;
  173   
  174               }
  175               catch (ClassNotFoundException e1)
  176               {
  177                   try
  178                   {
  179                       clazz = ClassLoader.getSystemClassLoader().loadClass(name);
  180   
  181                       return clazz;
  182   
  183                   }
  184                   catch (ClassNotFoundException e2)
  185                   {
  186                       return null;
  187                   }
  188   
  189               }
  190           }
  191   
  192       }
  193   
  194       /**
  195        * Check final modifier.
  196        * 
  197        * @param modifier modifier
  198        * @return true or false
  199        */
  200       public static boolean isFinal(Integer modifier)
  201       {
  202           Asserts.nullCheckForModifier(modifier);
  203   
  204           return Modifier.isFinal(modifier);
  205       }
  206   
  207       /**
  208        * Check abstract modifier.
  209        * 
  210        * @param modifier modifier
  211        * @return true or false
  212        */
  213       public static boolean isAbstract(Integer modifier)
  214       {
  215           Asserts.nullCheckForModifier(modifier);
  216   
  217           return Modifier.isAbstract(modifier);
  218       }
  219   
  220       /**
  221        * Check interface modifier.
  222        * 
  223        * @param modifier modifier
  224        * @return true or false
  225        */
  226       public static boolean isInterface(Integer modifier)
  227       {
  228           Asserts.nullCheckForModifier(modifier);
  229   
  230           return Modifier.isInterface(modifier);
  231       }
  232   
  233       /**
  234        * Check for class that has a final method or not.
  235        * 
  236        * @param clazz check methods of it
  237        * @return true or false
  238        */
  239       public static boolean hasFinalMethod(Class<?> clazz)
  240       {
  241           Asserts.nullCheckForClass(clazz);
  242   
  243           Method[] methods = clazz.getDeclaredMethods();
  244           for (Method m : methods)
  245           {
  246               if (isFinal(m.getModifiers()))
  247               {
  248                   return true;
  249               }
  250           }
  251   
  252           return false;
  253       }
  254   
  255       /**
  256        * Check the class is inner or not
  257        * 
  258        * @param modifier modifier
  259        * @return true or false
  260        */
  261       public static boolean isInnerClazz(Class<?> clazz)
  262       {
  263           Asserts.nullCheckForClass(clazz);
  264   
  265           return clazz.isMemberClass();
  266       }
  267   
  268       /**
  269        * Check the modifier contains static keyword.
  270        * 
  271        * @param modifier modifier
  272        * @return true or false
  273        */
  274       public static boolean isStatic(Integer modifier)
  275       {
  276           Asserts.nullCheckForModifier(modifier);
  277   
  278           return Modifier.isStatic(modifier);
  279       }
  280   
  281       /**
  282        * Check the modifier contains static keyword.
  283        * 
  284        * @param modifier modifier
  285        * @return true or false
  286        */
  287       public static boolean isPublic(Integer modifier)
  288       {
  289           Asserts.nullCheckForModifier(modifier);
  290   
  291           return Modifier.isPublic(modifier);
  292       }
  293   
  294       /**
  295        * Check the modifier contains static keyword.
  296        * 
  297        * @param modifier modifier
  298        * @return true or false
  299        */
  300       public static boolean isPrivate(Integer modifier)
  301       {
  302           Asserts.nullCheckForModifier(modifier);
  303   
  304           return Modifier.isPrivate(modifier);
  305       }
  306   
  307       /**
  308        * Gets the Java Standart Class default value.
  309        * 
  310        * @param <T> parametrized type
  311        * @param clazz class instance
  312        * @return default value of the class
  313        */
  314       public static <T> T defaultJavaValues(Class<T> clazz)
  315       {
  316           Asserts.nullCheckForClass(clazz);
  317   
  318           Set<Class<?>> keySet = PRIMITIVE_CLASS_DEFAULT_VALUES.keySet();
  319           Iterator<Class<?>> it = keySet.iterator();
  320           while (it.hasNext())
  321           {
  322               Class<?> obj = it.next();
  323               if (clazz.equals(obj))
  324               {
  325                   return (T) PRIMITIVE_CLASS_DEFAULT_VALUES.get(obj);
  326               }
  327           }
  328   
  329           return null;
  330   
  331       }
  332       
  333       public static Class<?>  getPrimitiveWrapper(Class<?> clazz)
  334       {
  335           Asserts.nullCheckForClass(clazz);
  336           
  337           return PRIMITIVE_TO_WRAPPERS_MAP.get(clazz);
  338   
  339       }
  340       
  341       public static Class<?> getWrapperPrimitive(Class<?> clazz)
  342       {
  343           Asserts.nullCheckForClass(clazz);
  344           
  345           Set<Class<?>> keySet = PRIMITIVE_TO_WRAPPERS_MAP.keySet();
  346           
  347           for(Class<?> key : keySet)
  348           {
  349                if(PRIMITIVE_TO_WRAPPERS_MAP.get(key).equals(clazz))
  350                {
  351                    return key;
  352                }
  353           }
  354           
  355           return null;
  356       }
  357   
  358       /**
  359        * Gets the class of the given type arguments.
  360        * <p>
  361        * If the given type {@link Type} parameters is an instance of the
  362        * {@link ParameterizedType}, it returns the raw type otherwise it return
  363        * the casted {@link Class} of the type argument.
  364        * </p>
  365        * 
  366        * @param type class or parametrized type
  367        * @return
  368        */
  369       public static Class<?> getClass(Type type)
  370       {
  371           return getClazz(type);
  372       }
  373   
  374       /**
  375        * Gets the declared methods of the given class.
  376        * 
  377        * @param clazz class instance
  378        * @return the declared methods
  379        */
  380       public static Method[] getDeclaredMethods(Class<?> clazz)
  381       {
  382           Asserts.nullCheckForClass(clazz);
  383           return clazz.getDeclaredMethods();
  384       }
  385   
  386       /**
  387        * Check that method has any formal arguments.
  388        * 
  389        * @param method method instance
  390        * @return true or false
  391        */
  392       public static boolean isMethodHasParameter(Method method)
  393       {
  394           Asserts.nullCheckForMethod(method);
  395   
  396           Class<?>[] types = method.getParameterTypes();
  397           if (types.length != 0)
  398           {
  399               return true;
  400           }
  401   
  402           return false;
  403       }
  404   
  405       /**
  406        * Gets the return type of the method.
  407        * 
  408        * @param method method instance
  409        * @return return type
  410        */
  411       public static Class<?> getReturnType(Method method)
  412       {
  413           Asserts.nullCheckForMethod(method);
  414           return method.getReturnType();
  415       }
  416   
  417       /**
  418        * Check method throws checked exception or not.
  419        * 
  420        * @param method method instance
  421        * @return trur or false
  422        */
  423       public static boolean isMethodHasCheckedException(Method method)
  424       {
  425           Asserts.nullCheckForMethod(method);
  426   
  427           Class<?>[] et = method.getExceptionTypes();
  428   
  429           if (et.length > 0)
  430           {
  431               for (Class<?> type : et)
  432               {
  433                   if (Error.class.isAssignableFrom(type) || RuntimeException.class.isAssignableFrom(type))
  434                   {
  435                       return false;
  436                   }
  437                   else
  438                   {
  439                       return true;
  440                   }
  441               }
  442   
  443           }
  444   
  445           return false;
  446       }
  447   
  448       /**
  449        * Check method throws Exception or not.
  450        * 
  451        * @param method method instance
  452        * @return trur or false
  453        */
  454       public static boolean isMethodHasException(Method method)
  455       {
  456           Asserts.nullCheckForMethod(method);
  457   
  458           Class<?>[] et = method.getExceptionTypes();
  459   
  460           if (et.length == 1)
  461           {
  462               if (et[0].equals(Exception.class))
  463                   return true;
  464           }
  465   
  466           return false;
  467       }
  468   
  469       /**
  470        * Call method on the instance with given arguments.
  471        * 
  472        * @param method method instance
  473        * @param instance object instance
  474        * @param args arguments
  475        * @return the method result
  476        */
  477       public static Object callInstanceMethod(Method method, Object instance, Object[] args)
  478       {
  479           Asserts.nullCheckForMethod(method);
  480           Asserts.assertNotNull(instance, "instance parameter can not be null");
  481   
  482           try
  483           {
  484               if (args == null)
  485               {
  486                   args = new Object[] {};
  487               }
  488               return method.invoke(instance, args);
  489   
  490           }
  491           catch (Exception e)
  492           {
  493               throw new WebBeansException("Exception occurs in the method call with method : " + method.getName() + " in class : " + instance.getClass().getName());
  494           }
  495   
  496       }
  497   
  498       public static List<Class<?>> getSuperClasses(Class<?> clazz, List<Class<?>> list)
  499       {
  500           Asserts.nullCheckForClass(clazz);
  501   
  502           Class<?> sc = clazz.getSuperclass();
  503           if (sc != null)
  504           {
  505               list.add(sc);
  506               getSuperClasses(sc, list);
  507           }
  508   
  509           return list;
  510   
  511       }
  512   
  513       public static Class<?>[] getMethodParameterTypes(Method method)
  514       {
  515           Asserts.nullCheckForMethod(method);
  516           return method.getParameterTypes();
  517       }
  518   
  519       public static List<String> getObjectMethodNames()
  520       {
  521           List<String> list = new ArrayList<String>();
  522           Class<?> clazz = Object.class;
  523   
  524           Method[] methods = clazz.getDeclaredMethods();
  525           for (Method method : methods)
  526           {
  527               list.add(method.getName());
  528           }
  529   
  530           return list;
  531       }
  532   
  533       public static boolean isObjectMethod(String methodName)
  534       {
  535           Asserts.assertNotNull(methodName, "methodName parameter can not be null");
  536           return getObjectMethodNames().contains(methodName);
  537       }
  538   
  539       public static boolean isMoreThanOneMethodWithName(String methodName, Class<?> clazz)
  540       {
  541           Asserts.assertNotNull(methodName, "methodName parameter can not be null");
  542           Asserts.nullCheckForClass(clazz);
  543   
  544           Method[] methods = clazz.getDeclaredMethods();
  545           int i = 0;
  546           for (Method m : methods)
  547           {
  548               if (m.getName().equals(methodName))
  549               {
  550                   i++;
  551               }
  552           }
  553   
  554           if (i > 1)
  555           {
  556               return true;
  557           }
  558   
  559           return false;
  560   
  561       }
  562   
  563       public static <T> Constructor<T> isContaintNoArgConstructor(Class<T> clazz)
  564       {
  565           Asserts.nullCheckForClass(clazz);
  566           try
  567           {
  568               return clazz.getDeclaredConstructor(new Class<?>[] {});
  569   
  570           }
  571           catch (Exception e)
  572           {
  573               return null;
  574           }
  575   
  576       }
  577   
  578       /**
  579        * Check the modifiers contains the public keyword.
  580        * 
  581        * @param modifs modifiers
  582        * @return true or false
  583        */
  584       public static boolean isPublic(int modifs)
  585       {
  586           return Modifier.isPublic(modifs);
  587       }
  588   
  589       /**
  590        * Gets java package if exist.
  591        * 
  592        * @param packageName package name
  593        * @return the package with given name
  594        */
  595       public Package getPackage(String packageName)
  596       {
  597           Asserts.assertNotNull(packageName, "packageName parameter can not be null");
  598   
  599           return Package.getPackage(packageName);
  600       }
  601   
  602       /**
  603        * Returns true if type is an instance of <code>ParameterizedType</code>
  604        * else otherwise.
  605        * 
  606        * @param type type of the artifact
  607        * @return true if type is an instance of <code>ParameterizedType</code>
  608        */
  609       public static boolean isParametrizedType(Type type)
  610       {
  611           Asserts.assertNotNull(type, "type parameter can not be null");
  612           if (type instanceof ParameterizedType)
  613           {
  614               return true;
  615           }
  616   
  617           return false;
  618       }
  619       
  620       /**
  621        * Returns true if type is an instance of <code>WildcardType</code>
  622        * else otherwise.
  623        * 
  624        * @param type type of the artifact
  625        * @return true if type is an instance of <code>WildcardType</code>
  626        */    
  627       public static boolean isWildCardType(Type type)
  628       {
  629           Asserts.assertNotNull(type, "type parameter can not be null");
  630           
  631           if (type instanceof WildcardType)
  632           {
  633               return true;
  634           }
  635   
  636           return false;
  637       }
  638       
  639       public static boolean isUnboundedTypeVariable(Type type)
  640       {
  641           Asserts.assertNotNull(type, "type parameter can not be null");
  642           
  643           if (type instanceof TypeVariable)
  644           {
  645               TypeVariable wc = (TypeVariable)type;
  646               Type[] upper = wc.getBounds();            
  647               
  648               
  649               if(upper.length > 1)
  650               {
  651                   return false;
  652               }            
  653               else
  654               {
  655                   Type arg = upper[0];
  656                   if(!(arg instanceof Class))
  657                   {
  658                       return false;
  659                   }
  660                   else
  661                   {
  662                       Class<?> clazz = (Class<?>)arg;
  663                       if(!clazz.equals(Object.class))
  664                       {
  665                           return false;
  666                       }                    
  667                   }
  668               }            
  669           }
  670           else
  671           {
  672               return false;
  673           }
  674   
  675           return true;
  676       }
  677       
  678       
  679       /**
  680        * Returns true if type is an instance of <code>TypeVariable</code>
  681        * else otherwise.
  682        * 
  683        * @param type type of the artifact
  684        * @return true if type is an instance of <code>TypeVariable</code>
  685        */    
  686       public static boolean isTypeVariable(Type type)
  687       {
  688           Asserts.assertNotNull(type, "type parameter can not be null");
  689   
  690           if (type instanceof TypeVariable)
  691           {
  692               return true;
  693           }
  694   
  695           return false;
  696   
  697       }
  698       
  699   
  700       /**
  701        * Returna true if the class is not abstract and interface.
  702        *     
  703        * @param clazz class type
  704        * @return true if the class is not abstract and interface
  705        */
  706       public static boolean isConcrete(Class<?> clazz)
  707       {
  708           Asserts.nullCheckForClass(clazz);
  709   
  710           Integer modifier = clazz.getModifiers();
  711   
  712           if (!isAbstract(modifier) && !isInterface(modifier))
  713           {
  714               return true;
  715           }
  716   
  717           return false;
  718       }
  719   
  720       /**
  721        * Returns class constructor array.
  722        * 
  723        * @param <T> class type arfument
  724        * @param clazz class that is searched for constructor.
  725        * @return class constructor array
  726        */
  727       public static <T> Constructor<T>[] getConstructors(Class<T> clazz)
  728       {
  729           Asserts.nullCheckForClass(clazz);
  730           
  731           return (Constructor<T>[]) clazz.getDeclaredConstructors();
  732       }
  733   
  734       /**
  735        * Returns true if class has a default constructor.
  736        * 
  737        * @param <T> type argument of class
  738        * @param clazz class type
  739        * @return true if class has a default constructor.
  740        */
  741       public static <T> boolean isDefaultConstructorExist(Class<T> clazz)
  742       {
  743           Asserts.nullCheckForClass(clazz);
  744           
  745           try
  746           {
  747               clazz.getDeclaredConstructor(new Class<?>[] {});
  748   
  749           }
  750           catch (SecurityException e)
  751           {
  752               throw new WebBeansException(e);
  753           }
  754           catch (NoSuchMethodException e)
  755           {
  756               return false;
  757           }
  758   
  759           return true;
  760       }
  761   
  762       public static boolean isAssignable(Type beanType, Type requiredType)
  763       {
  764           Asserts.assertNotNull(beanType, "beanType parameter can not be null");
  765           Asserts.assertNotNull(requiredType, "requiredType parameter can not be null");
  766   
  767           if (beanType instanceof ParameterizedType && requiredType instanceof ParameterizedType)
  768           {
  769               return isAssignableForParametrized((ParameterizedType) beanType, (ParameterizedType) requiredType);
  770           }
  771           else if (beanType instanceof Class && requiredType instanceof Class)
  772           {
  773               return isAssignable((Class) requiredType, (Class) beanType);
  774           }
  775           else if(beanType instanceof ParameterizedType && requiredType instanceof Class)
  776           {
  777               boolean ok = true;
  778               ParameterizedType ptBean = (ParameterizedType)beanType;
  779               Class<?> clazzBeanType = (Class<?>)ptBean.getRawType();
  780               Class<?> clazzReqType = (Class<?>)requiredType;
  781               if(isAssignable(clazzReqType, clazzBeanType ))
  782               {
  783                   Type[]  beanTypeArgs = ptBean.getActualTypeArguments();               
  784                   for(Type actual : beanTypeArgs)
  785                   {
  786                       if(!ClassUtil.isUnboundedTypeVariable(actual))
  787                       {
  788                           if(actual instanceof Class)
  789                           {
  790                               Class<?> clazz = (Class<?>)actual;
  791                               if(clazz.equals(Object.class))
  792                               {
  793                                   continue;
  794                               }
  795                               else
  796                               {
  797                                   ok = false;
  798                                   break;
  799                               }
  800                           }
  801                           else
  802                           {
  803                               ok = false;
  804                               break;
  805                           }
  806                       }
  807                   }                
  808               }
  809               else
  810               {
  811                   ok = false;
  812               }
  813               
  814               
  815               return ok;
  816           }
  817           else if(beanType instanceof Class && requiredType instanceof ParameterizedType)
  818           {
  819               Class<?> clazzBeanType = (Class<?>)beanType;
  820               ParameterizedType ptReq = (ParameterizedType)requiredType;
  821               Class<?> clazzReqType = (Class<?>)ptReq.getRawType();
  822               
  823               if(isAssignable(clazzReqType, clazzBeanType))
  824               {
  825                   return true;
  826               }
  827               
  828               return false;
  829           }
  830           else
  831           {
  832               return false;
  833           }
  834       }
  835   
  836       
  837       
  838       
  839       /**
  840        * Returns true if rhs is assignable type
  841        * to the lhs, false otherwise.
  842        * 
  843        * @param lhs left hand side class
  844        * @param rhs right hand side class
  845        * @return true if rhs is assignable to lhs
  846        */
  847       public static boolean isAssignable(Class<?> lhs, Class<?> rhs)
  848       {
  849           Asserts.assertNotNull(lhs, "lhs parameter can not be null");
  850           Asserts.assertNotNull(rhs, "rhs parameter can not be null");
  851   
  852           if(lhs.isPrimitive())
  853           {
  854               lhs = getPrimitiveWrapper(lhs);
  855           }
  856           
  857           if(rhs.isPrimitive())
  858           {
  859               rhs = getPrimitiveWrapper(rhs);
  860           }
  861           
  862           if (lhs.isAssignableFrom(rhs))
  863           {
  864               return true;
  865           }
  866   
  867           return false;
  868       }
  869   
  870       /**
  871        * Returns true if given bean's api type is injectable to
  872        * injection point required type.
  873        * 
  874        * @param beanType bean parametrized api type
  875        * @param requiredType injection point parametrized api type
  876        * @return if injection is possible false otherwise
  877        */
  878       public static boolean isAssignableForParametrized(ParameterizedType beanType, ParameterizedType requiredType)
  879       {
  880           Class<?> beanRawType = (Class<?>) beanType.getRawType();
  881           Class<?> requiredRawType = (Class<?>) requiredType.getRawType();
  882   
  883           if (ClassUtil.isAssignable(requiredRawType,beanRawType))
  884           {
  885               //Bean api type actual type arguments
  886               Type[] beanTypeArgs = beanType.getActualTypeArguments();
  887               
  888               //Injection point type actual arguments
  889               Type[] requiredTypeArgs = requiredType.getActualTypeArguments();
  890               
  891               if(beanTypeArgs.length != requiredTypeArgs.length)
  892               {                
  893                   return false;
  894               }
  895               else
  896               {
  897                   return isAssignableForParametrizedCheckArguments(beanTypeArgs, requiredTypeArgs);
  898               }
  899           }
  900   
  901           return false;
  902       }
  903       
  904       private static boolean isAssignableForParametrizedCheckArguments(Type[] beanTypeArgs, Type[] requiredTypeArgs)
  905       {
  906           Type requiredTypeArg = null;
  907           Type beanTypeArg = null;
  908           for(int i = 0; i< requiredTypeArgs.length;i++)
  909           {
  910               requiredTypeArg = requiredTypeArgs[i];
  911               beanTypeArg = beanTypeArgs[i];
  912               
  913               //Required type is parametrized
  914               if(ClassUtil.isParametrizedType(requiredTypeArg) && ClassUtil.isParametrizedType(beanTypeArg))
  915               {
  916                   return check1(beanTypeArg, requiredTypeArg);
  917               }
  918               //Required type is wildcard
  919               else if(ClassUtil.isWildCardType(requiredTypeArg))
  920               {
  921                   return check2(beanTypeArg, requiredTypeArg);
  922               }
  923               //Required type is actual type
  924               else if(requiredTypeArg instanceof Class && ClassUtil.isTypeVariable(beanTypeArg))
  925               {
  926                   return check3(beanTypeArg, requiredTypeArg);
  927               }
  928               //Required type is Type variable
  929               else if(ClassUtil.isTypeVariable(requiredTypeArg) && ClassUtil.isTypeVariable(beanTypeArg))
  930               {
  931                   return check4(beanTypeArg, requiredTypeArg);
  932               }      
  933               else if((beanTypeArg instanceof Class) && (requiredTypeArg instanceof Class))
  934               {
  935                   if(isAssignable(beanTypeArg, requiredTypeArg))
  936                   {
  937                       return true;
  938                   }
  939               }
  940           }
  941           
  942           return false;
  943       }
  944       
  945       private static boolean check1(Type beanTypeArg, Type requiredTypeArg)
  946       {
  947           ParameterizedType ptRequiredTypeArg = (ParameterizedType)requiredTypeArg;
  948           ParameterizedType ptBeanTypeArg = (ParameterizedType)beanTypeArg;
  949           
  950           //Equal raw types
  951           if(ptRequiredTypeArg.getRawType().equals(ptBeanTypeArg.getRawType()))
  952           {
  953               //Check arguments
  954               Type[] actualArgsRequiredType = ptRequiredTypeArg.getActualTypeArguments();
  955               Type[] actualArgsBeanType = ptRequiredTypeArg.getActualTypeArguments();
  956               
  957               if(actualArgsRequiredType.length > 0 && actualArgsBeanType.length == actualArgsRequiredType.length)
  958               {
  959                   return isAssignableForParametrizedCheckArguments(actualArgsBeanType, actualArgsRequiredType);
  960               }
  961               else
  962               {
  963                   return true;
  964               }
  965           }
  966           
  967           return false;
  968       }
  969       
  970       private static boolean check2(Type beanTypeArg, Type requiredTypeArg)
  971       {
  972           WildcardType wctRequiredTypeArg = (WildcardType)requiredTypeArg;
  973           Type upperBoundRequiredTypeArg =  wctRequiredTypeArg.getUpperBounds()[0];
  974           Type[] lowerBoundRequiredTypeArgs =  wctRequiredTypeArg.getLowerBounds();
  975           
  976           if(beanTypeArg instanceof Class)
  977           {
  978               Class<?> clazzBeanTypeArg = (Class<?>)beanTypeArg;
  979               if(upperBoundRequiredTypeArg instanceof Class)
  980               {
  981                   Class<?> clazzUpperBoundTypeArg = (Class<?>)upperBoundRequiredTypeArg;
  982                   if(clazzUpperBoundTypeArg.isAssignableFrom(clazzBeanTypeArg))
  983                   {                                         
  984                       if(lowerBoundRequiredTypeArgs.length > 0 &&  lowerBoundRequiredTypeArgs[0] instanceof Class)
  985                       {
  986                           Class<?> clazzLowerBoundTypeArg = (Class<?>)lowerBoundRequiredTypeArgs[0];
  987                           if(clazzBeanTypeArg.isAssignableFrom(clazzLowerBoundTypeArg))
  988                           {
  989                               return true;
  990                           }
  991                       }
  992                       else
  993                       {
  994                           return true;
  995                       }
  996                       
  997                   }
  998               }                    
  999           }
 1000           else if(ClassUtil.isTypeVariable(beanTypeArg))
 1001           {
 1002               TypeVariable<?> tvBeanTypeArg = (TypeVariable<?>)beanTypeArg;
 1003               Type tvBound = tvBeanTypeArg.getBounds()[0];
 1004               
 1005               if(tvBound instanceof Class)
 1006               {
 1007                   Class<?> clazzTvBound = (Class<?>)tvBound;
 1008                   
 1009                   if(upperBoundRequiredTypeArg instanceof Class)
 1010                   {
 1011                       Class<?> clazzUpperBoundTypeArg = (Class<?>)upperBoundRequiredTypeArg;
 1012                       if(clazzUpperBoundTypeArg.isAssignableFrom(clazzTvBound))
 1013                       {                            
 1014                           if(lowerBoundRequiredTypeArgs.length > 0 &&  lowerBoundRequiredTypeArgs[0] instanceof Class)
 1015                           {
 1016                               Class<?> clazzLowerBoundTypeArg = (Class<?>)lowerBoundRequiredTypeArgs[0];
 1017                               if(clazzTvBound.isAssignableFrom(clazzLowerBoundTypeArg))
 1018                               {
 1019                                   return true;
 1020                               }
 1021                           }
 1022                           else
 1023                           {
 1024                               return true;
 1025                           }
 1026                           
 1027                       }
 1028                   }                                    
 1029               }
 1030           }
 1031            
 1032           return false;
 1033       }
 1034       
 1035       private static boolean check3(Type beanTypeArg, Type requiredTypeArg)
 1036       {
 1037           Class<?> clazzRequiredType = (Class<?>)requiredTypeArg;
 1038           
 1039           TypeVariable<?> tvBeanTypeArg = (TypeVariable<?>)beanTypeArg;
 1040           Type tvBound = tvBeanTypeArg.getBounds()[0];
 1041           
 1042           if(tvBound instanceof Class)
 1043           {
 1044               Class<?> clazzTvBound = (Class<?>)tvBound;
 1045               
 1046               if(clazzRequiredType.isAssignableFrom(clazzTvBound))
 1047               {
 1048                   return true;
 1049               }                    
 1050           }
 1051           
 1052           return false;
 1053       }
 1054   
 1055       private static boolean check4(Type beanTypeArg, Type requiredTypeArg)
 1056       {
 1057           TypeVariable<?> tvBeanTypeArg = (TypeVariable<?>)beanTypeArg;
 1058           Type tvBeanBound = tvBeanTypeArg.getBounds()[0];
 1059           
 1060           TypeVariable<?> tvRequiredTypeArg = (TypeVariable<?>)requiredTypeArg;
 1061           Type tvRequiredBound = tvRequiredTypeArg.getBounds()[0];
 1062           
 1063           if(tvBeanBound instanceof Class && tvRequiredBound instanceof Class)
 1064           {
 1065               Class<?> clazzTvBeanBound = (Class<?>)tvBeanBound;
 1066               Class<?> clazzTvRequiredBound = (Class<?>)tvRequiredBound;
 1067               
 1068               if(clazzTvRequiredBound.isAssignableFrom(clazzTvBeanBound))
 1069               {
 1070                   return true;
 1071               }                    
 1072           }
 1073           
 1074           return false;
 1075       }
 1076   
 1077   
 1078       public static boolean classHasFieldWithName(Class<?> clazz, String fieldName)
 1079       {
 1080           Asserts.nullCheckForClass(clazz);
 1081           Asserts.assertNotNull(fieldName, "fieldName parameter can not be null");
 1082           try
 1083           {
 1084   
 1085               clazz.getDeclaredField(fieldName);
 1086   
 1087           }
 1088           catch (SecurityException e)
 1089           {
 1090               // we must throw here!
 1091               throw new WebBeansException(e);
 1092           }
 1093           catch (NoSuchFieldException e2)
 1094           {
 1095               return false;
 1096           }
 1097   
 1098           return true;
 1099       }
 1100   
 1101       public static boolean classHasMoreThanOneFieldWithName(Class<?> clazz, String fieldName)
 1102       {
 1103           Asserts.nullCheckForClass(clazz);
 1104           Asserts.assertNotNull(fieldName, "fieldName parameter can not be null");
 1105   
 1106           Field[] fields = clazz.getDeclaredFields();
 1107           boolean ok = false;
 1108           for (Field field : fields)
 1109           {
 1110               if (field.getName().equals(fieldName))
 1111               {
 1112                   if (ok)
 1113                   {
 1114                       return true;
 1115                   }
 1116                   else
 1117                   {
 1118                       ok = true;
 1119                   }
 1120               }
 1121           }
 1122   
 1123           return false;
 1124       }
 1125   
 1126       public static Field getFieldWithName(Class<?> clazz, String fieldName)
 1127       {
 1128           Asserts.nullCheckForClass(clazz);
 1129           Asserts.assertNotNull(fieldName, "fieldName parameter can not be null");
 1130           try
 1131           {
 1132   
 1133               return clazz.getDeclaredField(fieldName);
 1134   
 1135           }
 1136           catch (SecurityException e)
 1137           {
 1138               // we must throw here!
 1139               throw new WebBeansException(e);
 1140           }
 1141           catch (NoSuchFieldException e2)
 1142           {
 1143               return null;
 1144           }
 1145   
 1146       }
 1147   
 1148       /**
 1149        * @param clazz webbeans implementation class
 1150        * @param methodName name of the method that is searched
 1151        * @param parameterTypes parameter types of the method(it can be subtype of
 1152        *            the actual type arguments of the method)
 1153        * @return the list of method that satisfies the condition
 1154        */
 1155       public static List<Method> getClassMethodsWithTypes(Class<?> clazz, String methodName, List<Class<?>> parameterTypes)
 1156       {
 1157           Asserts.nullCheckForClass(clazz);
 1158           Asserts.assertNotNull(methodName, "methodName parameter can not be null");
 1159           Asserts.assertNotNull(parameterTypes, "parameterTypes parameter can not be null");
 1160   
 1161           List<Method> methodList = new ArrayList<Method>();
 1162   
 1163           Method[] methods = clazz.getDeclaredMethods();
 1164   
 1165           int j = 0;
 1166           for (Method method : methods)
 1167           {
 1168               if (method.getName().equals(methodName))
 1169               {
 1170                   Class<?>[] defineTypes = method.getParameterTypes();
 1171   
 1172                   if (defineTypes.length != parameterTypes.size())
 1173                   {
 1174                       continue;
 1175                   }
 1176   
 1177                   boolean ok = true;
 1178   
 1179                   if (parameterTypes != null && parameterTypes.size() > 0)
 1180                   {
 1181                       ok = false;
 1182                   }
 1183   
 1184                   if (!ok)
 1185                   {
 1186                       for (Class<?> defineType : defineTypes)
 1187                       {
 1188                           if (defineType.isAssignableFrom(parameterTypes.get(j)))
 1189                           {
 1190                               ok = true;
 1191                           }
 1192                           else
 1193                           {
 1194                               ok = false;
 1195                           }
 1196   
 1197                           j++;
 1198                       }
 1199                   }
 1200   
 1201                   if (ok)
 1202                   {
 1203                       methodList.add(method);
 1204                   }
 1205               }
 1206   
 1207           }
 1208   
 1209           return methodList;
 1210       }
 1211   
 1212       public static Method getClassMethodWithTypes(Class<?> clazz, String methodName, List<Class<?>> parameterTypes)
 1213       {
 1214           Asserts.nullCheckForClass(clazz);
 1215           Asserts.assertNotNull(methodName, "methodName parameter can not be null");
 1216           Asserts.assertNotNull(parameterTypes, "parameterTypes parameter can not be null");
 1217   
 1218           Method[] methods = clazz.getDeclaredMethods();
 1219   
 1220           int j = 0;
 1221           for (Method method : methods)
 1222           {
 1223               if (method.getName().equals(methodName))
 1224               {
 1225                   if (parameterTypes != null && parameterTypes.size() > 0)
 1226                   {
 1227                       Class<?>[] defineTypes = method.getParameterTypes();
 1228   
 1229                       if (defineTypes.length != parameterTypes.size())
 1230                       {
 1231                           continue;
 1232                       }
 1233   
 1234                       boolean ok = false;
 1235   
 1236                       for (Class<?> defineType : defineTypes)
 1237                       {
 1238                           if (defineType.equals(parameterTypes.get(j)))
 1239                           {
 1240                               ok = true;
 1241                           }
 1242                           else
 1243                           {
 1244                               ok = false;
 1245                           }
 1246                       }
 1247   
 1248                       if (ok)
 1249                       {
 1250                           return method;
 1251                       }
 1252                   }
 1253                   else
 1254                   {
 1255                       return method;
 1256                   }
 1257               }
 1258           }
 1259   
 1260           return null;
 1261       }
 1262   
 1263       public static boolean isMethodExistWithName(Class<?> clazz, String methodName)
 1264       {
 1265           Asserts.nullCheckForClass(clazz);
 1266           Asserts.assertNotNull(methodName, "methodName parameter can not be null");
 1267   
 1268           Method[] methods = clazz.getDeclaredMethods();
 1269   
 1270           for (Method method : methods)
 1271           {
 1272               if (method.getName().equals(methodName))
 1273               {
 1274                   return true;
 1275               }
 1276           }
 1277   
 1278           return false;
 1279       }
 1280   
 1281       public static boolean isPrimitive(Class<?> clazz)
 1282       {
 1283           Asserts.nullCheckForClass(clazz);
 1284   
 1285           return clazz.isPrimitive();
 1286       }
 1287   
 1288       public static boolean isPrimitiveWrapper(Class<?> clazz)
 1289       {
 1290           Asserts.nullCheckForClass(clazz);
 1291   
 1292           return PRIMITIVE_TO_WRAPPERS_MAP.containsValue(clazz);
 1293   
 1294       }
 1295   
 1296       public static boolean isArray(Class<?> clazz)
 1297       {
 1298           Asserts.nullCheckForClass(clazz);
 1299   
 1300           return clazz.isArray();
 1301       }
 1302   
 1303       public static boolean isEnum(Class<?> clazz)
 1304       {
 1305           return clazz.isEnum();
 1306       }
 1307   
 1308       public static boolean isInValueTypes(Class<?> clazz)
 1309       {
 1310           boolean result = VALUE_TYPES.contains(clazz);
 1311   
 1312           if (!result)
 1313           {
 1314               result = clazz.isPrimitive();
 1315           }
 1316   
 1317           if (!result)
 1318           {
 1319               if (Enum.class.isAssignableFrom(clazz))
 1320               {
 1321                   return true;
 1322               }
 1323           }
 1324   
 1325           return result;
 1326       }
 1327   
 1328       /**
 1329        * Gets the primitive/wrapper value of the parsed {@link String} parameter.
 1330        * 
 1331        * @param type primitive or wrapper of the primitive type
 1332        * @param value value of the type
 1333        * @return the parse of the given {@link String} value into the
 1334        *         corresponding value, if any exception occurs, returns null as the
 1335        *         value.
 1336        */
 1337       public static Object isValueOkForPrimitiveOrWrapper(Class<?> type, String value)
 1338       {
 1339           if (type.equals(Integer.TYPE) || type.equals(Integer.class))
 1340           {
 1341               return Integer.valueOf(value);
 1342           }
 1343   
 1344           if (type.equals(Float.TYPE) || type.equals(Float.class))
 1345           {
 1346               return Float.valueOf(value);
 1347           }
 1348   
 1349           if (type.equals(Double.TYPE) || type.equals(Double.class))
 1350           {
 1351               return Double.valueOf(value);
 1352           }
 1353   
 1354           if (type.equals(Character.TYPE) || type.equals(Character.class))
 1355           {
 1356               return value.toCharArray()[0];
 1357           }
 1358   
 1359           if (type.equals(Long.TYPE) || type.equals(Long.class))
 1360           {
 1361               return Long.valueOf(value);
 1362           }
 1363   
 1364           if (type.equals(Byte.TYPE) || type.equals(Byte.class))
 1365           {
 1366               return Byte.valueOf(value);
 1367           }
 1368   
 1369           if (type.equals(Short.TYPE) || type.equals(Short.class))
 1370           {
 1371               return Short.valueOf(value);
 1372           }
 1373   
 1374           if (type.equals(Boolean.TYPE) || type.equals(Boolean.class))
 1375           {
 1376               return Boolean.valueOf(value);
 1377           }
 1378   
 1379           return null;
 1380       }
 1381   
 1382       public static Enum isValueOkForEnum(Class clazz, String value)
 1383       {
 1384           Asserts.nullCheckForClass(clazz);
 1385           Asserts.assertNotNull(value, "value parameter can not be null");
 1386   
 1387           return Enum.valueOf(clazz, value);
 1388       }
 1389   
 1390       public static Date isValueOkForDate(String value) throws ParseException
 1391       {
 1392           try
 1393           {
 1394               Asserts.assertNotNull(value, "value parameter can not be null");
 1395               return DateFormat.getDateTimeInstance().parse(value);
 1396   
 1397           }
 1398           catch (ParseException e)
 1399           {
 1400               // Check for simple date format
 1401               SimpleDateFormat format = new SimpleDateFormat(WEBBEANS_DATE_FORMAT);
 1402   
 1403               return format.parse(value);
 1404           }
 1405       }
 1406   
 1407       public static Calendar isValueOkForCalendar(String value) throws ParseException
 1408       {
 1409           Calendar calendar = null;
 1410   
 1411           Asserts.assertNotNull(value, "value parameter can not be null");
 1412           Date date = isValueOkForDate(value);
 1413   
 1414           if (date == null)
 1415           {
 1416               return null;
 1417           }
 1418           else
 1419           {
 1420               calendar = Calendar.getInstance();
 1421               calendar.setTime(date);
 1422           }
 1423   
 1424           return calendar;
 1425       }
 1426   
 1427       public static Object isValueOkForBigDecimalOrInteger(Class<?> type, String value)
 1428       {
 1429           Asserts.assertNotNull(type);
 1430           Asserts.assertNotNull(value);
 1431   
 1432           if (type.equals(BigInteger.class))
 1433           {
 1434               return new BigInteger(value);
 1435           }
 1436           else if (type.equals(BigDecimal.class))
 1437           {
 1438               return new BigDecimal(value);
 1439           }
 1440           else
 1441           {
 1442               return new WebBeansException(new IllegalArgumentException("Argument is not valid"));
 1443           }
 1444       }
 1445   
 1446       public static boolean isDefinitionConstainsTypeVariables(Class<?> clazz)
 1447       {
 1448           Asserts.nullCheckForClass(clazz);
 1449           
 1450           return (clazz.getTypeParameters().length > 0) ? true : false;
 1451       }
 1452       
 1453       
 1454       public static TypeVariable<?>[] getTypeVariables(Class<?> clazz)
 1455       {
 1456           Asserts.assertNotNull("clazz argument can not be null");
 1457           
 1458           return clazz.getTypeParameters();
 1459       }
 1460   
 1461       public static Type[] getActualTypeArguements(Class<?> clazz)
 1462       {
 1463           Asserts.nullCheckForClass(clazz);
 1464   
 1465           if (clazz.getGenericSuperclass() instanceof ParameterizedType)
 1466           {
 1467               return ((ParameterizedType) clazz.getGenericSuperclass()).getActualTypeArguments();
 1468   
 1469           }
 1470           else
 1471           {
 1472               return new Type[0];
 1473           }
 1474       }
 1475   
 1476       public static Type[] getActualTypeArguements(Type type)
 1477       {
 1478           Asserts.assertNotNull(type, "type parameter can not be null");
 1479   
 1480           if (type instanceof ParameterizedType)
 1481           {
 1482               return ((ParameterizedType) type).getActualTypeArguments();
 1483   
 1484           }
 1485           else
 1486           {
 1487               return new Type[0];
 1488           }
 1489       }
 1490   
 1491       public static Class<?> getFirstRawType(Type type)
 1492       {
 1493           Asserts.assertNotNull(type, "type argument can not be null");
 1494   
 1495           if (type instanceof ParameterizedType)
 1496           {
 1497               ParameterizedType pt = (ParameterizedType) type;
 1498               return (Class<?>) pt.getRawType();
 1499           }
 1500   
 1501           return (Class<?>) type;
 1502       }
 1503   
 1504       public static Set<Type> setTypeHierarchy(Set<Type> set, Type clazz)
 1505       {
 1506           Class<?> raw = getClazz(clazz);
 1507           
 1508           if (raw == null) {
 1509               return null;
 1510           }
 1511           
 1512           set.add(clazz);
 1513   
 1514           Type sc = raw.getGenericSuperclass();
 1515   
 1516           if (sc != null)
 1517           {
 1518               setTypeHierarchy(set, sc);
 1519           }
 1520   
 1521           Type[] interfaces = raw.getGenericInterfaces();
 1522           for (Type cl : interfaces)
 1523           {            
 1524               setTypeHierarchy(set, cl);
 1525           }
 1526   
 1527           return set;
 1528       }
 1529       
 1530       
 1531       /**
 1532        * Return raw class type for given type.
 1533        * @param type base type instance
 1534        * @return class type for given type
 1535        */
 1536       public static Class<?> getClazz(Type type)
 1537       {
 1538           Class<?> raw = null;
 1539           
 1540           if(type instanceof ParameterizedType)
 1541           {
 1542               ParameterizedType pt = (ParameterizedType)type;
 1543               raw = (Class<?>)pt.getRawType();                
 1544           }
 1545           else if(type instanceof Class)
 1546           {
 1547               raw = (Class<?>)type;
 1548           }
 1549           else if(type instanceof GenericArrayType)
 1550           {
 1551               GenericArrayType arrayType = (GenericArrayType)type;
 1552               raw = getClazz(arrayType.getGenericComponentType());
 1553           }
 1554           
 1555           return raw;
 1556       }
 1557       
 1558       //For Ejb API Type
 1559       public static Set<Type> setClassTypeHierarchy(Set<Type> set, Class<?> clazz)
 1560       {
 1561           Asserts.nullCheckForClass(clazz);
 1562   
 1563           set.add(clazz);
 1564   
 1565           Class<?> sc = clazz.getSuperclass();
 1566   
 1567           if (sc != null)
 1568           {
 1569               setTypeHierarchy(set, sc);
 1570           }
 1571   
 1572           return set;
 1573       }
 1574       
 1575   
 1576       public static Set<Type> setInterfaceTypeHierarchy(Set<Type> set, Class<?> clazz)
 1577       {
 1578           Asserts.nullCheckForClass(clazz);
 1579   
 1580           Class<?>[] interfaces = clazz.getInterfaces();
 1581   
 1582           for (Class<?> cl : interfaces)
 1583           {
 1584               set.add(cl);
 1585   
 1586               setTypeHierarchy(set, cl);
 1587           }
 1588   
 1589           return set;
 1590       }
 1591   
 1592       public static Type[] getGenericSuperClassTypeArguments(Class<?> clazz)
 1593       {
 1594           Asserts.nullCheckForClass(clazz);
 1595           Type type = clazz.getGenericSuperclass();
 1596   
 1597           if (type != null)
 1598           {
 1599               if (type instanceof ParameterizedType)
 1600               {
 1601                   ParameterizedType pt = (ParameterizedType) type;
 1602   
 1603                   //if (checkParametrizedType(pt))
 1604                   //{
 1605                       return pt.getActualTypeArguments();
 1606                   //}
 1607               }
 1608           }
 1609   
 1610           return new Type[0];
 1611   
 1612       }
 1613   
 1614       /**
 1615        * Return true if it does not contain type variable for wildcard type
 1616        * false otherwise.
 1617        * 
 1618        * @param pType parameterized type
 1619        * @return true if it does not contain type variable for wildcard type
 1620        */
 1621       public static boolean checkParametrizedType(ParameterizedType pType)
 1622       {
 1623           Asserts.assertNotNull(pType, "pType argument can not be null");
 1624           
 1625           Type[] types = pType.getActualTypeArguments();
 1626   
 1627           for (Type type : types)
 1628           {
 1629               if (type instanceof ParameterizedType)
 1630               {
 1631                   return checkParametrizedType((ParameterizedType) type);
 1632               }
 1633               else if ((type instanceof TypeVariable) || (type instanceof WildcardType))
 1634               {
 1635                   return false;
 1636               }
 1637           }
 1638   
 1639           return true;
 1640       }
 1641   
 1642       public static boolean isFirstParametricTypeArgGeneric(ParameterizedType type)
 1643       {
 1644           Asserts.assertNotNull(type, "type parameter can not be null");
 1645           
 1646           Type[] args = type.getActualTypeArguments();
 1647           
 1648           if(args.length == 0)
 1649           {
 1650               return false;
 1651           }
 1652           
 1653           Type arg = args[0];
 1654   
 1655           if ((arg instanceof TypeVariable) || (arg instanceof WildcardType))
 1656           {
 1657               return true;
 1658           }
 1659   
 1660           return false;
 1661       }
 1662   
 1663       public static List<Type[]> getGenericSuperInterfacesTypeArguments(Class<?> clazz)
 1664       {
 1665           Asserts.nullCheckForClass(clazz);
 1666           List<Type[]> list = new ArrayList<Type[]>();
 1667   
 1668           Type[] types = clazz.getGenericInterfaces();
 1669           for (Type type : types)
 1670           {
 1671               if (type instanceof ParameterizedType)
 1672               {
 1673                   ParameterizedType pt = (ParameterizedType) type;
 1674   
 1675                   //if (checkParametrizedType(pt))
 1676                   //{
 1677                       list.add(pt.getActualTypeArguments());
 1678                   //}
 1679               }
 1680           }
 1681   
 1682           return list;
 1683       }
 1684   
 1685       public static Field getFieldWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotation)
 1686       {
 1687           Asserts.nullCheckForClass(clazz);
 1688           Asserts.assertNotNull(annotation, "annotation parameter can not be null");
 1689   
 1690           Field[] fields = clazz.getDeclaredFields();
 1691           for (Field field : fields)
 1692           {
 1693               if (AnnotationUtil.isAnnotationExist(field.getAnnotations(), annotation))
 1694               {
 1695                   return field;
 1696               }
 1697   
 1698           }
 1699   
 1700           return null;
 1701   
 1702       }
 1703   
 1704       public static boolean checkForTypeArguments(Class<?> src, Type[] typeArguments, Class<?> target)
 1705       {
 1706           Asserts.assertNotNull(src, "src parameter can not be null");
 1707           Asserts.assertNotNull(typeArguments, "typeArguments parameter can not be null");
 1708           Asserts.assertNotNull(target, "target parameter can not be null");
 1709   
 1710           Type[] types = getGenericSuperClassTypeArguments(target);
 1711   
 1712           boolean found = false;
 1713   
 1714           if (Arrays.equals(typeArguments, types))
 1715           {
 1716               return true;
 1717           }
 1718           else
 1719           {
 1720               Class<?> superClazz = target.getSuperclass();
 1721               if (superClazz != null)
 1722               {
 1723                   found = checkForTypeArguments(src, typeArguments, superClazz);
 1724               }
 1725           }
 1726   
 1727           if (!found)
 1728           {
 1729               List<Type[]> list = getGenericSuperInterfacesTypeArguments(target);
 1730               if (!list.isEmpty())
 1731               {
 1732                   Iterator<Type[]> it = list.iterator();
 1733                   while (it.hasNext())
 1734                   {
 1735                       types = it.next();
 1736                       if (Arrays.equals(typeArguments, types))
 1737                       {
 1738                           found = true;
 1739                           break;
 1740                       }
 1741                   }
 1742   
 1743               }
 1744           }
 1745   
 1746           if (!found)
 1747           {
 1748               Class<?>[] superInterfaces = target.getInterfaces();
 1749               for (Class<?> inter : superInterfaces)
 1750               {
 1751                   found = checkForTypeArguments(src, typeArguments, inter);
 1752                   if (found)
 1753                   {
 1754                       break;
 1755                   }
 1756               }
 1757           }
 1758   
 1759           return found;
 1760       }
 1761       
 1762       public static void setField(Object instance, Field field, Object value)
 1763       {
 1764           Asserts.assertNotNull(instance);
 1765           Asserts.assertNotNull(field);
 1766           
 1767           if(!field.isAccessible())
 1768           {
 1769               field.setAccessible(true);
 1770           }
 1771           
 1772           try
 1773           {
 1774               field.set(instance, value);
 1775           }
 1776           catch (IllegalArgumentException e)
 1777           {
 1778               throw new WebBeansException(e);
 1779           }
 1780           catch (IllegalAccessException e)
 1781           {
 1782               throw new WebBeansException(e);
 1783           }
 1784           
 1785       }
 1786    
 1787       public static Throwable getRootException(Throwable throwable)
 1788       {
 1789           if(throwable.getCause() == null)
 1790           {
 1791               return throwable;
 1792           }
 1793           else
 1794           {
 1795               return getRootException(throwable.getCause());
 1796           }
 1797       }
 1798       
 1799       /**
 1800        * Returns injection point raw type.
 1801        * 
 1802        * @param injectionPoint injection point definition
 1803        * @return injection point raw type
 1804        */
 1805       public static Class<?> getRawTypeForInjectionPoint(InjectionPoint injectionPoint)
 1806       {
 1807           Class<?> rawType = null;
 1808           Type type = injectionPoint.getType();
 1809           
 1810           if(type instanceof Class)
 1811           {
 1812               rawType = (Class<?>) type;
 1813           }
 1814           else if(type instanceof ParameterizedType)
 1815           {
 1816               ParameterizedType pt = (ParameterizedType)type;            
 1817               rawType = (Class<?>)pt.getRawType();                                                
 1818           }
 1819           
 1820           return rawType;
 1821       }
 1822       
 1823   }

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