Skip to content

Commit 2022c84

Browse files
committed
WIP: Improve IterableTypeExtractor
Allow the IterableTypeExtractor to determine the greatest common supertype of all of the elements in the Iterable TODO: move this logic somewhere else and make the search recursive over the supertypes of the supertypes
1 parent 08ff08a commit 2022c84

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

src/main/java/org/scijava/ops/types/IterableTypeExtractor.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.scijava.Priority;
3636
import org.scijava.plugin.Parameter;
3737
import org.scijava.plugin.Plugin;
38+
import org.scijava.util.Types;
3839

3940
/**
4041
* {@link TypeExtractor} plugin which operates on {@link Iterable} objects.
@@ -54,15 +55,36 @@ public class IterableTypeExtractor implements TypeExtractor<Iterable<?>> {
5455

5556
@Override
5657
public Type reify(final Iterable<?> o, final int n) {
57-
if (n != 0) throw new IndexOutOfBoundsException();
58+
if (n != 0)
59+
throw new IndexOutOfBoundsException();
5860

5961
final Iterator<?> iterator = o.iterator();
60-
if (!iterator.hasNext()) return null;
62+
if (!iterator.hasNext())
63+
return null;
6164

6265
// Obtain the element type using the TypeService.
6366
final Object element = iterator.next();
64-
return typeService.reify(element);
67+
Type elementType = typeService.reify(element);
68+
for (int i = 0; i < 100; i++) {
69+
if(!iterator.hasNext()) break;
70+
Type otherType = typeService.reify(iterator.next());
71+
if(Types.isAssignable(otherType, elementType)) continue;
72+
//TODO: recursive superTypes check
73+
Type superClass = Types.raw(elementType).getGenericSuperclass();
74+
if(Types.isAssignable(otherType, superClass)) {
75+
elementType = superClass;
76+
continue;
77+
}
78+
Type[] superTypes = elementType.getClass().getGenericInterfaces();
79+
for(Type superType: superTypes) {
80+
if(Types.isAssignable(otherType, superType)) {
81+
elementType = superType;
82+
continue;
83+
}
84+
}
85+
}
6586

87+
return elementType;
6688
// TODO: Avoid infinite recursion when the list references itself.
6789
}
6890

0 commit comments

Comments
 (0)