Skip to content

Commit 59bd207

Browse files
committed
Box primitive OpMethod parameters
This allows us to use LambdaMetaFactory/Javassist to call methods with primitive parameters as Ops
1 parent 4b1c4e3 commit 59bd207

5 files changed

Lines changed: 52 additions & 7 deletions

File tree

‎imagej/imagej-ops2/src/main/java/net/imagej/ops2/filter/gauss/Gaussians.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static <T extends NumericType<T> & NativeType<T>> void defaultGaussRAI(
114114
gaussRAISingleSigma( //
115115
final RandomAccessibleInterval<T> input, //
116116
final ExecutorService es, //
117-
final Double sigma, //
117+
final double sigma, //
118118
@Optional OutOfBoundsFactory<T, RandomAccessibleInterval<T>> outOfBounds, //
119119
final RandomAccessibleInterval<T> output //
120120
) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.Objects;
4444
import java.util.stream.Collectors;
4545

46+
import org.scijava.common3.Classes;
4647
import org.scijava.common3.validity.ValidityException;
4748
import org.scijava.common3.validity.ValidityProblem;
4849
import org.scijava.meta.Versions;
@@ -399,7 +400,7 @@ private String createFunctionalMethod(Method m) {
399400
Parameter[] mParams = m.getParameters();
400401
for (Parameter mParam : mParams) {
401402
Class<?> paramRawType = Types.raw(mParam.getParameterizedType());
402-
String castClassName = paramRawType.getName();
403+
String castClassName = Classes.box(paramRawType).getName();
403404
if (paramRawType.isArray()) castClassName = paramRawType.getSimpleName();
404405
sb.append("(") //
405406
.append(castClassName) //

‎scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/util/Adapt.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.function.BiFunction;
1111
import java.util.function.Function;
1212

13+
import org.scijava.common3.Classes;
1314
import org.scijava.function.Computers;
1415
import org.scijava.function.Functions;
1516
import org.scijava.function.Inplaces;
@@ -228,12 +229,19 @@ public static <T> T lambdaize(Class<T> functionalInterface, MethodHandle methodH
228229
.toArray(String[]::new);
229230
if (invokedNames.length != 1)
230231
throw new IllegalArgumentException("The passed class is not a functional interface");
231-
232232
// see the LambdaMetafactory javadocs for explanations on these MethodTypes.
233233
MethodType invokedType = MethodType.methodType(functionalInterface);
234234
MethodType methodType = methodHandle.type();
235+
// Box primitive parameter types
236+
for(int i = 0; i < methodType.parameterCount(); i++) {
237+
Class<?> paramType = methodType.parameterType(i);
238+
if (paramType.isPrimitive())
239+
methodType = methodType.changeParameterType(i, Classes.box(paramType));
240+
}
235241
Class<?> rType = methodType.returnType();
236-
MethodType samMethodType = methodType.generic()
242+
if (rType.isPrimitive() && rType != void.class)
243+
rType = Classes.box(rType);
244+
MethodType samMethodType = methodType.generic() //
237245
.changeReturnType(rType == void.class ? rType : Object.class);
238246
MethodHandle callSite = LambdaMetafactory.metafactory(caller, invokedNames[0], //
239247
invokedType, samMethodType, methodHandle, methodType).getTarget();

‎scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/util/internal/OpMethodUtils.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.scijava.ops.engine.util.internal;
22

33
import java.lang.reflect.Method;
4+
import java.lang.reflect.Parameter;
45
import java.lang.reflect.Type;
56
import java.lang.reflect.TypeVariable;
67
import java.util.Arrays;
78
import java.util.HashMap;
89
import java.util.Map;
910

11+
import org.scijava.common3.Classes;
1012
import org.scijava.ops.spi.OpDependency;
1113
import org.scijava.types.Types;
1214
import org.scijava.types.inference.GenericAssignability;
@@ -36,9 +38,11 @@ public static Type getOpMethodType(Class<?> opClass, Method opMethod) {
3638
GenericAssignability.inferTypeVariables(typeMethodParams, getOpParamTypes(
3739
opMethodParams), typeVarAssigns);
3840
if (abstractMethod.getReturnType() != void.class) {
41+
Type returnType = opMethod.getGenericReturnType();
42+
if (Types.raw(returnType).isPrimitive())
43+
returnType = Classes.box(Types.raw(returnType));
3944
GenericAssignability.inferTypeVariables(new Type[] { abstractMethod
40-
.getGenericReturnType() }, new Type[] { opMethod
41-
.getGenericReturnType() }, typeVarAssigns);
45+
.getGenericReturnType() }, new Type[] {returnType}, typeVarAssigns);
4246
}
4347

4448
// parameterize opClass
@@ -60,7 +64,9 @@ public static Type[] getOpParamTypes(
6064
return Arrays //
6165
.stream(methodParams) //
6266
.filter(param -> param.getAnnotation(OpDependency.class) == null) //
63-
.map(param -> param.getParameterizedType()).toArray(Type[]::new);
67+
.map(Parameter::getParameterizedType) //
68+
.map(param -> Types.raw(param).isPrimitive() ? Classes.box(Types.raw(param)) : param) //
69+
.toArray(Type[]::new);
6470
}
6571

6672
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.scijava.ops.engine;
2+
3+
import java.util.function.Function;
4+
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.Test;
8+
import org.scijava.ops.spi.OpCollection;
9+
import org.scijava.ops.spi.OpMethod;
10+
11+
public class OpMethodBoxTest extends AbstractTestEnvironment implements OpCollection {
12+
@BeforeAll
13+
public static void addNeededOps() {
14+
ops.register(new OpMethodBoxTest());
15+
}
16+
17+
@OpMethod(names = "test.box", type = Function.class)
18+
public static int increment(final int i1) {
19+
return i1 + 1;
20+
}
21+
22+
@Test
23+
public void testOpMethodBoxing(){
24+
Integer result = ops.op("test.box").arity1().input(1) //
25+
.outType(Integer.class).apply();
26+
Assertions.assertEquals(2, result);
27+
}
28+
29+
30+
}

0 commit comments

Comments
 (0)