Skip to content

Commit b5f023a

Browse files
Treiblesschorlegselzer
authored andcommitted
Add an op matching exception and move op creation to the op candidate
1 parent 772b483 commit b5f023a

5 files changed

Lines changed: 60 additions & 35 deletions

File tree

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

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.scijava.ops.matcher.OpClassInfo;
4949
import org.scijava.ops.matcher.OpFieldInfo;
5050
import org.scijava.ops.matcher.OpInfo;
51+
import org.scijava.ops.matcher.OpMatchingException;
5152
import org.scijava.ops.matcher.OpRef;
5253
import org.scijava.ops.matcher.OpTypeMatchingService;
5354
import org.scijava.ops.util.Inject;
@@ -58,7 +59,6 @@
5859
import org.scijava.service.AbstractService;
5960
import org.scijava.service.SciJavaService;
6061
import org.scijava.service.Service;
61-
import org.scijava.struct.StructInstance;
6262
import org.scijava.types.Nil;
6363
import org.scijava.util.ClassUtils;
6464

@@ -154,58 +154,43 @@ public Iterable<OpInfo> infos(String name) {
154154
return opCache.getAndBelow(new PrefixQuery(opName));
155155
}
156156

157-
public <T> StructInstance<T> findOpInstance(final String opName, final Nil<T> specialType, final Nil<?>[] inTypes,
157+
public <T> T findOpInstance(final String opName, final Nil<T> specialType, final Nil<?>[] inTypes,
158158
final Nil<?>[] outTypes, final Object... secondaryArgs) {
159159
final OpRef ref = OpRef.fromTypes(opName, toTypes(specialType), toTypes(outTypes), toTypes(inTypes));
160160

161-
// Find single match which matches the specified types
162-
OpCandidate match = findTypeMatch(ref);
163-
164-
@SuppressWarnings("unchecked")
165-
final StructInstance<T> opInst = (StructInstance<T>) match.createOpInstance();
166-
167-
// Inject the secondary args if there are any
168-
if (Inject.Structs.isInjectable(opInst)) {
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());
173-
}
174-
Inject.Structs.inputs(opInst, paddedArgs);
175-
// Secondary args are given, however there are no to inject
176-
} else if (secondaryArgs.length > 0) {
177-
log.warn(
178-
"Specified Op has no secondary args, however secondary args are given. "
179-
+ "The specified args will not be injected.");
161+
try {
162+
// Find single match which matches the specified types
163+
OpCandidate match = findTypeMatch(ref);
164+
return (T) match.createOp(secondaryArgs);
165+
} catch (OpMatchingException e) {
166+
throw new RuntimeException(e);
180167
}
181-
return opInst;
182168
}
183-
184169
public <T> T findOp(final Nil<T> specialType, final Nil<?>[] inTypes, final Nil<?>[] outTypes,
185170
final Object... secondaryArgs) {
186-
return findOpInstance(null, specialType, inTypes, outTypes, secondaryArgs).object();
171+
return findOpInstance(null, specialType, inTypes, outTypes, secondaryArgs);
187172
}
188173

189174
public <T> T findOp(final Nil<T> specialType, final Nil<?>[] inTypes, final Nil<?> outType,
190175
final Object... secondaryArgs) {
191-
return findOpInstance(null, specialType, inTypes, new Nil[] { outType }, secondaryArgs).object();
176+
return findOpInstance(null, specialType, inTypes, new Nil[] { outType }, secondaryArgs);
192177
}
193178

194179
public <T> T findOp(final String opName, final Nil<T> specialType, final Nil<?>[] inTypes, final Nil<?>[] outTypes,
195180
final Object... secondaryArgs) {
196-
return findOpInstance(opName, specialType, inTypes, outTypes, secondaryArgs).object();
181+
return findOpInstance(opName, specialType, inTypes, outTypes, secondaryArgs);
197182
}
198183

199184
public <T> T findOp(final String opName, final Nil<T> specialType, final Nil<?>[] inTypes,
200185
final Nil<?> outType, final Object... secondaryArgs) {
201-
return findOpInstance(opName, specialType, inTypes, new Nil[] { outType }, secondaryArgs).object();
186+
return findOpInstance(opName, specialType, inTypes, new Nil[] { outType }, secondaryArgs);
202187
}
203188

204189
public MatchingResult findTypeMatches(final OpRef ref) {
205190
return matcher.findMatch(this, ref);
206191
}
207192

