Coverage Report - org.apache.commons.lang3.ClassUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassUtils
96%
300/312
94%
228/242
4,423
ClassUtils$1
100%
3/3
N/A
4,423
ClassUtils$1$1
83%
5/6
100%
2/2
4,423
ClassUtils$2
100%
4/4
N/A
4,423
ClassUtils$2$1
94%
17/18
90%
9/10
4,423
ClassUtils$Interfaces
100%
2/2
N/A
4,423
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.lang3;
 18  
 
 19  
 import java.lang.reflect.Method;
 20  
 import java.lang.reflect.Modifier;
 21  
 import java.util.ArrayList;
 22  
 import java.util.Collections;
 23  
 import java.util.HashMap;
 24  
 import java.util.HashSet;
 25  
 import java.util.Iterator;
 26  
 import java.util.LinkedHashSet;
 27  
 import java.util.List;
 28  
 import java.util.Map;
 29  
 import java.util.Set;
 30  
 
 31  
 import org.apache.commons.lang3.mutable.MutableObject;
 32  
 
 33  
 /**
 34  
  * <p>Operates on classes without using reflection.</p>
 35  
  *
 36  
  * <p>This class handles invalid {@code null} inputs as best it can.
 37  
  * Each method documents its behaviour in more detail.</p>
 38  
  *
 39  
  * <p>The notion of a {@code canonical name} includes the human
 40  
  * readable name for the type, for example {@code int[]}. The
 41  
  * non-canonical method variants work with the JVM names, such as
 42  
  * {@code [I}. </p>
 43  
  *
 44  
  * @since 2.0
 45  
  * @version $Id: ClassUtils.java 1669311 2015-03-26 10:24:19Z britter $
 46  
  */
 47  
 public class ClassUtils {
 48  
     /**
 49  
      * Inclusivity literals for {@link #hierarchy(Class, Interfaces)}.
 50  
      * @since 3.2
 51  
      */
 52  3
     public enum Interfaces {
 53  1
         INCLUDE, EXCLUDE
 54  
     }
 55  
 
 56  
     /**
 57  
      * The package separator character: <code>'&#x2e;' == {@value}</code>.
 58  
      */
 59  
     public static final char PACKAGE_SEPARATOR_CHAR = '.';
 60  
 
 61  
     /**
 62  
      * The package separator String: <code>"&#x2e;"</code>.
 63  
      */
 64  1
     public static final String PACKAGE_SEPARATOR = String.valueOf(PACKAGE_SEPARATOR_CHAR);
 65  
 
 66  
     /**
 67  
      * The inner class separator character: <code>'$' == {@value}</code>.
 68  
      */
 69  
     public static final char INNER_CLASS_SEPARATOR_CHAR = '$';
 70  
 
 71  
     /**
 72  
      * The inner class separator String: {@code "$"}.
 73  
      */
 74  1
     public static final String INNER_CLASS_SEPARATOR = String.valueOf(INNER_CLASS_SEPARATOR_CHAR);
 75  
 
 76  
     /**
 77  
      * Maps primitive {@code Class}es to their corresponding wrapper {@code Class}.
 78  
      */
 79  1
     private static final Map<Class<?>, Class<?>> primitiveWrapperMap = new HashMap<Class<?>, Class<?>>();
 80  
     static {
 81  1
          primitiveWrapperMap.put(Boolean.TYPE, Boolean.class);
 82  1
          primitiveWrapperMap.put(Byte.TYPE, Byte.class);
 83  1
          primitiveWrapperMap.put(Character.TYPE, Character.class);
 84  1
          primitiveWrapperMap.put(Short.TYPE, Short.class);
 85  1
          primitiveWrapperMap.put(Integer.TYPE, Integer.class);
 86  1
          primitiveWrapperMap.put(Long.TYPE, Long.class);
 87  1
          primitiveWrapperMap.put(Double.TYPE, Double.class);
 88  1
          primitiveWrapperMap.put(Float.TYPE, Float.class);
 89  1
          primitiveWrapperMap.put(Void.TYPE, Void.TYPE);
 90  
     }
 91  
 
 92  
     /**
 93  
      * Maps wrapper {@code Class}es to their corresponding primitive types.
 94  
      */
 95  1
     private static final Map<Class<?>, Class<?>> wrapperPrimitiveMap = new HashMap<Class<?>, Class<?>>();
 96  
     static {
 97  1
         for (final Class<?> primitiveClass : primitiveWrapperMap.keySet()) {
 98  9
             final Class<?> wrapperClass = primitiveWrapperMap.get(primitiveClass);
 99  9
             if (!primitiveClass.equals(wrapperClass)) {
 100  8
                 wrapperPrimitiveMap.put(wrapperClass, primitiveClass);
 101  
             }
 102  9
         }
 103  
     }
 104  
 
 105  
     /**
 106  
      * Maps a primitive class name to its corresponding abbreviation used in array class names.
 107  
      */
 108  
     private static final Map<String, String> abbreviationMap;
 109  
 
 110  
     /**
 111  
      * Maps an abbreviation used in array class names to corresponding primitive class name.
 112  
      */
 113  
     private static final Map<String, String> reverseAbbreviationMap;
 114  
 
 115  
     /**
 116  
      * Feed abbreviation maps
 117  
      */
 118  
     static {
 119  1
         final Map<String, String> m = new HashMap<String, String>();
 120  1
         m.put("int", "I");
 121  1
         m.put("boolean", "Z");
 122  1
         m.put("float", "F");
 123  1
         m.put("long", "J");
 124  1
         m.put("short", "S");
 125  1
         m.put("byte", "B");
 126  1
         m.put("double", "D");
 127  1
         m.put("char", "C");
 128  1
         m.put("void", "V");
 129  1
         final Map<String, String> r = new HashMap<String, String>();
 130  1
         for (final Map.Entry<String, String> e : m.entrySet()) {
 131  9
             r.put(e.getValue(), e.getKey());
 132  9
         }
 133  1
         abbreviationMap = Collections.unmodifiableMap(m);
 134  1
         reverseAbbreviationMap = Collections.unmodifiableMap(r);
 135  1
     }
 136  
 
 137  
     /**
 138  
      * <p>ClassUtils instances should NOT be constructed in standard programming.
 139  
      * Instead, the class should be used as
 140  
      * {@code ClassUtils.getShortClassName(cls)}.</p>
 141  
      *
 142  
      * <p>This constructor is public to permit tools that require a JavaBean
 143  
      * instance to operate.</p>
 144  
      */
 145  
     public ClassUtils() {
 146  6
       super();
 147  6
     }
 148  
 
 149  
     // Short class name
 150  
     // ----------------------------------------------------------------------
 151  
     /**
 152  
      * <p>Gets the class name minus the package name for an {@code Object}.</p>
 153  
      *
 154  
      * @param object  the class to get the short name for, may be null
 155  
      * @param valueIfNull  the value to return if null
 156  
      * @return the class name of the object without the package name, or the null value
 157  
      */
 158  
     public static String getShortClassName(final Object object, final String valueIfNull) {
 159  11
         if (object == null) {
 160  1
             return valueIfNull;
 161  
         }
 162  10
         return getShortClassName(object.getClass());
 163  
     }
 164  
 
 165  
     /**
 166  
      * <p>Gets the class name minus the package name from a {@code Class}.</p>
 167  
      *
 168  
      * <p>Consider using the Java 5 API {@link Class#getSimpleName()} instead.
 169  
      * The one known difference is that this code will return {@code "Map.Entry"} while
 170  
      * the {@code java.lang.Class} variant will simply return {@code "Entry"}. </p>
 171  
      *
 172  
      * @param cls  the class to get the short name for.
 173  
      * @return the class name without the package name or an empty string
 174  
      */
 175  
     public static String getShortClassName(final Class<?> cls) {
 176  117
         if (cls == null) {
 177  1
             return StringUtils.EMPTY;
 178  
         }
 179  116
         return getShortClassName(cls.getName());
 180  
     }
 181  
 
 182  
     /**
 183  
      * <p>Gets the class name minus the package name from a String.</p>
 184  
      *
 185  
      * <p>The string passed in is assumed to be a class name - it is not checked.</p>
 186  
 
 187  
      * <p>Note that this method differs from Class.getSimpleName() in that this will
 188  
      * return {@code "Map.Entry"} whilst the {@code java.lang.Class} variant will simply
 189  
      * return {@code "Entry"}. </p>
 190  
      *
 191  
      * @param className  the className to get the short name for
 192  
      * @return the class name of the class without the package name or an empty string
 193  
      */
 194  
     public static String getShortClassName(String className) {
 195  148
         if (StringUtils.isEmpty(className)) {
 196  2
             return StringUtils.EMPTY;
 197  
         }
 198  
 
 199  146
         final StringBuilder arrayPrefix = new StringBuilder();
 200  
 
 201  
         // Handle array encoding
 202  146
         if (className.startsWith("[")) {
 203  32
             while (className.charAt(0) == '[') {
 204  19
                 className = className.substring(1);
 205  19
                 arrayPrefix.append("[]");
 206  
             }
 207  
             // Strip Object type encoding
 208  13
             if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';') {
 209  5
                 className = className.substring(1, className.length() - 1);
 210  
             }
 211  
 
 212  13
             if (reverseAbbreviationMap.containsKey(className)) {
 213  8
                 className = reverseAbbreviationMap.get(className);
 214  
             }
 215  
         }
 216  
 
 217  146
         final int lastDotIdx = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR);
 218  146
         final int innerIdx = className.indexOf(
 219  
                 INNER_CLASS_SEPARATOR_CHAR, lastDotIdx == -1 ? 0 : lastDotIdx + 1);
 220  146
         String out = className.substring(lastDotIdx + 1);
 221  146
         if (innerIdx != -1) {
 222  24
             out = out.replace(INNER_CLASS_SEPARATOR_CHAR, PACKAGE_SEPARATOR_CHAR);
 223  
         }
 224  146
         return out + arrayPrefix;
 225  
     }
 226  
 
 227  
     /**
 228  
      * <p>Null-safe version of <code>aClass.getSimpleName()</code></p>
 229  
      *
 230  
      * @param cls the class for which to get the simple name.
 231  
      * @return the simple class name.
 232  
      * @since 3.0
 233  
      * @see Class#getSimpleName()
 234  
      */
 235  
     public static String getSimpleName(final Class<?> cls) {
 236  29
         if (cls == null) {
 237  1
             return StringUtils.EMPTY;
 238  
         }
 239  28
         return cls.getSimpleName();
 240  
     }
 241  
 
 242  
     /**
 243  
      * <p>Null-safe version of <code>aClass.getSimpleName()</code></p>
 244  
      *
 245  
      * @param object the object for which to get the simple class name.
 246  
      * @param valueIfNull the value to return if <code>object</code> is <code>null</code>
 247  
      * @return the simple class name.
 248  
      * @since 3.0
 249  
      * @see Class#getSimpleName()
 250  
      */
 251  
     public static String getSimpleName(final Object object, final String valueIfNull) {
 252  4
         if (object == null) {
 253  1
             return valueIfNull;
 254  
         }
 255  3
         return getSimpleName(object.getClass());
 256  
     }
 257  
 
 258  
     // Package name
 259  
     // ----------------------------------------------------------------------
 260  
     /**
 261  
      * <p>Gets the package name of an {@code Object}.</p>
 262  
      *
 263  
      * @param object  the class to get the package name for, may be null
 264  
      * @param valueIfNull  the value to return if null
 265  
      * @return the package name of the object, or the null value
 266  
      */
 267  
     public static String getPackageName(final Object object, final String valueIfNull) {
 268  3
         if (object == null) {
 269  1
             return valueIfNull;
 270  
         }
 271  2
         return getPackageName(object.getClass());
 272  
     }
 273  
 
 274  
     /**
 275  
      * <p>Gets the package name of a {@code Class}.</p>
 276  
      *
 277  
      * @param cls  the class to get the package name for, may be {@code null}.
 278  
      * @return the package name or an empty string
 279  
      */
 280  
     public static String getPackageName(final Class<?> cls) {
 281  19
         if (cls == null) {
 282  1
             return StringUtils.EMPTY;
 283  
         }
 284  18
         return getPackageName(cls.getName());
 285  
     }
 286  
 
 287  
     /**
 288  
      * <p>Gets the package name from a {@code String}.</p>
 289  
      *
 290  
      * <p>The string passed in is assumed to be a class name - it is not checked.</p>
 291  
      * <p>If the class is unpackaged, return an empty string.</p>
 292  
      *
 293  
      * @param className  the className to get the package name for, may be {@code null}
 294  
      * @return the package name or an empty string
 295  
      */
 296  
     public static String getPackageName(String className) {
 297  50
         if (StringUtils.isEmpty(className)) {
 298  2
             return StringUtils.EMPTY;
 299  
         }
 300  
 
 301  
         // Strip array encoding
 302  66
         while (className.charAt(0) == '[') {
 303  18
             className = className.substring(1);
 304  
         }
 305  
         // Strip Object type encoding
 306  48
         if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';') {
 307  4
             className = className.substring(1);
 308  
         }
 309  
 
 310  48
         final int i = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR);
 311  48
         if (i == -1) {
 312  16
             return StringUtils.EMPTY;
 313  
         }
 314  32
         return className.substring(0, i);
 315  
     }
 316  
 
 317  
     // Abbreviated name
 318  
     // ----------------------------------------------------------------------
 319  
     /**
 320  
      * <p>Gets the abbreviated name of a {@code Class}.</p>
 321  
      *
 322  
      * @param cls  the class to get the abbreviated name for, may be {@code null}
 323  
      * @param len  the desired length of the abbreviated name
 324  
      * @return the abbreviated name or an empty string
 325  
      * @throws IllegalArgumentException if len &lt;= 0
 326  
      * @see #getAbbreviatedName(String, int)
 327  
      * @since 3.4
 328  
      */
 329  
     public static String getAbbreviatedName(final Class<?> cls, int len) {
 330  8
       if (cls == null) {
 331  1
         return StringUtils.EMPTY;
 332  
       }
 333  7
       return getAbbreviatedName(cls.getName(), len);
 334  
     }
 335  
 
 336  
     /**
 337  
      * <p>Gets the abbreviated class name from a {@code String}.</p>
 338  
      *
 339  
      * <p>The string passed in is assumed to be a class name - it is not checked.</p>
 340  
      *
 341  
      * <p>The abbreviation algorithm will shorten the class name, usually without
 342  
      * significant loss of meaning.</p>
 343  
      * <p>The abbreviated class name will always include the complete package hierarchy.
 344  
      * If enough space is available, rightmost sub-packages will be displayed in full
 345  
      * length.</p>
 346  
      *
 347  
      * <p>The following table illustrates the algorithm:</p>
 348  
      * <table summary="abbreviation examples">
 349  
      * <tr><td>className</td><td>len</td><td>return</td></tr>
 350  
      * <tr><td>              null</td><td> 1</td><td>""</td></tr>
 351  
      * <tr><td>"java.lang.String"</td><td> 5</td><td>"j.l.String"</td></tr>
 352  
      * <tr><td>"java.lang.String"</td><td>15</td><td>"j.lang.String"</td></tr>
 353  
      * <tr><td>"java.lang.String"</td><td>30</td><td>"java.lang.String"</td></tr>
 354  
      * </table>
 355  
      * @param className  the className to get the abbreviated name for, may be {@code null}
 356  
      * @param len  the desired length of the abbreviated name
 357  
      * @return the abbreviated name or an empty string
 358  
      * @throws IllegalArgumentException if len &lt;= 0
 359  
      * @since 3.4
 360  
      */
 361  
     public static String getAbbreviatedName(String className, int len) {
 362  10
       if (len <= 0) {
 363  2
         throw new IllegalArgumentException("len must be > 0");
 364  
       }
 365  8
       if (className == null) {
 366  1
         return StringUtils.EMPTY;
 367  
       }
 368  
 
 369  7
       int availableSpace = len;
 370  7
       int packageLevels = StringUtils.countMatches(className, '.');
 371  7
       String[] output = new String[packageLevels + 1];
 372  7
       int endIndex = className.length() - 1;
 373  26
       for (int level = packageLevels; level >= 0; level--) {
 374  19
         int startIndex = className.lastIndexOf('.', endIndex);
 375  19
         String part = className.substring(startIndex + 1, endIndex + 1);
 376  19
         availableSpace -= part.length();
 377  19
         if (level > 0) {
 378  
           // all elements except top level require an additional char space
 379  12
           availableSpace--;
 380  
         }
 381  19
         if (level == packageLevels) {
 382  
           // ClassName is always complete
 383  7
           output[level] = part;
 384  
         } else {
 385  12
           if (availableSpace > 0) {
 386  4
             output[level] = part;
 387  
           } else {
 388  
             // if no space is left still the first char is used
 389  8
             output[level] = part.substring(0, 1);
 390  
           }
 391  
         }
 392  19
         endIndex = startIndex - 1;
 393  
       }
 394  
 
 395  7
       return StringUtils.join(output, '.');
 396  
     }
 397  
 
 398  
     // Superclasses/Superinterfaces
 399  
     // ----------------------------------------------------------------------
 400  
     /**
 401  
      * <p>Gets a {@code List} of superclasses for the given class.</p>
 402  
      *
 403  
      * @param cls  the class to look up, may be {@code null}
 404  
      * @return the {@code List} of superclasses in order going up from this one
 405  
      *  {@code null} if null input
 406  
      */
 407  
     public static List<Class<?>> getAllSuperclasses(final Class<?> cls) {
 408  3
         if (cls == null) {
 409  1
             return null;
 410  
         }
 411  2
         final List<Class<?>> classes = new ArrayList<Class<?>>();
 412  2
         Class<?> superclass = cls.getSuperclass();
 413  6
         while (superclass != null) {
 414  4
             classes.add(superclass);
 415  4
             superclass = superclass.getSuperclass();
 416  
         }
 417  2
         return classes;
 418  
     }
 419  
 
 420  
     /**
 421  
      * <p>Gets a {@code List} of all interfaces implemented by the given
 422  
      * class and its superclasses.</p>
 423  
      *
 424  
      * <p>The order is determined by looking through each interface in turn as
 425  
      * declared in the source file and following its hierarchy up. Then each
 426  
      * superclass is considered in the same way. Later duplicates are ignored,
 427  
      * so the order is maintained.</p>
 428  
      *
 429  
      * @param cls  the class to look up, may be {@code null}
 430  
      * @return the {@code List} of interfaces in order,
 431  
      *  {@code null} if null input
 432  
      */
 433  
     public static List<Class<?>> getAllInterfaces(final Class<?> cls) {
 434  42
         if (cls == null) {
 435  1
             return null;
 436  
         }
 437  
 
 438  41
         final LinkedHashSet<Class<?>> interfacesFound = new LinkedHashSet<Class<?>>();
 439  41
         getAllInterfaces(cls, interfacesFound);
 440  
 
 441  41
         return new ArrayList<Class<?>>(interfacesFound);
 442  
     }
 443  
 
 444  
     /**
 445  
      * Get the interfaces for the specified class.
 446  
      *
 447  
      * @param cls  the class to look up, may be {@code null}
 448  
      * @param interfacesFound the {@code Set} of interfaces for the class
 449  
      */
 450  
     private static void getAllInterfaces(Class<?> cls, final HashSet<Class<?>> interfacesFound) {
 451  247
         while (cls != null) {
 452  162
             final Class<?>[] interfaces = cls.getInterfaces();
 453  
 
 454  210
             for (final Class<?> i : interfaces) {
 455  48
                 if (interfacesFound.add(i)) {
 456  44
                     getAllInterfaces(i, interfacesFound);
 457  
                 }
 458  
             }
 459  
 
 460  162
             cls = cls.getSuperclass();
 461  162
          }
 462  85
      }
 463  
 
 464  
     // Convert list
 465  
     // ----------------------------------------------------------------------
 466  
     /**
 467  
      * <p>Given a {@code List} of class names, this method converts them into classes.</p>
 468  
      *
 469  
      * <p>A new {@code List} is returned. If the class name cannot be found, {@code null}
 470  
      * is stored in the {@code List}. If the class name in the {@code List} is
 471  
      * {@code null}, {@code null} is stored in the output {@code List}.</p>
 472  
      *
 473  
      * @param classNames  the classNames to change
 474  
      * @return a {@code List} of Class objects corresponding to the class names,
 475  
      *  {@code null} if null input
 476  
      * @throws ClassCastException if classNames contains a non String entry
 477  
      */
 478  
     public static List<Class<?>> convertClassNamesToClasses(final List<String> classNames) {
 479  4
         if (classNames == null) {
 480  1
             return null;
 481  
         }
 482  3
         final List<Class<?>> classes = new ArrayList<Class<?>>(classNames.size());
 483  3
         for (final String className : classNames) {
 484  
             try {
 485  6
                 classes.add(Class.forName(className));
 486  2
             } catch (final Exception ex) {
 487  2
                 classes.add(null);
 488  4
             }
 489  6
         }
 490  2
         return classes;
 491  
     }
 492  
 
 493  
     /**
 494  
      * <p>Given a {@code List} of {@code Class} objects, this method converts
 495  
      * them into class names.</p>
 496  
      *
 497  
      * <p>A new {@code List} is returned. {@code null} objects will be copied into
 498  
      * the returned list as {@code null}.</p>
 499  
      *
 500  
      * @param classes  the classes to change
 501  
      * @return a {@code List} of class names corresponding to the Class objects,
 502  
      *  {@code null} if null input
 503  
      * @throws ClassCastException if {@code classes} contains a non-{@code Class} entry
 504  
      */
 505  
     public static List<String> convertClassesToClassNames(final List<Class<?>> classes) {
 506  4
         if (classes == null) {
 507  1
             return null;
 508  
         }
 509  3
         final List<String> classNames = new ArrayList<String>(classes.size());
 510  3
         for (final Class<?> cls : classes) {
 511  6
             if (cls == null) {
 512  2
                 classNames.add(null);
 513  
             } else {
 514  4
                 classNames.add(cls.getName());
 515  
             }
 516  6
         }
 517  2
         return classNames;
 518  
     }
 519  
 
 520  
     // Is assignable
 521  
     // ----------------------------------------------------------------------
 522  
     /**
 523  
      * <p>Checks if an array of Classes can be assigned to another array of Classes.</p>
 524  
      *
 525  
      * <p>This method calls {@link #isAssignable(Class, Class) isAssignable} for each
 526  
      * Class pair in the input arrays. It can be used to check if a set of arguments
 527  
      * (the first parameter) are suitably compatible with a set of method parameter types
 528  
      * (the second parameter).</p>
 529  
      *
 530  
      * <p>Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, this
 531  
      * method takes into account widenings of primitive classes and
 532  
      * {@code null}s.</p>
 533  
      *
 534  
      * <p>Primitive widenings allow an int to be assigned to a {@code long},
 535  
      * {@code float} or {@code double}. This method returns the correct
 536  
      * result for these cases.</p>
 537  
      *
 538  
      * <p>{@code Null} may be assigned to any reference type. This method will
 539  
      * return {@code true} if {@code null} is passed in and the toClass is
 540  
      * non-primitive.</p>
 541  
      *
 542  
      * <p>Specifically, this method tests whether the type represented by the
 543  
      * specified {@code Class} parameter can be converted to the type
 544  
      * represented by this {@code Class} object via an identity conversion
 545  
      * widening primitive or widening reference conversion. See
 546  
      * <em><a href="http://docs.oracle.com/javase/specs/">The Java Language Specification</a></em>,
 547  
      * sections 5.1.1, 5.1.2 and 5.1.4 for details.</p>
 548  
      *
 549  
      * <p><strong>Since Lang 3.0,</strong> this method will default behavior for
 550  
      * calculating assignability between primitive and wrapper types <em>corresponding
 551  
      * to the running Java version</em>; i.e. autoboxing will be the default
 552  
      * behavior in VMs running Java versions &gt; 1.5.</p>
 553  
      *
 554  
      * @param classArray  the array of Classes to check, may be {@code null}
 555  
      * @param toClassArray  the array of Classes to try to assign into, may be {@code null}
 556  
      * @return {@code true} if assignment possible
 557  
      */
 558  
     public static boolean isAssignable(final Class<?>[] classArray, final Class<?>... toClassArray) {
 559  15
         return isAssignable(classArray, toClassArray, SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_5));
 560  
     }
 561  
 
 562  
     /**
 563  
      * <p>Checks if an array of Classes can be assigned to another array of Classes.</p>
 564  
      *
 565  
      * <p>This method calls {@link #isAssignable(Class, Class) isAssignable} for each
 566  
      * Class pair in the input arrays. It can be used to check if a set of arguments
 567  
      * (the first parameter) are suitably compatible with a set of method parameter types
 568  
      * (the second parameter).</p>
 569  
      *
 570  
      * <p>Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, this
 571  
      * method takes into account widenings of primitive classes and
 572  
      * {@code null}s.</p>
 573  
      *
 574  
      * <p>Primitive widenings allow an int to be assigned to a {@code long},
 575  
      * {@code float} or {@code double}. This method returns the correct
 576  
      * result for these cases.</p>
 577  
      *
 578  
      * <p>{@code Null} may be assigned to any reference type. This method will
 579  
      * return {@code true} if {@code null} is passed in and the toClass is
 580  
      * non-primitive.</p>
 581  
      *
 582  
      * <p>Specifically, this method tests whether the type represented by the
 583  
      * specified {@code Class} parameter can be converted to the type
 584  
      * represented by this {@code Class} object via an identity conversion
 585  
      * widening primitive or widening reference conversion. See
 586  
      * <em><a href="http://docs.oracle.com/javase/specs/">The Java Language Specification</a></em>,
 587  
      * sections 5.1.1, 5.1.2 and 5.1.4 for details.</p>
 588  
      *
 589  
      * @param classArray  the array of Classes to check, may be {@code null}
 590  
      * @param toClassArray  the array of Classes to try to assign into, may be {@code null}
 591  
      * @param autoboxing  whether to use implicit autoboxing/unboxing between primitives and wrappers
 592  
      * @return {@code true} if assignment possible
 593  
      */
 594  
     public static boolean isAssignable(Class<?>[] classArray, Class<?>[] toClassArray, final boolean autoboxing) {
 595  281
         if (ArrayUtils.isSameLength(classArray, toClassArray) == false) {
 596  49
             return false;
 597  
         }
 598  232
         if (classArray == null) {
 599  6
             classArray = ArrayUtils.EMPTY_CLASS_ARRAY;
 600  
         }
 601  232
         if (toClassArray == null) {
 602  6
             toClassArray = ArrayUtils.EMPTY_CLASS_ARRAY;
 603  
         }
 604  356
         for (int i = 0; i < classArray.length; i++) {
 605  229
             if (isAssignable(classArray[i], toClassArray[i], autoboxing) == false) {
 606  105
                 return false;
 607  
             }
 608  
         }
 609  127
         return true;
 610  
     }
 611  
 
 612  
     /**
 613  
      * Returns whether the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},
 614  
      * {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
 615  
      *
 616  
      * @param type
 617  
      *            The class to query or null.
 618  
      * @return true if the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},
 619  
      *         {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
 620  
      * @since 3.1
 621  
      */
 622  
     public static boolean isPrimitiveOrWrapper(final Class<?> type) {
 623  21
         if (type == null) {
 624  1
             return false;
 625  
         }
 626  20
         return type.isPrimitive() || isPrimitiveWrapper(type);
 627  
     }
 628  
 
 629  
     /**
 630  
      * Returns whether the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character}, {@link Short},
 631  
      * {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
 632  
      *
 633  
      * @param type
 634  
      *            The class to query or null.
 635  
      * @return true if the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character}, {@link Short},
 636  
      *         {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
 637  
      * @since 3.1
 638  
      */
 639  
     public static boolean isPrimitiveWrapper(final Class<?> type) {
 640  60
         return wrapperPrimitiveMap.containsKey(type);
 641  
     }
 642  
 
 643  
     /**
 644  
      * <p>Checks if one {@code Class} can be assigned to a variable of
 645  
      * another {@code Class}.</p>
 646  
      *
 647  
      * <p>Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method,
 648  
      * this method takes into account widenings of primitive classes and
 649  
      * {@code null}s.</p>
 650  
      *
 651  
      * <p>Primitive widenings allow an int to be assigned to a long, float or
 652  
      * double. This method returns the correct result for these cases.</p>
 653  
      *
 654  
      * <p>{@code Null} may be assigned to any reference type. This method
 655  
      * will return {@code true} if {@code null} is passed in and the
 656  
      * toClass is non-primitive.</p>
 657  
      *
 658  
      * <p>Specifically, this method tests whether the type represented by the
 659  
      * specified {@code Class} parameter can be converted to the type
 660  
      * represented by this {@code Class} object via an identity conversion
 661  
      * widening primitive or widening reference conversion. See
 662  
      * <em><a href="http://docs.oracle.com/javase/specs/">The Java Language Specification</a></em>,
 663  
      * sections 5.1.1, 5.1.2 and 5.1.4 for details.</p>
 664  
      *
 665  
      * <p><strong>Since Lang 3.0,</strong> this method will default behavior for
 666  
      * calculating assignability between primitive and wrapper types <em>corresponding
 667  
      * to the running Java version</em>; i.e. autoboxing will be the default
 668  
      * behavior in VMs running Java versions &gt; 1.5.</p>
 669  
      *
 670  
      * @param cls  the Class to check, may be null
 671  
      * @param toClass  the Class to try to assign into, returns false if null
 672  
      * @return {@code true} if assignment possible
 673  
      */
 674  
     public static boolean isAssignable(final Class<?> cls, final Class<?> toClass) {
 675  304
         return isAssignable(cls, toClass, SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_5));
 676  
     }
 677  
 
 678  
     /**
 679  
      * <p>Checks if one {@code Class} can be assigned to a variable of
 680  
      * another {@code Class}.</p>
 681  
      *
 682  
      * <p>Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method,
 683  
      * this method takes into account widenings of primitive classes and
 684  
      * {@code null}s.</p>
 685  
      *
 686  
      * <p>Primitive widenings allow an int to be assigned to a long, float or
 687  
      * double. This method returns the correct result for these cases.</p>
 688  
      *
 689  
      * <p>{@code Null} may be assigned to any reference type. This method
 690  
      * will return {@code true} if {@code null} is passed in and the
 691  
      * toClass is non-primitive.</p>
 692  
      *
 693  
      * <p>Specifically, this method tests whether the type represented by the
 694  
      * specified {@code Class} parameter can be converted to the type
 695  
      * represented by this {@code Class} object via an identity conversion
 696  
      * widening primitive or widening reference conversion. See
 697  
      * <em><a href="http://docs.oracle.com/javase/specs/">The Java Language Specification</a></em>,
 698  
      * sections 5.1.1, 5.1.2 and 5.1.4 for details.</p>
 699  
      *
 700  
      * @param cls  the Class to check, may be null
 701  
      * @param toClass  the Class to try to assign into, returns false if null
 702  
      * @param autoboxing  whether to use implicit autoboxing/unboxing between primitives and wrappers
 703  
      * @return {@code true} if assignment possible
 704  
      */
 705  
     public static boolean isAssignable(Class<?> cls, final Class<?> toClass, final boolean autoboxing) {
 706  635
         if (toClass == null) {
 707  6
             return false;
 708  
         }
 709  
         // have to check for null, as isAssignableFrom doesn't
 710  629
         if (cls == null) {
 711  11
             return !toClass.isPrimitive();
 712  
         }
 713  
         //autoboxing:
 714  618
         if (autoboxing) {
 715  596
             if (cls.isPrimitive() && !toClass.isPrimitive()) {
 716  54
                 cls = primitiveToWrapper(cls);
 717  54
                 if (cls == null) {
 718  0
                     return false;
 719  
                 }
 720  
             }
 721  596
             if (toClass.isPrimitive() && !cls.isPrimitive()) {
 722  190
                 cls = wrapperToPrimitive(cls);
 723  190
                 if (cls == null) {
 724  0
                     return false;
 725  
                 }
 726  
             }
 727  
         }
 728  618
         if (cls.equals(toClass)) {
 729  65
             return true;
 730  
         }
 731  553
         if (cls.isPrimitive()) {
 732  249
             if (toClass.isPrimitive() == false) {
 733  6
                 return false;
 734  
             }
 735  243
             if (Integer.TYPE.equals(cls)) {
 736  24
                 return Long.TYPE.equals(toClass)
 737  
                     || Float.TYPE.equals(toClass)
 738  
                     || Double.TYPE.equals(toClass);
 739  
             }
 740  219
             if (Long.TYPE.equals(cls)) {
 741  36
                 return Float.TYPE.equals(toClass)
 742  
                     || Double.TYPE.equals(toClass);
 743  
             }
 744  183
             if (Boolean.TYPE.equals(cls)) {
 745  31
                 return false;
 746  
             }
 747  152
             if (Double.TYPE.equals(cls)) {
 748  26
                 return false;
 749  
             }
 750  126
             if (Float.TYPE.equals(cls)) {
 751  30
                 return Double.TYPE.equals(toClass);
 752  
             }
 753  96
             if (Character.TYPE.equals(cls)) {
 754  30
                 return Integer.TYPE.equals(toClass)
 755  
                     || Long.TYPE.equals(toClass)
 756  
                     || Float.TYPE.equals(toClass)
 757  
                     || Double.TYPE.equals(toClass);
 758  
             }
 759  66
             if (Short.TYPE.equals(cls)) {
 760  30
                 return Integer.TYPE.equals(toClass)
 761  
                     || Long.TYPE.equals(toClass)
 762  
                     || Float.TYPE.equals(toClass)
 763  
                     || Double.TYPE.equals(toClass);
 764  
             }
 765  36
             if (Byte.TYPE.equals(cls)) {
 766  36
                 return Short.TYPE.equals(toClass)
 767  
                     || Integer.TYPE.equals(toClass)
 768  
                     || Long.TYPE.equals(toClass)
 769  
                     || Float.TYPE.equals(toClass)
 770  
                     || Double.TYPE.equals(toClass);
 771  
             }
 772  
             // should never get here
 773  0
             return false;
 774  
         }
 775  304
         return toClass.isAssignableFrom(cls);
 776  
     }
 777  
 
 778  
     /**
 779  
      * <p>Converts the specified primitive Class object to its corresponding
 780  
      * wrapper Class object.</p>
 781  
      *
 782  
      * <p>NOTE: From v2.2, this method handles {@code Void.TYPE},
 783  
      * returning {@code Void.TYPE}.</p>
 784  
      *
 785  
      * @param cls  the class to convert, may be null
 786  
      * @return the wrapper class for {@code cls} or {@code cls} if
 787  
      * {@code cls} is not a primitive. {@code null} if null input.
 788  
      * @since 2.1
 789  
      */
 790  
     public static Class<?> primitiveToWrapper(final Class<?> cls) {
 791  101
         Class<?> convertedClass = cls;
 792  101
         if (cls != null && cls.isPrimitive()) {
 793  91
             convertedClass = primitiveWrapperMap.get(cls);
 794  
         }
 795  101
         return convertedClass;
 796  
     }
 797  
 
 798  
     /**
 799  
      * <p>Converts the specified array of primitive Class objects to an array of
 800  
      * its corresponding wrapper Class objects.</p>
 801  
      *
 802  
      * @param classes  the class array to convert, may be null or empty
 803  
      * @return an array which contains for each given class, the wrapper class or
 804  
      * the original class if class is not a primitive. {@code null} if null input.
 805  
      * Empty array if an empty array passed in.
 806  
      * @since 2.1
 807  
      */
 808  
     public static Class<?>[] primitivesToWrappers(final Class<?>... classes) {
 809  6
         if (classes == null) {
 810  1
             return null;
 811  
         }
 812  
 
 813  5
         if (classes.length == 0) {
 814  2
             return classes;
 815  
         }
 816  
 
 817  3
         final Class<?>[] convertedClasses = new Class[classes.length];
 818  17
         for (int i = 0; i < classes.length; i++) {
 819  14
             convertedClasses[i] = primitiveToWrapper(classes[i]);
 820  
         }
 821  3
         return convertedClasses;
 822  
     }
 823  
 
 824  
     /**
 825  
      * <p>Converts the specified wrapper class to its corresponding primitive
 826  
      * class.</p>
 827  
      *
 828  
      * <p>This method is the counter part of {@code primitiveToWrapper()}.
 829  
      * If the passed in class is a wrapper class for a primitive type, this
 830  
      * primitive type will be returned (e.g. {@code Integer.TYPE} for
 831  
      * {@code Integer.class}). For other classes, or if the parameter is
 832  
      * <b>null</b>, the return value is <b>null</b>.</p>
 833  
      *
 834  
      * @param cls the class to convert, may be <b>null</b>
 835  
      * @return the corresponding primitive type if {@code cls} is a
 836  
      * wrapper class, <b>null</b> otherwise
 837  
      * @see #primitiveToWrapper(Class)
 838  
      * @since 2.4
 839  
      */
 840  
     public static Class<?> wrapperToPrimitive(final Class<?> cls) {
 841  262
         return wrapperPrimitiveMap.get(cls);
 842  
     }
 843  
 
 844  
     /**
 845  
      * <p>Converts the specified array of wrapper Class objects to an array of
 846  
      * its corresponding primitive Class objects.</p>
 847  
      *
 848  
      * <p>This method invokes {@code wrapperToPrimitive()} for each element
 849  
      * of the passed in array.</p>
 850  
      *
 851  
      * @param classes  the class array to convert, may be null or empty
 852  
      * @return an array which contains for each given class, the primitive class or
 853  
      * <b>null</b> if the original class is not a wrapper class. {@code null} if null input.
 854  
      * Empty array if an empty array passed in.
 855  
      * @see #wrapperToPrimitive(Class)
 856  
      * @since 2.4
 857  
      */
 858  
     public static Class<?>[] wrappersToPrimitives(final Class<?>... classes) {
 859  5
         if (classes == null) {
 860  1
             return null;
 861  
         }
 862  
 
 863  4
         if (classes.length == 0) {
 864  2
             return classes;
 865  
         }
 866  
 
 867  2
         final Class<?>[] convertedClasses = new Class[classes.length];
 868  14
         for (int i = 0; i < classes.length; i++) {
 869  12
             convertedClasses[i] = wrapperToPrimitive(classes[i]);
 870  
         }
 871  2
         return convertedClasses;
 872  
     }
 873  
 
 874  
     // Inner class
 875  
     // ----------------------------------------------------------------------
 876  
     /**
 877  
      * <p>Is the specified class an inner class or static nested class.</p>
 878  
      *
 879  
      * @param cls  the class to check, may be null
 880  
      * @return {@code true} if the class is an inner or static nested class,
 881  
      *  false if not or {@code null}
 882  
      */
 883  
     public static boolean isInnerClass(final Class<?> cls) {
 884  6
         return cls != null && cls.getEnclosingClass() != null;
 885  
     }
 886  
 
 887  
     // Class loading
 888  
     // ----------------------------------------------------------------------
 889  
     /**
 890  
      * Returns the class represented by {@code className} using the
 891  
      * {@code classLoader}.  This implementation supports the syntaxes
 892  
      * "{@code java.util.Map.Entry[]}", "{@code java.util.Map$Entry[]}",
 893  
      * "{@code [Ljava.util.Map.Entry;}", and "{@code [Ljava.util.Map$Entry;}".
 894  
      *
 895  
      * @param classLoader  the class loader to use to load the class
 896  
      * @param className  the class name
 897  
      * @param initialize  whether the class must be initialized
 898  
      * @return the class represented by {@code className} using the {@code classLoader}
 899  
      * @throws ClassNotFoundException if the class is not found
 900  
      */
 901  
     public static Class<?> getClass(
 902  
             final ClassLoader classLoader, final String className, final boolean initialize) throws ClassNotFoundException {
 903  
         try {
 904  
             Class<?> clazz;
 905  78
             if (abbreviationMap.containsKey(className)) {
 906  8
                 final String clsName = "[" + abbreviationMap.get(className);
 907  8
                 clazz = Class.forName(clsName, initialize, classLoader).getComponentType();
 908  8
             } else {
 909  70
                 clazz = Class.forName(toCanonicalName(className), initialize, classLoader);
 910  
             }
 911  56
             return clazz;
 912  21
         } catch (final ClassNotFoundException ex) {
 913  
             // allow path separators (.) as inner class name separators
 914  21
             final int lastDotIndex = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR);
 915  
 
 916  21
             if (lastDotIndex != -1) {
 917  
                 try {
 918  12
                     return getClass(classLoader, className.substring(0, lastDotIndex) +
 919  
                             INNER_CLASS_SEPARATOR_CHAR + className.substring(lastDotIndex + 1),
 920  
                             initialize);
 921  6
                 } catch (final ClassNotFoundException ex2) { // NOPMD
 922  
                     // ignore exception
 923  
                 }
 924  
             }
 925  
 
 926  15
             throw ex;
 927  
         }
 928  
     }
 929  
 
 930  
     /**
 931  
      * Returns the (initialized) class represented by {@code className}
 932  
      * using the {@code classLoader}.  This implementation supports
 933  
      * the syntaxes "{@code java.util.Map.Entry[]}",
 934  
      * "{@code java.util.Map$Entry[]}", "{@code [Ljava.util.Map.Entry;}",
 935  
      * and "{@code [Ljava.util.Map$Entry;}".
 936  
      *
 937  
      * @param classLoader  the class loader to use to load the class
 938  
      * @param className  the class name
 939  
      * @return the class represented by {@code className} using the {@code classLoader}
 940  
      * @throws ClassNotFoundException if the class is not found
 941  
      */
 942  
     public static Class<?> getClass(final ClassLoader classLoader, final String className) throws ClassNotFoundException {
 943  0
         return getClass(classLoader, className, true);
 944  
     }
 945  
 
 946  
     /**
 947  
      * Returns the (initialized) class represented by {@code className}
 948  
      * using the current thread's context class loader. This implementation
 949  
      * supports the syntaxes "{@code java.util.Map.Entry[]}",
 950  
      * "{@code java.util.Map$Entry[]}", "{@code [Ljava.util.Map.Entry;}",
 951  
      * and "{@code [Ljava.util.Map$Entry;}".
 952  
      *
 953  
      * @param className  the class name
 954  
      * @return the class represented by {@code className} using the current thread's context class loader
 955  
      * @throws ClassNotFoundException if the class is not found
 956  
      */
 957  
     public static Class<?> getClass(final String className) throws ClassNotFoundException {
 958  66
         return getClass(className, true);
 959  
     }
 960  
 
 961  
     /**
 962  
      * Returns the class represented by {@code className} using the
 963  
      * current thread's context class loader. This implementation supports the
 964  
      * syntaxes "{@code java.util.Map.Entry[]}", "{@code java.util.Map$Entry[]}",
 965  
      * "{@code [Ljava.util.Map.Entry;}", and "{@code [Ljava.util.Map$Entry;}".
 966  
      *
 967  
      * @param className  the class name
 968  
      * @param initialize  whether the class must be initialized
 969  
      * @return the class represented by {@code className} using the current thread's context class loader
 970  
      * @throws ClassNotFoundException if the class is not found
 971  
      */
 972  
     public static Class<?> getClass(final String className, final boolean initialize) throws ClassNotFoundException {
 973  66
         final ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
 974  66
         final ClassLoader loader = contextCL == null ? ClassUtils.class.getClassLoader() : contextCL;
 975  66
         return getClass(loader, className, initialize);
 976  
     }
 977  
 
 978  
     // Public method
 979  
     // ----------------------------------------------------------------------
 980  
     /**
 981  
      * <p>Returns the desired Method much like {@code Class.getMethod}, however
 982  
      * it ensures that the returned Method is from a public class or interface and not
 983  
      * from an anonymous inner class. This means that the Method is invokable and
 984  
      * doesn't fall foul of Java bug
 985  
      * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4071957">4071957</a>).</p>
 986  
      *
 987  
      *  <pre>
 988  
      *  <code>Set set = Collections.unmodifiableSet(...);
 989  
      *  Method method = ClassUtils.getPublicMethod(set.getClass(), "isEmpty",  new Class[0]);
 990  
      *  Object result = method.invoke(set, new Object[]);</code>
 991  
      *  </pre>
 992  
      *
 993  
      * @param cls  the class to check, not null
 994  
      * @param methodName  the name of the method
 995  
      * @param parameterTypes  the list of parameters
 996  
      * @return the method
 997  
      * @throws NullPointerException if the class is null
 998  
      * @throws SecurityException if a security violation occurred
 999  
      * @throws NoSuchMethodException if the method is not found in the given class
 1000  
      *  or if the method doesn't conform with the requirements
 1001  
      */
 1002  
     public static Method getPublicMethod(final Class<?> cls, final String methodName, final Class<?>... parameterTypes)
 1003  
             throws SecurityException, NoSuchMethodException {
 1004  
 
 1005  2
         final Method declaredMethod = cls.getMethod(methodName, parameterTypes);
 1006  2
         if (Modifier.isPublic(declaredMethod.getDeclaringClass().getModifiers())) {
 1007  1
             return declaredMethod;
 1008  
         }
 1009  
 
 1010  1
         final List<Class<?>> candidateClasses = new ArrayList<Class<?>>();
 1011  1
         candidateClasses.addAll(getAllInterfaces(cls));
 1012  1
         candidateClasses.addAll(getAllSuperclasses(cls));
 1013  
 
 1014  1
         for (final Class<?> candidateClass : candidateClasses) {
 1015  1
             if (!Modifier.isPublic(candidateClass.getModifiers())) {
 1016  0
                 continue;
 1017  
             }
 1018  
             Method candidateMethod;
 1019  
             try {
 1020  1
                 candidateMethod = candidateClass.getMethod(methodName, parameterTypes);
 1021  0
             } catch (final NoSuchMethodException ex) {
 1022  0
                 continue;
 1023  1
             }
 1024  1
             if (Modifier.isPublic(candidateMethod.getDeclaringClass().getModifiers())) {
 1025  1
                 return candidateMethod;
 1026  
             }
 1027  0
         }
 1028  
 
 1029  0
         throw new NoSuchMethodException("Can't find a public method for " +
 1030  
                 methodName + " " + ArrayUtils.toString(parameterTypes));
 1031  
     }
 1032  
 
 1033  
     // ----------------------------------------------------------------------
 1034  
     /**
 1035  
      * Converts a class name to a JLS style class name.
 1036  
      *
 1037  
      * @param className  the class name
 1038  
      * @return the converted name
 1039  
      */
 1040  
     private static String toCanonicalName(String className) {
 1041  70
         className = StringUtils.deleteWhitespace(className);
 1042  70
         if (className == null) {
 1043  1
             throw new NullPointerException("className must not be null.");
 1044  69
         } else if (className.endsWith("[]")) {
 1045  29
             final StringBuilder classNameBuffer = new StringBuilder();
 1046  69
             while (className.endsWith("[]")) {
 1047  40
                 className = className.substring(0, className.length() - 2);
 1048  40
                 classNameBuffer.append("[");
 1049  
             }
 1050  29
             final String abbreviation = abbreviationMap.get(className);
 1051  29
             if (abbreviation != null) {
 1052  20
                 classNameBuffer.append(abbreviation);
 1053  
             } else {
 1054  9
                 classNameBuffer.append("L").append(className).append(";");
 1055  
             }
 1056  29
             className = classNameBuffer.toString();
 1057  
         }
 1058  69
         return className;
 1059  
     }
 1060  
 
 1061  
     /**
 1062  
      * <p>Converts an array of {@code Object} in to an array of {@code Class} objects.
 1063  
      * If any of these objects is null, a null element will be inserted into the array.</p>
 1064  
      *
 1065  
      * <p>This method returns {@code null} for a {@code null} input array.</p>
 1066  
      *
 1067  
      * @param array an {@code Object} array
 1068  
      * @return a {@code Class} array, {@code null} if null array input
 1069  
      * @since 2.4
 1070  
      */
 1071  
     public static Class<?>[] toClass(final Object... array) {
 1072  67
         if (array == null) {
 1073  1
             return null;
 1074  66
         } else if (array.length == 0) {
 1075  16
             return ArrayUtils.EMPTY_CLASS_ARRAY;
 1076  
         }
 1077  50
         final Class<?>[] classes = new Class[array.length];
 1078  104
         for (int i = 0; i < array.length; i++) {
 1079  54
             classes[i] = array[i] == null ? null : array[i].getClass();
 1080  
         }
 1081  50
         return classes;
 1082  
     }
 1083  
 
 1084  
     // Short canonical name
 1085  
     // ----------------------------------------------------------------------
 1086  
     /**
 1087  
      * <p>Gets the canonical name minus the package name for an {@code Object}.</p>
 1088  
      *
 1089  
      * @param object  the class to get the short name for, may be null
 1090  
      * @param valueIfNull  the value to return if null
 1091  
      * @return the canonical name of the object without the package name, or the null value
 1092  
      * @since 2.4
 1093  
      */
 1094  
     public static String getShortCanonicalName(final Object object, final String valueIfNull) {
 1095  9
         if (object == null) {
 1096  1
             return valueIfNull;
 1097  
         }
 1098  8
         return getShortCanonicalName(object.getClass().getName());
 1099  
     }
 1100  
 
 1101  
     /**
 1102  
      * <p>Gets the canonical name minus the package name from a {@code Class}.</p>
 1103  
      *
 1104  
      * @param cls  the class to get the short name for.
 1105  
      * @return the canonical name without the package name or an empty string
 1106  
      * @since 2.4
 1107  
      */
 1108  
     public static String getShortCanonicalName(final Class<?> cls) {
 1109  8
         if (cls == null) {
 1110  0
             return StringUtils.EMPTY;
 1111  
         }
 1112  8
         return getShortCanonicalName(cls.getName());
 1113  
     }
 1114  
 
 1115  
     /**
 1116  
      * <p>Gets the canonical name minus the package name from a String.</p>
 1117  
      *
 1118  
      * <p>The string passed in is assumed to be a canonical name - it is not checked.</p>
 1119  
      *
 1120  
      * @param canonicalName  the class name to get the short name for
 1121  
      * @return the canonical name of the class without the package name or an empty string
 1122  
      * @since 2.4
 1123  
      */
 1124  
     public static String getShortCanonicalName(final String canonicalName) {
 1125  28
         return ClassUtils.getShortClassName(getCanonicalName(canonicalName));
 1126  
     }
 1127  
 
 1128  
     // Package name
 1129  
     // ----------------------------------------------------------------------
 1130  
     /**
 1131  
      * <p>Gets the package name from the canonical name of an {@code Object}.</p>
 1132  
      *
 1133  
      * @param object  the class to get the package name for, may be null
 1134  
      * @param valueIfNull  the value to return if null
 1135  
      * @return the package name of the object, or the null value
 1136  
      * @since 2.4
 1137  
      */
 1138  
     public static String getPackageCanonicalName(final Object object, final String valueIfNull) {
 1139  9
         if (object == null) {
 1140  1
             return valueIfNull;
 1141  
         }
 1142  8
         return getPackageCanonicalName(object.getClass().getName());
 1143  
     }
 1144  
 
 1145  
     /**
 1146  
      * <p>Gets the package name from the canonical name of a {@code Class}.</p>
 1147  
      *
 1148  
      * @param cls  the class to get the package name for, may be {@code null}.
 1149  
      * @return the package name or an empty string
 1150  
      * @since 2.4
 1151  
      */
 1152  
     public static String getPackageCanonicalName(final Class<?> cls) {
 1153  8
         if (cls == null) {
 1154  0
             return StringUtils.EMPTY;
 1155  
         }
 1156  8
         return getPackageCanonicalName(cls.getName());
 1157  
     }
 1158  
 
 1159  
     /**
 1160  
      * <p>Gets the package name from the canonical name. </p>
 1161  
      *
 1162  
      * <p>The string passed in is assumed to be a canonical name - it is not checked.</p>
 1163  
      * <p>If the class is unpackaged, return an empty string.</p>
 1164  
      *
 1165  
      * @param canonicalName  the canonical name to get the package name for, may be {@code null}
 1166  
      * @return the package name or an empty string
 1167  
      * @since 2.4
 1168  
      */
 1169  
     public static String getPackageCanonicalName(final String canonicalName) {
 1170  28
         return ClassUtils.getPackageName(getCanonicalName(canonicalName));
 1171  
     }
 1172  
 
 1173  
     /**
 1174  
      * <p>Converts a given name of class into canonical format.
 1175  
      * If name of class is not a name of array class it returns
 1176  
      * unchanged name.</p>
 1177  
      * <p>Example:
 1178  
      * <ul>
 1179  
      * <li>{@code getCanonicalName("[I") = "int[]"}</li>
 1180  
      * <li>{@code getCanonicalName("[Ljava.lang.String;") = "java.lang.String[]"}</li>
 1181  
      * <li>{@code getCanonicalName("java.lang.String") = "java.lang.String"}</li>
 1182  
      * </ul>
 1183  
      * </p>
 1184  
      *
 1185  
      * @param className the name of class
 1186  
      * @return canonical form of class name
 1187  
      * @since 2.4
 1188  
      */
 1189  
     private static String getCanonicalName(String className) {
 1190  56
         className = StringUtils.deleteWhitespace(className);
 1191  56
         if (className == null) {
 1192  0
             return null;
 1193  
         }
 1194  56
         int dim = 0;
 1195  92
         while (className.startsWith("[")) {
 1196  36
             dim++;
 1197  36
             className = className.substring(1);
 1198  
         }
 1199  56
         if (dim < 1) {
 1200  32
             return className;
 1201  
         }
 1202  24
         if (className.startsWith("L")) {
 1203  12
             className = className.substring(
 1204  
                 1,
 1205  
                 className.endsWith(";")
 1206  
                     ? className.length() - 1
 1207  
                     : className.length());
 1208  
         } else {
 1209  12
             if (className.length() > 0) {
 1210  12
                 className = reverseAbbreviationMap.get(className.substring(0, 1));
 1211  
             }
 1212  
         }
 1213  24
         final StringBuilder canonicalClassNameBuffer = new StringBuilder(className);
 1214  60
         for (int i = 0; i < dim; i++) {
 1215  36
             canonicalClassNameBuffer.append("[]");
 1216  
         }
 1217  24
         return canonicalClassNameBuffer.toString();
 1218  
     }
 1219  
 
 1220  
     /**
 1221  
      * Get an {@link Iterable} that can iterate over a class hierarchy in ascending (subclass to superclass) order,
 1222  
      * excluding interfaces.
 1223  
      *
 1224  
      * @param type the type to get the class hierarchy from
 1225  
      * @return Iterable an Iterable over the class hierarchy of the given class
 1226  
      * @since 3.2
 1227  
      */
 1228  
     public static Iterable<Class<?>> hierarchy(final Class<?> type) {
 1229  1
         return hierarchy(type, Interfaces.EXCLUDE);
 1230  
     }
 1231  
 
 1232  
     /**
 1233  
      * Get an {@link Iterable} that can iterate over a class hierarchy in ascending (subclass to superclass) order.
 1234  
      *
 1235  
      * @param type the type to get the class hierarchy from
 1236  
      * @param interfacesBehavior switch indicating whether to include or exclude interfaces
 1237  
      * @return Iterable an Iterable over the class hierarchy of the given class
 1238  
      * @since 3.2
 1239  
      */
 1240  
     public static Iterable<Class<?>> hierarchy(final Class<?> type, final Interfaces interfacesBehavior) {
 1241  4
         final Iterable<Class<?>> classes = new Iterable<Class<?>>() {
 1242  
     
 1243  
             @Override
 1244  
             public Iterator<Class<?>> iterator() {
 1245  4
                 final MutableObject<Class<?>> next = new MutableObject<Class<?>>(type);
 1246  4
                 return new Iterator<Class<?>>() {
 1247  
     
 1248  
                     @Override
 1249  
                     public boolean hasNext() {
 1250  8
                         return next.getValue() != null;
 1251  
                     }
 1252  
     
 1253  
                     @Override
 1254  
                     public Class<?> next() {
 1255  12
                         final Class<?> result = next.getValue();
 1256  12
                         next.setValue(result.getSuperclass());
 1257  12
                         return result;
 1258  
                     }
 1259  
     
 1260  
                     @Override
 1261  
                     public void remove() {
 1262  0
                         throw new UnsupportedOperationException();
 1263  
                     }
 1264  
     
 1265  
                 };
 1266  
             }
 1267  
     
 1268  
         };
 1269  4
         if (interfacesBehavior != Interfaces.INCLUDE) {
 1270  2
             return classes;
 1271  
         }
 1272  2
         return new Iterable<Class<?>>() {
 1273  
     
 1274  
             @Override
 1275  
             public Iterator<Class<?>> iterator() {
 1276  2
                 final Set<Class<?>> seenInterfaces = new HashSet<Class<?>>();
 1277  2
                 final Iterator<Class<?>> wrapped = classes.iterator();
 1278  
     
 1279  2
                 return new Iterator<Class<?>>() {
 1280  2
                     Iterator<Class<?>> interfaces = Collections.<Class<?>> emptySet().iterator();
 1281  
     
 1282  
                     @Override
 1283  
                     public boolean hasNext() {
 1284  5
                         return interfaces.hasNext() || wrapped.hasNext();
 1285  
                     }
 1286  
     
 1287  
                     @Override
 1288  
                     public Class<?> next() {
 1289  8
                         if (interfaces.hasNext()) {
 1290  2
                             final Class<?> nextInterface = interfaces.next();
 1291  2
                             seenInterfaces.add(nextInterface);
 1292  2
                             return nextInterface;
 1293  
                         }
 1294  6
                         final Class<?> nextSuperclass = wrapped.next();
 1295  6
                         final Set<Class<?>> currentInterfaces = new LinkedHashSet<Class<?>>();
 1296  6
                         walkInterfaces(currentInterfaces, nextSuperclass);
 1297  6
                         interfaces = currentInterfaces.iterator();
 1298  6
                         return nextSuperclass;
 1299  
                     }
 1300  
     
 1301  
                     private void walkInterfaces(final Set<Class<?>> addTo, final Class<?> c) {
 1302  10
                         for (final Class<?> iface : c.getInterfaces()) {
 1303  2
                             if (!seenInterfaces.contains(iface)) {
 1304  2
                                 addTo.add(iface);
 1305  
                             }
 1306  2
                             walkInterfaces(addTo, iface);
 1307  
                         }
 1308  8
                     }
 1309  
     
 1310  
                     @Override
 1311  
                     public void remove() {
 1312  0
                         throw new UnsupportedOperationException();
 1313  
                     }
 1314  
     
 1315  
                 };
 1316  
             }
 1317  
         };
 1318  
     }
 1319  
 
 1320  
 }