Skip to content

Commit 54b8af3

Browse files
authored
Merge pull request #93 from scijava/scijava/scijava-ops-engine/simplify-op-wrapping
Sipmlify getWrapperClass
2 parents 45f4c15 + 3571f19 commit 54b8af3

2 files changed

Lines changed: 49 additions & 32 deletions

File tree

‎scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/impl/DefaultOpEnvironment.java‎

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,21 @@ private <T> RichOp<T> wrapOp(OpInstance<T> instance, Hints hints)
493493

494494
try {
495495
// find the opWrapper that wraps this type of Op
496-
Class<?> wrapper = getWrapperClass(instance.op(), instance.infoTree()
497-
.info());
496+
OpInfo info = instance.infoTree().info();
497+
Class<?> rawType = Types.raw(info.opType());
498+
Class<?> wrapper = getWrapperClass(rawType, info);
499+
if (wrapper == null) {
500+
throw new IllegalArgumentException(info.implementationName() +
501+
": matched op Type " + info.opType().getClass() +
502+
" does not match a wrappable Op type.");
503+
}
504+
if (!wrapper.equals(rawType)) {
505+
log.warn("OpInfo" + info.implementationName() +
506+
" could not be wrapped as a " + rawType +
507+
", so it is instead wrapped as a " + wrapper +
508+
". If you want it to be wrapped as a " + rawType +
509+
", then you must define a new OpWrapper for that class!");
510+
}
498511
// obtain the generic type of the Op w.r.t. the Wrapper class
499512
Type reifiedSuperType = Types.getExactSuperType(instance.getType(),
500513
wrapper);
@@ -513,37 +526,24 @@ private <T> RichOp<T> wrapOp(OpInstance<T> instance, Hints hints)
513526
}
514527
}
515528

516-
private Class<?> getWrapperClass(Object op, OpInfo info) {
517-
List<Class<?>> suitableWrappers = wrappers.keySet().stream().filter(
518-
wrapper -> wrapper.isInstance(op)).collect(Collectors.toList());
519-
List<Class<?>> filteredWrappers = filterWrapperSuperclasses(
520-
suitableWrappers);
521-
if (filteredWrappers.size() == 0) throw new IllegalArgumentException(info
522-
.implementationName() + ": matched op Type " + info.opType().getClass() +
523-
" does not match a wrappable Op type.");
524-
if (filteredWrappers.size() > 1) throw new IllegalArgumentException(
525-
"Matched op Type " + info.opType().getClass() +
526-
" matches multiple Op types: " + filteredWrappers);
527-
if (!Types.isAssignable(Types.raw(info.opType()), filteredWrappers.get(0)))
528-
throw new IllegalArgumentException(Types.raw(info.opType()) +
529-
"cannot be wrapped as a " + filteredWrappers.get(0));
530-
return filteredWrappers.get(0);
531-
}
532-
533-
private List<Class<?>> filterWrapperSuperclasses(
534-
List<Class<?>> suitableWrappers)
535-
{
536-
if (suitableWrappers.size() < 2) return suitableWrappers;
537-
List<Class<?>> list = new ArrayList<>();
538-
for (Class<?> c : suitableWrappers) {
539-
boolean isSuperclass = false;
540-
for (Class<?> other : suitableWrappers) {
541-
if (c.equals(other)) continue;
542-
if (c.isAssignableFrom(other)) isSuperclass = true;
543-
}
544-
if (!isSuperclass) list.add(c);
529+
private Class<?> getWrapperClass(Class<?> opType, OpInfo info) {
530+
if (opType == null)
531+
return null;
532+
// Check opType itself
533+
if (wrappers.containsKey(opType))
534+
return opType;
535+
// Check superclass of opType
536+
Class<?> wrapperSuperClass = getWrapperClass(opType.getSuperclass(), info);
537+
if (wrapperSuperClass != null)
538+
return wrapperSuperClass;
539+
// Check interfaces of opType
540+
for (Class<?> iFace: opType.getInterfaces()) {
541+
Class<?> wrapperIFace = getWrapperClass(iFace, info);
542+
if (wrapperIFace != null)
543+
return wrapperIFace;
545544
}
546-
return list;
545+
// There is no wrapper
546+
return null;
547547
}
548548

549549
private List<RichOp<?>> resolveOpDependencies(OpCandidate candidate,

‎scijava/scijava-ops-engine/src/test/java/org/scijava/ops/engine/OpMethodInParentTest.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.junit.jupiter.api.Test;
3737
import org.scijava.ops.spi.Op;
3838
import org.scijava.ops.spi.OpClass;
39+
import org.scijava.types.Nil;
3940

4041
public class OpMethodInParentTest extends AbstractTestEnvironment {
4142

@@ -63,6 +64,22 @@ public void testFMethodInInterface() {
6364
Assertions.assertEquals(expected, actual);
6465
}
6566

67+
@Test
68+
public void testRequestingFunctionalTypeSubclass() {
69+
// Assert that things work just fine when asking for a Function
70+
Function<String, String> function = ops//
71+
.op("test.superMethod", new Nil<>()
72+
{}, new Nil[] { new Nil<String>() {} }, new Nil<String>() {});
73+
74+
// Assert that things don't work when asking for a SuperOpMethodHousingClass
75+
Assertions.assertThrows(ClassCastException.class, () -> {
76+
SuperOpMethodHousingClass op = ops.op("test.superMethod", new Nil<>() {
77+
78+
}, new Nil[] { new Nil<String>() {} }, new Nil<String>() {});
79+
});
80+
81+
}
82+
6683
}
6784

6885
abstract class OpMethodHousingClass<T> implements Function<T, T> {

0 commit comments

Comments
 (0)