Skip to content

Commit 7dac25d

Browse files
committed
inferTypeVariables: handle Object inferFrom
This situation comes up often when calling something along the lines of: ops.op("name").inType(Double.class).apply() which creates an OpRef looking for a Function<Double, Object> Suppose the Op we wanted to match was a Function<I, Iterable<O>> (Disregard how bad of a Function typing this is) MatchingUtils.inferTypeVariables() did not put the O in its typeMappings, which throws IAEs later on. This is specifically because Object IS NOT a superType of Iterable (and therefore I do not think this problem can occur with any type other than Object??). This problem is solved by adding Anys whenever an Object is found.
1 parent 8c9fff5 commit 7dac25d

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,11 @@ private static void inferTypeVariables(ParameterizedType type, Type inferFrom, M
612612
// of type.
613613
ParameterizedType superTypeOfType = (ParameterizedType) Types
614614
.getExactSuperType(type, Types.raw(inferFrom));
615-
if (superTypeOfType == null) throw new TypeInferenceException(
616-
inferFrom + " cannot be implicitly cast to " + type +
615+
if (superTypeOfType == null) {
616+
throw new TypeInferenceException(inferFrom +
617+
" cannot be implicitly cast to " + type +
617618
", thus it is impossible to infer type variables for " + inferFrom);
619+
}
618620
inferTypeVariables(superTypeOfType.getActualTypeArguments(),
619621
((ParameterizedType) inferFrom).getActualTypeArguments(),
620622
typeMappings, false);
@@ -625,6 +627,13 @@ private static void inferTypeVariables(ParameterizedType type, Type inferFrom, M
625627
}
626628
}
627629
else {
630+
// getExactSuperType does not return Object as a superType for any
631+
// interface, so we cover it here.
632+
// TODO: clean
633+
if (Object.class.equals(inferFrom)) {
634+
mapTypeVarsToAny(type, typeMappings);
635+
return;
636+
}
628637
if (inferFrom instanceof WildcardType) {
629638
WildcardType inferFromWildcard = (WildcardType) inferFrom;
630639
if (inferFromWildcard.getUpperBounds().length == 1) inferFrom = inferFromWildcard.getUpperBounds()[0];
@@ -640,6 +649,37 @@ private static void inferTypeVariables(ParameterizedType type, Type inferFrom, M
640649
}
641650
}
642651

652+
private static void mapTypeVarsToAny(Type type,
653+
Map<TypeVariable<?>, TypeMapping> typeMappings)
654+
{
655+
if(!Types.containsTypeVars(type)) return;
656+
657+
if(type instanceof TypeVariable) {
658+
if (typeMappings.containsKey(type)) return;
659+
TypeVariable<?> typeVar = (TypeVariable<?>) type;
660+
typeMappings.put(typeVar, suitableTypeMapping(typeVar, new Any(), true));
661+
}
662+
else if(type instanceof ParameterizedType) {
663+
ParameterizedType pType = (ParameterizedType) type;
664+
Type[] typeParams = pType.getActualTypeArguments();
665+
for (Type typeParam : typeParams) {
666+
mapTypeVarsToAny(typeParam, typeMappings);
667+
}
668+
}
669+
else if (type instanceof WildcardType) {
670+
WildcardType wildcard = (WildcardType) type;
671+
for (Type lowerBound : wildcard.getLowerBounds())
672+
mapTypeVarsToAny(lowerBound, typeMappings);
673+
for (Type upperBound : wildcard.getUpperBounds())
674+
mapTypeVarsToAny(upperBound, typeMappings);
675+
}
676+
else if (type instanceof Class) {
677+
Class<?> clazz = (Class<?>) type;
678+
for (Type typeParam : clazz.getTypeParameters())
679+
mapTypeVarsToAny(typeParam, typeMappings);
680+
}
681+
}
682+
643683
private static void inferTypeVariables(WildcardType type, Type inferFrom, Map<TypeVariable<?>, TypeMapping> typeMappings) {
644684
Type inferrableBound = getInferrableBound(type);
645685
if (inferFrom instanceof WildcardType) {

0 commit comments

Comments
 (0)