Skip to content

Commit 397aa0b

Browse files
committed
Clean DefaultOpEnvironment.resolveOpDependencies
1 parent 11e53d4 commit 397aa0b

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

scijava-ops-engine/src/main/java/org/scijava/ops/engine/impl/DefaultOpEnvironment.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.scijava.struct.FunctionalMethodType;
5252
import org.scijava.struct.ItemIO;
5353
import org.scijava.types.*;
54+
import org.scijava.types.inference.GenericAssignability;
5455
import org.slf4j.Logger;
5556
import org.slf4j.LoggerFactory;
5657

@@ -595,27 +596,16 @@ private List<RichOp<?>> resolveOpDependencies(OpInfo info,
595596
final OpRequest request = inferOpRequest(dependency,
596597
dependencyTypeVarAssigns);
597598
try {
599+
// match Op
598600
var depHints = baseDepHints.plus(dependency.hints());
599-
600-
MatchingConditions conditions = MatchingConditions.from(request,
601-
depHints);
602-
// see if the request has been matched already
603-
// OpInstance<?> cachedOp = getInstance(conditions);
604-
// if (cachedOp != null) return conditions;
605-
606-
// obtain suitable OpCandidate
607-
// FIXME: Check cache
608-
var candidate = findOpCandidate(request, depHints);
609-
var instance = instantiateOp(candidate, depHints);
610-
611-
// cache instance
612-
setInstance(conditions, instance);
613-
614-
for (var entry : candidate.typeVarAssigns().entrySet()) {
615-
dependencyTypeVarAssigns.putIfAbsent(entry.getKey(), entry
616-
.getValue());
617-
}
601+
var conditions = MatchingConditions.from(request, depHints);
602+
OpInstance<?> instance = generateCacheHit(conditions);
603+
// add Op to dependencies
618604
dependencyChains.add(wrapOp(instance, conditions));
605+
// refine current type variable knowledge
606+
GenericAssignability //
607+
.inferTypeVariables(request.getType(), instance.getType()) //
608+
.forEach(dependencyTypeVarAssigns::putIfAbsent);
619609

620610
}
621611
catch (final OpMatchingException e) {

scijava-types/src/main/java/org/scijava/types/inference/GenericAssignability.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,25 @@ public static Class<?> getClass(final Object obj) {
242242
return obj.getClass();
243243
}
244244

245+
/**
246+
* Tries to infer type vars contained in types from corresponding types from
247+
* inferFrom, putting them into the specified map. <b>When a
248+
* {@link TypeInferenceException} is thrown, the caller should assume that
249+
* some of the mappings within {@code typeMappings} are incorrect.</b>
250+
*
251+
* @param type
252+
* @param inferFrom
253+
* @return the mapping of {@link TypeVariable}s in {@code type} to
254+
* {@link Type}s in {@code inferFrom}
255+
*/
256+
public static Map<TypeVariable<?>, Type> inferTypeVariables(Type type,
257+
Type inferFrom)
258+
{
259+
Map<TypeVariable<?>, Type> map = new HashMap<>();
260+
inferTypeVariables(new Type[] { type }, new Type[] { inferFrom }, map);
261+
return map;
262+
}
263+
245264
/**
246265
* Tries to infer type vars contained in types from corresponding types from
247266
* inferFrom, putting them into the specified map. <b>When a
@@ -457,6 +476,11 @@ else throw new TypeInferenceException(inferFrom +
457476
private static void inferTypeVariables(ParameterizedType type, Type inferFrom,
458477
Map<TypeVariable<?>, TypeMapping> typeMappings)
459478
{
479+
// Nothing to infer here...
480+
if (type.equals(inferFrom)) {
481+
return;
482+
}
483+
460484
if (inferFrom instanceof WildcardType) {
461485
inferFrom = getInferrableBound((WildcardType) inferFrom);
462486
}

0 commit comments

Comments
 (0)