Skip to content

Commit ca42427

Browse files
Treiblesschorlectrueden
authored andcommitted
Fix OpRef transformer for Function to Computer
1 parent 17a7746 commit ca42427

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/main/java/org/scijava/ops/transform/TypeModUtils.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.lang.reflect.Array;
3333
import java.lang.reflect.ParameterizedType;
3434
import java.lang.reflect.Type;
35+
import java.util.ArrayList;
3536
import java.util.Arrays;
3637
import java.util.List;
3738
import java.util.function.Function;
@@ -219,6 +220,37 @@ public static Type replaceRawType(Type type, Class<?> searchClass, Class<?> repl
219220
return null;
220221
}
221222

223+
/**
224+
* Insert the specified type at the specified index into the specified array.
225+
* Has the same behavior as {@link List#add(int, Object)}.
226+
*
227+
* @param types
228+
* @param type
229+
* @param index
230+
* @return
231+
*/
232+
public static Type[] insert(Type[] types, Type type, int index) {
233+
List<Type> out = new ArrayList<>();
234+
out.addAll(Arrays.asList(types));
235+
out.add(index, type);
236+
return out.toArray(new Type[out.size()]);
237+
}
238+
239+
/**
240+
* Remove the specified index from the specified array.
241+
* Has the same behavior as {@link List#remove(int)}.
242+
*
243+
* @param types
244+
* @param index
245+
* @return
246+
*/
247+
public static Type[] remove(Type[] types, int index) {
248+
List<Type> out = new ArrayList<>();
249+
out.addAll(Arrays.asList(types));
250+
out.remove(index);
251+
return out.toArray(new Type[out.size()]);
252+
}
253+
222254
private static boolean mutateTypes(Type[] types, Function<Type, Type> typeFunc, Integer... indices) {
223255
List<Integer> is = Arrays.asList(indices);
224256

src/main/java/org/scijava/ops/transform/impl/FunctionToComputerTransformer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public OpRef getFromTransformTo(OpRef toRef) {
3636
Type[] refTypes = toRef.getTypes();
3737
boolean hit = TypeModUtils.replaceRawTypes(refTypes, Computer.class, Function.class);
3838
if (hit) {
39-
return OpRef.fromTypes(toRef.getName(), refTypes, toRef.getOutTypes(), toRef.getArgs());
39+
// The computer has a ItemIO.BOTH as second functional parameter of type output.
40+
// This is not the case for a Function, hence we remove it.
41+
Type[] functionArgs = TypeModUtils.remove(toRef.getArgs(), 1);
42+
return OpRef.fromTypes(toRef.getName(), refTypes, toRef.getOutTypes(), functionArgs);
4043
}
4144
return null;
4245
}

0 commit comments

Comments
 (0)