001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.lang3.reflect;
018
019import java.lang.reflect.Array;
020import java.lang.reflect.GenericArrayType;
021import java.lang.reflect.GenericDeclaration;
022import java.lang.reflect.ParameterizedType;
023import java.lang.reflect.Type;
024import java.lang.reflect.TypeVariable;
025import java.lang.reflect.WildcardType;
026import java.util.Arrays;
027import java.util.Collections;
028import java.util.HashMap;
029import java.util.HashSet;
030import java.util.List;
031import java.util.Map;
032import java.util.Objects;
033import java.util.Set;
034
035import org.apache.commons.lang3.ArrayUtils;
036import org.apache.commons.lang3.ClassUtils;
037import org.apache.commons.lang3.ObjectUtils;
038import org.apache.commons.lang3.Validate;
039import org.apache.commons.lang3.builder.Builder;
040
041/**
042 * <p> Utility methods focusing on type inspection, particularly with regard to
043 * generics. </p>
044 *
045 * @since 3.0
046 */
047public class TypeUtils {
048
049    /**
050     * {@link WildcardType} builder.
051     * @since 3.2
052     */
053    public static class WildcardTypeBuilder implements Builder<WildcardType> {
054        /**
055         * Constructor
056         */
057        private WildcardTypeBuilder() {
058        }
059
060        private Type[] upperBounds;
061        private Type[] lowerBounds;
062
063        /**
064         * Specify upper bounds of the wildcard type to build.
065         * @param bounds to set
066         * @return {@code this}
067         */
068        public WildcardTypeBuilder withUpperBounds(final Type... bounds) {
069            this.upperBounds = bounds;
070            return this;
071        }
072
073        /**
074         * Specify lower bounds of the wildcard type to build.
075         * @param bounds to set
076         * @return {@code this}
077         */
078        public WildcardTypeBuilder withLowerBounds(final Type... bounds) {
079            this.lowerBounds = bounds;
080            return this;
081        }
082
083        /**
084         * {@inheritDoc}
085         */
086        @Override
087        public WildcardType build() {
088            return new WildcardTypeImpl(upperBounds, lowerBounds);
089        }
090    }
091
092    /**
093     * GenericArrayType implementation class.
094     * @since 3.2
095     */
096    private static final class GenericArrayTypeImpl implements GenericArrayType {
097        private final Type componentType;
098
099        /**
100         * Constructor
101         * @param componentType of this array type
102         */
103        private GenericArrayTypeImpl(final Type componentType) {
104            this.componentType = componentType;
105        }
106
107        /**
108         * {@inheritDoc}
109         */
110        @Override
111        public Type getGenericComponentType() {
112            return componentType;
113        }
114
115        /**
116         * {@inheritDoc}
117         */
118        @Override
119        public String toString() {
120            return TypeUtils.toString(this);
121        }
122
123        /**
124         * {@inheritDoc}
125         */
126        @Override
127        public boolean equals(final Object obj) {
128            return obj == this || obj instanceof GenericArrayType && TypeUtils.equals(this, (GenericArrayType) obj);
129        }
130
131        /**
132         * {@inheritDoc}
133         */
134        @Override
135        public int hashCode() {
136            int result = 67 << 4;
137            result |= componentType.hashCode();
138            return result;
139        }
140    }
141
142    /**
143     * ParameterizedType implementation class.
144     * @since 3.2
145     */
146    private static final class ParameterizedTypeImpl implements ParameterizedType {
147        private final Class<?> raw;
148        private final Type useOwner;
149        private final Type[] typeArguments;
150
151        /**
152         * Constructor
153         * @param rawClass type
154         * @param useOwner owner type to use, if any
155         * @param typeArguments formal type arguments
156         */
157        private ParameterizedTypeImpl(final Class<?> rawClass, final Type useOwner, final Type[] typeArguments) {
158            this.raw = rawClass;
159            this.useOwner = useOwner;
160            this.typeArguments = Arrays.copyOf(typeArguments, typeArguments.length, Type[].class);
161        }
162
163        /**
164         * {@inheritDoc}
165         */
166        @Override
167        public Type getRawType() {
168            return raw;
169        }
170
171        /**
172         * {@inheritDoc}
173         */
174        @Override
175        public Type getOwnerType() {
176            return useOwner;
177        }
178
179        /**
180         * {@inheritDoc}
181         */
182        @Override
183        public Type[] getActualTypeArguments() {
184            return typeArguments.clone();
185        }
186
187        /**
188         * {@inheritDoc}
189         */
190        @Override
191        public String toString() {
192            return TypeUtils.toString(this);
193        }
194
195        /**
196         * {@inheritDoc}
197         */
198        @Override
199        public boolean equals(final Object obj) {
200            return obj == this || obj instanceof ParameterizedType && TypeUtils.equals(this, ((ParameterizedType) obj));
201        }
202
203        /**
204         * {@inheritDoc}
205         */
206        @Override
207        public int hashCode() {
208            int result = 71 << 4;
209            result |= raw.hashCode();
210            result <<= 4;
211            result |= Objects.hashCode(useOwner);
212            result <<= 8;
213            result |= Arrays.hashCode(typeArguments);
214            return result;
215        }
216    }
217
218    /**
219     * WildcardType implementation class.
220     * @since 3.2
221     */
222    private static final class WildcardTypeImpl implements WildcardType {
223        private final Type[] upperBounds;
224        private final Type[] lowerBounds;
225
226        /**
227         * Constructor
228         * @param upperBounds of this type
229         * @param lowerBounds of this type
230         */
231        private WildcardTypeImpl(final Type[] upperBounds, final Type[] lowerBounds) {
232            this.upperBounds = ObjectUtils.defaultIfNull(upperBounds, ArrayUtils.EMPTY_TYPE_ARRAY);
233            this.lowerBounds = ObjectUtils.defaultIfNull(lowerBounds, ArrayUtils.EMPTY_TYPE_ARRAY);
234        }
235
236        /**
237         * {@inheritDoc}
238         */
239        @Override
240        public Type[] getUpperBounds() {
241            return upperBounds.clone();
242        }
243
244        /**
245         * {@inheritDoc}
246         */
247        @Override
248        public Type[] getLowerBounds() {
249            return lowerBounds.clone();
250        }
251
252        /**
253         * {@inheritDoc}
254         */
255        @Override
256        public String toString() {
257            return TypeUtils.toString(this);
258        }
259
260        /**
261         * {@inheritDoc}
262         */
263        @Override
264        public boolean equals(final Object obj) {
265            return obj == this || obj instanceof WildcardType && TypeUtils.equals(this, (WildcardType) obj);
266        }
267
268        /**
269         * {@inheritDoc}
270         */
271        @Override
272        public int hashCode() {
273            int result = 73 << 8;
274            result |= Arrays.hashCode(upperBounds);
275            result <<= 8;
276            result |= Arrays.hashCode(lowerBounds);
277            return result;
278        }
279    }
280
281    /**
282     * A wildcard instance matching {@code ?}.
283     * @since 3.2
284     */
285    public static final WildcardType WILDCARD_ALL = wildcardType().withUpperBounds(Object.class).build();
286
287    /**
288     * <p>{@code TypeUtils} instances should NOT be constructed in standard
289     * programming. Instead, the class should be used as
290     * {@code TypeUtils.isAssignable(cls, toClass)}.</p> <p>This
291     * constructor is public to permit tools that require a JavaBean instance to
292     * operate.</p>
293     */
294    public TypeUtils() {
295        super();
296    }
297
298    /**
299     * <p>Checks if the subject type may be implicitly cast to the target type
300     * following the Java generics rules. If both types are {@link Class}
301     * objects, the method returns the result of
302     * {@link ClassUtils#isAssignable(Class, Class)}.</p>
303     *
304     * @param type the subject type to be assigned to the target type
305     * @param toType the target type
306     * @return {@code true} if {@code type} is assignable to {@code toType}.
307     */
308    public static boolean isAssignable(final Type type, final Type toType) {
309        return isAssignable(type, toType, null);
310    }
311
312    /**
313     * <p>Checks if the subject type may be implicitly cast to the target type
314     * following the Java generics rules.</p>
315     *
316     * @param type the subject type to be assigned to the target type
317     * @param toType the target type
318     * @param typeVarAssigns optional map of type variable assignments
319     * @return {@code true} if {@code type} is assignable to {@code toType}.
320     */
321    private static boolean isAssignable(final Type type, final Type toType,
322            final Map<TypeVariable<?>, Type> typeVarAssigns) {
323        if (toType == null || toType instanceof Class<?>) {
324            return isAssignable(type, (Class<?>) toType);
325        }
326
327        if (toType instanceof ParameterizedType) {
328            return isAssignable(type, (ParameterizedType) toType, typeVarAssigns);
329        }
330
331        if (toType instanceof GenericArrayType) {
332            return isAssignable(type, (GenericArrayType) toType, typeVarAssigns);
333        }
334
335        if (toType instanceof WildcardType) {
336            return isAssignable(type, (WildcardType) toType, typeVarAssigns);
337        }
338
339        if (toType instanceof TypeVariable<?>) {
340            return isAssignable(type, (TypeVariable<?>) toType, typeVarAssigns);
341        }
342
343        throw new IllegalStateException("found an unhandled type: " + toType);
344    }
345
346    /**
347     * <p>Checks if the subject type may be implicitly cast to the target class
348     * following the Java generics rules.</p>
349     *
350     * @param type the subject type to be assigned to the target type
351     * @param toClass the target class
352     * @return {@code true} if {@code type} is assignable to {@code toClass}.
353     */
354    private static boolean isAssignable(final Type type, final Class<?> toClass) {
355        if (type == null) {
356            // consistency with ClassUtils.isAssignable() behavior
357            return toClass == null || !toClass.isPrimitive();
358        }
359
360        // only a null type can be assigned to null type which
361        // would have cause the previous to return true
362        if (toClass == null) {
363            return false;
364        }
365
366        // all types are assignable to themselves
367        if (toClass.equals(type)) {
368            return true;
369        }
370
371        if (type instanceof Class<?>) {
372            // just comparing two classes
373            return ClassUtils.isAssignable((Class<?>) type, toClass);
374        }
375
376        if (type instanceof ParameterizedType) {
377            // only have to compare the raw type to the class
378            return isAssignable(getRawType((ParameterizedType) type), toClass);
379        }
380
381        // *
382        if (type instanceof TypeVariable<?>) {
383            // if any of the bounds are assignable to the class, then the
384            // type is assignable to the class.
385            for (final Type bound : ((TypeVariable<?>) type).getBounds()) {
386                if (isAssignable(bound, toClass)) {
387                    return true;
388                }
389            }
390
391            return false;
392        }
393
394        // the only classes to which a generic array type can be assigned
395        // are class Object and array classes
396        if (type instanceof GenericArrayType) {
397            return toClass.equals(Object.class)
398                    || toClass.isArray()
399                    && isAssignable(((GenericArrayType) type).getGenericComponentType(), toClass
400                            .getComponentType());
401        }
402
403        // wildcard types are not assignable to a class (though one would think
404        // "? super Object" would be assignable to Object)
405        if (type instanceof WildcardType) {
406            return false;
407        }
408
409        throw new IllegalStateException("found an unhandled type: " + type);
410    }
411
412    /**
413     * <p>Checks if the subject type may be implicitly cast to the target
414     * parameterized type following the Java generics rules.</p>
415     *
416     * @param type the subject type to be assigned to the target type
417     * @param toParameterizedType the target parameterized type
418     * @param typeVarAssigns a map with type variables
419     * @return {@code true} if {@code type} is assignable to {@code toType}.
420     */
421    private static boolean isAssignable(final Type type, final ParameterizedType toParameterizedType,
422            final Map<TypeVariable<?>, Type> typeVarAssigns) {
423        if (type == null) {
424            return true;
425        }
426
427        // only a null type can be assigned to null type which
428        // would have cause the previous to return true
429        if (toParameterizedType == null) {
430            return false;
431        }
432
433        // all types are assignable to themselves
434        if (toParameterizedType.equals(type)) {
435            return true;
436        }
437
438        // get the target type's raw type
439        final Class<?> toClass = getRawType(toParameterizedType);
440        // get the subject type's type arguments including owner type arguments
441        // and supertype arguments up to and including the target class.
442        final Map<TypeVariable<?>, Type> fromTypeVarAssigns = getTypeArguments(type, toClass, null);
443
444        // null means the two types are not compatible
445        if (fromTypeVarAssigns == null) {
446            return false;
447        }
448
449        // compatible types, but there's no type arguments. this is equivalent
450        // to comparing Map< ?, ? > to Map, and raw types are always assignable
451        // to parameterized types.
452        if (fromTypeVarAssigns.isEmpty()) {
453            return true;
454        }
455
456        // get the target type's type arguments including owner type arguments
457        final Map<TypeVariable<?>, Type> toTypeVarAssigns = getTypeArguments(toParameterizedType,
458                toClass, typeVarAssigns);
459
460        // now to check each type argument
461        for (final TypeVariable<?> var : toTypeVarAssigns.keySet()) {
462            final Type toTypeArg = unrollVariableAssignments(var, toTypeVarAssigns);
463            final Type fromTypeArg = unrollVariableAssignments(var, fromTypeVarAssigns);
464
465            if (toTypeArg == null && fromTypeArg instanceof Class) {
466                continue;
467            }
468
469            // parameters must either be absent from the subject type, within
470            // the bounds of the wildcard type, or be an exact match to the
471            // parameters of the target type.
472            if (fromTypeArg != null
473                    && !toTypeArg.equals(fromTypeArg)
474                    && !(toTypeArg instanceof WildcardType && isAssignable(fromTypeArg, toTypeArg,
475                            typeVarAssigns))) {
476                return false;
477            }
478        }
479        return true;
480    }
481
482    /**
483     * Look up {@code var} in {@code typeVarAssigns} <em>transitively</em>,
484     * i.e. keep looking until the value found is <em>not</em> a type variable.
485     * @param typeVariable the type variable to look up
486     * @param typeVarAssigns the map used for the look up
487     * @return Type or {@code null} if some variable was not in the map
488     * @since 3.2
489     */
490    private static Type unrollVariableAssignments(TypeVariable<?> typeVariable,
491        final Map<TypeVariable<?>, Type> typeVarAssigns) {
492        Type result;
493        do {
494            result = typeVarAssigns.get(typeVariable);
495            if (result instanceof TypeVariable<?> && !result.equals(typeVariable)) {
496                typeVariable = (TypeVariable<?>) result;
497                continue;
498            }
499            break;
500        } while (true);
501        return result;
502    }
503
504    /**
505     * <p>Checks if the subject type may be implicitly cast to the target
506     * generic array type following the Java generics rules.</p>
507     *
508     * @param type the subject type to be assigned to the target type
509     * @param toGenericArrayType the target generic array type
510     * @param typeVarAssigns a map with type variables
511     * @return {@code true} if {@code type} is assignable to
512     * {@code toGenericArrayType}.
513     */
514    private static boolean isAssignable(final Type type, final GenericArrayType toGenericArrayType,
515            final Map<TypeVariable<?>, Type> typeVarAssigns) {
516        if (type == null) {
517            return true;
518        }
519
520        // only a null type can be assigned to null type which
521        // would have cause the previous to return true
522        if (toGenericArrayType == null) {
523            return false;
524        }
525
526        // all types are assignable to themselves
527        if (toGenericArrayType.equals(type)) {
528            return true;
529        }
530
531        final Type toComponentType = toGenericArrayType.getGenericComponentType();
532
533        if (type instanceof Class<?>) {
534            final Class<?> cls = (Class<?>) type;
535
536            // compare the component types
537            return cls.isArray()
538                    && isAssignable(cls.getComponentType(), toComponentType, typeVarAssigns);
539        }
540
541        if (type instanceof GenericArrayType) {
542            // compare the component types
543            return isAssignable(((GenericArrayType) type).getGenericComponentType(),
544                    toComponentType, typeVarAssigns);
545        }
546
547        if (type instanceof WildcardType) {
548            // so long as one of the upper bounds is assignable, it's good
549            for (final Type bound : getImplicitUpperBounds((WildcardType) type)) {
550                if (isAssignable(bound, toGenericArrayType)) {
551                    return true;
552                }
553            }
554
555            return false;
556        }
557
558        if (type instanceof TypeVariable<?>) {
559            // probably should remove the following logic and just return false.
560            // type variables cannot specify arrays as bounds.
561            for (final Type bound : getImplicitBounds((TypeVariable<?>) type)) {
562                if (isAssignable(bound, toGenericArrayType)) {
563                    return true;
564                }
565            }
566
567            return false;
568        }
569
570        if (type instanceof ParameterizedType) {
571            // the raw type of a parameterized type is never an array or
572            // generic array, otherwise the declaration would look like this:
573            // Collection[]< ? extends String > collection;
574            return false;
575        }
576
577        throw new IllegalStateException("found an unhandled type: " + type);
578    }
579
580    /**
581     * <p>Checks if the subject type may be implicitly cast to the target
582     * wildcard type following the Java generics rules.</p>
583     *
584     * @param type the subject type to be assigned to the target type
585     * @param toWildcardType the target wildcard type
586     * @param typeVarAssigns a map with type variables
587     * @return {@code true} if {@code type} is assignable to
588     * {@code toWildcardType}.
589     */
590    private static boolean isAssignable(final Type type, final WildcardType toWildcardType,
591            final Map<TypeVariable<?>, Type> typeVarAssigns) {
592        if (type == null) {
593            return true;
594        }
595
596        // only a null type can be assigned to null type which
597        // would have cause the previous to return true
598        if (toWildcardType == null) {
599            return false;
600        }
601
602        // all types are assignable to themselves
603        if (toWildcardType.equals(type)) {
604            return true;
605        }
606
607        final Type[] toUpperBounds = getImplicitUpperBounds(toWildcardType);
608        final Type[] toLowerBounds = getImplicitLowerBounds(toWildcardType);
609
610        if (type instanceof WildcardType) {
611            final WildcardType wildcardType = (WildcardType) type;
612            final Type[] upperBounds = getImplicitUpperBounds(wildcardType);
613            final Type[] lowerBounds = getImplicitLowerBounds(wildcardType);
614
615            for (Type toBound : toUpperBounds) {
616                // if there are assignments for unresolved type variables,
617                // now's the time to substitute them.
618                toBound = substituteTypeVariables(toBound, typeVarAssigns);
619
620                // each upper bound of the subject type has to be assignable to
621                // each
622                // upper bound of the target type
623                for (final Type bound : upperBounds) {
624                    if (!isAssignable(bound, toBound, typeVarAssigns)) {
625                        return false;
626                    }
627                }
628            }
629
630            for (Type toBound : toLowerBounds) {
631                // if there are assignments for unresolved type variables,
632                // now's the time to substitute them.
633                toBound = substituteTypeVariables(toBound, typeVarAssigns);
634
635                // each lower bound of the target type has to be assignable to
636                // each
637                // lower bound of the subject type
638                for (final Type bound : lowerBounds) {
639                    if (!isAssignable(toBound, bound, typeVarAssigns)) {
640                        return false;
641                    }
642                }
643            }
644            return true;
645        }
646
647        for (final Type toBound : toUpperBounds) {
648            // if there are assignments for unresolved type variables,
649            // now's the time to substitute them.
650            if (!isAssignable(type, substituteTypeVariables(toBound, typeVarAssigns),
651                    typeVarAssigns)) {
652                return false;
653            }
654        }
655
656        for (final Type toBound : toLowerBounds) {
657            // if there are assignments for unresolved type variables,
658            // now's the time to substitute them.
659            if (!isAssignable(substituteTypeVariables(toBound, typeVarAssigns), type,
660                    typeVarAssigns)) {
661                return false;
662            }
663        }
664        return true;
665    }
666
667    /**
668     * <p>Checks if the subject type may be implicitly cast to the target type
669     * variable following the Java generics rules.</p>
670     *
671     * @param type the subject type to be assigned to the target type
672     * @param toTypeVariable the target type variable
673     * @param typeVarAssigns a map with type variables
674     * @return {@code true} if {@code type} is assignable to
675     * {@code toTypeVariable}.
676     */
677    private static boolean isAssignable(final Type type, final TypeVariable<?> toTypeVariable,
678            final Map<TypeVariable<?>, Type> typeVarAssigns) {
679        if (type == null) {
680            return true;
681        }
682
683        // only a null type can be assigned to null type which
684        // would have cause the previous to return true
685        if (toTypeVariable == null) {
686            return false;
687        }
688
689        // all types are assignable to themselves
690        if (toTypeVariable.equals(type)) {
691            return true;
692        }
693
694        if (type instanceof TypeVariable<?>) {
695            // a type variable is assignable to another type variable, if
696            // and only if the former is the latter, extends the latter, or
697            // is otherwise a descendant of the latter.
698            final Type[] bounds = getImplicitBounds((TypeVariable<?>) type);
699
700            for (final Type bound : bounds) {
701                if (isAssignable(bound, toTypeVariable, typeVarAssigns)) {
702                    return true;
703                }
704            }
705        }
706
707        if (type instanceof Class<?> || type instanceof ParameterizedType
708                || type instanceof GenericArrayType || type instanceof WildcardType) {
709            return false;
710        }
711
712        throw new IllegalStateException("found an unhandled type: " + type);
713    }
714
715    /**
716     * <p>Find the mapping for {@code type} in {@code typeVarAssigns}.</p>
717     *
718     * @param type the type to be replaced
719     * @param typeVarAssigns the map with type variables
720     * @return the replaced type
721     * @throws IllegalArgumentException if the type cannot be substituted
722     */
723    private static Type substituteTypeVariables(final Type type, final Map<TypeVariable<?>, Type> typeVarAssigns) {
724        if (type instanceof TypeVariable<?> && typeVarAssigns != null) {
725            final Type replacementType = typeVarAssigns.get(type);
726
727            if (replacementType == null) {
728                throw new IllegalArgumentException("missing assignment type for type variable "
729                        + type);
730            }
731            return replacementType;
732        }
733        return type;
734    }
735
736    /**
737     * <p>Retrieves all the type arguments for this parameterized type
738     * including owner hierarchy arguments such as
739     * {@code Outer<K, V>.Inner<T>.DeepInner<E>} .
740     * The arguments are returned in a
741     * {@link Map} specifying the argument type for each {@link TypeVariable}.
742     * </p>
743     *
744     * @param type specifies the subject parameterized type from which to
745     *             harvest the parameters.
746     * @return a {@code Map} of the type arguments to their respective type
747     * variables.
748     */
749    public static Map<TypeVariable<?>, Type> getTypeArguments(final ParameterizedType type) {
750        return getTypeArguments(type, getRawType(type), null);
751    }
752
753    /**
754     * <p>Gets the type arguments of a class/interface based on a subtype. For
755     * instance, this method will determine that both of the parameters for the
756     * interface {@link Map} are {@link Object} for the subtype
757     * {@link java.util.Properties Properties} even though the subtype does not
758     * directly implement the {@code Map} interface.</p>
759     * <p>This method returns {@code null} if {@code type} is not assignable to
760     * {@code toClass}. It returns an empty map if none of the classes or
761     * interfaces in its inheritance hierarchy specify any type arguments.</p>
762     * <p>A side effect of this method is that it also retrieves the type
763     * arguments for the classes and interfaces that are part of the hierarchy
764     * between {@code type} and {@code toClass}. So with the above
765     * example, this method will also determine that the type arguments for
766     * {@link java.util.Hashtable Hashtable} are also both {@code Object}.
767     * In cases where the interface specified by {@code toClass} is
768     * (indirectly) implemented more than once (e.g. where {@code toClass}
769     * specifies the interface {@link java.lang.Iterable Iterable} and
770     * {@code type} specifies a parameterized type that implements both
771     * {@link java.util.Set Set} and {@link java.util.Collection Collection}),
772     * this method will look at the inheritance hierarchy of only one of the
773     * implementations/subclasses; the first interface encountered that isn't a
774     * subinterface to one of the others in the {@code type} to
775     * {@code toClass} hierarchy.</p>
776     *
777     * @param type the type from which to determine the type parameters of
778     * {@code toClass}
779     * @param toClass the class whose type parameters are to be determined based
780     * on the subtype {@code type}
781     * @return a {@code Map} of the type assignments for the type variables in
782     * each type in the inheritance hierarchy from {@code type} to
783     * {@code toClass} inclusive.
784     */
785    public static Map<TypeVariable<?>, Type> getTypeArguments(final Type type, final Class<?> toClass) {
786        return getTypeArguments(type, toClass, null);
787    }
788
789    /**
790     * <p>Return a map of the type arguments of {@code type} in the context of {@code toClass}.</p>
791     *
792     * @param type the type in question
793     * @param toClass the class
794     * @param subtypeVarAssigns a map with type variables
795     * @return the {@code Map} with type arguments
796     */
797    private static Map<TypeVariable<?>, Type> getTypeArguments(final Type type, final Class<?> toClass,
798            final Map<TypeVariable<?>, Type> subtypeVarAssigns) {
799        if (type instanceof Class<?>) {
800            return getTypeArguments((Class<?>) type, toClass, subtypeVarAssigns);
801        }
802
803        if (type instanceof ParameterizedType) {
804            return getTypeArguments((ParameterizedType) type, toClass, subtypeVarAssigns);
805        }
806
807        if (type instanceof GenericArrayType) {
808            return getTypeArguments(((GenericArrayType) type).getGenericComponentType(), toClass
809                    .isArray() ? toClass.getComponentType() : toClass, subtypeVarAssigns);
810        }
811
812        // since wildcard types are not assignable to classes, should this just
813        // return null?
814        if (type instanceof WildcardType) {
815            for (final Type bound : getImplicitUpperBounds((WildcardType) type)) {
816                // find the first bound that is assignable to the target class
817                if (isAssignable(bound, toClass)) {
818                    return getTypeArguments(bound, toClass, subtypeVarAssigns);
819                }
820            }
821
822            return null;
823        }
824
825        if (type instanceof TypeVariable<?>) {
826            for (final Type bound : getImplicitBounds((TypeVariable<?>) type)) {
827                // find the first bound that is assignable to the target class
828                if (isAssignable(bound, toClass)) {
829                    return getTypeArguments(bound, toClass, subtypeVarAssigns);
830                }
831            }
832
833            return null;
834        }
835        throw new IllegalStateException("found an unhandled type: " + type);
836    }
837
838    /**
839     * <p>Return a map of the type arguments of a parameterized type in the context of {@code toClass}.</p>
840     *
841     * @param parameterizedType the parameterized type
842     * @param toClass the class
843     * @param subtypeVarAssigns a map with type variables
844     * @return the {@code Map} with type arguments
845     */
846    private static Map<TypeVariable<?>, Type> getTypeArguments(
847            final ParameterizedType parameterizedType, final Class<?> toClass,
848            final Map<TypeVariable<?>, Type> subtypeVarAssigns) {
849        final Class<?> cls = getRawType(parameterizedType);
850
851        // make sure they're assignable
852        if (!isAssignable(cls, toClass)) {
853            return null;
854        }
855
856        final Type ownerType = parameterizedType.getOwnerType();
857        Map<TypeVariable<?>, Type> typeVarAssigns;
858
859        if (ownerType instanceof ParameterizedType) {
860            // get the owner type arguments first
861            final ParameterizedType parameterizedOwnerType = (ParameterizedType) ownerType;
862            typeVarAssigns = getTypeArguments(parameterizedOwnerType,
863                    getRawType(parameterizedOwnerType), subtypeVarAssigns);
864        } else {
865            // no owner, prep the type variable assignments map
866            typeVarAssigns = subtypeVarAssigns == null ? new HashMap<>()
867                    : new HashMap<>(subtypeVarAssigns);
868        }
869
870        // get the subject parameterized type's arguments
871        final Type[] typeArgs = parameterizedType.getActualTypeArguments();
872        // and get the corresponding type variables from the raw class
873        final TypeVariable<?>[] typeParams = cls.getTypeParameters();
874
875        // map the arguments to their respective type variables
876        for (int i = 0; i < typeParams.length; i++) {
877            final Type typeArg = typeArgs[i];
878            typeVarAssigns.put(
879                    typeParams[i],
880                    typeVarAssigns.getOrDefault(typeArg, typeArg)
881            );
882        }
883
884        if (toClass.equals(cls)) {
885            // target class has been reached. Done.
886            return typeVarAssigns;
887        }
888
889        // walk the inheritance hierarchy until the target class is reached
890        return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns);
891    }
892
893    /**
894     * <p>Return a map of the type arguments of a class in the context of {@code toClass}.</p>
895     *
896     * @param cls the class in question
897     * @param toClass the context class
898     * @param subtypeVarAssigns a map with type variables
899     * @return the {@code Map} with type arguments
900     */
901    private static Map<TypeVariable<?>, Type> getTypeArguments(Class<?> cls, final Class<?> toClass,
902            final Map<TypeVariable<?>, Type> subtypeVarAssigns) {
903        // make sure they're assignable
904        if (!isAssignable(cls, toClass)) {
905            return null;
906        }
907
908        // can't work with primitives
909        if (cls.isPrimitive()) {
910            // both classes are primitives?
911            if (toClass.isPrimitive()) {
912                // dealing with widening here. No type arguments to be
913                // harvested with these two types.
914                return new HashMap<>();
915            }
916
917            // work with wrapper the wrapper class instead of the primitive
918            cls = ClassUtils.primitiveToWrapper(cls);
919        }
920
921        // create a copy of the incoming map, or an empty one if it's null
922        final HashMap<TypeVariable<?>, Type> typeVarAssigns = subtypeVarAssigns == null ? new HashMap<>()
923                : new HashMap<>(subtypeVarAssigns);
924
925        // has target class been reached?
926        if (toClass.equals(cls)) {
927            return typeVarAssigns;
928        }
929
930        // walk the inheritance hierarchy until the target class is reached
931        return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns);
932    }
933
934    /**
935     * <p>Tries to determine the type arguments of a class/interface based on a
936     * super parameterized type's type arguments. This method is the inverse of
937     * {@link #getTypeArguments(Type, Class)} which gets a class/interface's
938     * type arguments based on a subtype. It is far more limited in determining
939     * the type arguments for the subject class's type variables in that it can
940     * only determine those parameters that map from the subject {@link Class}
941     * object to the supertype.</p> <p>Example: {@link java.util.TreeSet
942     * TreeSet} sets its parameter as the parameter for
943     * {@link java.util.NavigableSet NavigableSet}, which in turn sets the
944     * parameter of {@link java.util.SortedSet}, which in turn sets the
945     * parameter of {@link Set}, which in turn sets the parameter of
946     * {@link java.util.Collection}, which in turn sets the parameter of
947     * {@link java.lang.Iterable}. Since {@code TreeSet}'s parameter maps
948     * (indirectly) to {@code Iterable}'s parameter, it will be able to
949     * determine that based on the super type {@code Iterable<? extends
950     * Map<Integer, ? extends Collection<?>>>}, the parameter of
951     * {@code TreeSet} is {@code ? extends Map<Integer, ? extends
952     * Collection<?>>}.</p>
953     *
954     * @param cls the class whose type parameters are to be determined, not {@code null}
955     * @param superType the super type from which {@code cls}'s type
956     * arguments are to be determined, not {@code null}
957     * @return a {@code Map} of the type assignments that could be determined
958     * for the type variables in each type in the inheritance hierarchy from
959     * {@code type} to {@code toClass} inclusive.
960     */
961    public static Map<TypeVariable<?>, Type> determineTypeArguments(final Class<?> cls,
962            final ParameterizedType superType) {
963        Validate.notNull(cls, "cls is null");
964        Validate.notNull(superType, "superType is null");
965
966        final Class<?> superClass = getRawType(superType);
967
968        // compatibility check
969        if (!isAssignable(cls, superClass)) {
970            return null;
971        }
972
973        if (cls.equals(superClass)) {
974            return getTypeArguments(superType, superClass, null);
975        }
976
977        // get the next class in the inheritance hierarchy
978        final Type midType = getClosestParentType(cls, superClass);
979
980        // can only be a class or a parameterized type
981        if (midType instanceof Class<?>) {
982            return determineTypeArguments((Class<?>) midType, superType);
983        }
984
985        final ParameterizedType midParameterizedType = (ParameterizedType) midType;
986        final Class<?> midClass = getRawType(midParameterizedType);
987        // get the type variables of the mid class that map to the type
988        // arguments of the super class
989        final Map<TypeVariable<?>, Type> typeVarAssigns = determineTypeArguments(midClass, superType);
990        // map the arguments of the mid type to the class type variables
991        mapTypeVariablesToArguments(cls, midParameterizedType, typeVarAssigns);
992
993        return typeVarAssigns;
994    }
995
996    /**
997     * <p>Performs a mapping of type variables.</p>
998     *
999     * @param <T> the generic type of the class in question
1000     * @param cls the class in question
1001     * @param parameterizedType the parameterized type
1002     * @param typeVarAssigns the map to be filled
1003     */
1004    private static <T> void mapTypeVariablesToArguments(final Class<T> cls,
1005            final ParameterizedType parameterizedType, final Map<TypeVariable<?>, Type> typeVarAssigns) {
1006        // capture the type variables from the owner type that have assignments
1007        final Type ownerType = parameterizedType.getOwnerType();
1008
1009        if (ownerType instanceof ParameterizedType) {
1010            // recursion to make sure the owner's owner type gets processed
1011            mapTypeVariablesToArguments(cls, (ParameterizedType) ownerType, typeVarAssigns);
1012        }
1013
1014        // parameterizedType is a generic interface/class (or it's in the owner
1015        // hierarchy of said interface/class) implemented/extended by the class
1016        // cls. Find out which type variables of cls are type arguments of
1017        // parameterizedType:
1018        final Type[] typeArgs = parameterizedType.getActualTypeArguments();
1019
1020        // of the cls's type variables that are arguments of parameterizedType,
1021        // find out which ones can be determined from the super type's arguments
1022        final TypeVariable<?>[] typeVars = getRawType(parameterizedType).getTypeParameters();
1023
1024        // use List view of type parameters of cls so the contains() method can be used:
1025        final List<TypeVariable<Class<T>>> typeVarList = Arrays.asList(cls
1026                .getTypeParameters());
1027
1028        for (int i = 0; i < typeArgs.length; i++) {
1029            final TypeVariable<?> typeVar = typeVars[i];
1030            final Type typeArg = typeArgs[i];
1031
1032            // argument of parameterizedType is a type variable of cls
1033            if (typeVarList.contains(typeArg)
1034            // type variable of parameterizedType has an assignment in
1035                    // the super type.
1036                    && typeVarAssigns.containsKey(typeVar)) {
1037                // map the assignment to the cls's type variable
1038                typeVarAssigns.put((TypeVariable<?>) typeArg, typeVarAssigns.get(typeVar));
1039            }
1040        }
1041    }
1042
1043    /**
1044     * <p>Get the closest parent type to the
1045     * super class specified by {@code superClass}.</p>
1046     *
1047     * @param cls the class in question
1048     * @param superClass the super class
1049     * @return the closes parent type
1050     */
1051    private static Type getClosestParentType(final Class<?> cls, final Class<?> superClass) {
1052        // only look at the interfaces if the super class is also an interface
1053        if (superClass.isInterface()) {
1054            // get the generic interfaces of the subject class
1055            final Type[] interfaceTypes = cls.getGenericInterfaces();
1056            // will hold the best generic interface match found
1057            Type genericInterface = null;
1058
1059            // find the interface closest to the super class
1060            for (final Type midType : interfaceTypes) {
1061                Class<?> midClass = null;
1062
1063                if (midType instanceof ParameterizedType) {
1064                    midClass = getRawType((ParameterizedType) midType);
1065                } else if (midType instanceof Class<?>) {
1066                    midClass = (Class<?>) midType;
1067                } else {
1068                    throw new IllegalStateException("Unexpected generic"
1069                            + " interface type found: " + midType);
1070                }
1071
1072                // check if this interface is further up the inheritance chain
1073                // than the previously found match
1074                if (isAssignable(midClass, superClass)
1075                        && isAssignable(genericInterface, (Type) midClass)) {
1076                    genericInterface = midType;
1077                }
1078            }
1079
1080            // found a match?
1081            if (genericInterface != null) {
1082                return genericInterface;
1083            }
1084        }
1085
1086        // none of the interfaces were descendants of the target class, so the
1087        // super class has to be one, instead
1088        return cls.getGenericSuperclass();
1089    }
1090
1091    /**
1092     * <p>Checks if the given value can be assigned to the target type
1093     * following the Java generics rules.</p>
1094     *
1095     * @param value the value to be checked
1096     * @param type the target type
1097     * @return {@code true} if {@code value} is an instance of {@code type}.
1098     */
1099    public static boolean isInstance(final Object value, final Type type) {
1100        if (type == null) {
1101            return false;
1102        }
1103
1104        return value == null ? !(type instanceof Class<?>) || !((Class<?>) type).isPrimitive()
1105                : isAssignable(value.getClass(), type, null);
1106    }
1107
1108    /**
1109     * <p>This method strips out the redundant upper bound types in type
1110     * variable types and wildcard types (or it would with wildcard types if
1111     * multiple upper bounds were allowed).</p> <p>Example, with the variable
1112     * type declaration:
1113     *
1114     * <pre>&lt;K extends java.util.Collection&lt;String&gt; &amp;
1115     * java.util.List&lt;String&gt;&gt;</pre>
1116     *
1117     * <p>
1118     * since {@code List} is a subinterface of {@code Collection},
1119     * this method will return the bounds as if the declaration had been:
1120     * </p>
1121     *
1122     * <pre>&lt;K extends java.util.List&lt;String&gt;&gt;</pre>
1123     *
1124     * @param bounds an array of types representing the upper bounds of either
1125     * {@link WildcardType} or {@link TypeVariable}, not {@code null}.
1126     * @return an array containing the values from {@code bounds} minus the
1127     * redundant types.
1128     */
1129    public static Type[] normalizeUpperBounds(final Type[] bounds) {
1130        Validate.notNull(bounds, "null value specified for bounds array");
1131        // don't bother if there's only one (or none) type
1132        if (bounds.length < 2) {
1133            return bounds;
1134        }
1135
1136        final Set<Type> types = new HashSet<>(bounds.length);
1137
1138        for (final Type type1 : bounds) {
1139            boolean subtypeFound = false;
1140
1141            for (final Type type2 : bounds) {
1142                if (type1 != type2 && isAssignable(type2, type1, null)) {
1143                    subtypeFound = true;
1144                    break;
1145                }
1146            }
1147
1148            if (!subtypeFound) {
1149                types.add(type1);
1150            }
1151        }
1152
1153        return types.toArray(ArrayUtils.EMPTY_TYPE_ARRAY);
1154    }
1155
1156    /**
1157     * <p>Returns an array containing the sole type of {@link Object} if
1158     * {@link TypeVariable#getBounds()} returns an empty array. Otherwise, it
1159     * returns the result of {@link TypeVariable#getBounds()} passed into
1160     * {@link #normalizeUpperBounds}.</p>
1161     *
1162     * @param typeVariable the subject type variable, not {@code null}
1163     * @return a non-empty array containing the bounds of the type variable.
1164     */
1165    public static Type[] getImplicitBounds(final TypeVariable<?> typeVariable) {
1166        Validate.notNull(typeVariable, "typeVariable is null");
1167        final Type[] bounds = typeVariable.getBounds();
1168
1169        return bounds.length == 0 ? new Type[] { Object.class } : normalizeUpperBounds(bounds);
1170    }
1171
1172    /**
1173     * <p>Returns an array containing the sole value of {@link Object} if
1174     * {@link WildcardType#getUpperBounds()} returns an empty array. Otherwise,
1175     * it returns the result of {@link WildcardType#getUpperBounds()}
1176     * passed into {@link #normalizeUpperBounds}.</p>
1177     *
1178     * @param wildcardType the subject wildcard type, not {@code null}
1179     * @return a non-empty array containing the upper bounds of the wildcard
1180     * type.
1181     */
1182    public static Type[] getImplicitUpperBounds(final WildcardType wildcardType) {
1183        Validate.notNull(wildcardType, "wildcardType is null");
1184        final Type[] bounds = wildcardType.getUpperBounds();
1185
1186        return bounds.length == 0 ? new Type[] { Object.class } : normalizeUpperBounds(bounds);
1187    }
1188
1189    /**
1190     * <p>Returns an array containing a single value of {@code null} if
1191     * {@link WildcardType#getLowerBounds()} returns an empty array. Otherwise,
1192     * it returns the result of {@link WildcardType#getLowerBounds()}.</p>
1193     *
1194     * @param wildcardType the subject wildcard type, not {@code null}
1195     * @return a non-empty array containing the lower bounds of the wildcard
1196     * type.
1197     */
1198    public static Type[] getImplicitLowerBounds(final WildcardType wildcardType) {
1199        Validate.notNull(wildcardType, "wildcardType is null");
1200        final Type[] bounds = wildcardType.getLowerBounds();
1201
1202        return bounds.length == 0 ? new Type[] { null } : bounds;
1203    }
1204
1205    /**
1206     * <p>Determines whether or not specified types satisfy the bounds of their
1207     * mapped type variables. When a type parameter extends another (such as
1208     * {@code <T, S extends T>}), uses another as a type parameter (such as
1209     * {@code <T, S extends Comparable>>}), or otherwise depends on
1210     * another type variable to be specified, the dependencies must be included
1211     * in {@code typeVarAssigns}.</p>
1212     *
1213     * @param typeVarAssigns specifies the potential types to be assigned to the
1214     * type variables, not {@code null}.
1215     * @return whether or not the types can be assigned to their respective type
1216     * variables.
1217     */
1218    public static boolean typesSatisfyVariables(final Map<TypeVariable<?>, Type> typeVarAssigns) {
1219        Validate.notNull(typeVarAssigns, "typeVarAssigns is null");
1220        // all types must be assignable to all the bounds of their mapped
1221        // type variable.
1222        for (final Map.Entry<TypeVariable<?>, Type> entry : typeVarAssigns.entrySet()) {
1223            final TypeVariable<?> typeVar = entry.getKey();
1224            final Type type = entry.getValue();
1225
1226            for (final Type bound : getImplicitBounds(typeVar)) {
1227                if (!isAssignable(type, substituteTypeVariables(bound, typeVarAssigns),
1228                        typeVarAssigns)) {
1229                    return false;
1230                }
1231            }
1232        }
1233        return true;
1234    }
1235
1236    /**
1237     * <p>Transforms the passed in type to a {@link Class} object. Type-checking method of convenience.</p>
1238     *
1239     * @param parameterizedType the type to be converted
1240     * @return the corresponding {@code Class} object
1241     * @throws IllegalStateException if the conversion fails
1242     */
1243    private static Class<?> getRawType(final ParameterizedType parameterizedType) {
1244        final Type rawType = parameterizedType.getRawType();
1245
1246        // check if raw type is a Class object
1247        // not currently necessary, but since the return type is Type instead of
1248        // Class, there's enough reason to believe that future versions of Java
1249        // may return other Type implementations. And type-safety checking is
1250        // rarely a bad idea.
1251        if (!(rawType instanceof Class<?>)) {
1252            throw new IllegalStateException("Wait... What!? Type of rawType: " + rawType);
1253        }
1254
1255        return (Class<?>) rawType;
1256    }
1257
1258    /**
1259     * <p>Get the raw type of a Java type, given its context. Primarily for use
1260     * with {@link TypeVariable}s and {@link GenericArrayType}s, or when you do
1261     * not know the runtime type of {@code type}: if you know you have a
1262     * {@link Class} instance, it is already raw; if you know you have a
1263     * {@link ParameterizedType}, its raw type is only a method call away.</p>
1264     *
1265     * @param type to resolve
1266     * @param assigningType type to be resolved against
1267     * @return the resolved {@link Class} object or {@code null} if
1268     * the type could not be resolved
1269     */
1270    public static Class<?> getRawType(final Type type, final Type assigningType) {
1271        if (type instanceof Class<?>) {
1272            // it is raw, no problem
1273            return (Class<?>) type;
1274        }
1275
1276        if (type instanceof ParameterizedType) {
1277            // simple enough to get the raw type of a ParameterizedType
1278            return getRawType((ParameterizedType) type);
1279        }
1280
1281        if (type instanceof TypeVariable<?>) {
1282            if (assigningType == null) {
1283                return null;
1284            }
1285
1286            // get the entity declaring this type variable
1287            final Object genericDeclaration = ((TypeVariable<?>) type).getGenericDeclaration();
1288
1289            // can't get the raw type of a method- or constructor-declared type
1290            // variable
1291            if (!(genericDeclaration instanceof Class<?>)) {
1292                return null;
1293            }
1294
1295            // get the type arguments for the declaring class/interface based
1296            // on the enclosing type
1297            final Map<TypeVariable<?>, Type> typeVarAssigns = getTypeArguments(assigningType,
1298                    (Class<?>) genericDeclaration);
1299
1300            // enclosingType has to be a subclass (or subinterface) of the
1301            // declaring type
1302            if (typeVarAssigns == null) {
1303                return null;
1304            }
1305
1306            // get the argument assigned to this type variable
1307            final Type typeArgument = typeVarAssigns.get(type);
1308
1309            if (typeArgument == null) {
1310                return null;
1311            }
1312
1313            // get the argument for this type variable
1314            return getRawType(typeArgument, assigningType);
1315        }
1316
1317        if (type instanceof GenericArrayType) {
1318            // get raw component type
1319            final Class<?> rawComponentType = getRawType(((GenericArrayType) type)
1320                    .getGenericComponentType(), assigningType);
1321
1322            // create array type from raw component type and return its class
1323            return Array.newInstance(rawComponentType, 0).getClass();
1324        }
1325
1326        // (hand-waving) this is not the method you're looking for
1327        if (type instanceof WildcardType) {
1328            return null;
1329        }
1330
1331        throw new IllegalArgumentException("unknown type: " + type);
1332    }
1333
1334    /**
1335     * Learn whether the specified type denotes an array type.
1336     * @param type the type to be checked
1337     * @return {@code true} if {@code type} is an array class or a {@link GenericArrayType}.
1338     */
1339    public static boolean isArrayType(final Type type) {
1340        return type instanceof GenericArrayType || type instanceof Class<?> && ((Class<?>) type).isArray();
1341    }
1342
1343    /**
1344     * Gets the array component type of {@code type}.
1345     * @param type the type to be checked
1346     * @return component type or null if type is not an array type
1347     */
1348    public static Type getArrayComponentType(final Type type) {
1349        if (type instanceof Class<?>) {
1350            final Class<?> cls = (Class<?>) type;
1351            return cls.isArray() ? cls.getComponentType() : null;
1352        }
1353        if (type instanceof GenericArrayType) {
1354            return ((GenericArrayType) type).getGenericComponentType();
1355        }
1356        return null;
1357    }
1358
1359    /**
1360     * Gets a type representing {@code type} with variable assignments "unrolled."
1361     *
1362     * @param typeArguments as from {@link TypeUtils#getTypeArguments(Type, Class)}
1363     * @param type the type to unroll variable assignments for
1364     * @return Type
1365     * @since 3.2
1366     */
1367    public static Type unrollVariables(Map<TypeVariable<?>, Type> typeArguments, final Type type) {
1368        if (typeArguments == null) {
1369            typeArguments = Collections.emptyMap();
1370        }
1371        if (containsTypeVariables(type)) {
1372            if (type instanceof TypeVariable<?>) {
1373                return unrollVariables(typeArguments, typeArguments.get(type));
1374            }
1375            if (type instanceof ParameterizedType) {
1376                final ParameterizedType p = (ParameterizedType) type;
1377                final Map<TypeVariable<?>, Type> parameterizedTypeArguments;
1378                if (p.getOwnerType() == null) {
1379                    parameterizedTypeArguments = typeArguments;
1380                } else {
1381                    parameterizedTypeArguments = new HashMap<>(typeArguments);
1382                    parameterizedTypeArguments.putAll(getTypeArguments(p));
1383                }
1384                final Type[] args = p.getActualTypeArguments();
1385                for (int i = 0; i < args.length; i++) {
1386                    final Type unrolled = unrollVariables(parameterizedTypeArguments, args[i]);
1387                    if (unrolled != null) {
1388                        args[i] = unrolled;
1389                    }
1390                }
1391                return parameterizeWithOwner(p.getOwnerType(), (Class<?>) p.getRawType(), args);
1392            }
1393            if (type instanceof WildcardType) {
1394                final WildcardType wild = (WildcardType) type;
1395                return wildcardType().withUpperBounds(unrollBounds(typeArguments, wild.getUpperBounds()))
1396                    .withLowerBounds(unrollBounds(typeArguments, wild.getLowerBounds())).build();
1397            }
1398        }
1399        return type;
1400    }
1401
1402    /**
1403     * Local helper method to unroll variables in a type bounds array.
1404     *
1405     * @param typeArguments assignments {@link Map}
1406     * @param bounds in which to expand variables
1407     * @return {@code bounds} with any variables reassigned
1408     * @since 3.2
1409     */
1410    private static Type[] unrollBounds(final Map<TypeVariable<?>, Type> typeArguments, final Type[] bounds) {
1411        Type[] result = bounds;
1412        int i = 0;
1413        for (; i < result.length; i++) {
1414            final Type unrolled = unrollVariables(typeArguments, result[i]);
1415            if (unrolled == null) {
1416                result = ArrayUtils.remove(result, i--);
1417            } else {
1418                result[i] = unrolled;
1419            }
1420        }
1421        return result;
1422    }
1423
1424    /**
1425     * Learn, recursively, whether any of the type parameters associated with {@code type} are bound to variables.
1426     *
1427     * @param type the type to check for type variables
1428     * @return boolean
1429     * @since 3.2
1430     */
1431    public static boolean containsTypeVariables(final Type type) {
1432        if (type instanceof TypeVariable<?>) {
1433            return true;
1434        }
1435        if (type instanceof Class<?>) {
1436            return ((Class<?>) type).getTypeParameters().length > 0;
1437        }
1438        if (type instanceof ParameterizedType) {
1439            for (final Type arg : ((ParameterizedType) type).getActualTypeArguments()) {
1440                if (containsTypeVariables(arg)) {
1441                    return true;
1442                }
1443            }
1444            return false;
1445        }
1446        if (type instanceof WildcardType) {
1447            final WildcardType wild = (WildcardType) type;
1448            return containsTypeVariables(getImplicitLowerBounds(wild)[0])
1449                || containsTypeVariables(getImplicitUpperBounds(wild)[0]);
1450        }
1451        return false;
1452    }
1453
1454    /**
1455     * Create a parameterized type instance.
1456     *
1457     * @param rawClass the raw class to create a parameterized type instance for
1458     * @param typeArguments the types used for parameterization
1459     * @return {@link ParameterizedType}
1460     * @since 3.2
1461     */
1462    public static final ParameterizedType parameterize(final Class<?> rawClass, final Type... typeArguments) {
1463        return parameterizeWithOwner(null, rawClass, typeArguments);
1464    }
1465
1466    /**
1467     * Create a parameterized type instance.
1468     *
1469     * @param rawClass the raw class to create a parameterized type instance for
1470     * @param typeArgMappings the mapping used for parameterization
1471     * @return {@link ParameterizedType}
1472     * @since 3.2
1473     */
1474    public static final ParameterizedType parameterize(final Class<?> rawClass,
1475        final Map<TypeVariable<?>, Type> typeArgMappings) {
1476        Validate.notNull(rawClass, "raw class is null");
1477        Validate.notNull(typeArgMappings, "typeArgMappings is null");
1478        return parameterizeWithOwner(null, rawClass,
1479            extractTypeArgumentsFrom(typeArgMappings, rawClass.getTypeParameters()));
1480    }
1481
1482    /**
1483     * Create a parameterized type instance.
1484     *
1485     * @param owner the owning type
1486     * @param rawClass the raw class to create a parameterized type instance for
1487     * @param typeArguments the types used for parameterization
1488     *
1489     * @return {@link ParameterizedType}
1490     * @since 3.2
1491     */
1492    public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class<?> rawClass,
1493        final Type... typeArguments) {
1494        Validate.notNull(rawClass, "raw class is null");
1495        final Type useOwner;
1496        if (rawClass.getEnclosingClass() == null) {
1497            Validate.isTrue(owner == null, "no owner allowed for top-level %s", rawClass);
1498            useOwner = null;
1499        } else if (owner == null) {
1500            useOwner = rawClass.getEnclosingClass();
1501        } else {
1502            Validate.isTrue(isAssignable(owner, rawClass.getEnclosingClass()),
1503                "%s is invalid owner type for parameterized %s", owner, rawClass);
1504            useOwner = owner;
1505        }
1506        Validate.noNullElements(typeArguments, "null type argument at index %s");
1507        Validate.isTrue(rawClass.getTypeParameters().length == typeArguments.length,
1508            "invalid number of type parameters specified: expected %d, got %d", rawClass.getTypeParameters().length,
1509            typeArguments.length);
1510
1511        return new ParameterizedTypeImpl(rawClass, useOwner, typeArguments);
1512    }
1513
1514    /**
1515     * Create a parameterized type instance.
1516     *
1517     * @param owner the owning type
1518     * @param rawClass the raw class to create a parameterized type instance for
1519     * @param typeArgMappings the mapping used for parameterization
1520     * @return {@link ParameterizedType}
1521     * @since 3.2
1522     */
1523    public static final ParameterizedType parameterizeWithOwner(final Type owner, final Class<?> rawClass,
1524        final Map<TypeVariable<?>, Type> typeArgMappings) {
1525        Validate.notNull(rawClass, "raw class is null");
1526        Validate.notNull(typeArgMappings, "typeArgMappings is null");
1527        return parameterizeWithOwner(owner, rawClass,
1528            extractTypeArgumentsFrom(typeArgMappings, rawClass.getTypeParameters()));
1529    }
1530
1531    /**
1532     * Helper method to establish the formal parameters for a parameterized type.
1533     * @param mappings map containing the assignments
1534     * @param variables expected map keys
1535     * @return array of map values corresponding to specified keys
1536     */
1537    private static Type[] extractTypeArgumentsFrom(final Map<TypeVariable<?>, Type> mappings, final TypeVariable<?>[] variables) {
1538        final Type[] result = new Type[variables.length];
1539        int index = 0;
1540        for (final TypeVariable<?> var : variables) {
1541            Validate.isTrue(mappings.containsKey(var), "missing argument mapping for %s", toString(var));
1542            result[index++] = mappings.get(var);
1543        }
1544        return result;
1545    }
1546
1547    /**
1548     * Gets a {@link WildcardTypeBuilder}.
1549     * @return {@link WildcardTypeBuilder}
1550     * @since 3.2
1551     */
1552    public static WildcardTypeBuilder wildcardType() {
1553        return new WildcardTypeBuilder();
1554    }
1555
1556    /**
1557     * Create a generic array type instance.
1558     *
1559     * @param componentType the type of the elements of the array. For example the component type of {@code boolean[]}
1560     *                      is {@code boolean}
1561     * @return {@link GenericArrayType}
1562     * @since 3.2
1563     */
1564    public static GenericArrayType genericArrayType(final Type componentType) {
1565        return new GenericArrayTypeImpl(Validate.notNull(componentType, "componentType is null"));
1566    }
1567
1568    /**
1569     * Check equality of types.
1570     *
1571     * @param type1 the first type
1572     * @param type2 the second type
1573     * @return boolean
1574     * @since 3.2
1575     */
1576    public static boolean equals(final Type type1, final Type type2) {
1577        if (Objects.equals(type1, type2)) {
1578            return true;
1579        }
1580        if (type1 instanceof ParameterizedType) {
1581            return equals((ParameterizedType) type1, type2);
1582        }
1583        if (type1 instanceof GenericArrayType) {
1584            return equals((GenericArrayType) type1, type2);
1585        }
1586        if (type1 instanceof WildcardType) {
1587            return equals((WildcardType) type1, type2);
1588        }
1589        return false;
1590    }
1591
1592    /**
1593     * Learn whether {@code t} equals {@code p}.
1594     * @param parameterizedType LHS
1595     * @param type RHS
1596     * @return boolean
1597     * @since 3.2
1598     */
1599    private static boolean equals(final ParameterizedType parameterizedType, final Type type) {
1600        if (type instanceof ParameterizedType) {
1601            final ParameterizedType other = (ParameterizedType) type;
1602            if (equals(parameterizedType.getRawType(), other.getRawType())
1603                && equals(parameterizedType.getOwnerType(), other.getOwnerType())) {
1604                return equals(parameterizedType.getActualTypeArguments(), other.getActualTypeArguments());
1605            }
1606        }
1607        return false;
1608    }
1609
1610    /**
1611     * Learn whether {@code t} equals {@code a}.
1612     * @param genericArrayType LHS
1613     * @param type RHS
1614     * @return boolean
1615     * @since 3.2
1616     */
1617    private static boolean equals(final GenericArrayType genericArrayType, final Type type) {
1618        return type instanceof GenericArrayType
1619            && equals(genericArrayType.getGenericComponentType(), ((GenericArrayType) type).getGenericComponentType());
1620    }
1621
1622    /**
1623     * Learn whether {@code t} equals {@code w}.
1624     * @param wildcardType LHS
1625     * @param type RHS
1626     * @return boolean
1627     * @since 3.2
1628     */
1629    private static boolean equals(final WildcardType wildcardType, final Type type) {
1630        if (type instanceof WildcardType) {
1631            final WildcardType other = (WildcardType) type;
1632            return equals(getImplicitLowerBounds(wildcardType), getImplicitLowerBounds(other))
1633                && equals(getImplicitUpperBounds(wildcardType), getImplicitUpperBounds(other));
1634        }
1635        return false;
1636    }
1637
1638    /**
1639     * Learn whether {@code t1} equals {@code t2}.
1640     * @param type1 LHS
1641     * @param type2 RHS
1642     * @return boolean
1643     * @since 3.2
1644     */
1645    private static boolean equals(final Type[] type1, final Type[] type2) {
1646        if (type1.length == type2.length) {
1647            for (int i = 0; i < type1.length; i++) {
1648                if (!equals(type1[i], type2[i])) {
1649                    return false;
1650                }
1651            }
1652            return true;
1653        }
1654        return false;
1655    }
1656
1657    /**
1658     * Present a given type as a Java-esque String.
1659     *
1660     * @param type the type to create a String representation for, not {@code null}
1661     * @return String
1662     * @since 3.2
1663     */
1664    public static String toString(final Type type) {
1665        Validate.notNull(type);
1666        if (type instanceof Class<?>) {
1667            return classToString((Class<?>) type);
1668        }
1669        if (type instanceof ParameterizedType) {
1670            return parameterizedTypeToString((ParameterizedType) type);
1671        }
1672        if (type instanceof WildcardType) {
1673            return wildcardTypeToString((WildcardType) type);
1674        }
1675        if (type instanceof TypeVariable<?>) {
1676            return typeVariableToString((TypeVariable<?>) type);
1677        }
1678        if (type instanceof GenericArrayType) {
1679            return genericArrayTypeToString((GenericArrayType) type);
1680        }
1681        throw new IllegalArgumentException(ObjectUtils.identityToString(type));
1682    }
1683
1684    /**
1685     * Format a {@link TypeVariable} including its {@link GenericDeclaration}.
1686     *
1687     * @param var the type variable to create a String representation for, not {@code null}
1688     * @return String
1689     * @since 3.2
1690     */
1691    public static String toLongString(final TypeVariable<?> var) {
1692        Validate.notNull(var, "var is null");
1693        final StringBuilder buf = new StringBuilder();
1694        final GenericDeclaration d = var.getGenericDeclaration();
1695        if (d instanceof Class<?>) {
1696            Class<?> c = (Class<?>) d;
1697            while (true) {
1698                if (c.getEnclosingClass() == null) {
1699                    buf.insert(0, c.getName());
1700                    break;
1701                }
1702                buf.insert(0, c.getSimpleName()).insert(0, '.');
1703                c = c.getEnclosingClass();
1704            }
1705        } else if (d instanceof Type) {// not possible as of now
1706            buf.append(toString((Type) d));
1707        } else {
1708            buf.append(d);
1709        }
1710        return buf.append(':').append(typeVariableToString(var)).toString();
1711    }
1712
1713    /**
1714     * Wrap the specified {@link Type} in a {@link Typed} wrapper.
1715     *
1716     * @param <T> inferred generic type
1717     * @param type to wrap
1718     * @return Typed&lt;T&gt;
1719     * @since 3.2
1720     */
1721    public static <T> Typed<T> wrap(final Type type) {
1722        return () -> type;
1723    }
1724
1725    /**
1726     * Wrap the specified {@link Class} in a {@link Typed} wrapper.
1727     *
1728     * @param <T> generic type
1729     * @param type to wrap
1730     * @return Typed&lt;T&gt;
1731     * @since 3.2
1732     */
1733    public static <T> Typed<T> wrap(final Class<T> type) {
1734        return wrap((Type) type);
1735    }
1736
1737    /**
1738     * Format a {@link Class} as a {@link String}.
1739     * @param cls {@code Class} to format
1740     * @return String
1741     * @since 3.2
1742     */
1743    private static String classToString(final Class<?> cls) {
1744        if (cls.isArray()) {
1745            return toString(cls.getComponentType()) + "[]";
1746        }
1747
1748        final StringBuilder buf = new StringBuilder();
1749
1750        if (cls.getEnclosingClass() != null) {
1751            buf.append(classToString(cls.getEnclosingClass())).append('.').append(cls.getSimpleName());
1752        } else {
1753            buf.append(cls.getName());
1754        }
1755        if (cls.getTypeParameters().length > 0) {
1756            buf.append('<');
1757            appendAllTo(buf, ", ", cls.getTypeParameters());
1758            buf.append('>');
1759        }
1760        return buf.toString();
1761    }
1762
1763    /**
1764     * Format a {@link TypeVariable} as a {@link String}.
1765     * @param typeVariable {@code TypeVariable} to format
1766     * @return String
1767     * @since 3.2
1768     */
1769    private static String typeVariableToString(final TypeVariable<?> typeVariable) {
1770        final StringBuilder buf = new StringBuilder(typeVariable.getName());
1771        final Type[] bounds = typeVariable.getBounds();
1772        if (bounds.length > 0 && !(bounds.length == 1 && Object.class.equals(bounds[0]))) {
1773            buf.append(" extends ");
1774            appendAllTo(buf, " & ", typeVariable.getBounds());
1775        }
1776        return buf.toString();
1777    }
1778
1779    /**
1780     * Format a {@link ParameterizedType} as a {@link String}.
1781     * @param parameterizedType {@code ParameterizedType} to format
1782     * @return String
1783     * @since 3.2
1784     */
1785    private static String parameterizedTypeToString(final ParameterizedType parameterizedType) {
1786        final StringBuilder builder = new StringBuilder();
1787
1788        final Type useOwner = parameterizedType.getOwnerType();
1789        final Class<?> raw = (Class<?>) parameterizedType.getRawType();
1790
1791        if (useOwner == null) {
1792            builder.append(raw.getName());
1793        } else {
1794            if (useOwner instanceof Class<?>) {
1795                builder.append(((Class<?>) useOwner).getName());
1796            } else {
1797                builder.append(useOwner.toString());
1798            }
1799            builder.append('.').append(raw.getSimpleName());
1800        }
1801
1802        final int[] recursiveTypeIndexes = findRecursiveTypes(parameterizedType);
1803
1804        if (recursiveTypeIndexes.length > 0) {
1805            appendRecursiveTypes(builder, recursiveTypeIndexes, parameterizedType.getActualTypeArguments());
1806        } else {
1807            appendAllTo(builder.append('<'), ", ", parameterizedType.getActualTypeArguments()).append('>');
1808        }
1809
1810        return builder.toString();
1811    }
1812
1813    private static void appendRecursiveTypes(final StringBuilder builder, final int[] recursiveTypeIndexes,
1814        final Type[] argumentTypes) {
1815        for (int i = 0; i < recursiveTypeIndexes.length; i++) {
1816            appendAllTo(builder.append('<'), ", ", argumentTypes[i].toString()).append('>');
1817        }
1818
1819        final Type[] argumentsFiltered = ArrayUtils.removeAll(argumentTypes, recursiveTypeIndexes);
1820
1821        if (argumentsFiltered.length > 0) {
1822            appendAllTo(builder.append('<'), ", ", argumentsFiltered).append('>');
1823        }
1824    }
1825
1826    private static int[] findRecursiveTypes(final ParameterizedType parameterizedType) {
1827        final Type[] filteredArgumentTypes = Arrays.copyOf(parameterizedType.getActualTypeArguments(),
1828            parameterizedType.getActualTypeArguments().length);
1829        int[] indexesToRemove = {};
1830        for (int i = 0; i < filteredArgumentTypes.length; i++) {
1831            if (filteredArgumentTypes[i] instanceof TypeVariable<?>) {
1832                if (containsVariableTypeSameParametrizedTypeBound(((TypeVariable<?>) filteredArgumentTypes[i]),
1833                    parameterizedType)) {
1834                    indexesToRemove = ArrayUtils.add(indexesToRemove, i);
1835                }
1836            }
1837        }
1838        return indexesToRemove;
1839    }
1840
1841    private static boolean containsVariableTypeSameParametrizedTypeBound(final TypeVariable<?> typeVariable,
1842        final ParameterizedType parameterizedType) {
1843        return ArrayUtils.contains(typeVariable.getBounds(), parameterizedType);
1844    }
1845
1846    /**
1847     * Format a {@link WildcardType} as a {@link String}.
1848     * @param wildcardType {@code WildcardType} to format
1849     * @return String
1850     * @since 3.2
1851     */
1852    private static String wildcardTypeToString(final WildcardType wildcardType) {
1853        final StringBuilder buf = new StringBuilder().append('?');
1854        final Type[] lowerBounds = wildcardType.getLowerBounds();
1855        final Type[] upperBounds = wildcardType.getUpperBounds();
1856        if (lowerBounds.length > 1 || lowerBounds.length == 1 && lowerBounds[0] != null) {
1857            appendAllTo(buf.append(" super "), " & ", lowerBounds);
1858        } else if (upperBounds.length > 1 || upperBounds.length == 1 && !Object.class.equals(upperBounds[0])) {
1859            appendAllTo(buf.append(" extends "), " & ", upperBounds);
1860        }
1861        return buf.toString();
1862    }
1863
1864    /**
1865     * Format a {@link GenericArrayType} as a {@link String}.
1866     * @param genericArrayType {@code GenericArrayType} to format
1867     * @return String
1868     * @since 3.2
1869     */
1870    private static String genericArrayTypeToString(final GenericArrayType genericArrayType) {
1871        return String.format("%s[]", toString(genericArrayType.getGenericComponentType()));
1872    }
1873
1874    /**
1875     * Append {@code types} to {@code builder} with separator {@code sep}.
1876     * @param builder destination
1877     * @param sep separator
1878     * @param types to append
1879     * @return {@code builder}
1880     * @since 3.2
1881     */
1882    private static <T> StringBuilder appendAllTo(final StringBuilder builder, final String sep,
1883        @SuppressWarnings("unchecked") final T... types) {
1884        Validate.notEmpty(Validate.noNullElements(types));
1885        if (types.length > 0) {
1886            builder.append(toString(types[0]));
1887            for (int i = 1; i < types.length; i++) {
1888                builder.append(sep).append(toString(types[i]));
1889            }
1890        }
1891        return builder;
1892    }
1893
1894    private static <T> String toString(final T object) {
1895        return object instanceof Type ? toString((Type) object) : object.toString();
1896    }
1897
1898}