Skip to content

Commit cc1c4ee

Browse files
committed
Merge branch 'scijava/scijava-ops/inferTypeVariablesWithAny'
Closes #49 See scijava/incubator#22
2 parents 8c9fff5 + dee9d1b commit cc1c4ee

8 files changed

Lines changed: 446 additions & 118 deletions

File tree

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

Lines changed: 123 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,9 @@ else if (type instanceof Class) {
550550

551551
}
552552

553-
private static void inferTypeVariables(TypeVariable<?> type, Type inferFrom, Map<TypeVariable<?>, TypeMapping> typeMappings, boolean malleable) {
553+
private static void inferTypeVariables(TypeVariable<?> type, Type inferFrom,
554+
Map<TypeVariable<?>, TypeMapping> typeMappings, boolean malleable)
555+
{
554556
TypeMapping typeData = typeMappings.get(type);
555557
// If current is not null then we have already encountered that
556558
// variable. If so, we require them to be exactly the same, and throw a
@@ -560,86 +562,140 @@ private static void inferTypeVariables(TypeVariable<?> type, Type inferFrom, Map
560562
}
561563
else {
562564
resolveTypeInMap(type, inferFrom, typeMappings, malleable);
563-
}
564-
565-
// Bounds could also contain type vars, hence possibly go into
566-
// recursion
567-
for (Type bound : type.getBounds()) {
568-
if (bound instanceof TypeVariable && typeMappings.get(bound) != null) {
569-
// If the bound of the current var (let's call it A) to
570-
// infer is also a var (let's call it B):
571-
// If we already encountered B, we check if the current
572-
// type to infer from is assignable to
573-
// the already inferred type for B. In this case we do
574-
// not require equality as one var is
575-
// bounded by another and it is not the same. E.g.
576-
// assume we want to infer the types of vars:
577-
// - - - A extends Number, B extends A
578-
// From types:
579-
// - - - Number, Double
580-
// First A is bound to Number, next B to Double. Then we
581-
// check the bounds for B. We encounter A,
582-
// for which we already inferred Number. Hence, it
583-
// suffices to check whether Double can be assigned
584-
// to Number, it does not have to be equal as it is just
585-
// a transitive bound for B.
586-
Type typeAssignForBound = typeMappings.get(bound).getType();
587-
if (!Types.isAssignable(inferFrom, typeAssignForBound)) {
588-
throw new TypeInferenceException();
565+
// Bounds could also contain type vars, hence possibly go into
566+
// recursion
567+
for (Type bound : type.getBounds()) {
568+
if (bound instanceof TypeVariable && typeMappings.get(bound) != null) {
569+
// If the bound of the current var (let's call it A) to
570+
// infer is also a var (let's call it B):
571+
// If we already encountered B, we check if the current
572+
// type to infer from is assignable to
573+
// the already inferred type for B. In this case we do
574+
// not require equality as one var is
575+
// bounded by another and it is not the same. E.g.
576+
// assume we want to infer the types of vars:
577+
// - - - A extends Number, B extends A
578+
// From types:
579+
// - - - Number, Double
580+
// First A is bound to Number, next B to Double. Then we
581+
// check the bounds for B. We encounter A,
582+
// for which we already inferred Number. Hence, it
583+
// suffices to check whether Double can be assigned
584+
// to Number, it does not have to be equal as it is just
585+
// a transitive bound for B.
586+
Type typeAssignForBound = typeMappings.get(bound).getType();
587+
if (!Types.isAssignable(inferFrom, typeAssignForBound)) {
588+
throw new TypeInferenceException();
589+
}
590+
}
591+
else {
592+
// Else go into recursion as we encountered a new var.
593+
inferTypeVariables(bound, inferFrom, typeMappings);
589594
}
590-
} else {
591-
// Else go into recursion as we encountered a new var.
592-
inferTypeVariables( bound, inferFrom, typeMappings);
593-
}
595+
}
596+
594597
}
595598
}
596599

597-
private static void inferTypeVariables(ParameterizedType type, Type inferFrom, Map<TypeVariable<?>, TypeMapping> typeMappings) {
598-
// Recursively follow parameterized types
599-
if (inferFrom instanceof ParameterizedType) {
600-
// Finding the supertype here is really important. Suppose that we are
601-
// inferring from a StrangeThing<Long> extends Thing<Double> and our
602-
// Op requires a Thing<T>. We need to ensure that T gets
603-
// resolved to a Double and NOT a Long.
604-
ParameterizedType paramInferFrom = (ParameterizedType) Types
605-
.getExactSuperType(inferFrom, Types.raw(type));
606-
if (paramInferFrom == null) {
607-
// this means that inferFrom is NOT a superType of type. There is,
608-
// however, one more thing that we can try. It is possible for inferFrom
609-
// to be a subType of type (this can often happen when
610-
// checkGenericAssignability is working with Functions). In this case,
611-
// it is possible that inferFrom has information about the typeVariables
612-
// of type.
613-
ParameterizedType superTypeOfType = (ParameterizedType) Types
614-
.getExactSuperType(type, Types.raw(inferFrom));
615-
if (superTypeOfType == null) throw new TypeInferenceException(
616-
inferFrom + " cannot be implicitly cast to " + type +
617-
", thus it is impossible to infer type variables for " + inferFrom);
618-
inferTypeVariables(superTypeOfType.getActualTypeArguments(),
619-
((ParameterizedType) inferFrom).getActualTypeArguments(),
620-
typeMappings, false);
621-
}
622-
else {
623-
inferTypeVariables(type.getActualTypeArguments(), paramInferFrom
624-
.getActualTypeArguments(), typeMappings, false);
625-
}
600+
private static void inferTypeVariables(ParameterizedType type, Type inferFrom,
601+
Map<TypeVariable<?>, TypeMapping> typeMappings)
602+
{
603+
if (inferFrom instanceof WildcardType) {
604+
inferFrom = getInferrableBound((WildcardType) inferFrom);
626605
}
627-
else {
628-
if (inferFrom instanceof WildcardType) {
629-
WildcardType inferFromWildcard = (WildcardType) inferFrom;
630-
if (inferFromWildcard.getUpperBounds().length == 1) inferFrom = inferFromWildcard.getUpperBounds()[0];
631-
}
606+
if (inferFrom instanceof Any) {
607+
Any any = (Any) inferFrom;
608+
mapTypeVarsToAny(type, any, typeMappings);
609+
return;
610+
}
611+
// Finding the supertype here is really important. Suppose that we are
612+
// inferring from a StrangeThing<Long> extends Thing<Double> and our
613+
// Op requires a Thing<T>. We need to ensure that T gets
614+
// resolved to a Double and NOT a Long.
615+
Type superInferFrom = Types.getExactSuperType(inferFrom, Types.raw(type));
616+
if (superInferFrom instanceof ParameterizedType) {
617+
ParameterizedType paramInferFrom = (ParameterizedType) superInferFrom;
618+
inferTypeVariables(type.getActualTypeArguments(), paramInferFrom
619+
.getActualTypeArguments(), typeMappings, false);
620+
}
621+
else if (superInferFrom instanceof Class) {
632622
TypeVarAssigns typeVarAssigns = new TypeVarAssigns(typeMappings);
633623
Type mappedType = Types.mapVarToTypes(type, typeVarAssigns);
634624
// Use isAssignable to attempt to infer the type variables present in type
635-
if (!Types.isAssignable(inferFrom, mappedType, typeVarAssigns)) {
625+
if (!Types.isAssignable(superInferFrom, mappedType, typeVarAssigns)) {
636626
throw new TypeInferenceException(inferFrom +
637627
" cannot be implicitly cast to " + mappedType +
638628
", thus it is impossible to infer type variables for " + inferFrom);
639629
}
630+
// for all remaining unmapped type vars, map to Any
631+
mapTypeVarsToAny(type, typeMappings);
632+
}
633+
// -- edge cases -> do our best -- //
634+
else if (superInferFrom == null) {
635+
// edge case 1: if inferFrom is an Object, superInferFrom will be null
636+
// when type is some interface.
637+
if (Object.class.equals(inferFrom)) {
638+
mapTypeVarsToAny(type, typeMappings);
639+
return;
640+
}
641+
// edge case 2: if inferFrom is a superType of type, we can get (some of)
642+
// the types of type by finding the exact superType of type w.r.t.
643+
// inferFrom.
644+
Type superTypeOfType = Types.getExactSuperType(type, Types.raw(
645+
inferFrom));
646+
if (superTypeOfType == null) {
647+
throw new TypeInferenceException(inferFrom +
648+
" cannot be implicitly cast to " + type +
649+
", thus it is impossible to infer type variables for " + inferFrom);
650+
}
651+
inferTypeVariables(superTypeOfType, inferFrom, typeMappings, false);
652+
mapTypeVarsToAny(type, typeMappings);
640653
}
654+
// TODO: elaborate
655+
else throw new IllegalStateException(superInferFrom +
656+
" is the supertype of " + inferFrom + " with respect to " + type +
657+
", however this cannot be (since " + type +
658+
" is a ParamterizedType)! (Only a ParameterizedType, Class, or null " +
659+
"can be returned from Types.getExactSuperType when it is called with a ParameterizedType!)");
641660
}
642-
661+
662+
private static void mapTypeVarsToAny(Type type,
663+
Map<TypeVariable<?>, TypeMapping> typeMappings)
664+
{
665+
mapTypeVarsToAny(type, new Any(), typeMappings);
666+
}
667+
668+
private static void mapTypeVarsToAny(Type type, Any any,
669+
Map<TypeVariable<?>, TypeMapping> typeMappings)
670+
{
671+
if (!Types.containsTypeVars(type)) return;
672+
673+
if (type instanceof TypeVariable) {
674+
if (typeMappings.containsKey(type)) return;
675+
TypeVariable<?> typeVar = (TypeVariable<?>) type;
676+
typeMappings.put(typeVar, suitableTypeMapping(typeVar, any, true));
677+
}
678+
else if (type instanceof ParameterizedType) {
679+
ParameterizedType pType = (ParameterizedType) type;
680+
Type[] typeParams = pType.getActualTypeArguments();
681+
for (Type typeParam : typeParams) {
682+
mapTypeVarsToAny(typeParam, typeMappings);
683+
}
684+
}
685+
else if (type instanceof WildcardType) {
686+
WildcardType wildcard = (WildcardType) type;
687+
for (Type lowerBound : wildcard.getLowerBounds())
688+
mapTypeVarsToAny(lowerBound, typeMappings);
689+
for (Type upperBound : wildcard.getUpperBounds())
690+
mapTypeVarsToAny(upperBound, typeMappings);
691+
}
692+
else if (type instanceof Class) {
693+
Class<?> clazz = (Class<?>) type;
694+
for (Type typeParam : clazz.getTypeParameters())
695+
mapTypeVarsToAny(typeParam, typeMappings);
696+
}
697+
}
698+
643699
private static void inferTypeVariables(WildcardType type, Type inferFrom, Map<TypeVariable<?>, TypeMapping> typeMappings) {
644700
Type inferrableBound = getInferrableBound(type);
645701
if (inferFrom instanceof WildcardType) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
package org.scijava.ops.core.builder;
3+
4+
import java.lang.reflect.Type;
5+
import java.util.ArrayList;
6+
import java.util.function.Function;
7+
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
import org.scijava.ops.AbstractTestEnvironment;
11+
import org.scijava.ops.OpField;
12+
import org.scijava.ops.core.OpCollection;
13+
import org.scijava.param.Parameter;
14+
import org.scijava.plugin.Plugin;
15+
import org.scijava.struct.ItemIO;
16+
import org.scijava.types.Nil;
17+
import org.scijava.types.TypeService;
18+
19+
/**
20+
* Ensures correct behavior in {@link OpBuilder} calls <b>where no output type
21+
* or <code>Object</code> is given</b>.
22+
*
23+
* @author Gabriel Selzer
24+
* @see OpBuilderTest
25+
*/
26+
@Plugin(type = OpCollection.class)
27+
public class OpBuilderNoOutputTest<T extends Number> extends
28+
AbstractTestEnvironment
29+
{
30+
31+
public final String opName = "test.noOutput";
32+
33+
// private wrapper class
34+
private static class WrappedList<E> extends ArrayList<E> {}
35+
36+
@OpField(names = opName)
37+
@Parameter(key = "in", itemIO = ItemIO.INPUT)
38+
@Parameter(key = "out", itemIO = ItemIO.OUTPUT)
39+
public final Function<T, WrappedList<T>> func = in -> {
40+
41+
WrappedList<T> out = new WrappedList<>();
42+
out.add(in);
43+
return out;
44+
};
45+
46+
@Test
47+
public void testNoParameterizedTypeOutputGiven() {
48+
Object output = ops.op(opName).input(5.).apply();
49+
TypeService types = context.getService(TypeService.class);
50+
Type expectedOutputType = new Nil<WrappedList<Double>>() {}.getType();
51+
Assert.assertEquals(types.reify(output), expectedOutputType);
52+
}
53+
}

‎scijava/scijava-ops/src/test/java/org/scijava/ops/core/builder/OpBuilderTest.java‎

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,47 @@
4343
import org.scijava.ops.function.Inplaces;
4444
import org.scijava.ops.function.Producer;
4545

46-
// @formatter:off
4746
/**
48-
* Tests {@link OpBuilder}.
49-
*
50-
* For each arity, we test the following matches and run the following commands
51-
* based on the information scenarios.
52-
*
53-
* * Input TYPES are given (IT)
54-
* 1) The output is unspecified (OU):
55-
* a) match: Function, Inplace
56-
* b) run: none
57-
* 2) The output type is given (OT):
58-
* a) match: Function, Computer
59-
* b) run: none
60-
*
61-
* * Input VALUES are given (IV) (N.B. this case applies for Arity0):
62-
* 1) The output is unspecified (OU):
63-
* a) match: Function, Inplace
64-
* b) run: apply, mutate
65-
* 2) The output type is given (OT):
66-
* a) match: Function, Computer
67-
* b) run: apply
68-
* 3) The output value is given (OV):
69-
* a) match: Computer
70-
* b) run: compute
47+
* Tests {@link OpBuilder}. For each arity, we test the following matches and
48+
* run the following commands based on the information scenarios.
49+
* <p>
50+
* Input TYPES are given (IT):
51+
* <p>
52+
* <ol>
53+
* <li>The output is unspecified (OU):
54+
* <ol type="a">
55+
* <li>match: Function, Inplace
56+
* <li>run: none
57+
* </ol>
58+
* <li>The output type is given (OT):
59+
* <ol type="a">
60+
* <li>match: Function, Computer
61+
* <li>run: none
62+
* </ol>
63+
* </ol>
64+
* Input VALUES are given (IV) (N.B. this case applies for Arity0):
65+
* <p>
66+
* <ol>
67+
* <li>The output is unspecified (OU):
68+
* <ol type="a">
69+
* <li>match: Function, Inplace
70+
* <li>run: apply, mutate
71+
* </ol>
72+
* <li>The output type is given (OT):
73+
* <ol type="a">
74+
* <li>match: Function, Computer
75+
* <li>run: apply
76+
* </ol>
77+
* <li>The output value is given (OV):
78+
* <ol type="a">
79+
* <li>match: Computer
80+
* <li>run: compute
81+
* </ol>
82+
* </ol>
7183
*
7284
* @author Curtis Rueden
7385
* @author Gabriel Selzer
7486
*/
75-
// @formatter:on
7687
public class OpBuilderTest extends AbstractTestEnvironment {
7788

7889
final double[] halves = new double[10];

0 commit comments

Comments
 (0)