Skip to content

Commit 2f32ddc

Browse files
Treiblesschorlegselzer
authored andcommitted
Generalize generic assignability to Type input
1 parent f68d311 commit 2f32ddc

1 file changed

Lines changed: 25 additions & 14 deletions

File tree

‎src/main/java/org/scijava/ops/matcher/MatchingUtils.java‎

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,9 @@ public static int checkGenericOutputsAssignability(Type[] froms, Type[] tos,
113113
}
114114

115115
/**
116-
* Checks whether it would be legal to assign the {@link ParameterizedType}
117-
* source, represented as a raw type, to the specified
118-
* {@link ParameterizedType} destination (which could possibly be a
119-
* supertype of the source type). Thereby, possible {@link TypeVariable}s
116+
* Checks whether it would be legal to assign the {@link Type} source to the
117+
* specified {@link ParameterizedType} destination (which could possibly be
118+
* a supertype of the source type). Thereby, possible {@link TypeVariable}s
120119
* contained in the parameters of the source are tried to be inferred in the
121120
* sense of empty angle brackets when a new object is created:
122121
*
@@ -188,27 +187,39 @@ public static int checkGenericOutputsAssignability(Type[] froms, Type[] tos,
188187
* </ul>
189188
*
190189
* @param src
191-
* raw type representing the parameterized type of which
192-
* assignment should be checked
190+
* the type for which assignment should be checked from
193191
* @param dest
194192
* the parameterized type for which assignment should be checked
195193
* to
196194
* @return whether and assignment of source to destination would be a legal
197195
* java statement
198196
*/
199-
public static boolean checkGenericAssignability(Class<?> src, ParameterizedType dest) {
197+
public static boolean checkGenericAssignability(Type src, ParameterizedType dest) {
200198
// check raw assignability
201-
if (!Types.isAssignable(src, Types.raw(dest)))
199+
if (!Types.isAssignable(Types.raw(src), Types.raw(dest)))
202200
return false;
203201

204-
Type[] destTypes = dest.getActualTypeArguments();
205-
// get type arguments of raw src for common (possible supertype) dest
206-
Type[] srcTypes = getParams(src, Types.raw(dest));
202+
Type[] destTypes;
203+
Type[] srcTypes;
207204

205+
if (src instanceof Class) {
206+
destTypes = dest.getActualTypeArguments();
207+
// get type arguments of raw src for common (possible supertype)
208+
// dest
209+
srcTypes = getParams((Class<?>) src, Types.raw(dest));
210+
} else if (src instanceof ParameterizedType) {
211+
destTypes = dest.getActualTypeArguments();
212+
srcTypes = ((ParameterizedType) src).getActualTypeArguments();
213+
} else {
214+
return Types.isAssignable(src, dest);
215+
}
216+
217+
return checkGenericAssignability(srcTypes, destTypes, dest);
218+
}
219+
220+
private static boolean checkGenericAssignability(Type[] srcTypes, Type[] destTypes, Type dest) {
208221
// if the number of type arguments does not match, the types can't be
209222
// assignable
210-
// TODO: Find out if this could ever happen with the way how we retrieve
211-
// the arguments above
212223
if (srcTypes.length != destTypes.length) {
213224
return false;
214225
}
@@ -229,7 +240,7 @@ public static boolean checkGenericAssignability(Class<?> src, ParameterizedType
229240
// Build a new parameterized type from inferred types and check
230241
// assignability
231242
Class<?> matchingRawType = Types.raw(dest);
232-
Type inferredSrcType = containsNull(mappedSrcTypes) ? src : Types.parameterize(matchingRawType, mappedSrcTypes);
243+
Type inferredSrcType = Types.parameterize(matchingRawType, mappedSrcTypes);
233244
if (!Types.isAssignable(inferredSrcType, dest)) {
234245
return false;
235246
}

0 commit comments

Comments
 (0)