Skip to content

Commit 3fba5b9

Browse files
committed
Types: improve TypeVariable support
There were a number of cases either not being handled or handled incorrectly, particularly with respect to nested type variables and parameterized types, as well as recursive types. Tests have been added covering these cases. The core of the logic to support these cases centers around looking at a variables bounds when it hasn't otherwise been accounted for.
1 parent 54b8af3 commit 3fba5b9

3 files changed

Lines changed: 160 additions & 9 deletions

File tree

‎scijava/scijava-types/src/main/java/org/scijava/types/Types.java‎

Lines changed: 132 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,20 @@ private static boolean isAssignable(final Type type,
17681768

17691769
// null means the two types are not compatible
17701770
if (fromTypeVarAssigns == null) {
1771+
if (type instanceof TypeVariable) {
1772+
TypeVariable<?> typeVar = (TypeVariable<?>)type;
1773+
Type[] bounds = typeVar.getBounds();
1774+
if (typeVarAssigns.containsKey(type)) {
1775+
return isAssignable(typeVarAssigns.get(type), toParameterizedType, typeVarAssigns);
1776+
}
1777+
if (bounds.length == 1 && Object.class.equals(bounds[0])) {
1778+
// We have an unbound (as bound is Object) type variable with no
1779+
// assignment in the type variable map. Thus we can bind it to
1780+
// our toType
1781+
typeVarAssigns.put((TypeVariable) type, toParameterizedType);
1782+
return true;
1783+
}
1784+
}
17711785
return false;
17721786
}
17731787

@@ -1789,12 +1803,37 @@ private static boolean isAssignable(final Type type,
17891803

17901804
Type toResolved = resolveTypeParameters(toTypeArg, toTypeVarAssigns);
17911805
Type fromResolved = resolveTypeParameters(fromTypeArg, fromTypeVarAssigns);
1792-
1793-
// if toResolved is null, it must be linked to a unbounded type variable.
1806+
1807+
Type[] toBounds = determineBounds(var, toTypeArg, toResolved, toTypeVarAssigns);
1808+
Type[] fromBounds = determineBounds(var, fromTypeArg, fromResolved, fromTypeVarAssigns);
1809+
1810+
// if either type is bounded we need to recurse
1811+
if (toBounds != null) {
1812+
for (Type to : toBounds) {
1813+
if (fromBounds != null) {
1814+
for (Type from : fromBounds) {
1815+
if (!isAssignable(from, to, typeVarAssigns)) return false;
1816+
}
1817+
} else if (!isAssignable(fromResolved == null ? fromTypeArg : fromResolved, to, typeVarAssigns)) return false;
1818+
}
1819+
if (toTypeArg == null && toResolved == null && fromResolved != null && typeVarAssigns != null) {
1820+
TypeVariable<?> unbounded = (TypeVariable<?>) toTypeVarAssigns.get(var);
1821+
typeVarAssigns.put(unbounded, fromResolved);
1822+
}
1823+
continue;
1824+
}
1825+
if (fromBounds != null) {
1826+
for (Type from : fromBounds) {
1827+
if (!isAssignable(from, toResolved == null ? toTypeArg : toResolved, typeVarAssigns)) return false;
1828+
}
1829+
continue;
1830+
}
1831+
1832+
// if toResolved and toTypeArg are null, it must be linked to an unbounded type variable.
17941833
// We can then bound it to fromResolved (assuming fromResolved actually
17951834
// represents some type i.e. not null) in toTypeVarAssigns and typeVarAssigns.
17961835
// Effectively toResolved = fromResolved.
1797-
if (toResolved == null && fromResolved != null && typeVarAssigns != null) {
1836+
if (toTypeArg == null && toResolved == null && fromResolved != null && typeVarAssigns != null) {
17981837
TypeVariable<?> unbounded = (TypeVariable<?>) toTypeVarAssigns.get(var);
17991838
typeVarAssigns.put(unbounded, fromResolved);
18001839
toResolved = fromResolved;
@@ -1803,8 +1842,7 @@ private static boolean isAssignable(final Type type,
18031842
// parameters must either be absent from the subject type, within
18041843
// the bounds of the wildcard type, or be an exact match to the
18051844
// parameters of the target type.
1806-
if (fromResolved != null && !fromResolved.equals(toResolved) && !(toResolved instanceof WildcardType
1807-
&& isAssignable(fromResolved, toResolved, typeVarAssigns))) {
1845+
if (fromResolved != null && !fromResolved.equals(toResolved)) {
18081846
// check for anys
18091847
if (fromResolved instanceof Any || toResolved instanceof Any)
18101848
continue;
@@ -1830,7 +1868,75 @@ && isAssignable(fromResolved, toResolved, typeVarAssigns))) {
18301868
}
18311869
return true;
18321870
}
1833-
1871+
1872+
/**
1873+
* Helper method to try and find the most appropriate type bounds for a
1874+
* particular {@code var}.
1875+
*
1876+
* @param var the type variable to look up
1877+
* @param typeArg The {@link #unrollVariableAssignments} result for {@code var}
1878+
* @param resolvedType The {@link #resolveTypeParameters} result for {@code typeArg}
1879+
* @param typeVarAssigns the map used for the look up
1880+
* @return An array of {@code Types} representing the bounds of the given {@code var}
1881+
*/
1882+
private static Type[] determineBounds(TypeVariable<?> var, Type typeArg, Type resolvedType, Map<TypeVariable<?>, Type> typeVarAssigns) {
1883+
Type[] bounds = null;
1884+
if (resolvedType == null && typeArg == null) {
1885+
typeArg = unrollVariableAssignments(var, typeVarAssigns, true);
1886+
if (typeArg != null) {
1887+
// Definitely have a bounded param
1888+
bounds = ((TypeVariable<?>)typeArg).getBounds();
1889+
Set<Type> nonObjects = new HashSet<>(bounds.length);
1890+
for (Type t : bounds) {
1891+
if (!Object.class.equals(t)) {
1892+
nonObjects.add(t);
1893+
}
1894+
}
1895+
if (nonObjects.isEmpty()) bounds = null;
1896+
else if (nonObjects.size() != bounds.length) {
1897+
bounds = nonObjects.toArray(new Type[nonObjects.size()]);
1898+
}
1899+
}
1900+
} else {
1901+
WildcardType wt = null;
1902+
if (resolvedType != null && resolvedType instanceof WildcardType) {
1903+
wt = ((WildcardType) resolvedType);
1904+
} else if (typeArg != null && typeArg instanceof WildcardType) {
1905+
wt = ((WildcardType) typeArg);
1906+
}
1907+
if (wt != null) {
1908+
bounds = wt.getUpperBounds();
1909+
}
1910+
}
1911+
1912+
if (bounds != null) {
1913+
Set<Type> actualTypes = new HashSet<>(bounds.length);
1914+
Set<Type> parentBounds = new HashSet<>(Arrays.asList(bounds));
1915+
for (Type b : bounds) {
1916+
if (b instanceof ParameterizedType) {
1917+
for (Type t : ((ParameterizedType)b).getActualTypeArguments()) {
1918+
if (t instanceof TypeVariable) {
1919+
Type[] subBounds = ((TypeVariable<?>)t).getBounds();
1920+
for (Type s : subBounds) {
1921+
// This stops recursive types from infinite recursion
1922+
if (parentBounds.contains(s)) {
1923+
actualTypes.add(((ParameterizedType)b).getRawType());
1924+
} else {
1925+
actualTypes.add(b);
1926+
}
1927+
}
1928+
} else {
1929+
actualTypes.add(b);
1930+
}
1931+
}
1932+
} else actualTypes.add(b);
1933+
1934+
bounds = actualTypes.toArray(new Type[actualTypes.size()]);
1935+
}
1936+
}
1937+
return bounds;
1938+
}
1939+
18341940
private static Type resolveTypeParameters(Type type, Map<TypeVariable<?>, Type> typeVarAssigns) {
18351941
Type toTypeArg;
18361942
if (!(Types.containsTypeVars(type))) return type;
@@ -1851,17 +1957,31 @@ private static Type resolveTypeParameters(Type type, Map<TypeVariable<?>, Type>
18511957
return toTypeArg;
18521958
}
18531959

1960+
/**
1961+
* As {@link #unrollVariableAssignments(TypeVariable, Map, boolean)} with
1962+
* {@code acceptBounds} defaulting to {@code false}, so that only true
1963+
* assignments will be unrolled.
1964+
*/
1965+
private static Type unrollVariableAssignments(TypeVariable<?> var,
1966+
final Map<TypeVariable<?>, Type> typeVarAssigns) {
1967+
return unrollVariableAssignments(var, typeVarAssigns, false);
1968+
}
1969+
18541970
/**
18551971
* Look up {@code var} in {@code typeVarAssigns} <em>transitively</em>, i.e.
18561972
* keep looking until the value found is <em>not</em> a type variable.
18571973
*
18581974
* @param var the type variable to look up
18591975
* @param typeVarAssigns the map used for the look up
1976+
* @param acceptBounds If {@code true} and a variable chain does not
1977+
* lead to a concrete type assignment, then the bounds
1978+
* of the leaf variable will be returned instead of
1979+
* {@code null}.
18601980
* @return Type or {@code null} if some variable was not in the map
18611981
* @since 3.2
18621982
*/
18631983
private static Type unrollVariableAssignments(TypeVariable<?> var,
1864-
final Map<TypeVariable<?>, Type> typeVarAssigns)
1984+
final Map<TypeVariable<?>, Type> typeVarAssigns, boolean acceptBounds)
18651985
{
18661986
Type result;
18671987
do {
@@ -1870,6 +1990,11 @@ private static Type unrollVariableAssignments(TypeVariable<?> var,
18701990
var = (TypeVariable<?>) result;
18711991
continue;
18721992
}
1993+
else if (result == null && acceptBounds && var.getBounds().length > 0) {
1994+
// We didn't find an absolute mapping for the given variable, but the
1995+
// variable is bounded so we can return it.
1996+
result = var;
1997+
}
18731998
break;
18741999
}
18752000
while (true);

‎scijava/scijava-types/src/main/java/org/scijava/types/inference/GenericAssignability.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,8 @@ else if (superInferFrom instanceof Class) {
489489
else if (superInferFrom == null) {
490490
// edge case 1: if inferFrom is an Object, superInferFrom will be null
491491
// when type is some interface.
492-
if (Object.class.equals(inferFrom)) {
492+
if (Object.class.equals(inferFrom) ||
493+
(inferFrom instanceof TypeVariable && Object.class.equals(((TypeVariable<?>) inferFrom).getBounds()[0]))) {
493494
mapTypeVarsToAny(type, typeMappings);
494495
return;
495496
}

‎scijava/scijava-types/src/test/java/org/scijava/types/TypesTest.java‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ public <T extends Number> void testIsAssignableT() {
250250
assertTrue(Types.isAssignable(listNumber, listT));
251251
assertTrue(Types.isAssignable(listInteger, listT));
252252
assertTrue(Types.isAssignable(listExtendsNumber, listT));
253-
assertFalse(Types.isAssignable(listExtendsNumber, listNumber));
253+
// List<? extends Number> l = new ArrayList<>();
254+
// List<Number> l2 = (List<Number>) l;
255+
assertTrue(Types.isAssignable(listExtendsNumber, listNumber));
254256

255257
assertTrue(Types.isAssignable(listT, listRaw));
256258
assertTrue(Types.isAssignable(listNumber, listRaw));
@@ -264,6 +266,29 @@ public <T extends Number> void testIsAssignableT() {
264266
assertTrue(Types.isAssignable(listListRaw, listListRaw));
265267
}
266268

269+
/** Tests {@link Types#isAssignable(Type, Type)} with type variables themselves parameterized with type variables. */
270+
@Test
271+
public <N extends Number, S extends String, T extends List<N>> void testIsAssignableParameterizedT() {
272+
final Type t = new Nil<T>() {}.getType();
273+
final Type listN = new Nil<List<N>>() {}.getType();
274+
final Type listS = new Nil<List<S>>() {}.getType();
275+
final Type listNumber = new Nil<List<Number>>() {}.getType();
276+
final Type listInteger = new Nil<List<Integer>>() {}.getType();
277+
final Type listExtendsNumber = new Nil<List<? extends Number>>() {}.getType();
278+
// T list = (T) new ArrayList<N>();
279+
assertTrue(Types.isAssignable(listN, t));
280+
// T list = (T) new ArrayList<Number>();
281+
assertTrue(Types.isAssignable(listNumber, t));
282+
// T list = (T) new ArrayList<Integer>();
283+
assertTrue(Types.isAssignable(listInteger, t));
284+
// List<? extends Number> l = new ArrayList<Number>();
285+
// T list = (T) l;
286+
assertTrue(Types.isAssignable(listExtendsNumber, t));
287+
// T list = (T) new ArrayList<S>();
288+
assertFalse(Types.isAssignable(listS, t));
289+
290+
}
291+
267292
/** Tests {@link Types#isAssignable(Type, Type)} against {@link Object} */
268293
@Test
269294
public <T extends Number> void testIsAssignableObject() {

0 commit comments

Comments
 (0)