|
66 | 66 | import java.util.List; |
67 | 67 | import java.util.Map; |
68 | 68 | import java.util.Objects; |
| 69 | +import java.util.Optional; |
69 | 70 | import java.util.Set; |
70 | 71 |
|
| 72 | +import com.google.common.reflect.TypeToken; |
| 73 | + |
71 | 74 | /** |
72 | 75 | * Utility class for working with generic types, fields and methods. |
73 | 76 | * <p> |
@@ -763,6 +766,244 @@ public static boolean isInstance(final Object obj, final Class<?> dest) { |
763 | 766 | if (dest == null) return false; |
764 | 767 | return obj == null || dest.isInstance(obj); |
765 | 768 | } |
| 769 | + |
| 770 | + /** |
| 771 | + * Discerns whether it would be legal to pass a sequence of references of the |
| 772 | + * given source types to a method with parameters typed according to the |
| 773 | + * specified sequence of destination types. |
| 774 | + * <p> |
| 775 | + * An example: suppose you have a method |
| 776 | + * {@code <T extends Number> mergeLists(List<T> list1, List<T> list2)}. It is |
| 777 | + * legal to pass two {@code List<Integer>} instances to the method, but |
| 778 | + * illegal to pass a {@code List<Integer>} and {@code List<Double>} because |
| 779 | + * {@code T} cannot be both {@code Integer} and {@code Double} simultaneously. |
| 780 | + * </p> |
| 781 | + * |
| 782 | + * @param src |
| 783 | + * @param dest |
| 784 | + * @return -1 if the args satisfy the params, otherwise the index of the arg |
| 785 | + * that does not satisfy its parameter. |
| 786 | + */ |
| 787 | + public static int satisfies(final Type[] args, final Type[] params) { |
| 788 | + // create a HashMap to monitor the restrictions of the type variables. |
| 789 | + final HashMap<TypeVariable<?>, TypeVarInfo> typeBounds = new HashMap<>(); |
| 790 | + |
| 791 | + return satisfies(args, params, typeBounds); |
| 792 | + } |
| 793 | + |
| 794 | + public static int satisfies(final Type[] args, final Type[] params, |
| 795 | + final HashMap<TypeVariable<?>, TypeVarInfo> typeBounds) |
| 796 | + { |
| 797 | + if (args.length != params.length) { |
| 798 | + throw new IllegalArgumentException("src and dest lengths differ"); |
| 799 | + } |
| 800 | + |
| 801 | + for (int i = 0; i < params.length; i++) { |
| 802 | + Type arg = args[i]; |
| 803 | + Type param = params[i]; |
| 804 | + // First, check raw type assignability. |
| 805 | + if (!satisfiesRawTypes(arg, param)) return i; |
| 806 | + |
| 807 | + if (param instanceof ParameterizedType) { |
| 808 | + if (!satisfiesParameterizedTypes(arg, (ParameterizedType) param, |
| 809 | + typeBounds)) return i; |
| 810 | + } |
| 811 | + else if (param instanceof TypeVariable) { |
| 812 | + if (!satisfiesTypeVariable(arg, (TypeVariable<?>) param, |
| 813 | + typeBounds)) return i; |
| 814 | + } |
| 815 | + else if (param instanceof WildcardType) { |
| 816 | + if (!satisfiesWildcardType(arg, (WildcardType) param)) return i; |
| 817 | + } |
| 818 | + else if (param instanceof GenericArrayType) { |
| 819 | + if (!satisfiesGenericArrayType(arg, (GenericArrayType) param, |
| 820 | + typeBounds)) return i; |
| 821 | + } |
| 822 | + final Type t = TypeToken.of(param).resolveType(arg).getType(); |
| 823 | +// System.out.println("src[" + i + "] = " + t); |
| 824 | + } |
| 825 | + return -1; |
| 826 | + } |
| 827 | + |
| 828 | + public static boolean satisfies( |
| 829 | + final Map<TypeVariable<?>, Type> typeVarAssigns) |
| 830 | + { |
| 831 | + return TypeUtils.typesSatisfyVariables(typeVarAssigns); |
| 832 | + } |
| 833 | + |
| 834 | + private static boolean satisfiesRawTypes(final Type arg, final Type param) { |
| 835 | + final List<Class<?>> srcClasses = Types.raws(arg); |
| 836 | + final List<Class<?>> destClasses = Types.raws(param); |
| 837 | + for (final Class<?> destClass : destClasses) { |
| 838 | + final Optional<Class<?>> first = srcClasses.stream().filter( |
| 839 | + srcClass -> destClass.isAssignableFrom(srcClass)).findFirst(); |
| 840 | + if (!first.isPresent()) { |
| 841 | + // TODO can we remove this? |
| 842 | +// throw new IllegalArgumentException("Argument #" + i + // |
| 843 | +// " (" + Types.name(src[i]) + ") is not assignable to " + // |
| 844 | +// "destination type (" + Types.name(dest[i]) + ")"); |
| 845 | + return false; |
| 846 | + } |
| 847 | + } |
| 848 | + return true; |
| 849 | + } |
| 850 | + |
| 851 | + private static boolean satisfiesParameterizedTypes(final Type arg, |
| 852 | + final ParameterizedType param, |
| 853 | + final HashMap<TypeVariable<?>, TypeVarInfo> typeBounds) |
| 854 | + { |
| 855 | + // get an array of the destination parameter types |
| 856 | + final Type[] destTypes = param.getActualTypeArguments(); |
| 857 | + |
| 858 | + // get an array of the source argument types |
| 859 | + final Type[] srcTypes = new Type[destTypes.length]; |
| 860 | + for (int i = 0; i < srcTypes.length; i++) { |
| 861 | + srcTypes[i] = Types.param(arg, Types.raw(arg), i); |
| 862 | + } |
| 863 | + |
| 864 | + // check to see if any of the Types of this ParameterizedType are |
| 865 | + // TypeVariables, if so restrict them to the type parameter of the argument. |
| 866 | + for (int i = 0; i < destTypes.length; i++) { |
| 867 | + final Type destType = destTypes[i]; |
| 868 | + if (destType instanceof TypeVariable<?>) { |
| 869 | + final Type srcType = srcTypes[i]; |
| 870 | + final TypeVariable<?> destTypeVar = (TypeVariable<?>) destType; |
| 871 | + if (!satisfiesTypeParameter(srcType, destTypeVar, typeBounds)) |
| 872 | + return false; |
| 873 | + } |
| 874 | + } |
| 875 | + // recursively run satisfies on these arrays |
| 876 | + return satisfies(srcTypes, destTypes, typeBounds) == -1; |
| 877 | + |
| 878 | + } |
| 879 | + |
| 880 | + /** |
| 881 | + * Determines whether or not the {@link Type} {@code arg} can satisfy the |
| 882 | + * {@link TypeVariable} {@code param} given the preexisting limitations of |
| 883 | + * {@code param}. If it is determined that this replacement is allowed the |
| 884 | + * {@link TypeVariable} is then restriced to the {@link Type} of {@code arg} |
| 885 | + * (if this is not desired use {@link #satisfiesTypeVariable} instead}. |
| 886 | + * |
| 887 | + * @param arg - a Type Parameter for a given {@link ParameterizedType} |
| 888 | + * @param param - a {@link TypeVariable} that could potentially be replaced |
| 889 | + * with {@code arg} |
| 890 | + * @param typeBounds - a {@link HashMap} containing the current restrictions |
| 891 | + * of the {@link TypeVariable}s |
| 892 | + * @return {@code boolean} - true if the replacement of {@code param} with |
| 893 | + * {@code arg} is allowed. |
| 894 | + */ |
| 895 | + private static boolean satisfiesTypeParameter(final Type arg, |
| 896 | + final TypeVariable<?> param, |
| 897 | + final HashMap<TypeVariable<?>, TypeVarInfo> typeBounds) |
| 898 | + { |
| 899 | + // add the type variable to the HashMap if it does not yet exist. |
| 900 | + if (!typeBounds.containsKey(param)) { |
| 901 | + final TypeVariable<?> newTypeVar = param; |
| 902 | + typeBounds.put(newTypeVar, new TypeVarInfo(newTypeVar)); |
| 903 | + } |
| 904 | + // attempt to restrict the bounds of the type variable to the argument. |
| 905 | + if (!typeBounds.get(param).fixBounds(arg)) return false; |
| 906 | + |
| 907 | + // TODO do we need type parameter recursion here as we do in |
| 908 | + // satisfiesTypeVariable? |
| 909 | + |
| 910 | + return true; |
| 911 | + } |
| 912 | + |
| 913 | + /** |
| 914 | + * @param arg |
| 915 | + * @param param |
| 916 | + * @param typeBounds |
| 917 | + * @return |
| 918 | + */ |
| 919 | + private static boolean satisfiesTypeVariable(final Type arg, |
| 920 | + final TypeVariable<?> param, |
| 921 | + final HashMap<TypeVariable<?>, TypeVarInfo> typeBounds) |
| 922 | + { |
| 923 | + // if the TypeVariable is not already in the hashMap add it. |
| 924 | + if (!typeBounds.containsKey(param)) typeBounds.put(param, new TypeVarInfo( |
| 925 | + param)); |
| 926 | + // check to make sure that arg is a viable replacement for the type |
| 927 | + // variable. |
| 928 | + if (!typeBounds.get(param).allowType(arg)) return false; |
| 929 | + |
| 930 | + // if the type variable refers to some parameterized type (e.g. T extends |
| 931 | + // List<?>), call satisfiesTypeParameters on the bounds of param that |
| 932 | + // are ParameterizedTypes. |
| 933 | + final Type[] paramBounds = typeBounds.get(param).upperBounds; |
| 934 | + for (final Type paramBound : paramBounds) { |
| 935 | + // only have to check the bounds of the |
| 936 | + if (paramBound instanceof ParameterizedType) { |
| 937 | + |
| 938 | + final ParameterizedType paramBoundType = (ParameterizedType) paramBound; |
| 939 | + final Type[] paramBoundTypes = paramBoundType.getActualTypeArguments(); |
| 940 | + for (int i = 0; i < paramBoundTypes.length; i++) { |
| 941 | + final Type argType = Types.param(arg, Types.raw(param), i); |
| 942 | + if (paramBoundTypes[i] instanceof TypeVariable<?> && |
| 943 | + !satisfiesTypeParameter(argType, |
| 944 | + (TypeVariable<?>) paramBoundTypes[i], typeBounds)) return false; |
| 945 | + else if (satisfies(new Type[] { argType }, new Type[] { |
| 946 | + paramBoundTypes[i] }, typeBounds) != -1) return false; |
| 947 | + } |
| 948 | + } |
| 949 | + } |
| 950 | + |
| 951 | + return true; |
| 952 | + } |
| 953 | + |
| 954 | + private static boolean satisfiesWildcardType(final Type arg, |
| 955 | + final WildcardType param) |
| 956 | + { |
| 957 | + final Type[] upperBounds = param.getUpperBounds(); |
| 958 | + final Type[] lowerBounds = param.getLowerBounds(); |
| 959 | + final Class<?> argType = Types.raw(arg); |
| 960 | + for (final Type upperBound : upperBounds) { |
| 961 | + // check that the argument can satisfy the parameter |
| 962 | + final Class<?> upperType = Types.raw(upperBound); |
| 963 | + if (!upperType.isAssignableFrom(argType)) return false; |
| 964 | + } |
| 965 | + for (final Type lowerBound : lowerBounds) { |
| 966 | + final Class<?> lowerType = Types.raw(lowerBound); |
| 967 | + // check that the argument can satisfy the parameter |
| 968 | + if (!argType.isAssignableFrom(lowerType)) return false; |
| 969 | + } |
| 970 | + |
| 971 | + return true; |
| 972 | + } |
| 973 | + |
| 974 | + private static boolean satisfiesGenericArrayType(final Type arg, |
| 975 | + final GenericArrayType param, |
| 976 | + final HashMap<TypeVariable<?>, TypeVarInfo> typeBounds) |
| 977 | + { |
| 978 | + // get a class object of the component type of each array |
| 979 | + final Type argComponent = Types.component(arg); |
| 980 | + final Type paramComponent = Types.component(param); |
| 981 | + |
| 982 | + if (paramComponent instanceof ParameterizedType) { |
| 983 | + // TODO are these casts safe? |
| 984 | + final ParameterizedType argType = (ParameterizedType) argComponent; |
| 985 | + final ParameterizedType paramType = (ParameterizedType) paramComponent; |
| 986 | + |
| 987 | + if (!satisfiesParameterizedTypes(argType, paramType, typeBounds)) |
| 988 | + return false; |
| 989 | + } |
| 990 | + |
| 991 | + else if (paramComponent instanceof TypeVariable) { |
| 992 | + // TODO are these casts safe? |
| 993 | + final TypeVariable<?> paramType = (TypeVariable<?>) paramComponent; |
| 994 | + |
| 995 | + if (!satisfiesTypeVariable(argComponent, paramType, typeBounds)) |
| 996 | + return false; |
| 997 | + } |
| 998 | + |
| 999 | + // TODO is this necessary? It is only necessary if the component is allowed |
| 1000 | + // to be |
| 1001 | + // something other than a ParameterizedType or a TypeVariable. |
| 1002 | + final Class<?> argClass = Types.raw(argComponent); |
| 1003 | + final Class<?> paramClass = Types.raw(paramComponent); |
| 1004 | + if (!paramClass.isAssignableFrom(argClass)) return false; |
| 1005 | + return true; |
| 1006 | + } |
766 | 1007 |
|
767 | 1008 | /** |
768 | 1009 | * Casts the given object to the specified type, or null if the types are |
@@ -994,6 +1235,73 @@ private static Class<?> arrayOrNull(final Class<?> componentType) { |
994 | 1235 | return null; |
995 | 1236 | } |
996 | 1237 | } |
| 1238 | + |
| 1239 | + /** |
| 1240 | + * maintains info about an {@link TypeVariable}. |
| 1241 | + */ |
| 1242 | + private static class TypeVarInfo { |
| 1243 | + |
| 1244 | + private final TypeVariable<?> var; |
| 1245 | + private final Type[] upperBounds; |
| 1246 | + private List<Type> types; |
| 1247 | + |
| 1248 | + public TypeVarInfo(final TypeVariable<?> var) { |
| 1249 | + this.var = var; |
| 1250 | + this.upperBounds = var.getBounds(); |
| 1251 | + this.types = new ArrayList<>(); |
| 1252 | + } |
| 1253 | + |
| 1254 | + /** |
| 1255 | + * Sets the type of this {@link TypeVarInfo} to the type parameter, if it is |
| 1256 | + * allowed. If the type parameter is not contained within the bounds of the |
| 1257 | + * {@link TypeVariable}, then we return false, and the type of the |
| 1258 | + * {@code TypeVariable} is not changed. N.B. if this {@code TypeVariable} is |
| 1259 | + * being used in an {@link ParameterizedType} then |
| 1260 | + * {@link TypeVarInfo#fixBounds} should be used instead. |
| 1261 | + * |
| 1262 | + * @param type - the {@link Type} we are trying to assign to this |
| 1263 | + * {@link TypeVarInfo}. |
| 1264 | + * @return {@code boolean} - false if the {@code Type} is not assignable to |
| 1265 | + * this {@code TypeVarInfo} |
| 1266 | + */ |
| 1267 | + public boolean allowType(final Type type) { |
| 1268 | + final Class<?> typeClass = Types.raw(type); |
| 1269 | + // make sure that type extends all of the bounds of the type variable |
| 1270 | + for (final Type upperBound : upperBounds) { |
| 1271 | + if (!Types.raw(upperBound).isAssignableFrom(typeClass)) return false; |
| 1272 | + } |
| 1273 | + |
| 1274 | + types.add(type); |
| 1275 | + return true; |
| 1276 | + } |
| 1277 | + |
| 1278 | + /** |
| 1279 | + * If a {@link TypeVariable} is used in a {@link ParameterizedType}, the |
| 1280 | + * bounds have to be fixed such that the upperBound is the {@link Type} |
| 1281 | + * parameterizing that {@link ParameterizedType}. |
| 1282 | + * |
| 1283 | + * @param bound |
| 1284 | + * @return |
| 1285 | + */ |
| 1286 | + public boolean fixBounds(final Type bound) { |
| 1287 | + for (int i = 0; i < upperBounds.length; i++) { |
| 1288 | + if (Types.raw(upperBounds[i]).isAssignableFrom(Types.raw(bound))) |
| 1289 | + upperBounds[i] = bound; |
| 1290 | + else return false; |
| 1291 | + } |
| 1292 | + |
| 1293 | + // make sure that all of the types that already fit in this variable |
| 1294 | + // before the variable was fixed still are allowed by the type variable. |
| 1295 | + final List<Type> temp = types; |
| 1296 | + temp.add(bound); |
| 1297 | + types = new ArrayList<>(); |
| 1298 | + for (final Type type : temp) { |
| 1299 | + if (!allowType(type)) return false; |
| 1300 | + } |
| 1301 | + |
| 1302 | + return true; |
| 1303 | + } |
| 1304 | + } |
997 | 1305 |
|
998 | 1306 | // -- BEGIN FORK OF APACHE COMMONS LANG 3.4 CODE -- |
999 | 1307 |
|
|
0 commit comments