6868import java .util .Objects ;
6969import java .util .Optional ;
7070import java .util .Set ;
71+ import java .util .stream .IntStream ;
7172
7273import com .google .common .reflect .TypeToken ;
7374
@@ -844,10 +845,6 @@ private static boolean satisfiesRawTypes(final Type arg, final Type param) {
844845 final Optional <Class <?>> first = srcClasses .stream ().filter (
845846 srcClass -> Types .isAssignable (srcClass , destClass )).findFirst ();
846847 if (!first .isPresent ()) {
847- // TODO can we remove this?
848- // throw new IllegalArgumentException("Argument #" + i + //
849- // " (" + Types.name(src[i]) + ") is not assignable to " + //
850- // "destination type (" + Types.name(dest[i]) + ")");
851848 return false ;
852849 }
853850 }
@@ -859,7 +856,7 @@ private static boolean satisfiesParameterizedTypes(final Type arg,
859856 final HashMap <TypeVariable <?>, TypeVarInfo > typeBounds )
860857 {
861858 // get an array of the destination parameter types
862- final Type [] destTypes = param .getActualTypeArguments ();
859+ Type [] destTypes = param .getActualTypeArguments ();
863860
864861 // get an array of the source argument types
865862 Type argType = arg ;
@@ -868,8 +865,15 @@ private static boolean satisfiesParameterizedTypes(final Type arg,
868865 }
869866 ParameterizedType parameterizedSuperType = (ParameterizedType ) GenericTypeReflector
870867 .getExactSuperType (argType , Types .raw (param ));
871- final Type [] srcTypes = parameterizedSuperType .getActualTypeArguments ();
868+ Type [] srcTypes = parameterizedSuperType .getActualTypeArguments ();
872869
870+ // List to collect the indices of destination parameters that are type vars
871+ // If a type vars is contain within a parameterized type if must not be checked
872+ // recursively in the last call of this method. If done, one loses the information
873+ // that the type var was contained in a parameterized type. Hence, it will be handled
874+ // like a normal type var which allow more assignability regarding wildcards compared
875+ // to type vars contained in parameterized types
876+ List <Integer > ignoredIndices = new ArrayList <Integer >();
873877 // check to see if any of the Types of this ParameterizedType are
874878 // TypeVariables, if so restrict them to the type parameter of the argument.
875879 for (int i = 0 ; i < destTypes .length ; i ++) {
@@ -879,13 +883,15 @@ private static boolean satisfiesParameterizedTypes(final Type arg,
879883 final TypeVariable <?> destTypeVar = (TypeVariable <?>) destType ;
880884 if (!satisfiesTypeParameter (srcType , destTypeVar , typeBounds ))
881885 return false ;
886+ ignoredIndices .add (i );
882887 }
883888 }
889+ srcTypes = filterIndices (srcTypes , ignoredIndices );
890+ destTypes = filterIndices (destTypes , ignoredIndices );
884891 // recursively run satisfies on these arrays
885892 return satisfies (srcTypes , destTypes , typeBounds ) == -1 ;
886-
887893 }
888-
894+
889895 /**
890896 * Determines whether or not the {@link Type} {@code arg} can satisfy the
891897 * {@link TypeVariable} {@code param} given the preexisting limitations of
@@ -908,23 +914,22 @@ private static boolean satisfiesTypeParameter(final Type arg,
908914 // add the type variable to the HashMap if it does not yet exist.
909915 if (!typeBounds .containsKey (param )) {
910916 final TypeVariable <?> newTypeVar = param ;
911- typeBounds .put (newTypeVar , new TypeVarInfo (newTypeVar ));
917+ typeBounds .put (newTypeVar , new TypeVarFromParameterizedTypeInfo (newTypeVar ));
912918 }
913919 // attempt to restrict the bounds of the type variable to the argument.
914- if (!typeBounds .get (param ).fixBounds (arg )) return false ;
915-
916- // TODO do we need type parameter recursion here as we do in
917- // satisfiesTypeVariable?
918-
920+ // We call the fixBounds method the refuseWildcards flag to be true,
921+ // as the type variable was contained in a parameterized type. Hence,
922+ // the var is not applicable for any wildcards if it already allows
923+ // any type.
924+ if (!typeBounds .get (param ).fixBounds (arg , true )) return false ;
925+
926+ // if the type variable refers to some parameterized type (e.g. T extends
927+ // List<?>), call satisfiesTypeParameters on the bounds of param that
928+ // are ParameterizedTypes.
929+ if (!satisfiesTypeVariableBounds (arg , param , typeBounds )) return false ;
919930 return true ;
920931 }
921932
922- /**
923- * @param arg
924- * @param param
925- * @param typeBounds
926- * @return
927- */
928933 private static boolean satisfiesTypeVariable (final Type arg ,
929934 final TypeVariable <?> param ,
930935 final HashMap <TypeVariable <?>, TypeVarInfo > typeBounds )
@@ -934,11 +939,35 @@ private static boolean satisfiesTypeVariable(final Type arg,
934939 param ));
935940 // check to make sure that arg is a viable replacement for the type
936941 // variable.
937- if (!typeBounds .get (param ).allowType (arg )) return false ;
942+ // We call the fixBounds method the refuseWildcards flag to be false,
943+ // as the type variable was not contained in a parameterized type. Hence,
944+ // the var may be applicable to wildcards
945+ if (!typeBounds .get (param ).allowType (arg , false )) return false ;
938946
939947 // if the type variable refers to some parameterized type (e.g. T extends
940948 // List<?>), call satisfiesTypeParameters on the bounds of param that
941949 // are ParameterizedTypes.
950+ if (!satisfiesTypeVariableBounds (arg , param , typeBounds )) return false ;
951+ return true ;
952+ }
953+
954+ /**
955+ * Determines whether or not the {@link Type} {@code arg} can satisfy the
956+ * bounds of {@link TypeVariable} {@code param} given the preexisting
957+ * limitations of {@code param}.
958+ *
959+ * @param arg - a Type Parameter for a given {@link ParameterizedType}
960+ * @param param - a {@link TypeVariable} that could potentially be replaced
961+ * with {@code arg}
962+ * @param typeBounds - a {@link HashMap} containing the current restrictions
963+ * of the {@link TypeVariable}s
964+ * @return {@code boolean} - true if {@code arg} can satisfy the bounds of
965+ * {@code param}.
966+ */
967+ private static boolean satisfiesTypeVariableBounds (final Type arg ,
968+ final TypeVariable <?> param ,
969+ final HashMap <TypeVariable <?>, TypeVarInfo > typeBounds )
970+ {
942971 final Type [] paramBounds = typeBounds .get (param ).upperBounds ;
943972 for (final Type paramBound : paramBounds ) {
944973 // only have to check the bounds of the
@@ -961,7 +990,6 @@ else if (satisfies(new Type[] { argType }, new Type[] {
961990 }
962991 }
963992 }
964-
965993 return true ;
966994 }
967995
@@ -1270,6 +1298,14 @@ public static Type parameterizeRaw(Class<?> rawType) {
12701298 }
12711299
12721300 // -- Helper methods --
1301+
1302+ private static Type [] filterIndices (Type [] types , List <Integer > indices ) {
1303+ return IntStream
1304+ .range (0 , types .length )
1305+ .filter (i -> !indices .contains (i ))
1306+ .mapToObj (i -> types [i ])
1307+ .toArray (Type []::new );
1308+ }
12731309
12741310 private static IllegalArgumentException iae (final String ... s ) {
12751311 return iae (null , s );
@@ -1307,21 +1343,46 @@ public TypeVarInfo(final TypeVariable<?> var) {
13071343 this .upperBounds = var .getBounds ();
13081344 this .types = new HashSet <>();
13091345 }
1346+
1347+ public boolean typesContainWildcard () {
1348+ return types .stream ().anyMatch (t -> t instanceof WildcardType );
1349+ }
1350+
1351+ public boolean wildcardAllowedInParameterizedType (Type type ) {
1352+ if (typesContainWildcard ()) {
1353+ return false ;
1354+ }
1355+ if (!types .isEmpty () && type instanceof WildcardType ) {
1356+ return false ;
1357+ }
1358+
1359+ return true ;
1360+ }
13101361
13111362 /**
1312- * Sets the type of this {@link TypeVarInfo} to the type parameter, if it is
1363+ * Adds the type of this {@link TypeVarInfo} to the type parameter, if it is
13131364 * allowed. If the type parameter is not contained within the bounds of the
13141365 * {@link TypeVariable}, then we return false, and the type of the
13151366 * {@code TypeVariable} is not changed. N.B. if this {@code TypeVariable} is
13161367 * being used in an {@link ParameterizedType} then
13171368 * {@link TypeVarInfo#fixBounds} should be used instead.
1369+ * Furthermore, it can be specified if wildcards should be refused, which
1370+ * means that the method will return false if:
1371+ * <li>a wilcard is already allowed for this type var</li>
1372+ * <li>{@code bound} is a wildcard and other types are already allowed</li>
1373+ * This is useful if the type variable was contained in a parameterized
1374+ * type, hence it only allows wildcards if it was not bound to a type yet.
13181375 *
13191376 * @param type - the {@link Type} we are trying to assign to this
13201377 * {@link TypeVarInfo}.
13211378 * @return {@code boolean} - false if the {@code Type} is not assignable to
13221379 * this {@code TypeVarInfo}
13231380 */
1324- public boolean allowType (final Type type ) {
1381+ public boolean allowType (final Type type , boolean refuseWildcards ) {
1382+ if (refuseWildcards && !wildcardAllowedInParameterizedType (type )) {
1383+ return false ;
1384+ }
1385+
13251386 final Class <?> typeClass = Types .raw (type );
13261387 // make sure that type extends all of the bounds of the type variable
13271388 for (final Type upperBound : upperBounds ) {
@@ -1336,11 +1397,22 @@ public boolean allowType(final Type type) {
13361397 * If a {@link TypeVariable} is used in a {@link ParameterizedType}, the
13371398 * bounds have to be fixed such that the upperBound is the {@link Type}
13381399 * parameterizing that {@link ParameterizedType}.
1339- *
1400+ * Furthermore, it can be specified if wildcards should be refused, which
1401+ * means that the method will return false if:
1402+ * <li>a wilcard is already allowed for this type var</li>
1403+ * <li>{@code bound} is a wildcard and other types are already allowed</li>
1404+ * This is useful if the type variable was contained in a parameterized
1405+ * type, hence it only allows wildcards if it was not bound to a type yet.
1406+ *
13401407 * @param bound
1408+ * @param refuseWildcards
13411409 * @return
13421410 */
1343- public boolean fixBounds (final Type bound ) {
1411+ public boolean fixBounds (final Type bound , boolean refuseWildcards ) {
1412+ if (refuseWildcards && !wildcardAllowedInParameterizedType (bound )) {
1413+ return false ;
1414+ }
1415+
13441416 for (int i = 0 ; i < upperBounds .length ; i ++) {
13451417 if (Types .raw (upperBounds [i ]).isAssignableFrom (Types .raw (bound )))
13461418 upperBounds [i ] = bound ;
@@ -1353,7 +1425,7 @@ public boolean fixBounds(final Type bound) {
13531425 temp .add (bound );
13541426 types = new HashSet <>();
13551427 for (final Type type : temp ) {
1356- if (!allowType (type )) return false ;
1428+ if (!allowType (type , false )) return false ;
13571429 }
13581430 return true ;
13591431 }
@@ -1381,6 +1453,26 @@ public String toString() {
13811453 return s .toString ();
13821454 }
13831455 }
1456+
1457+ /**
1458+ * Class for {@link TypeVariable} which were contained in {@link ParameterizedType}s.
1459+ * The only difference to {@link TypeVarInfo} is that {@link TypeVarInfo#allowType(Type, boolean)}
1460+ * will be always called with refuseWildcards flag to be true.
1461+ */
1462+ private static class TypeVarFromParameterizedTypeInfo extends TypeVarInfo {
1463+
1464+ public TypeVarFromParameterizedTypeInfo (TypeVariable <?> var ) {
1465+ super (var );
1466+ }
1467+
1468+ /**
1469+ * @param refuseWildcards hardcoded to true
1470+ */
1471+ @ Override
1472+ public boolean allowType (Type type , boolean refuseWildcards ) {
1473+ return super .allowType (type , true );
1474+ }
1475+ }
13841476
13851477 // -- BEGIN FORK OF APACHE COMMONS LANG 3.4 CODE --
13861478
0 commit comments