Skip to content

Commit fef11ee

Browse files
committed
Use OpInfo to resolve Op dependencies
1 parent 73dee10 commit fef11ee

1 file changed

Lines changed: 32 additions & 29 deletions

File tree

src/main/java/org/scijava/ops/OpService.java

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -182,49 +182,52 @@ public LogService logger() {
182182
* object by looking for Ops matching the field type and the name specified in
183183
* the annotation. The field type is assumed to be functional.
184184
*
185-
* @param obj
185+
* @param op
186186
* @throws OpMatchingException
187187
* if the type of the specified object is not functional, if the Op
188188
* matching the functional type and the name could not be found, if
189189
* an exception occurs during injection
190190
*/
191-
private void resolveOpDependencies(Object obj, OpCandidate parentOp) throws OpMatchingException {
192-
Object op = obj;
191+
private void resolveOpDependencies(Object op, OpCandidate parentOp) throws OpMatchingException {
193192
// HACK: Only works with Op instances and OpRunner, not extensible.
194193
// Consider extensible ways to achieve something similar e.g. extending OpInfo
195194
// to support OpDependencies.
196-
if (obj instanceof OpRunner) {
197-
op = ((OpRunner) obj).getAdaptedOp();
198-
}
199-
final List<Field> opFields = ClassUtils.getAnnotatedFields(op.getClass(), OpDependency.class);
200-
201-
for (final Field opField : opFields) {
202-
final String opName = opField.getAnnotation(OpDependency.class).name();
203-
final Type fieldType = Types.fieldType(opField, op.getClass());
204-
final Type mappedFieldType = Types.mapVarToTypes(new Type[] { fieldType }, parentOp.typeVarAssigns())[0];
205-
206-
OpRef inferredRef = inferOpRef(mappedFieldType, opName, parentOp.typeVarAssigns());
195+
if (op instanceof OpRunner) {
196+
op = ((OpRunner<?>) op).getAdaptedOp();
197+
}
198+
final List<OpDependencyMember<?>> dependencies = parentOp.opInfo()
199+
.dependencies();
200+
for (final OpDependencyMember<?> dependency : dependencies) {
201+
final String dependencyName = dependency.getDependencyName();
202+
final Type mappedDependencyType = Types.mapVarToTypes(new Type[] {
203+
dependency.getType() }, parentOp.typeVarAssigns())[0];
204+
final OpRef inferredRef = inferOpRef(mappedDependencyType, dependencyName,
205+
parentOp.typeVarAssigns());
207206
if (inferredRef == null) {
208-
throw new OpMatchingException(
209-
"Could not infer functional " + "method inputs and outputs of Op dependency field: " + opField);
207+
throw new OpMatchingException("Could not infer functional " +
208+
"method inputs and outputs of Op dependency field: " + dependency
209+
.getKey());
210210
}
211-
212211
Object matchedOp = null;
213212
try {
214-
matchedOp = findOpInstance(opName, inferredRef);
215-
} catch (Exception e) {
216-
throw new OpMatchingException("Could not find Op that matches requested Op dependency field:"
217-
+ "\nOp class: " + op.getClass().getName() + "\nDependency field: " + opField.getName()
218-
+ "\n\n Attempted request:\n" + inferredRef, e);
213+
matchedOp = findOpInstance(dependencyName, inferredRef);
214+
}
215+
catch (final Exception e) {
216+
throw new OpMatchingException(
217+
"Could not find Op that matches requested Op dependency field:" +
218+
"\nOp class: " + op.getClass().getName() + //
219+
"\nDependency field: " + dependency.getKey() + //
220+
"\n\n Attempted request:\n" + inferredRef, e);
219221
}
220-
221222
try {
222-
opField.setAccessible(true);
223-
opField.set(op, matchedOp);
224-
} catch (IllegalArgumentException | IllegalAccessException e) {
225-
throw new OpMatchingException("Exception trying to inject Op dependency field.\n"
226-
+ "\tOp dependency field to resolve: " + opField + "\n" + "\tFound Op to inject: "
227-
+ matchedOp.getClass().getName() + "\n" + "\tWith inferred OpRef: " + inferredRef, e);
223+
dependency.createInstance(op).set(matchedOp);
224+
}
225+
catch (final Exception e) {
226+
throw new OpMatchingException(
227+
"Exception trying to inject Op dependency field.\n" +
228+
"\tOp dependency field to resolve: " + dependency.getKey() + "\n" +
229+
"\tFound Op to inject: " + matchedOp.getClass().getName() + "\n" +
230+
"\tWith inferred OpRef: " + inferredRef, e);
228231
}
229232
}
230233
}

0 commit comments

Comments
 (0)