208-
public OpCandidate findTypeMatch(final OpRef ref) {
193+
public OpCandidate findTypeMatch(final OpRef ref) throws OpMatchingException {
209194
return findTypeMatches(ref).singleMatch();
210195
}
211196

src/main/java/org/scijava/ops/matcher/MatchingResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public List<OpCandidate> getMatches() {
4141
return matches;
4242
}
4343

44-
public OpCandidate singleMatch() {
44+
public OpCandidate singleMatch() throws OpMatchingException {
4545
if (matches.size() == 1) {
4646
// if (log.isDebug()) {
4747
// log.debug("Selected '" + match.getRef().getLabel() + "' op: " +
@@ -57,7 +57,7 @@ public OpCandidate singleMatch() {
5757
return matches.get(0);
5858
} else {
5959
final String analysis = OpUtils.matchInfo(this);
60-
throw new IllegalArgumentException(analysis);
60+
throw new OpMatchingException(analysis);
6161
}
6262
}
6363
}

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import org.scijava.ops.OpEnvironment;
3535
import org.scijava.ops.OpUtils;
36+
import org.scijava.ops.util.Inject;
3637
import org.scijava.param.ValidityProblem;
3738
import org.scijava.struct.Member;
3839
import org.scijava.struct.Struct;
@@ -196,12 +197,36 @@ public String toString() {
196197
return info.toString();
197198
}
198199

199-
public StructInstance<?> createOpInstance() {
200+
public StructInstance<?> createOpInstance(Object... secondaryArgs) throws OpMatchingException {
200201
if (!getStatusCode().equals(StatusCode.MATCH)) {
201-
throw new IllegalArgumentException(
202+
throw new OpMatchingException(
202203
"Status of candidate to create op " + "from indicates a problem: " + getStatus());
203204
}
204205

205-
return opInfo().createOpInstance();
206+
StructInstance<?> inst = opInfo().createOpInstance();
207+
inject(inst, secondaryArgs);
208+
return inst;
209+
}
210+
211+
public Object createOp(Object... secondaryArgs) throws OpMatchingException {
212+
return createOpInstance(secondaryArgs).object();
213+
}
214+
215+
private void inject(StructInstance<?> opInst, Object... secondaryArgs) throws OpMatchingException {
216+
// Inject the secondary args if there are any
217+
if (Inject.Structs.isInjectable(opInst)) {
218+
// Get padded secondary args
219+
Object[] paddedArgs = OpUtils.padArgs(this, true, secondaryArgs);
220+
if (paddedArgs == null) {
221+
throw new OpMatchingException(opInfo().implementationName() + " | " + getStatus());
222+
}
223+
Inject.Structs.inputs(opInst, paddedArgs);
224+
// Secondary args are given, however there are no to inject
225+
} else if (secondaryArgs.length > 0) {
226+
//TODO where get the logger from?
227+
// log.warn(
228+
// "Specified Op has no secondary args, however secondary args are given. "
229+
// + "The specified args will not be injected.");
230+
}
206231
}
207232
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.scijava.ops.matcher;
2+
3+
/**
4+
* Indicates that no op matching a given {@link OpRef} could be found.
5+
*
6+
* @author David Kolb
7+
*/
8+
public class OpMatchingException extends Exception {
9+
10+
private static final long serialVersionUID = 2334342967056340218L;
11+
12+
public OpMatchingException(String message) {
13+
super(message);
14+
}
15+
}

src/test/java/org/scijava/ops/OptionalParametersTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void testSecondaryArgs() throws ValidityException {
7979
assertTrue(Math.abs(0.5 - Arrays.stream(res).min().getAsDouble()) < 0.000001);
8080
}
8181

82-
@Test(expected = IllegalStateException.class)
82+
@Test(expected = RuntimeException.class)
8383
public void testRequiredMissing() throws ValidityException {
8484
// One required, but none are given
8585
ops().findOp( //
@@ -90,7 +90,7 @@ public void testRequiredMissing() throws ValidityException {
9090
);
9191
}
9292

93-
@Test(expected = IllegalStateException.class)
93+
@Test(expected = RuntimeException.class)
9494
public void testTooMannyArgs() throws ValidityException {
9595
// One required, one optional, but three given
9696
ops().findOp( //

0 commit comments

Comments
 (0)