Skip to content

Commit 16653e7

Browse files
committed
Remove any-injecting behavior for lambda args
This could cause nasty ClassCastExceptions if users naively pass through lambdas that do not provide the needed types.
1 parent 43d8ecc commit 16653e7

3 files changed

Lines changed: 9 additions & 18 deletions

File tree

src/main/java/org/scijava/util/Types.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -888,15 +888,10 @@ private static boolean isApplicableToParameterizedTypes(final Type arg,
888888

889889
// get an array of the source argument types
890890
Type argType = arg;
891-
if (arg instanceof Class) {
892-
for(int i = 0; i < srcTypes.length; i++)
893-
srcTypes[i] = new Any();
894-
}
895-
else {
896-
ParameterizedType parameterizedSuperType = (ParameterizedType) GenericTypeReflector
897-
.getExactSuperType(argType, Types.raw(param));
898-
srcTypes = parameterizedSuperType.getActualTypeArguments();
899-
}
891+
Type superType = Types
892+
.getExactSuperType(argType, Types.raw(param));
893+
if (superType == null || !(superType instanceof ParameterizedType)) return false;
894+
srcTypes = ((ParameterizedType)superType).getActualTypeArguments();
900895

901896
// List to collect the indices of destination parameters that are type vars
902897
// If a type vars is contain within a parameterized type if must not be checked

src/test/java/org/scijava/ops/types/AnyTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,6 @@ public void testRunAnyFunction1FromComputer2() {
7878
assertEquals(Long.toString(in1 + in2), out.getValue());
7979
}
8080

81-
@SuppressWarnings("cast")
82-
@Test
83-
public void testAnyInjectionIntoFunctionRaws() {
84-
final Function<Long, Long> func = (in) -> in / 2;
85-
final Long output = (Long) new OpBuilder(ops, "test.functionAndLongToLong").input(func, 20l).outType(Long.class).apply();
86-
assert(output == 10);
87-
}
8881
}
8982

9083
@Plugin(type = Op.class, name = "test.functionAndLongToLong")

src/test/java/org/scijava/ops/util/OpsAsParametersTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ public class OpsAsParametersTest extends AbstractTestEnvironment {
3636
return output;
3737
};
3838

39-
@Test
40-
public void TestOpWithOpField() {
39+
// TODO: find a better way to check that this call fails BECAUSE func cannot be
40+
// reified
41+
@Test(expected=IllegalArgumentException.class)
42+
public void TestOpWithNonReifiableFunction() {
4143

4244
List<Number> list = new ArrayList<>();
4345
list.add(40l);
@@ -77,6 +79,7 @@ public void TestOpWithOpClass() {
7779
}, new Nil<Double>() {
7880
});
7981

82+
@SuppressWarnings("unused")
8083
List<Double> output = new OpBuilder(ops, "test.parameter.op").input(list, funcClass).outType(new Nil<List<Double>>() {}).apply();
8184
}
8285

0 commit comments

Comments
 (0)