Skip to content

Commit 0c4c3d6

Browse files
Treiblesschorlegselzer
authored andcommitted
Add check for possible incomplete output type matching and message
1 parent 3fcacbb commit 0c4c3d6

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929

3030
package org.scijava.ops;
3131

32+
import java.lang.reflect.ParameterizedType;
3233
import java.lang.reflect.Type;
3334
import java.util.Arrays;
3435
import java.util.List;
3536
import java.util.stream.Collectors;
3637

38+
import org.scijava.ops.matcher.DefaultOpTypeMatchingService;
3739
import org.scijava.ops.matcher.MatchingResult;
3840
import org.scijava.ops.matcher.OpCandidate;
3941
import org.scijava.ops.matcher.OpInfo;
@@ -80,15 +82,15 @@ public static List<Member<?>> inputs(final Struct struct) {
8082
}
8183

8284
public static Type[] inputTypes(OpCandidate candidate) {
83-
return inputs(candidate.struct()).stream().map(m -> m.getType()).toArray(Type[]::new);
85+
return getTypes(inputs(candidate.struct()));
8486
}
8587

8688
public static List<Member<?>> outputs(OpCandidate candidate) {
8789
return outputs(candidate.struct());
8890
}
89-
91+
9092
public static Type[] outputTypes(OpCandidate candidate) {
91-
return outputs(candidate.struct()).stream().map(m -> m.getType()).toArray(Type[]::new);
93+
return getTypes(outputs(candidate.struct()));
9294
}
9395

9496
public static List<Member<?>> outputs(final Struct struct) {
@@ -102,13 +104,17 @@ public static List<MemberInstance<?>> outputs(StructInstance<?> op) {
102104
.filter(memberInstance -> memberInstance.member().isOutput()) //
103105
.collect(Collectors.toList());
104106
}
107+
108+
public static Type[] types(OpCandidate candidate) {
109+
return getTypes(candidate.struct().members());
110+
}
105111

106112
public static double getPriority(final OpCandidate candidate) {
107113
// TODO: Think about what to do about non @Plugin-based ops...?
108114
// What if there is no annotation? How to discern a priority?
109115
return candidate.opInfo().getAnnotation().priority();
110116
}
111-
117+
112118
public static Type[] padArgs(final OpCandidate candidate) {
113119
int inputCount = 0, requiredCount = 0;
114120
for (final Member<?> item : OpUtils.inputs(candidate.struct())) {
@@ -154,6 +160,33 @@ public static boolean isRequired(final Member<?> item) {
154160
((ParameterMember<?>) item).isRequired();
155161
}
156162

163+
/**
164+
* Checks if incomplete type matching could have occurred. If we have
165+
* several matches that do not have equal output types, output types may not
166+
* completely match the request as only raw type assignability will be checked
167+
* at the moment.
168+
* @see DefaultOpTypeMatchingService#typesMatch(OpCandidate)
169+
* @param matches
170+
* @return
171+
*/
172+
private static boolean typeCheckingIncomplete(List<OpCandidate> matches) {
173+
Type[] outputTypes = null;
174+
for (OpCandidate match : matches) {
175+
Type[] ts = getTypes(outputs(match));
176+
if (outputTypes == null || Arrays.deepEquals(outputTypes, ts)) {
177+
outputTypes = ts;
178+
continue;
179+
} else {
180+
return true;
181+
}
182+
}
183+
return false;
184+
}
185+
186+
public static Type[] getTypes(List<Member<?>> members) {
187+
return members.stream().map(m -> m.getType()).toArray(Type[]::new);
188+
}
189+
157190
/**
158191
* Gets a string with an analysis of a particular match request failure.
159192
* <p>
@@ -174,14 +207,17 @@ public static String matchInfo(final MatchingResult res) {
174207
List<OpCandidate> candidates = res.getCandidates();
175208
List<OpCandidate> matches = res.getMatches();
176209

177-
final OpRef ref = candidates.get(0).getRef();
210+
final OpRef ref = res.getOriginalQueries().get(0);
178211
if (matches.isEmpty()) {
179212
// no matches
180213
sb.append("No matching '" + ref.getLabel() + "' op\n");
181214
} else {
182215
// multiple matches
183216
final double priority = getPriority(matches.get(0));
184217
sb.append("Multiple '" + ref.getLabel() + "' ops of priority " + priority + ":\n");
218+
if (typeCheckingIncomplete(matches)) {
219+
sb.append("Incomplete output type checking may have occured!\n");
220+
}
185221
int count = 0;
186222
for (final OpCandidate match : matches) {
187223
sb.append(++count + ". ");

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public MatchingResult(final List<OpCandidate> candidates, final List<OpCandidate
2929
this.originalQueries = originalQueries;
3030
}
3131

32+
public List<OpRef> getOriginalQueries() {
33+
return originalQueries;
34+
}
35+
3236
public List<OpCandidate> getCandidates() {
3337
return candidates;
3438
}
@@ -51,11 +55,9 @@ public OpCandidate singleMatch() {
5155
// }
5256

5357
return matches.get(0);
54-
} else if (matches.size() > 1) {
58+
} else {
5559
final String analysis = OpUtils.matchInfo(this);
5660
throw new IllegalArgumentException(analysis);
57-
} else {
58-
throw new IllegalArgumentException("No candidate op for: '" + originalQueries.get(0).getLabel() + "'");
5961
}
6062
}
6163
}

0 commit comments

Comments
 (0)