Skip to content

Commit 1523ba3

Browse files
gselzerhinerm
authored andcommitted
Make reduction work outside of the engine
It seems that javassisted Ops cannot make direct calls into classes that the engine module does not have JPMS read access to (i.e. granted by reqiuires). The simplest workaround is to rely on reflection instead, which access is granted by JPMS opens..to. See #110
1 parent d6dc506 commit 1523ba3

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

‎scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/reduce/ReductionUtils.java‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.scijava.ops.engine.reduce;
22

3-
import com.google.common.collect.Streams;
4-
53
import java.lang.invoke.MethodHandles;
64
import java.lang.reflect.Method;
75
import java.util.Arrays;
@@ -13,6 +11,9 @@
1311
import org.scijava.ops.spi.Optional;
1412
import org.scijava.struct.Member;
1513
import org.scijava.types.Types;
14+
import org.scijava.types.inference.InterfaceInference;
15+
16+
import com.google.common.collect.Streams;
1617

1718
import javassist.CannotCompileException;
1819
import javassist.ClassPool;
@@ -24,7 +25,6 @@
2425
import javassist.CtNewMethod;
2526
import javassist.Modifier;
2627
import javassist.NotFoundException;
27-
import org.scijava.types.inference.InterfaceInference;
2828

2929
public class ReductionUtils {
3030

@@ -49,6 +49,7 @@ protected static Object javassistOp(Object originalOp, ReducedOpInfo reducedInfo
4949
ClassPool pool = ClassPool.getDefault();
5050

5151
// Create wrapper class
52+
Method opMethod = OpUtils.findFunctionalMethod(originalOp.getClass());
5253
String className = formClassName(reducedInfo);
5354
Class<?> c;
5455
try {
@@ -60,8 +61,8 @@ protected static Object javassistOp(Object originalOp, ReducedOpInfo reducedInfo
6061
}
6162

6263
// Return Op instance
63-
return c.getDeclaredConstructor(Types.raw(reducedInfo.srcInfo().opType()))
64-
.newInstance(originalOp);
64+
return c.getDeclaredConstructor(Types.raw(reducedInfo.srcInfo().opType()), Method.class)
65+
.newInstance(originalOp, opMethod);
6566
}
6667

6768
/**
@@ -152,6 +153,9 @@ private static CtClass generateSimplifiedWrapper(ClassPool pool, String classNam
152153
CtField opField = createOpField(pool, cc, Types.raw(reducedInfo.srcInfo().opType()), "op");
153154
cc.addField(opField);
154155

156+
CtField methodField = createOpField(pool, cc, Method.class, "opMethod");
157+
cc.addField(methodField);
158+
155159
// Add constructor to take the Simplifiers, as well as the original op.
156160
CtConstructor constructor = CtNewConstructor.make(createConstructor(cc,
157161
reducedInfo), cc);
@@ -179,10 +183,11 @@ private static String createConstructor(CtClass cc, ReducedOpInfo reducedInfo) {
179183
sb.append("public " + cc.getSimpleName() + "(");
180184
// argument - original op
181185
Class<?> opClass = Types.raw(reducedInfo.srcInfo().opType());
182-
sb.append(" " + opClass.getName() + " op");
186+
sb.append(" " + opClass.getName() + " op, " + Method.class.getName() + " opMethod");
183187
sb.append(") {");
184188

185189
sb.append("this.op = op;");
190+
sb.append("this.opMethod = opMethod;");
186191
sb.append("}");
187192
return sb.toString();
188193
}
@@ -227,7 +232,11 @@ private static String createFunctionalMethod(ReducedOpInfo info) {
227232
if (OpUtils.hasPureOutput(info)) {
228233
sb.append(info.output().getType().getTypeName() + " " + opOutput + " = ");
229234
}
230-
sb.append("op." + srcM.getName() + "(");
235+
// NB we cannot call op.compute(), op.apply(), etc. directly here if the Op
236+
// is outside of this module. Calling that method will produce an
237+
// IllegalAccessException. But we can use good ol' reflection to get around
238+
// that!
239+
sb.append("opMethod.invoke(op, new Object[] {");
231240
int i;
232241
List<Member<?>> totalArguments = info.srcInfo().inputs();
233242
int totalArgs = totalArguments.size();
@@ -252,8 +261,7 @@ private static String createFunctionalMethod(ReducedOpInfo info) {
252261
}
253262
if (i + 1 < totalArguments.size()) sb.append(",");
254263
}
255-
256-
sb.append(");");
264+
sb.append("});");
257265

258266
// if pure output, return it
259267
if (OpUtils.hasPureOutput(info)) {

0 commit comments

Comments
 (0)