Skip to content

Commit 3ab2c37

Browse files
Treiblesschorlegselzer
authored andcommitted
Add correct handling of secondary args (required vs. non-required)
1 parent 13ba815 commit 3ab2c37

3 files changed

Lines changed: 46 additions & 22 deletions

File tree

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,24 @@ public <T> StructInstance<T> findOpInstance(final String opName, final Nil<T> sp
159159
final OpRef ref = OpRef.fromTypes(opName, toTypes(specialType), toTypes(outTypes), toTypes(inTypes));
160160

161161
// Find single match which matches the specified types
162+
OpCandidate match = findTypeMatch(ref);
163+
162164
@SuppressWarnings("unchecked")
163-
final StructInstance<T> opInst = (StructInstance<T>) findTypeMatch(ref).createOp();
165+
final StructInstance<T> opInst = (StructInstance<T>) match.createOp();
164166

165167
// Inject the secondary args if there are any
166168
if (Inject.Structs.isInjectable(opInst)) {
167-
if (secondaryArgs.length != 0) {
168-
Inject.Structs.inputs(opInst, secondaryArgs);
169-
} else {
170-
log.warn(
171-
"Specified Op has secondary args however no secondary args are given. Op execution may lead to errors.");
169+
// Get padded secondary args
170+
Object[] paddedArgs = OpUtils.padArgs(match, true, secondaryArgs);
171+
if (paddedArgs == null) {
172+
throw new IllegalStateException(match.opInfo().implementationName() + " | " + match.getStatus());
172173
}
174+
Inject.Structs.inputs(opInst, paddedArgs);
175+
// Secondary args are given, however there are no to inject
173176
} else if (secondaryArgs.length > 0) {
174177
log.warn(
175-
"Specified Op has no secondary args however secondary args are given. The specified args will not be injected.");
178+
"Specified Op has no secondary args, however secondary args are given. "
179+
+ "The specified args will not be injected.");
176180
}
177181
return opInst;
178182
}

‎src/main/java/org/scijava/ops/OpUtils.java‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.scijava.struct.MemberInstance;
4646
import org.scijava.struct.Struct;
4747
import org.scijava.struct.StructInstance;
48+
import org.scijava.struct.ValueAccessible;
4849

4950
/**
5051
* Utility methods for working with ops.
@@ -159,36 +160,53 @@ public static double getPriority(final OpCandidate candidate) {
159160
return candidate.opInfo().priority();
160161
}
161162

162-
public static Type[] padArgs(final OpCandidate candidate) {
163+
public static Type[] padTypes(final OpCandidate candidate, Type[] types) {
164+
return (Type[]) padArgs(candidate, false, (Object[])types);
165+
}
166+
167+
public static Object[] padArgs(final OpCandidate candidate, final boolean secondary, Object... args) {
168+
List<Member<?>> members;
169+
String argName;
170+
if (secondary) {
171+
members = OpUtils.injectableMembers(candidate.struct());
172+
argName = "secondary args";
173+
} else {
174+
members = OpUtils.inputs(candidate.struct());
175+
argName = "args";
176+
}
177+
163178
int inputCount = 0, requiredCount = 0;
164-
for (final Member<?> item : OpUtils.inputs(candidate.struct())) {
179+
for (final Member<?> item : members) {
165180
inputCount++;
166181
if (isRequired(item))
167182
requiredCount++;
168183
}
169-
final Type[] args = candidate.getRef().getArgs();
170184
if (args.length == inputCount) {
171185
// correct number of arguments
172186
return args;
173187
}
174188
if (args.length > inputCount) {
175189
// too many arguments
176-
candidate.setStatus(StatusCode.TOO_MANY_ARGS, args.length + " > " + inputCount);
190+
candidate.setStatus(StatusCode.TOO_MANY_ARGS,
191+
"\nNumber of " + argName + " given: " + args.length + " > " +
192+
"Number of " + argName + " of op: " + inputCount);
177193
return null;
178194
}
179195
if (args.length < requiredCount) {
180196
// too few arguments
181-
candidate.setStatus(StatusCode.TOO_FEW_ARGS, args.length + " < " + requiredCount);
197+
candidate.setStatus(StatusCode.TOO_FEW_ARGS,
198+
"\nNumber of " + argName + " given: " + args.length + " <" +
199+
"Number of required " + argName + " of op: " + requiredCount);
182200
return null;
183201
}
184202

185203
// pad optional parameters with null (from right to left)
186204
final int argsToPad = inputCount - args.length;
187205
final int optionalCount = inputCount - requiredCount;
188206
final int optionalsToFill = optionalCount - argsToPad;
189-
final Type[] paddedArgs = new Type[inputCount];
207+
final Object[] paddedArgs = new Object[inputCount];
190208
int argIndex = 0, paddedIndex = 0, optionalIndex = 0;
191-
for (final Member<?> item : candidate.struct().members()) {
209+
for (final Member<?> item : members) {
192210
if (!isRequired(item) && optionalIndex++ >= optionalsToFill) {
193211
// skip this optional parameter (pad with null)
194212
paddedIndex++;
@@ -203,6 +221,13 @@ public static boolean isRequired(final Member<?> item) {
203221
return item instanceof ParameterMember && //
204222
((ParameterMember<?>) item).isRequired();
205223
}
224+
225+
public static List<Member<?>> injectableMembers(Struct struct) {
226+
return struct.members()
227+
.stream()
228+
.filter(m -> m instanceof ValueAccessible)
229+
.collect(Collectors.toList());
230+
}
206231

207232
/**
208233
* Checks if incomplete type matching could have occurred. If we have

‎src/main/java/org/scijava/ops/matcher/OpCandidate.java‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public OpCandidate(final OpEnvironment ops, final OpRef ref, final OpInfo info)
7878
this.ref = ref;
7979
this.info = info;
8080

81-
this.paddedArgs = OpUtils.padArgs(this);
81+
this.paddedArgs = OpUtils.padTypes(this, getRef().getArgs());
8282
}
8383

8484
/** Gets the op execution environment of the desired match. */
@@ -131,11 +131,6 @@ public StatusCode getStatusCode() {
131131
return code;
132132
}
133133

134-
/** Gets a message elaborating on the matching status, if any. */
135-
public String getStatusMessage() {
136-
return message;
137-
}
138-
139134
/**
140135
* Gets the status item related to the matching status, if any. Typically,
141136
* if set, this is the parameter for which matching failed.
@@ -187,9 +182,9 @@ public String getStatus() {
187182
sb.append("Inputs do not conform to op rules");
188183
break;
189184
default:
190-
return getStatusMessage();
185+
return message;
191186
}
192-
final String msg = getStatusMessage();
187+
final String msg = message;
193188
if (msg != null)
194189
sb.append(": " + msg);
195190

0 commit comments

Comments
 (0)