commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r963845 - in /commons/proper/lang/trunk: pom.xml src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
Date Tue, 13 Jul 2010 19:56:46 GMT
Author: mbenson
Date: Tue Jul 13 19:56:45 2010
New Revision: 963845

URL: http://svn.apache.org/viewvc?rev=963845&view=rev
Log:
[LANG-597] vastly expanded TypeUtils

Modified:
    commons/proper/lang/trunk/pom.xml
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java

Modified: commons/proper/lang/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/pom.xml?rev=963845&r1=963844&r2=963845&view=diff
==============================================================================
--- commons/proper/lang/trunk/pom.xml (original)
+++ commons/proper/lang/trunk/pom.xml Tue Jul 13 19:56:45 2010
@@ -364,6 +364,9 @@
             <name>Ville Skytta</name>
         </contributor>
         <contributor>
+            <name>David M. Sledge</name>
+        </contributor>
+        <contributor>
             <name>Jan Sorensen</name>
         </contributor>
         <contributor>

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java?rev=963845&r1=963844&r2=963845&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java Tue Jul 13 19:56:45 2010
@@ -21,105 +21,1040 @@ import java.lang.reflect.GenericArrayTyp
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
+import java.lang.reflect.WildcardType;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
-import org.apache.commons.lang3.Validate;
+import org.apache.commons.lang3.ClassUtils;
 
 /**
- * <p>Utility methods focusing on type inspection, particularly with regard to
- * generics.</p>
- * @author James Carman
+ * <p> Utility methods focusing on type inspection, particularly with regard to
+ * generics. </p>
+ *
+ * @author David M. Sledge
  * @author Matt Benson
+ * @author James Carman
  * @since 3.0
  * @version $Id$
  */
 public class TypeUtils {
 
     /**
-     * Get the raw type of a Java type, given its context. Primarily for use
+     * <p> TypeUtils instances should NOT be constructed in standard
+     * programming. Instead, the class should be used as
+     * <code>TypeUtils.isAssignable(cls, toClass)</code>. </p> <p> This
+     * constructor is public to permit tools that require a JavaBean instance to
+     * operate. </p>
+     */
+    public TypeUtils() {
+        super();
+    }
+
+    /**
+     * <p> Checks if the subject type may be implicitly cast to the target type
+     * following the Java generics rules. If both types are {@link Class}
+     * objects, the method returns the result of
+     * {@link ClassUtils#isAssignable(Class, Class)}. </p>
+     *
+     * @param type the subject type to be assigned to the target type
+     * @param toType the target type
+     * @return <code>true</code> if <code>type</code> is assignable to <code>toType</code>.
+     */
+    public static boolean isAssignable(Type type, Type toType) {
+        return isAssignable(type, toType, null);
+    }
+
+    /**
+     * <p> Checks if the subject type may be implicitly cast to the target type
+     * following the Java generics rules. </p>
+     *
+     * @param type the subject type to be assigned to the target type
+     * @param toType the target type
+     * @param typeVarAssigns optional map of type variable assignments
+     * @return <code>true</code> if <code>type</code> is assignable to <code>toType</code>.
+     */
+    private static boolean isAssignable(Type type, Type toType,
+            Map<TypeVariable<?>, Type> typeVarAssigns) {
+        if (toType == null || toType instanceof Class<?>) {
+            return isAssignable(type, (Class<?>) toType);
+        }
+
+        if (toType instanceof ParameterizedType) {
+            return isAssignable(type, (ParameterizedType) toType, typeVarAssigns);
+        }
+
+        if (toType instanceof GenericArrayType) {
+            return isAssignable(type, (GenericArrayType) toType, typeVarAssigns);
+        }
+
+        if (toType instanceof WildcardType) {
+            return isAssignable(type, (WildcardType) toType, typeVarAssigns);
+        }
+
+        // *
+        if (toType instanceof TypeVariable<?>) {
+            return isAssignable(type, (TypeVariable<?>) toType, typeVarAssigns);
+        }
+        // */
+
+        throw new IllegalStateException("found an unhandled type: " + toType);
+    }
+
+    /**
+     * <p> Checks if the subject type may be implicitly cast to the target class
+     * following the Java generics rules. </p>
+     *
+     * @param type the subject type to be assigned to the target type
+     * @param toClass the target class
+     * @return true if <code>type</code> is assignable to <code>toClass</code>.
+     */
+    private static boolean isAssignable(Type type, Class<?> toClass) {
+        if (type == null) {
+            // consistency with ClassUtils.isAssignable() behavior
+            return toClass == null || !toClass.isPrimitive();
+        }
+
+        // only a null type can be assigned to null type which
+        // would have cause the previous to return true
+        if (toClass == null) {
+            return false;
+        }
+
+        // all types are assignable to themselves
+        if (toClass.equals(type)) {
+            return true;
+        }
+
+        if (type instanceof Class<?>) {
+            // just comparing two classes
+            return ClassUtils.isAssignable((Class<?>) type, toClass);
+        }
+
+        if (type instanceof ParameterizedType) {
+            // only have to compare the raw type to the class
+            return isAssignable(getRawType((ParameterizedType) type), toClass);
+        }
+
+        // *
+        if (type instanceof TypeVariable<?>) {
+            // if any of the bounds are assignable to the class, then the
+            // type is assignable to the class.
+            for (Type bound : ((TypeVariable<?>) type).getBounds()) {
+                if (isAssignable(bound, toClass)) {
+                    return true;
+                }
+            }
+
+            return false;
+        }
+
+        // the only classes to which a generic array type can be assigned
+        // are class Object and array classes
+        if (type instanceof GenericArrayType) {
+            return toClass.equals(Object.class)
+                    || toClass.isArray()
+                    && isAssignable(((GenericArrayType) type).getGenericComponentType(), toClass
+                            .getComponentType());
+        }
+
+        // wildcard types are not assignable to a class (though one would think
+        // "? super Object" would be assignable to Object)
+        if (type instanceof WildcardType) {
+            return false;
+        }
+
+        throw new IllegalStateException("found an unhandled type: " + type);
+    }
+
+    /**
+     * <p> Checks if the subject type may be implicitly cast to the target
+     * parameterized type following the Java generics rules. </p>
+     *
+     * @param type the subject type to be assigned to the target type
+     * @param toParameterizedType the target parameterized type
+     * @return true if <code>type</code> is assignable to <code>toType</code>.
+     */
+    private static boolean isAssignable(Type type, ParameterizedType toParameterizedType,
+            Map<TypeVariable<?>, Type> typeVarAssigns) {
+        if (type == null) {
+            return true;
+        }
+
+        // only a null type can be assigned to null type which
+        // would have cause the previous to return true
+        if (toParameterizedType == null) {
+            return false;
+        }
+
+        // all types are assignable to themselves
+        if (toParameterizedType.equals(type)) {
+            return true;
+        }
+
+        // get the target type's raw type
+        Class<?> toClass = getRawType(toParameterizedType);
+        // get the subject type's type arguments including owner type arguments
+        // and supertype arguments up to and including the target class.
+        Map<TypeVariable<?>, Type> fromTypeVarAssigns = getTypeArguments(type, toClass, null);
+
+        // null means the two types are not compatible
+        if (fromTypeVarAssigns == null) {
+            return false;
+        }
+
+        // compatible types, but there's no type arguments. this is equivalent
+        // to comparing Map< ?, ? > to Map, and raw types are always assignable
+        // to parameterized types.
+        if (fromTypeVarAssigns.isEmpty()) {
+            return true;
+        }
+
+        // get the target type's type arguments including owner type arguments
+        Map<TypeVariable<?>, Type> toTypeVarAssigns = getTypeArguments(toParameterizedType,
+                toClass, typeVarAssigns);
+
+        // now to check each type argument
+        for (Map.Entry<TypeVariable<?>, Type> entry : toTypeVarAssigns.entrySet()) {
+            Type toTypeArg = entry.getValue();
+            Type fromTypeArg = fromTypeVarAssigns.get(entry.getKey());
+
+            // parameters must either be absent from the subject type, within
+            // the bounds of the wildcard type, or be an exact match to the
+            // parameters of the target type.
+            if (fromTypeArg != null
+                    && !toTypeArg.equals(fromTypeArg)
+                    && !(toTypeArg instanceof WildcardType && isAssignable(fromTypeArg, toTypeArg,
+                            typeVarAssigns))) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    /**
+     * <p> Checks if the subject type may be implicitly cast to the target
+     * generic array type following the Java generics rules. </p>
+     *
+     * @param type the subject type to be assigned to the target type
+     * @param toGenericArrayType the target generic array type
+     * @return true if <code>type</code> is assignable to
+     * <code>toGenericArrayType</code>.
+     */
+    private static boolean isAssignable(Type type, GenericArrayType toGenericArrayType,
+            Map<TypeVariable<?>, Type> typeVarAssigns) {
+        if (type == null) {
+            return true;
+        }
+
+        // only a null type can be assigned to null type which
+        // would have cause the previous to return true
+        if (toGenericArrayType == null) {
+            return false;
+        }
+
+        // all types are assignable to themselves
+        if (toGenericArrayType.equals(type)) {
+            return true;
+        }
+
+        Type toComponentType = toGenericArrayType.getGenericComponentType();
+
+        if (type instanceof Class<?>) {
+            Class<?> cls = (Class<?>) type;
+
+            // compare the component types
+            return cls.isArray()
+                    && isAssignable(cls.getComponentType(), toComponentType, typeVarAssigns);
+        }
+
+        if (type instanceof GenericArrayType) {
+            // compare the component types
+            return isAssignable(((GenericArrayType) type).getGenericComponentType(),
+                    toComponentType, typeVarAssigns);
+        }
+
+        if (type instanceof WildcardType) {
+            // so long as one of the upper bounds is assignable, it's good
+            for (Type bound : getImplicitUpperBounds((WildcardType) type)) {
+                if (isAssignable(bound, toGenericArrayType)) {
+                    return true;
+                }
+            }
+
+            return false;
+        }
+
+        if (type instanceof TypeVariable<?>) {
+            // probably should remove the following logic and just return false.
+            // type variables cannot specify arrays as bounds.
+            for (Type bound : getImplicitBounds((TypeVariable<?>) type)) {
+                if (isAssignable(bound, toGenericArrayType)) {
+                    return true;
+                }
+            }
+
+            return false;
+        }
+
+        if (type instanceof ParameterizedType) {
+            // the raw type of a parameterized type is never an array or
+            // generic array, otherwise the declaration would look like this:
+            // Collection[]< ? extends String > collection;
+            return false;
+        }
+
+        throw new IllegalStateException("found an unhandled type: " + type);
+    }
+
+    /**
+     * <p> Checks if the subject type may be implicitly cast to the target
+     * wildcard type following the Java generics rules. </p>
+     *
+     * @param type the subject type to be assigned to the target type
+     * @param toWildcardType the target wildcard type
+     * @return true if <code>type</code> is assignable to
+     * <code>toWildcardType</code>.
+     */
+    private static boolean isAssignable(Type type, WildcardType toWildcardType,
+            Map<TypeVariable<?>, Type> typeVarAssigns) {
+        if (type == null) {
+            return true;
+        }
+
+        // only a null type can be assigned to null type which
+        // would have cause the previous to return true
+        if (toWildcardType == null) {
+            return false;
+        }
+
+        // all types are assignable to themselves
+        if (toWildcardType.equals(type)) {
+            return true;
+        }
+
+        Type[] toUpperBounds = getImplicitUpperBounds(toWildcardType);
+        Type[] toLowerBounds = getImplicitLowerBounds(toWildcardType);
+
+        if (type instanceof WildcardType) {
+            WildcardType wildcardType = (WildcardType) type;
+            Type[] upperBounds = getImplicitUpperBounds(wildcardType);
+            Type[] lowerBounds = getImplicitLowerBounds(wildcardType);
+
+            for (Type toBound : toUpperBounds) {
+                // if there are assignments for unresolved type variables,
+                // now's the time to substitute them.
+                toBound = substituteTypeVariables(toBound, typeVarAssigns);
+
+                // each upper bound of the subject type has to be assignable to
+                // each
+                // upper bound of the target type
+                for (Type bound : upperBounds) {
+                    if (!isAssignable(bound, toBound, typeVarAssigns)) {
+                        return false;
+                    }
+                }
+            }
+
+            for (Type toBound : toLowerBounds) {
+                // if there are assignments for unresolved type variables,
+                // now's the time to substitute them.
+                toBound = substituteTypeVariables(toBound, typeVarAssigns);
+
+                // each lower bound of the target type has to be assignable to
+                // each
+                // lower bound of the subject type
+                for (Type bound : lowerBounds) {
+                    if (!isAssignable(toBound, bound, typeVarAssigns)) {
+                        return false;
+                    }
+                }
+            }
+
+            return true;
+        }
+
+        for (Type toBound : toUpperBounds) {
+            // if there are assignments for unresolved type variables,
+            // now's the time to substitute them.
+            if (!isAssignable(type, substituteTypeVariables(toBound, typeVarAssigns),
+                    typeVarAssigns)) {
+                return false;
+            }
+        }
+
+        for (Type toBound : toLowerBounds) {
+            // if there are assignments for unresolved type variables,
+            // now's the time to substitute them.
+            if (!isAssignable(substituteTypeVariables(toBound, typeVarAssigns), type,
+                    typeVarAssigns)) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    /**
+     * <p> Checks if the subject type may be implicitly cast to the target type
+     * variable following the Java generics rules. </p>
+     *
+     * @param type the subject type to be assigned to the target type
+     * @param toTypeVariable the target type variable
+     * @return true if <code>type</code> is assignable to
+     * <code>toTypeVariable</code>.
+     */
+    private static boolean isAssignable(Type type, TypeVariable<?> toTypeVariable,
+            Map<TypeVariable<?>, Type> typeVarAssigns) {
+        if (type == null) {
+            return true;
+        }
+
+        // only a null type can be assigned to null type which
+        // would have cause the previous to return true
+        if (toTypeVariable == null) {
+            return false;
+        }
+
+        // all types are assignable to themselves
+        if (toTypeVariable.equals(type)) {
+            return true;
+        }
+
+        if (type instanceof TypeVariable<?>) {
+            // a type variable is assignable to another type variable, if
+            // and only if the former is the latter, extends the latter, or
+            // is otherwise a descendant of the latter.
+            Type[] bounds = getImplicitBounds((TypeVariable<?>) type);
+
+            for (Type bound : bounds) {
+                if (isAssignable(bound, toTypeVariable, typeVarAssigns)) {
+                    return true;
+                }
+            }
+        }
+
+        if (type instanceof Class<?> || type instanceof ParameterizedType
+                || type instanceof GenericArrayType || type instanceof WildcardType) {
+            return false;
+        }
+
+        throw new IllegalStateException("found an unhandled type: " + type);
+    }
+
+    /**
+     * <p> </p>
+     *
+     * @param type
+     * @param typeVarAssigns
+     * @return
+     */
+    private static Type substituteTypeVariables(Type type, Map<TypeVariable<?>, Type> typeVarAssigns) {
+        if (type instanceof TypeVariable<?> && typeVarAssigns != null) {
+            Type replacementType = typeVarAssigns.get(type);
+
+            if (replacementType == null) {
+                throw new IllegalArgumentException("missing assignment type for type variable "
+                        + type);
+            }
+
+            return replacementType;
+        }
+
+        return type;
+    }
+
+    /**
+     * <p> Retrieves all the type arguments for this parameterized type
+     * including owner hierarchy arguments such as <code>
+     * Outer<K,V>.Inner<T>.DeepInner<E></code> . The arguments are returned in a
+     * {@link Map} specifying the argument type for each {@link TypeVariable}.
+     * </p>
+     *
+     * @param type specifies the subject parameterized type from which to
+     * harvest the parameters.
+     * @return a map of the type arguments to their respective type variables.
+     */
+    public static Map<TypeVariable<?>, Type> getTypeArguments(ParameterizedType type) {
+        return getTypeArguments(type, getRawType(type), null);
+    }
+
+    /**
+     * <p> Gets the type arguments of a class/interface based on a subtype. For
+     * instance, this method will determine that both of the parameters for the
+     * interface {@link Map} are {@link Object} for the subtype
+     * {@link java.util.Properties Properties} even though the subtype does not
+     * directly implement the <code>Map</code> interface. <p> </p> This method
+     * returns <code>null</code> if <code>type</code> is not assignable to
+     * <code>toClass</code>. It returns an empty map if none of the classes or
+     * interfaces in its inheritance hierarchy specify any type arguments. </p>
+     * <p> A side-effect of this method is that it also retrieves the type
+     * arguments for the classes and interfaces that are part of the hierarchy
+     * between <code>type</code> and <code>toClass</code>. So with the above
+     * example, this method will also determine that the type arguments for
+     * {@link java.util.Hashtable Hashtable} are also both <code>Object</code>.
+     * In cases where the interface specified by <code>toClass</code> is
+     * (indirectly) implemented more than once (e.g. where <code>toClass</code>
+     * specifies the interface {@link java.lang.Iterable Iterable} and
+     * <code>type</code> specifies a parameterized type that implements both
+     * {@link java.util.Set Set} and {@link java.lang.Collection Collection}),
+     * this method will look at the inheritance hierarchy of only one of the
+     * implementations/subclasses; the first interface encountered that isn't a
+     * subinterface to one of the others in the <code>type</code> to
+     * <code>toClass</code> hierarchy. </p>
+     *
+     * @param type the type from which to determine the type parameters of
+     * <code>toClass</code>
+     * @param toClass the class whose type parameters are to be determined based
+     * on the subtype <code>type</code>
+     * @return a map of the type assignments for the type variables in each type
+     * in the inheritance hierarchy from <code>type</code> to
+     * <code>toClass</code> inclusive.
+     */
+    public static Map<TypeVariable<?>, Type> getTypeArguments(Type type, Class<?> toClass) {
+        return getTypeArguments(type, toClass, null);
+    }
+
+    /**
+     * <p> Return a map of the type arguments of <code>type</code> in the context of <code>toClass</code>. </p>
+     *
+     * @param type
+     * @param toClass
+     * @param subtypeVarAssigns
+     * @return
+     */
+    private static Map<TypeVariable<?>, Type> getTypeArguments(Type type, Class<?> toClass,
+            Map<TypeVariable<?>, Type> subtypeVarAssigns) {
+        if (type instanceof Class<?>) {
+            return getTypeArguments((Class<?>) type, toClass, subtypeVarAssigns);
+        }
+
+        if (type instanceof ParameterizedType) {
+            return getTypeArguments((ParameterizedType) type, toClass, subtypeVarAssigns);
+        }
+
+        if (type instanceof GenericArrayType) {
+            return getTypeArguments(((GenericArrayType) type).getGenericComponentType(), toClass
+                    .isArray() ? toClass.getComponentType() : toClass, subtypeVarAssigns);
+        }
+
+        // since wildcard types are not assignable to classes, should this just
+        // return null?
+        if (type instanceof WildcardType) {
+            for (Type bound : getImplicitUpperBounds((WildcardType) type)) {
+                // find the first bound that is assignable to the target class
+                if (isAssignable(bound, toClass)) {
+                    return getTypeArguments(bound, toClass, subtypeVarAssigns);
+                }
+            }
+
+            return null;
+        }
+
+        // *
+        if (type instanceof TypeVariable<?>) {
+            for (Type bound : getImplicitBounds((TypeVariable<?>) type)) {
+                // find the first bound that is assignable to the target class
+                if (isAssignable(bound, toClass)) {
+                    return getTypeArguments(bound, toClass, subtypeVarAssigns);
+                }
+            }
+
+            return null;
+        }
+        // */
+
+        throw new IllegalStateException("found an unhandled type: " + type);
+    }
+
+    /**
+     * <p> </p>
+     *
+     * @param parameterizedType
+     * @param toClass
+     * @param subtypeVarAssigns
+     * @return
+     */
+    private static Map<TypeVariable<?>, Type> getTypeArguments(
+            ParameterizedType parameterizedType, Class<?> toClass,
+            Map<TypeVariable<?>, Type> subtypeVarAssigns) {
+        Class<?> cls = getRawType(parameterizedType);
+
+        // make sure they're assignable
+        if (!isAssignable(cls, toClass)) {
+            return null;
+        }
+
+        Type ownerType = parameterizedType.getOwnerType();
+        Map<TypeVariable<?>, Type> typeVarAssigns;
+
+        if (ownerType instanceof ParameterizedType) {
+            // get the owner type arguments first
+            ParameterizedType parameterizedOwnerType = (ParameterizedType) ownerType;
+            typeVarAssigns = getTypeArguments(parameterizedOwnerType,
+                    getRawType(parameterizedOwnerType), subtypeVarAssigns);
+        } else {
+            // no owner, prep the type variable assignments map
+            typeVarAssigns = subtypeVarAssigns == null ? new HashMap<TypeVariable<?>, Type>()
+                    : new HashMap<TypeVariable<?>, Type>(subtypeVarAssigns);
+        }
+
+        // get the subject parameterized type's arguments
+        Type[] typeArgs = parameterizedType.getActualTypeArguments();
+        // and get the corresponding type variables from the raw class
+        TypeVariable<?>[] typeParams = cls.getTypeParameters();
+
+        // map the arguments to their respective type variables
+        for (int i = 0; i < typeParams.length; i++) {
+            Type typeArg = typeArgs[i];
+            typeVarAssigns.put(typeParams[i], typeVarAssigns.containsKey(typeArg) ? typeVarAssigns
+                    .get(typeArg) : typeArg);
+        }
+
+        if (toClass.equals(cls)) {
+            // target class has been reached. Done.
+            return typeVarAssigns;
+        }
+
+        // walk the inheritance hierarchy until the target class is reached
+        return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns);
+    }
+
+    /**
+     * <p> </p>
+     *
+     * @param cls
+     * @param toClass
+     * @param subtypeVarAssigns
+     * @return
+     */
+    private static Map<TypeVariable<?>, Type> getTypeArguments(Class<?> cls, Class<?> toClass,
+            Map<TypeVariable<?>, Type> subtypeVarAssigns) {
+        // make sure they're assignable
+        if (!isAssignable(cls, toClass)) {
+            return null;
+        }
+
+        // can't work with primitives
+        if (cls.isPrimitive()) {
+            // both classes are primitives?
+            if (toClass.isPrimitive()) {
+                // dealing with widening here. No type arguments to be
+                // harvested with these two types.
+                return new HashMap<TypeVariable<?>, Type>();
+            }
+
+            // work with wrapper the wrapper class instead of the primitive
+            cls = ClassUtils.primitiveToWrapper(cls);
+        }
+
+        // create a copy of the incoming map, or an empty one if it's null
+        HashMap<TypeVariable<?>, Type> typeVarAssigns = subtypeVarAssigns == null ? new HashMap<TypeVariable<?>, Type>()
+                : new HashMap<TypeVariable<?>, Type>(subtypeVarAssigns);
+
+        // no arguments for the parameters, or target class has been reached
+        if (cls.getTypeParameters().length > 0 || toClass.equals(cls)) {
+            return typeVarAssigns;
+        }
+
+        // walk the inheritance hierarchy until the target class is reached
+        return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns);
+    }
+
+    /**
+     * <p> Tries to determine the type arguments of a class/interface based on a
+     * super parameterized type's type arguments. This method is the inverse of
+     * {@link #getTypeArguments(Type, Class)} which gets a class/interface's
+     * type arguments based on a subtype. It is far more limited in determining
+     * the type arguments for the subject class's type variables in that it can
+     * only determine those parameters that map from the subject {@link Class}
+     * object to the supertype. </p> <p> Example: {@link java.util.TreeSet
+     * TreeSet} sets its parameter as the parameter for
+     * {@link java.util.NavigableSet NavigableSet}, which in turn sets the
+     * parameter of {@link java.util.SortedSet}, which in turn sets the
+     * parameter of {@link Set}, which in turn sets the parameter of
+     * {@link java.util.Collection}, which in turn sets the parameter of
+     * {@link java.util.Iterable}. Since <code>TreeSet</code>'s parameter maps
+     * (indirectly) to <code>Iterable</code>'s parameter, it will be able to
+     * determine that based on the super type <code>Iterable<? extends
+     * Map<Integer,? extends Collection<?>>></code>, the parameter of
+     * <code>TreeSet</code> is <code>? extends Map<Integer,? extends
+     * Collection<?>></code>. </p>
+     *
+     * @param cls the class whose type parameters are to be determined
+     * @param superType the super type from which <code>cls</code>'s type
+     * arguments are to be determined
+     * @return a map of the type assignments that could be determined for the
+     * type variables in each type in the inheritance hierarchy from
+     * <code>type</code> to <code>toClass</code> inclusive.
+     */
+    public static Map<TypeVariable<?>, Type> determineTypeArguments(Class<?> cls,
+            ParameterizedType superType) {
+        Class<?> superClass = getRawType(superType);
+
+        // compatibility check
+        if (!isAssignable(cls, superClass)) {
+            return null;
+        }
+
+        if (cls.equals(superClass)) {
+            return getTypeArguments(superType, superClass, null);
+        }
+
+        // get the next class in the inheritance hierarchy
+        Type midType = getClosestParentType(cls, superClass);
+
+        // can only be a class or a parameterized type
+        if (midType instanceof Class<?>) {
+            return determineTypeArguments((Class<?>) midType, superType);
+        }
+
+        ParameterizedType midParameterizedType = (ParameterizedType) midType;
+        Class<?> midClass = getRawType(midParameterizedType);
+        // get the type variables of the mid class that map to the type
+        // arguments of the super class
+        Map<TypeVariable<?>, Type> typeVarAssigns = determineTypeArguments(midClass, superType);
+        // map the arguments of the mid type to the class type variables
+        mapTypeVariablesToArguments(cls, midParameterizedType, typeVarAssigns);
+
+        return typeVarAssigns;
+    }
+
+    /**
+     * <p> </p>
+     *
+     * @param cls
+     * @param parameterizedType
+     * @param typeVarAssigns
+     */
+    private static <T> void mapTypeVariablesToArguments(Class<T> cls,
+            ParameterizedType parameterizedType, Map<TypeVariable<?>, Type> typeVarAssigns) {
+        // capture the type variables from the owner type that have assignments
+        Type ownerType = parameterizedType.getOwnerType();
+
+        if (ownerType instanceof ParameterizedType) {
+            // recursion to make sure the owner's owner type gets processed
+            mapTypeVariablesToArguments(cls, (ParameterizedType) ownerType, typeVarAssigns);
+        }
+
+        // parameterizedType is a generic interface/class (or it's in the owner
+        // hierarchy of said interface/class) implemented/extended by the class
+        // cls. Find out which type variables of cls are type arguments of
+        // parameterizedType:
+        Type[] typeArgs = parameterizedType.getActualTypeArguments();
+
+        // of the cls's type variables that are arguments of parameterizedType,
+        // find out which ones can be determined from the super type's arguments
+        TypeVariable<?>[] typeVars = getRawType(parameterizedType).getTypeParameters();
+
+        // use List view of type parameters of cls so the contains() method can be used:
+        List<TypeVariable<Class<T>>> typeVarList = Arrays.asList(cls
+                .getTypeParameters());
+
+        for (int i = 0; i < typeArgs.length; i++) {
+            TypeVariable<?> typeVar = typeVars[i];
+            Type typeArg = typeArgs[i];
+
+            // argument of parameterizedType is a type variable of cls
+            if (typeVarList.contains(typeArg)
+            // type variable of parameterizedType has an assignment in
+                    // the super type.
+                    && typeVarAssigns.containsKey(typeVar)) {
+                // map the assignment to the cls's type variable
+                typeVarAssigns.put((TypeVariable<?>) typeArg, typeVarAssigns.get(typeVar));
+            }
+        }
+    }
+
+    /**
+     * <p> Closest parent type? Closest to what? The closest parent type to the
+     * super class specified by <code>superClass</code>. </p>
+     *
+     * @param cls
+     * @param superClass
+     * @return
+     */
+    private static Type getClosestParentType(Class<?> cls, Class<?> superClass) {
+        // only look at the interfaces if the super class is also an interface
+        if (superClass.isInterface()) {
+            // get the generic interfaces of the subject class
+            Type[] interfaceTypes = cls.getGenericInterfaces();
+            // will hold the best generic interface match found
+            Type genericInterface = null;
+
+            // find the interface closest to the super class
+            for (int i = 0; i < interfaceTypes.length; i++) {
+                Type midType = interfaceTypes[i];
+                Class<?> midClass = null;
+
+                if (midType instanceof ParameterizedType) {
+                    midClass = getRawType((ParameterizedType) midType);
+                } else if (midType instanceof Class<?>) {
+                    midClass = (Class<?>) midType;
+                } else {
+                    throw new IllegalStateException("Unexpected generic"
+                            + " interface type found: " + midType);
+                }
+
+                // check if this interface is further up the inheritance chain
+                // than the previously found match
+                if (isAssignable(midClass, superClass)
+                        && isAssignable((Type) genericInterface, (Type) midClass)) {
+                    genericInterface = midType;
+                }
+            }
+
+            // found a match?
+            if (genericInterface != null) {
+                return genericInterface;
+            }
+        }
+
+        // none of the interfaces were descendants of the target class, so the
+        // super class has to be one, instead
+        return cls.getGenericSuperclass();
+    }
+
+    /**
+     * <p> Checks if the given value can be assigned to the target type
+     * following the Java generics rules. </p>
+     *
+     * @param value
+     * @param type
+     * @return true of <code>value</code> is an instance of <code>type</code>.
+     */
+    public static boolean isInstance(Object value, Type type) {
+        if (type == null) {
+            return false;
+        }
+
+        return value == null ? !(type instanceof Class<?>) || !((Class<?>) type).isPrimitive()
+                : isAssignable(value.getClass(), type, null);
+    }
+
+    /**
+     * <p> This method strips out the redundant upper bound types in type
+     * variable types and wildcard types (or it would with wildcard types if
+     * multiple upper bounds were allowed). </p> <p> Example: with the variable
+     * type declaration:
+     *
+     * <pre> &lt;K extends java.util.Collection&lt;String&gt; &amp;
+     * java.util.List&lt;String&gt;&gt; </pre>
+     *
+     * since <code>List</code> is a subinterface of <code>Collection</code>,
+     * this method will return the bounds as if the declaration had been:
+     *
+     * <pre> &lt;K extends java.util.List&lt;String&gt;&gt; </pre>
+     *
+     * </p>
+     *
+     * @param bounds an array of types representing the upper bounds of either
+     * <code>WildcardType</code> or <code>TypeVariable</code>.
+     * @return an array containing the values from <code>bounds</code> minus the
+     * redundant types.
+     */
+    public static Type[] normalizeUpperBounds(Type[] bounds) {
+        // don't bother if there's only one (or none) type
+        if (bounds.length < 2) {
+            return bounds;
+        }
+
+        Set<Type> types = new HashSet<Type>(bounds.length);
+
+        for (Type type1 : bounds) {
+            boolean subtypeFound = false;
+
+            for (Type type2 : bounds) {
+                if (type1 != type2 && isAssignable(type2, type1, null)) {
+                    subtypeFound = true;
+                    break;
+                }
+            }
+
+            if (!subtypeFound) {
+                types.add(type1);
+            }
+        }
+
+        return types.toArray(new Type[0]);
+    }
+
+    /**
+     * <p> Returns an array containing the sole type of {@link Object} if
+     * {@link TypeVariable#getBounds()} returns an empty array. Otherwise, it
+     * returns the result of <code>TypeVariable.getBounds()</code> passed into
+     * {@link normalizeUpperBounds}. </p>
+     *
+     * @param typeVariable the subject type variable
+     * @return a non-empty array containing the bounds of the type variable.
+     */
+    public static Type[] getImplicitBounds(TypeVariable<?> typeVariable) {
+        Type[] bounds = typeVariable.getBounds();
+
+        return bounds.length == 0 ? new Type[] { Object.class } : normalizeUpperBounds(bounds);
+    }
+
+    /**
+     * <p> Returns an array containing the sole value of {@link Object} if
+     * {@link WildcardType#getUpperBounds()} returns an empty array. Otherwise,
+     * it returns the result of <code>WildcardType.getUpperBounds()</code>
+     * passed into {@link normalizeUpperBounds}. </p>
+     *
+     * @param wildcardType the subject wildcard type
+     * @return a non-empty array containing the upper bounds of the wildcard
+     * type.
+     */
+    public static Type[] getImplicitUpperBounds(WildcardType wildcardType) {
+        Type[] bounds = wildcardType.getUpperBounds();
+
+        return bounds.length == 0 ? new Type[] { Object.class } : normalizeUpperBounds(bounds);
+    }
+
+    /**
+     * <p> Returns an array containing a single value of <code>null</code> if
+     * {@link WildcardType#getLowerBounds()} returns an empty array. Otherwise,
+     * it returns the result of <code>WildcardType.getLowerBounds()</code>. </p>
+     *
+     * @param type the subject wildcard type
+     * @return a non-empty array containing the lower bounds of the wildcard
+     * type.
+     */
+    public static Type[] getImplicitLowerBounds(WildcardType wildcardType) {
+        Type[] bounds = wildcardType.getLowerBounds();
+
+        return bounds.length == 0 ? new Type[] { null } : bounds;
+    }
+
+    /**
+     * <p> Determines whether or not specified types satisfy the bounds of their
+     * mapped type variables. When a type parameter extends another (such as
+     * <code><T, S extends T></code>), uses another as a type parameter (such as
+     * <code><T, S extends Comparable<T></code>), or otherwise depends on
+     * another type variable to be specified, the dependencies must be included
+     * in <code>typeVarAssigns</code>. </p>
+     *
+     * @param typeVarAssigns specifies the potential types to be assigned to the
+     * type variables.
+     * @return whether or not the types can be assigned to their respective type
+     * variables.
+     */
+    public static boolean typesSatisfyVariables(Map<TypeVariable<?>, Type> typeVarAssigns) {
+        // all types must be assignable to all the bounds of the their mapped
+        // type variable.
+        for (Map.Entry<TypeVariable<?>, Type> entry : typeVarAssigns.entrySet()) {
+            TypeVariable<?> typeVar = entry.getKey();
+            Type type = entry.getValue();
+
+            for (Type bound : getImplicitBounds(typeVar)) {
+                if (!isAssignable(type, substituteTypeVariables(bound, typeVarAssigns),
+                        typeVarAssigns)) {
+                    return false;
+                }
+            }
+        }
+
+        return true;
+    }
+
+    /**
+     * <p> Type-checking method of convenience. </p>
+     *
+     * @param parameterizedType
+     * @return
+     */
+    private static Class<?> getRawType(ParameterizedType parameterizedType) {
+        Type rawType = parameterizedType.getRawType();
+
+        // check if raw type is a Class object
+        // not currently necessary, but since the return type is Type instead of
+        // Class, there's enough reason to believe that future versions of Java
+        // may return other Type implementations. And type-safety checking is
+        // rarely a bad idea.
+        if (!(rawType instanceof Class<?>)) {
+            throw new IllegalStateException("Wait... What!? Type of rawType: " + rawType);
+        }
+
+        return (Class<?>) rawType;
+    }
+
+    /**
+     * <p> Get the raw type of a Java type, given its context. Primarily for use
      * with {@link TypeVariable}s and {@link GenericArrayType}s, or when you do
      * not know the runtime type of <code>type</code>: if you know you have a
      * {@link Class} instance, it is already raw; if you know you have a
-     * {@link ParameterizedType}, its raw type is only a method call away.
-     * @param enclosingType context
-     * @param type to read
-     * @return Class<?>
+     * {@link ParameterizedType}, its raw type is only a method call away. </p>
+     *
+     * @param type to resolve
+     * @param assigningType type to be resolved against
+     * @return the resolved <code>Class</code> object or <code>null</code> if
+     * the type could not be resolved
      */
-    // original code taken from commons [proxy]'s 2.0 branch, then kneaded until firm
-    public static Class<?> getRawType(Type enclosingType, Type type) {
+    public static Class<?> getRawType(Type type, Type assigningType) {
         if (type instanceof Class<?>) {
             // it is raw, no problem
             return (Class<?>) type;
         }
+
         if (type instanceof ParameterizedType) {
             // simple enough to get the raw type of a ParameterizedType
-            return (Class<?>) ((ParameterizedType) type).getRawType();
+            return getRawType((ParameterizedType) type);
         }
+
         if (type instanceof TypeVariable<?>) {
-            Validate.notNull(enclosingType,
-                    "Cannot get raw type of TypeVariable without enclosing type");
-            // resolve the variable against the enclosing type, hope for the best (casting)
-            return (Class<?>) resolveVariable(enclosingType, (TypeVariable<?>) type);
+            if (assigningType == null) {
+                return null;
+            }
+
+            // get the entity declaring this type variable
+            Object genericDeclaration = ((TypeVariable<?>) type).getGenericDeclaration();
+
+            // can't get the raw type of a method- or constructor-declared type
+            // variable
+            if (!(genericDeclaration instanceof Class<?>)) {
+                return null;
+            }
+
+            // get the type arguments for the declaring class/interface based
+            // on the enclosing type
+            Map<TypeVariable<?>, Type> typeVarAssigns = getTypeArguments(assigningType,
+                    (Class<?>) genericDeclaration);
+
+            // enclosingType has to be a subclass (or subinterface) of the
+            // declaring type
+            if (typeVarAssigns == null) {
+                return null;
+            }
+
+            // get the argument assigned to this type variable
+            Type typeArgument = typeVarAssigns.get(type);
+
+            if (typeArgument == null) {
+                return null;
+            }
+
+            // get the argument for this type variable
+            return getRawType(typeArgument, assigningType);
         }
+
         if (type instanceof GenericArrayType) {
-            Validate.notNull(enclosingType,
-                    "Cannot get raw type of GenericArrayType without enclosing type");
-            // not included in original code, but not too difficult:  just have to get raw component type...
-            Class<?> rawComponentType = getRawType(enclosingType, ((GenericArrayType) type)
-                    .getGenericComponentType());
-            // ...and know how to reflectively create array types, uncommon but not unheard of:
+            // get raw component type
+            Class<?> rawComponentType = getRawType(((GenericArrayType) type)
+                    .getGenericComponentType(), assigningType);
+
+            // create array type from raw component type and return its class
             return Array.newInstance(rawComponentType, 0).getClass();
         }
-        throw new IllegalArgumentException(String.valueOf(type));
-    }
 
-    /**
-     * We plan to return Class<?> from the top-level call, as evidenced by the
-     * cast in the above method, but to handle recursion and falling back up the
-     * graph, as it were, return Type
-     * @param enclosingType
-     * @param typeVar
-     * @return Type resolved
-     */
-    // original code taken from commons [proxy]'s 2.0 branch, then kneaded until firm
-    private static Type resolveVariable(Type enclosingType, TypeVariable<?> typeVar) {
-        if (enclosingType instanceof ParameterizedType) {
-            ParameterizedType parameterizedEnclosingType = (ParameterizedType) enclosingType;
-            TypeVariable<?>[] typeVariables = getRawType(null,
-                    parameterizedEnclosingType.getRawType()).getTypeParameters();
-            //look for the matching variable:
-            for (int i = 0; i < typeVariables.length; i++) {
-                if (typeVariables[i].equals(typeVar)) {
-                    return parameterizedEnclosingType.getActualTypeArguments()[i];
-                }
-            }
-            //otherwise recurse to try against raw class
-            Type result = resolveVariable(parameterizedEnclosingType.getRawType(), typeVar);
-            //unroll variable if returned
-            if (result instanceof TypeVariable<?>) {
-                return resolveVariable(enclosingType, (TypeVariable<?>) result);
-            }
-            return result;
-        }
-        if (enclosingType instanceof Class<?>) {
-            Class<?> enclosingClass = (Class<?>) enclosingType;
-            Type result = null;
-            Type genericSuperclass = enclosingClass.getGenericSuperclass();
-            if (genericSuperclass != null && !Object.class.equals(genericSuperclass)) {
-                result = resolveVariable(genericSuperclass, typeVar);
-            }
-            if (result == null) {
-                for (Type genericInterface : enclosingClass.getGenericInterfaces()) {
-                    result = resolveVariable(genericInterface, typeVar);
-                    if (result != null) {
-                        break;
-                    }
-                }
-            }
-            if (result != null) {
-                return result;
-            }
+        // (hand-waving) this is not the method you're looking for
+        if (type instanceof WildcardType) {
+            return null;
         }
-        throw new IllegalArgumentException(String.valueOf(typeVar));
+
+        throw new IllegalArgumentException("unknown type: " + type);
     }
 
 }

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java?rev=963845&r1=963844&r2=963845&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java Tue Jul 13 19:56:45 2010
@@ -16,74 +16,611 @@
  */
 package org.apache.commons.lang3.reflect;
 
-import static junit.framework.Assert.*;
-
-import java.lang.reflect.Field;
 import java.lang.reflect.TypeVariable;
 import java.util.List;
 
-import org.apache.commons.lang3.reflect.testbed.*;
-import org.junit.Before;
 import org.junit.Test;
 
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.TreeSet;
+
+import junit.framework.Assert;
+
+import org.apache.commons.lang3.StringEscapeUtils;
+import org.apache.commons.lang3.reflect.testbed.Foo;
+import org.apache.commons.lang3.reflect.testbed.GenericParent;
+import org.apache.commons.lang3.reflect.testbed.GenericTypeHolder;
+import org.apache.commons.lang3.reflect.testbed.StringParameterizedChild;
+
 /**
  * Test TypeUtils
- * @author mbenson
+ * @author David M. Sledge
  * @version $Id$
  */
-public class TypeUtilsTest {
-    private Field stringParentField;
-    private Field integerParentField;
-    private Field foosField;
-    private Field barParentsField;
-    private TypeVariable<?> genericParentT;
-    private TypeVariable<?> listType;
-    private TypeVariable<?> iterableType;
+@SuppressWarnings({ "unchecked", "unused" })
+//raw types, where used, are used purposely
+public class TypeUtilsTest<B> {
+
+    public interface This<K, V> {
+    }
 
-    @Before
-    public void setup() throws Exception {
-        stringParentField = GenericTypeHolder.class.getDeclaredField("stringParent");
-        integerParentField = GenericTypeHolder.class.getDeclaredField("integerParent");
-        foosField = GenericTypeHolder.class.getDeclaredField("foos");
-        barParentsField = GenericTypeHolder.class.getDeclaredField("barParents");
-        genericParentT = GenericParent.class.getTypeParameters()[0];
-        listType = List.class.getTypeParameters()[0];
-        iterableType = Iterable.class.getTypeParameters()[0];
+    public class That<K, V> implements This<K, V> {
+    }
+
+    public interface And<K, V> extends This<Number, Number> {
+    }
+
+    public class The<K, V> extends That<Number, Number> implements And<String, String> {
+    }
+
+    public class Other<T> implements This<String, T> {
+    }
+
+    public class Thing<Q> extends Other<B> {
+    }
+
+    public class Tester implements This<String, B> {
+    }
+
+    public This<String, String> dis;
+
+    public That<String, String> dat;
+
+    public The<String, String> da;
+
+    public Other<String> uhder;
+
+    public Thing ding;
+
+    public TypeUtilsTest<String>.Tester tester;
+
+    public Tester tester2;
+
+    public TypeUtilsTest<String>.That<String, String> dat2;
+
+    public TypeUtilsTest<Number>.That<String, String> dat3;
+
+    public Comparable<? extends Integer>[] intWildcardComparable;
+
+    public static Comparable<String> stringComparable;
+
+    public static Comparable<URI> uriComparable;
+
+    public static Comparable<Integer> intComparable;
+
+    public static Comparable<Long> longComparable;
+
+    public static URI uri;
+
+    public void dummyMethod(List list0, List<Object> list1, List<?> list2,
+            List<? super Object> list3, List<String> list4, List<? extends String> list5,
+            List<? super String> list6, List[] list7, List<Object>[] list8, List<?>[] list9,
+            List<? super Object>[] list10, List<String>[] list11, List<? extends String>[] list12,
+            List<? super String>[] list13) {
+    }
+
+    @Test
+    public void testIsAssignable() throws SecurityException, NoSuchMethodException,
+            NoSuchFieldException {
+        List list0 = null;
+        List<Object> list1 = null;
+        List<?> list2 = null;
+        List<? super Object> list3 = null;
+        List<String> list4 = null;
+        List<? extends String> list5 = null;
+        List<? super String> list6 = null;
+        List[] list7 = null;
+        List<Object>[] list8 = null;
+        List<?>[] list9 = null;
+        List<? super Object>[] list10 = null;
+        List<String>[] list11 = null;
+        List<? extends String>[] list12 = null;
+        List<? super String>[] list13;
+        Class<?> clazz = getClass();
+        Method method = clazz.getMethod("dummyMethod", List.class, List.class, List.class,
+                List.class, List.class, List.class, List.class, List[].class, List[].class,
+                List[].class, List[].class, List[].class, List[].class, List[].class);
+        Type[] types = method.getGenericParameterTypes();
+        list0 = list0;
+        delegateBooleanAssertion(types, 0, 0, true);
+        list1 = list0;
+        delegateBooleanAssertion(types, 0, 1, true);
+        list0 = list1;
+        delegateBooleanAssertion(types, 1, 0, true);
+        list2 = list0;
+        delegateBooleanAssertion(types, 0, 2, true);
+        list0 = list2;
+        delegateBooleanAssertion(types, 2, 0, true);
+        list3 = list0;
+        delegateBooleanAssertion(types, 0, 3, true);
+        list0 = list3;
+        delegateBooleanAssertion(types, 3, 0, true);
+        list4 = list0;
+        delegateBooleanAssertion(types, 0, 4, true);
+        list0 = list4;
+        delegateBooleanAssertion(types, 4, 0, true);
+        list5 = list0;
+        delegateBooleanAssertion(types, 0, 5, true);
+        list0 = list5;
+        delegateBooleanAssertion(types, 5, 0, true);
+        list6 = list0;
+        delegateBooleanAssertion(types, 0, 6, true);
+        list0 = list6;
+        delegateBooleanAssertion(types, 6, 0, true);
+        list1 = list1;
+        delegateBooleanAssertion(types, 1, 1, true);
+        list2 = list1;
+        delegateBooleanAssertion(types, 1, 2, true);
+        list1 = (List<Object>) list2;
+        delegateBooleanAssertion(types, 2, 1, false);
+        list3 = list1;
+        delegateBooleanAssertion(types, 1, 3, true);
+        list1 = (List<Object>) list3;
+        delegateBooleanAssertion(types, 3, 1, false);
+        // list4 = list1;
+        delegateBooleanAssertion(types, 1, 4, false);
+        // list1 = list4;
+        delegateBooleanAssertion(types, 4, 1, false);
+        // list5 = list1;
+        delegateBooleanAssertion(types, 1, 5, false);
+        // list1 = list5;
+        delegateBooleanAssertion(types, 5, 1, false);
+        list6 = list1;
+        delegateBooleanAssertion(types, 1, 6, true);
+        list1 = (List<Object>) list6;
+        delegateBooleanAssertion(types, 6, 1, false);
+        list2 = list2;
+        delegateBooleanAssertion(types, 2, 2, true);
+        list2 = list3;
+        delegateBooleanAssertion(types, 2, 3, false);
+        list2 = list4;
+        delegateBooleanAssertion(types, 3, 2, true);
+        list3 = (List<? super Object>) list2;
+        delegateBooleanAssertion(types, 2, 4, false);
+        list2 = list5;
+        delegateBooleanAssertion(types, 4, 2, true);
+        list4 = (List<String>) list2;
+        delegateBooleanAssertion(types, 2, 5, false);
+        list2 = list6;
+        delegateBooleanAssertion(types, 5, 2, true);
+        list5 = (List<? extends String>) list2;
+        delegateBooleanAssertion(types, 2, 6, false);
+        list3 = list3;
+        delegateBooleanAssertion(types, 6, 2, true);
+        list6 = (List<? super String>) list2;
+        delegateBooleanAssertion(types, 3, 3, true);
+        // list4 = list3;
+        delegateBooleanAssertion(types, 3, 4, false);
+        // list3 = list4;
+        delegateBooleanAssertion(types, 4, 3, false);
+        // list5 = list3;
+        delegateBooleanAssertion(types, 3, 5, false);
+        // list3 = list5;
+        delegateBooleanAssertion(types, 5, 3, false);
+        list6 = list3;
+        delegateBooleanAssertion(types, 3, 6, true);
+        list3 = (List<? super Object>) list6;
+        delegateBooleanAssertion(types, 6, 3, false);
+        list4 = list4;
+        delegateBooleanAssertion(types, 4, 4, true);
+        list5 = list4;
+        delegateBooleanAssertion(types, 4, 5, true);
+        list4 = (List<String>) list5;
+        delegateBooleanAssertion(types, 5, 4, false);
+        list6 = list4;
+        delegateBooleanAssertion(types, 4, 6, true);
+        list4 = (List<String>) list6;
+        delegateBooleanAssertion(types, 6, 4, false);
+        list5 = list5;
+        delegateBooleanAssertion(types, 5, 5, true);
+        list6 = (List<? super String>) list5;
+        delegateBooleanAssertion(types, 5, 6, false);
+        list5 = (List<? extends String>) list6;
+        delegateBooleanAssertion(types, 6, 5, false);
+        list6 = list6;
+        delegateBooleanAssertion(types, 6, 6, true);
+
+        list7 = list7;
+        delegateBooleanAssertion(types, 7, 7, true);
+        list8 = list7;
+        delegateBooleanAssertion(types, 7, 8, true);
+        list7 = list8;
+        delegateBooleanAssertion(types, 8, 7, true);
+        list9 = list7;
+        delegateBooleanAssertion(types, 7, 9, true);
+        list7 = list9;
+        delegateBooleanAssertion(types, 9, 7, true);
+        list10 = list7;
+        delegateBooleanAssertion(types, 7, 10, true);
+        list7 = list10;
+        delegateBooleanAssertion(types, 10, 7, true);
+        list11 = list7;
+        delegateBooleanAssertion(types, 7, 11, true);
+        list7 = list11;
+        delegateBooleanAssertion(types, 11, 7, true);
+        list12 = list7;
+        delegateBooleanAssertion(types, 7, 12, true);
+        list7 = list12;
+        delegateBooleanAssertion(types, 12, 7, true);
+        list13 = list7;
+        delegateBooleanAssertion(types, 7, 13, true);
+        list7 = list13;
+        delegateBooleanAssertion(types, 13, 7, true);
+        list8 = list8;
+        delegateBooleanAssertion(types, 8, 8, true);
+        list9 = list8;
+        delegateBooleanAssertion(types, 8, 9, true);
+        list8 = (List<Object>[]) list9;
+        delegateBooleanAssertion(types, 9, 8, false);
+        list10 = list8;
+        delegateBooleanAssertion(types, 8, 10, true);
+        list8 = (List<Object>[]) list10;
+        delegateBooleanAssertion(types, 10, 8, false);
+        // list11 = list8;
+        delegateBooleanAssertion(types, 8, 11, false);
+        // list8 = list11;
+        delegateBooleanAssertion(types, 11, 8, false);
+        // list12 = list8;
+        delegateBooleanAssertion(types, 8, 12, false);
+        // list8 = list12;
+        delegateBooleanAssertion(types, 12, 8, false);
+        list13 = list8;
+        delegateBooleanAssertion(types, 8, 13, true);
+        list8 = (List<Object>[]) list13;
+        delegateBooleanAssertion(types, 13, 8, false);
+        list9 = list9;
+        delegateBooleanAssertion(types, 9, 9, true);
+        list10 = (List<? super Object>[]) list9;
+        delegateBooleanAssertion(types, 9, 10, false);
+        list9 = list10;
+        delegateBooleanAssertion(types, 10, 9, true);
+        list11 = (List<String>[]) list9;
+        delegateBooleanAssertion(types, 9, 11, false);
+        list9 = list11;
+        delegateBooleanAssertion(types, 11, 9, true);
+        list12 = (List<? extends String>[]) list9;
+        delegateBooleanAssertion(types, 9, 12, false);
+        list9 = list12;
+        delegateBooleanAssertion(types, 12, 9, true);
+        list13 = (List<? super String>[]) list9;
+        delegateBooleanAssertion(types, 9, 13, false);
+        list9 = list13;
+        delegateBooleanAssertion(types, 13, 9, true);
+        list10 = list10;
+        delegateBooleanAssertion(types, 10, 10, true);
+        // list11 = list10;
+        delegateBooleanAssertion(types, 10, 11, false);
+        // list10 = list11;
+        delegateBooleanAssertion(types, 11, 10, false);
+        // list12 = list10;
+        delegateBooleanAssertion(types, 10, 12, false);
+        // list10 = list12;
+        delegateBooleanAssertion(types, 12, 10, false);
+        list13 = list10;
+        delegateBooleanAssertion(types, 10, 13, true);
+        list10 = (List<? super Object>[]) list13;
+        delegateBooleanAssertion(types, 13, 10, false);
+        list11 = list11;
+        delegateBooleanAssertion(types, 11, 11, true);
+        list12 = list11;
+        delegateBooleanAssertion(types, 11, 12, true);
+        list11 = (List<String>[]) list12;
+        delegateBooleanAssertion(types, 12, 11, false);
+        list13 = list11;
+        delegateBooleanAssertion(types, 11, 13, true);
+        list11 = (List<String>[]) list13;
+        delegateBooleanAssertion(types, 13, 11, false);
+        list12 = list12;
+        delegateBooleanAssertion(types, 12, 12, true);
+        list13 = (List<? super String>[]) list12;
+        delegateBooleanAssertion(types, 12, 13, false);
+        list12 = (List<? extends String>[]) list13;
+        delegateBooleanAssertion(types, 13, 12, false);
+        list13 = list13;
+        delegateBooleanAssertion(types, 13, 13, true);
+        Type disType = getClass().getField("dis").getGenericType();
+        // Reporter.log( ( ( ParameterizedType ) disType
+        // ).getOwnerType().getClass().toString() );
+        Type datType = getClass().getField("dat").getGenericType();
+        Type daType = getClass().getField("da").getGenericType();
+        Type uhderType = getClass().getField("uhder").getGenericType();
+        Type dingType = getClass().getField("ding").getGenericType();
+        Type testerType = getClass().getField("tester").getGenericType();
+        Type tester2Type = getClass().getField("tester2").getGenericType();
+        Type dat2Type = getClass().getField("dat2").getGenericType();
+        Type dat3Type = getClass().getField("dat3").getGenericType();
+        dis = dat;
+        Assert.assertTrue(TypeUtils.isAssignable(datType, disType));
+        // dis = da;
+        Assert.assertFalse(TypeUtils.isAssignable(daType, disType));
+        dis = uhder;
+        Assert.assertTrue(TypeUtils.isAssignable(uhderType, disType));
+        dis = ding;
+        Assert.assertTrue("WRONG!", TypeUtils.isAssignable(dingType, disType));
+        dis = tester;
+        Assert.assertTrue(TypeUtils.isAssignable(testerType, disType));
+        // dis = tester2;
+        Assert.assertFalse(TypeUtils.isAssignable(tester2Type, disType));
+        // dat = dat2;
+        Assert.assertFalse(TypeUtils.isAssignable(dat2Type, datType));
+        // dat2 = dat;
+        Assert.assertFalse(TypeUtils.isAssignable(datType, dat2Type));
+        // dat = dat3;
+        Assert.assertFalse(TypeUtils.isAssignable(dat3Type, datType));
+        char ch = 0;
+        boolean bo = false;
+        byte by = 0;
+        short sh = 0;
+        int in = 0;
+        long lo = 0;
+        float fl = 0;
+        double du = 0;
+        du = ch;
+        Assert.assertTrue(TypeUtils.isAssignable(char.class, double.class));
+        du = by;
+        Assert.assertTrue(TypeUtils.isAssignable(byte.class, double.class));
+        du = sh;
+        Assert.assertTrue(TypeUtils.isAssignable(short.class, double.class));
+        du = in;
+        Assert.assertTrue(TypeUtils.isAssignable(int.class, double.class));
+        du = lo;
+        Assert.assertTrue(TypeUtils.isAssignable(long.class, double.class));
+        du = fl;
+        Assert.assertTrue(TypeUtils.isAssignable(float.class, double.class));
+        // du = bo;
+        Assert.assertTrue(TypeUtils.isAssignable(int.class, long.class));
+        lo = new Integer(0);
+        Assert.assertTrue(TypeUtils.isAssignable(Integer.class, long.class));
+        // Long lngW = 1;
+        Assert.assertFalse(TypeUtils.isAssignable(int.class, Long.class));
+        // lngW = new Integer( 0 );
+        Assert.assertFalse(TypeUtils.isAssignable(Integer.class, Long.class));
+        in = new Integer(0);
+        Assert.assertTrue(TypeUtils.isAssignable(Integer.class, int.class));
+        Integer inte = in;
+        Assert.assertTrue(TypeUtils.isAssignable(int.class, Integer.class));
+        Assert.assertTrue(TypeUtils.isAssignable(int.class, Number.class));
+        Assert.assertTrue(TypeUtils.isAssignable(int.class, Object.class));
+        Type intComparableType = getClass().getField("intComparable").getGenericType();
+        intComparable = 1;
+        Assert.assertTrue(TypeUtils.isAssignable(int.class, intComparableType));
+        Assert.assertTrue(TypeUtils.isAssignable(int.class, Comparable.class));
+        Serializable ser = 1;
+        Assert.assertTrue(TypeUtils.isAssignable(int.class, Serializable.class));
+        Type longComparableType = getClass().getField("longComparable").getGenericType();
+        // longComparable = 1;
+        Assert.assertFalse(TypeUtils.isAssignable(int.class, longComparableType));
+        // longComparable = new Integer( 0 );
+        Assert.assertFalse(TypeUtils.isAssignable(Integer.class, longComparableType));
+        // int[] ia;
+        // long[] la = ia;
+        Assert.assertFalse(TypeUtils.isAssignable(int[].class, long[].class));
+        Integer[] ia = null;
+        Type caType = getClass().getField("intWildcardComparable").getGenericType();
+        intWildcardComparable = ia;
+        Assert.assertTrue(TypeUtils.isAssignable(Integer[].class, caType));
+        // int[] ina = ia;
+        Assert.assertFalse(TypeUtils.isAssignable(Integer[].class, int[].class));
+        int[] ina = null;
+        Object[] oa;
+        // oa = ina;
+        Assert.assertFalse(TypeUtils.isAssignable(int[].class, Object[].class));
+        oa = new Integer[0];
+        Assert.assertTrue(TypeUtils.isAssignable(Integer[].class, Object[].class));
+        Type bClassType = AClass.class.getField("bClass").getGenericType();
+        Type cClassType = AClass.class.getField("cClass").getGenericType();
+        Type dClassType = AClass.class.getField("dClass").getGenericType();
+        Type eClassType = AClass.class.getField("eClass").getGenericType();
+        Type fClassType = AClass.class.getField("fClass").getGenericType();
+        AClass aClass = new AClass(new AAClass<String>());
+        aClass.bClass = aClass.cClass;
+        Assert.assertTrue(TypeUtils.isAssignable(cClassType, bClassType));
+        aClass.bClass = aClass.dClass;
+        Assert.assertTrue(TypeUtils.isAssignable(dClassType, bClassType));
+        aClass.bClass = aClass.eClass;
+        Assert.assertTrue(TypeUtils.isAssignable(eClassType, bClassType));
+        aClass.bClass = aClass.fClass;
+        Assert.assertTrue(TypeUtils.isAssignable(fClassType, bClassType));
+        aClass.cClass = aClass.dClass;
+        Assert.assertTrue(TypeUtils.isAssignable(dClassType, cClassType));
+        aClass.cClass = aClass.eClass;
+        Assert.assertTrue(TypeUtils.isAssignable(eClassType, cClassType));
+        aClass.cClass = aClass.fClass;
+        Assert.assertTrue(TypeUtils.isAssignable(fClassType, cClassType));
+        aClass.dClass = aClass.eClass;
+        Assert.assertTrue(TypeUtils.isAssignable(eClassType, dClassType));
+        aClass.dClass = aClass.fClass;
+        Assert.assertTrue(TypeUtils.isAssignable(fClassType, dClassType));
+        aClass.eClass = aClass.fClass;
+        Assert.assertTrue(TypeUtils.isAssignable(fClassType, eClassType));
+    }
+
+    public void delegateBooleanAssertion(Type[] types, int i2, int i1, boolean expected) {
+        Type type1 = types[i1];
+        Type type2 = types[i2];
+        boolean isAssignable = TypeUtils.isAssignable(type2, type1);
+
+        if (expected) {
+            Assert.assertTrue("[" + i1 + ", " + i2 + "]: From "
+                    + StringEscapeUtils.escapeHtml4(String.valueOf(type2)) + " to "
+                    + StringEscapeUtils.escapeHtml4(String.valueOf(type1)), isAssignable);
+        } else {
+            Assert.assertFalse("[" + i1 + ", " + i2 + "]: From "
+                    + StringEscapeUtils.escapeHtml4(String.valueOf(type2)) + " to "
+                    + StringEscapeUtils.escapeHtml4(String.valueOf(type1)), isAssignable);
+        }
     }
 
     @Test
-    public void testGetRawTypeClass() throws Exception {
-        assertEquals(GenericParent.class, TypeUtils.getRawType(null, GenericParent.class));
+    public void testIsInstance() throws SecurityException, NoSuchFieldException {
+        Type intComparableType = getClass().getField("intComparable").getGenericType();
+        Type uriComparableType = getClass().getField("uriComparable").getGenericType();
+        intComparable = 1;
+        Assert.assertTrue(TypeUtils.isInstance(1, intComparableType));
+        // uriComparable = 1;
+        Assert.assertFalse(TypeUtils.isInstance(1, uriComparableType));
     }
 
     @Test
-    public void testGetRawTypeParameterizedType() throws Exception {
-        assertEquals(GenericParent.class, TypeUtils.getRawType(GenericTypeHolder.class,
-                stringParentField.getGenericType()));
-        assertEquals(GenericParent.class, TypeUtils.getRawType(GenericTypeHolder.class,
-                integerParentField.getGenericType()));
-        assertEquals(List.class, TypeUtils.getRawType(GenericTypeHolder.class, foosField
-                .getGenericType()));
+    public void testGetTypeArguments() {
+        Map<TypeVariable<?>, Type> typeVarAssigns;
+        TypeVariable<?> treeSetTypeVar;
+        Type typeArg;
+
+        typeVarAssigns = TypeUtils.getTypeArguments(Integer.class, Comparable.class);
+        treeSetTypeVar = Comparable.class.getTypeParameters()[0];
+        Assert.assertTrue("Type var assigns for Comparable from Integer: " + typeVarAssigns,
+                typeVarAssigns.containsKey(treeSetTypeVar));
+        typeArg = typeVarAssigns.get(treeSetTypeVar);
+        Assert.assertEquals("Type argument of Comparable from Integer: " + typeArg, Integer.class,
+                typeVarAssigns.get(treeSetTypeVar));
+
+        typeVarAssigns = TypeUtils.getTypeArguments(int.class, Comparable.class);
+        treeSetTypeVar = Comparable.class.getTypeParameters()[0];
+        Assert.assertTrue("Type var assigns for Comparable from int: " + typeVarAssigns,
+                typeVarAssigns.containsKey(treeSetTypeVar));
+        typeArg = typeVarAssigns.get(treeSetTypeVar);
+        Assert.assertEquals("Type argument of Comparable from int: " + typeArg, Integer.class,
+                typeVarAssigns.get(treeSetTypeVar));
+
+        Collection<Integer> col = Arrays.asList(new Integer[0]);
+        typeVarAssigns = TypeUtils.getTypeArguments(List.class, Collection.class);
+        treeSetTypeVar = Comparable.class.getTypeParameters()[0];
+        Assert.assertFalse("Type var assigns for Collection from List: " + typeVarAssigns,
+                typeVarAssigns.containsKey(treeSetTypeVar));
+
+        typeVarAssigns = TypeUtils.getTypeArguments(AAAClass.BBBClass.class, AAClass.BBClass.class);
+        Assert.assertTrue(typeVarAssigns.size() == 2);
+        Assert.assertEquals(String.class, typeVarAssigns.get(AAClass.class.getTypeParameters()[0]));
+        Assert.assertEquals(String.class, typeVarAssigns.get(AAClass.BBClass.class.getTypeParameters()[0]));
     }
 
     @Test
-    public void testGetRawTypeTypeVariable() throws Exception {
-        assertEquals(String.class, TypeUtils.getRawType(StringParameterizedChild.class,
-                genericParentT));
-        assertEquals(String.class, TypeUtils.getRawType(stringParentField.getGenericType(),
-                genericParentT));
-        assertEquals(Foo.class, TypeUtils.getRawType(foosField.getGenericType(), iterableType));
-        assertEquals(Foo.class, TypeUtils.getRawType(foosField.getGenericType(), listType));
+    public void testTypesSatisfyVariables() throws SecurityException, NoSuchFieldException,
+            NoSuchMethodException {
+        Map<TypeVariable<?>, Type> typeVarAssigns = new HashMap<TypeVariable<?>, Type>();
+        Integer max = TypeUtilsTest.stub();
+        typeVarAssigns.put(getClass().getMethod("stub").getTypeParameters()[0], Integer.class);
+        Assert.assertTrue(TypeUtils.typesSatisfyVariables(typeVarAssigns));
+        typeVarAssigns.clear();
+        typeVarAssigns.put(getClass().getMethod("stub2").getTypeParameters()[0], Integer.class);
+        Assert.assertTrue(TypeUtils.typesSatisfyVariables(typeVarAssigns));
+        typeVarAssigns.clear();
+        typeVarAssigns.put(getClass().getMethod("stub3").getTypeParameters()[0], Integer.class);
+        Assert.assertTrue(TypeUtils.typesSatisfyVariables(typeVarAssigns));
     }
 
-    @Test(expected = IllegalArgumentException.class)
-    public void testGetRawTypeUnresolvableTypeVariable() {
-        TypeUtils.getRawType(GenericParent.class, genericParentT);
+    @Test
+    public void testDetermineTypeVariableAssignments() throws SecurityException,
+            NoSuchFieldException, NoSuchMethodException {
+        ParameterizedType iterableType = (ParameterizedType) getClass().getField("iterable")
+                .getGenericType();
+        Map<TypeVariable<?>, Type> typeVarAssigns = TypeUtils.determineTypeArguments(TreeSet.class,
+                iterableType);
+        TypeVariable<?> treeSetTypeVar = TreeSet.class.getTypeParameters()[0];
+        Assert.assertTrue(typeVarAssigns.containsKey(treeSetTypeVar));
+        Assert.assertEquals(iterableType.getActualTypeArguments()[0], typeVarAssigns
+                .get(treeSetTypeVar));
     }
 
     @Test
-    public void testGetRawTypeGenericArray() throws Exception {
-        assertEquals(GenericParent[].class, TypeUtils.getRawType(GenericTypeHolder.class,
-                barParentsField.getGenericType()));
+    public void testGetRawType() throws SecurityException, NoSuchFieldException {
+        Type stringParentFieldType = GenericTypeHolder.class.getDeclaredField("stringParent")
+                .getGenericType();
+        Type integerParentFieldType = GenericTypeHolder.class.getDeclaredField("integerParent")
+                .getGenericType();
+        Type foosFieldType = GenericTypeHolder.class.getDeclaredField("foos").getGenericType();
+        Type genericParentT = GenericParent.class.getTypeParameters()[0];
+        Assert.assertEquals(GenericParent.class, TypeUtils.getRawType(stringParentFieldType, null));
+        Assert
+                .assertEquals(GenericParent.class, TypeUtils.getRawType(integerParentFieldType,
+                        null));
+        Assert.assertEquals(List.class, TypeUtils.getRawType(foosFieldType, null));
+        Assert.assertEquals(String.class, TypeUtils.getRawType(genericParentT,
+                StringParameterizedChild.class));
+        Assert.assertEquals(String.class, TypeUtils.getRawType(genericParentT,
+                stringParentFieldType));
+        Assert.assertEquals(Foo.class, TypeUtils.getRawType(Iterable.class.getTypeParameters()[0],
+                foosFieldType));
+        Assert.assertEquals(Foo.class, TypeUtils.getRawType(List.class.getTypeParameters()[0],
+                foosFieldType));
+        Assert.assertNull(TypeUtils.getRawType(genericParentT, GenericParent.class));
+        Assert.assertEquals(GenericParent[].class, TypeUtils.getRawType(GenericTypeHolder.class
+                .getDeclaredField("barParents").getGenericType(), null));
+    }
+
+    public Iterable<? extends Map<Integer, ? extends Collection<?>>> iterable;
+
+    public static <G extends Comparable<G>> G stub() {
+        return null;
+    }
+
+    public static <G extends Comparable<? super G>> G stub2() {
+        return null;
+    }
+
+    public static <T extends Comparable<? extends T>> T stub3() {
+        return null;
     }
 }
+
+class AAClass<T> {
+
+    public class BBClass<S> {
+    }
+}
+
+class AAAClass extends AAClass<String> {
+    public class BBBClass extends BBClass<String> {
+    }
+}
+
+@SuppressWarnings("unchecked")
+//raw types, where used, are used purposely
+class AClass extends AAClass<String>.BBClass<Number> {
+
+    public AClass(AAClass<String> enclosingInstance) {
+        enclosingInstance.super();
+    }
+
+    public class BClass<T> {
+    }
+
+    public class CClass<T> extends BClass {
+    }
+
+    public class DClass<T> extends CClass<T> {
+    }
+
+    public class EClass<T> extends DClass {
+    }
+
+    public class FClass extends EClass<String> {
+    }
+
+    public class GClass<T extends BClass<? extends T> & AInterface<AInterface<? super T>>> {
+    }
+
+    public BClass<Number> bClass;
+
+    public CClass<? extends String> cClass;
+
+    public DClass<String> dClass;
+
+    public EClass<String> eClass;
+
+    public FClass fClass;
+
+    public GClass gClass;
+
+    public interface AInterface<T> {
+    }
+}
\ No newline at end of file



Mime
View raw message