Coverage Report - org.jtheque.utils.GenericsUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericsUtils
0%
0/35
0%
0/22
3.75
 
 1  
 package org.jtheque.utils;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import java.lang.reflect.Array;
 20  
 import java.lang.reflect.GenericArrayType;
 21  
 import java.lang.reflect.GenericDeclaration;
 22  
 import java.lang.reflect.ParameterizedType;
 23  
 import java.lang.reflect.Type;
 24  
 import java.lang.reflect.TypeVariable;
 25  
 import java.util.ArrayList;
 26  
 import java.util.Collection;
 27  
 import java.util.HashMap;
 28  
 import java.util.Map;
 29  
 
 30  
 /**
 31  
  * A generic class utils.
 32  
  *
 33  
  * @author Ian Robertson
 34  
  * @author Baptiste Wicht
 35  
  */
 36  
 public final class GenericsUtils {
 37  
     /**
 38  
      * Construct a new GenericsUtils.
 39  
      */
 40  
     private GenericsUtils() {
 41  0
         super();
 42  0
     }
 43  
 
 44  
     /**
 45  
      * Get the actual type arguments a child class has used to extend a generic
 46  
      * base class.
 47  
      *
 48  
      * @param baseClass  the base class
 49  
      * @param childClass the child class
 50  
      * @param <T>        The type.
 51  
      * @return a list of the raw classes for the actual type arguments.
 52  
      */
 53  
     public static <T> Collection<Class<?>> getTypeArguments(final Class<T> baseClass,
 54  
                                                             final Type childClass) {
 55  0
         Map<Type, Type> resolvedTypes = new HashMap<Type, Type>(10);
 56  0
         Type type = childClass;
 57  
         // start walking up the inheritance hierarchy until we hit baseClass
 58  0
         while (!getClass(type).equals(baseClass)) {
 59  0
             if (type instanceof Class) {
 60  
                 // there is no useful information for us in raw types, so just
 61  
                 // keep going.
 62  0
                 type = ((Class<?>) type).getGenericSuperclass();
 63  
             } else {
 64  0
                 ParameterizedType parameterizedType = (ParameterizedType) type;
 65  0
                 Class<?> rawType = (Class<?>) parameterizedType.getRawType();
 66  
 
 67  0
                 Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
 68  0
                 TypeVariable<?>[] typeParameters = rawType.getTypeParameters();
 69  
 
 70  0
                 for (int i = 0; i < actualTypeArguments.length; i++) {
 71  0
                     resolvedTypes.put(typeParameters[i], actualTypeArguments[i]);
 72  
                 }
 73  
 
 74  0
                 if (!rawType.equals(baseClass)) {
 75  0
                     type = rawType.getGenericSuperclass();
 76  
                 }
 77  0
             }
 78  
         }
 79  
 
 80  0
         return determineRawClasses(resolvedTypes, type);
 81  
     }
 82  
 
 83  
     /**
 84  
      * Determine the raw classes of the resolved types.
 85  
      *
 86  
      * @param resolvedTypes The resolved types.
 87  
      * @param type          The type.
 88  
      * @return The raw types.
 89  
      */
 90  
     private static Collection<Class<?>> determineRawClasses(Map<Type, Type> resolvedTypes, Type type) {
 91  0
         Type[] actualTypeArguments = type instanceof Class ? ((GenericDeclaration) type).getTypeParameters() : ((ParameterizedType) type).getActualTypeArguments();
 92  
 
 93  0
         Collection<Class<?>> typeArgumentsAsClasses = new ArrayList<Class<?>>(actualTypeArguments.length);
 94  
         // resolve types by chasing down type variables.
 95  0
         for (Type baseType : actualTypeArguments) {
 96  0
             while (resolvedTypes.containsKey(baseType)) {
 97  0
                 baseType = resolvedTypes.get(baseType);
 98  
             }
 99  
 
 100  0
             typeArgumentsAsClasses.add(getClass(baseType));
 101  
         }
 102  0
         return typeArgumentsAsClasses;
 103  
     }
 104  
 
 105  
     /**
 106  
      * Get the underlying class for a type, or null if the type is a variable
 107  
      * type.
 108  
      *
 109  
      * @param type the type
 110  
      * @return the underlying class
 111  
      */
 112  
     private static Class<?> getClass(Type type) {
 113  0
         Class<?> typeClass = null;
 114  
 
 115  0
         if (type instanceof Class) {
 116  0
             typeClass = (Class<?>) type;
 117  0
         } else if (type instanceof ParameterizedType) {
 118  0
             typeClass = getClass(((ParameterizedType) type).getRawType());
 119  0
         } else if (type instanceof GenericArrayType) {
 120  0
             Type componentType = ((GenericArrayType) type).getGenericComponentType();
 121  0
             Class<?> componentClass = getClass(componentType);
 122  
 
 123  0
             if (componentClass != null) {
 124  0
                 typeClass = Array.newInstance(componentClass, 0).getClass();
 125  
             }
 126  
         }
 127  
 
 128  0
         return typeClass;
 129  
     }
 130  
 }