Skip to content

Commit 752c64d

Browse files
committed
Types: Consolidate redundant methods
The substituteTypeVars method and both mapVarToTypes methods are all just unrolling of type variables with followTypeVars set to false.
1 parent c36470e commit 752c64d

File tree

10 files changed

+56
-77
lines changed

10 files changed

+56
-77
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -677,8 +677,8 @@ private List<RichOp<?>> resolveOpDependencies(OpInfo info,
677677
private OpRequest inferOpRequest(OpDependencyMember<?> dependency,
678678
Map<TypeVariable<?>, Type> typeVarAssigns)
679679
{
680-
final Type mappedDependencyType = Types.mapVarToTypes(new Type[] {
681-
dependency.type() }, typeVarAssigns)[0];
680+
final Type mappedDependencyType = Types.unroll(
681+
new Type[] { dependency.type() }, typeVarAssigns)[0];
682682
final String dependencyName = dependency.getDependencyName();
683683
return inferOpRequest(mappedDependencyType, dependencyName, typeVarAssigns);
684684
}
@@ -731,8 +731,8 @@ private OpRequest inferOpRequest(Type type, String name,
731731
.map(FunctionalMethodType::type) //
732732
.toArray(Type[]::new);
733733

734-
Type[] mappedInputs = Types.mapVarToTypes(inputs, typeVarAssigns);
735-
Type[] mappedOutputs = Types.mapVarToTypes(outputs, typeVarAssigns);
734+
Type[] mappedInputs = Types.unroll(inputs, typeVarAssigns);
735+
Type[] mappedOutputs = Types.unroll(outputs, typeVarAssigns);
736736

737737
final int numOutputs = mappedOutputs.length;
738738
if (numOutputs != 1) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static Type getReifiedType(OpRequest request, OpInfo info,
104104
{
105105
Type exactSuperType = Types.superTypeOf(info.opType(), Types.raw(
106106
request.type()));
107-
return Types.mapVarToTypes(exactSuperType, typeVarAssigns);
107+
return Types.unroll(exactSuperType, typeVarAssigns);
108108
}
109109

110110
/** Gets the op execution environment of the desired match. */

scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/adapt/AdaptationInfoTreeGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public InfoTree generate(OpEnvironment env, String signature,
7979
if (!Types.isAssignable(originalInfo.opType(), adaptorTree.info().inputs()
8080
.get(0).type(), typeVarAssigns)) throw new IllegalArgumentException(
8181
"The adaptor cannot be used on Op " + originalInfo);
82-
Type adaptedOpType = Types.substituteTypeVars(adaptorTree.info()
82+
Type adaptedOpType = Types.unroll(adaptorTree.info()
8383
.output().type(), typeVarAssigns);
8484
OpInfo adaptedInfo = new OpAdaptationInfo(originalInfo, adaptedOpType,
8585
adaptorTree);

scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/adapt/AdaptationMatchingRoutine.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public OpCandidate findMatch(MatchingConditions conditions, OpMatcher matcher,
120120
// an Op that will then be adapted (this will be the only input of the
121121
// adaptor since we know it is a Function)
122122
Type adaptFrom = adaptor.inputTypes().get(0);
123-
Type srcOpType = Types.substituteTypeVars(adaptFrom, map);
123+
Type srcOpType = Types.unroll(adaptFrom, map);
124124
final OpRequest srcOpRequest = inferOpRequest(srcOpType, conditions
125125
.request().name(), map);
126126
final OpCandidate srcCandidate = matcher.match(MatchingConditions.from(
@@ -144,7 +144,7 @@ public OpCandidate findMatch(MatchingConditions conditions, OpMatcher matcher,
144144
return Ops.infoTree(op);
145145
}).collect(Collectors.toList());
146146
// And return the Adaptor, wrapped up into an OpCandidate
147-
Type adapterOpType = Types.substituteTypeVars(adaptor.output()
147+
Type adapterOpType = Types.unroll(adaptor.output()
148148
.type(), map);
149149
InfoTree adaptorChain = new InfoTree(adaptor, depTrees);
150150
OpAdaptationInfo adaptedInfo = new OpAdaptationInfo(srcCandidate
@@ -205,7 +205,7 @@ private void captureTypeVarsFromCandidate(Type srcType, OpCandidate candidate,
205205
private OpRequest inferOpRequest(OpDependencyMember<?> dependency,
206206
Map<TypeVariable<?>, Type> typeVarAssigns)
207207
{
208-
final Type mappedDependencyType = Types.mapVarToTypes(new Type[] {
208+
final Type mappedDependencyType = Types.unroll(new Type[] {
209209
dependency.type() }, typeVarAssigns)[0];
210210
final String dependencyName = dependency.getDependencyName();
211211
final OpRequest inferred = inferOpRequest(mappedDependencyType,
@@ -277,8 +277,8 @@ private OpRequest inferOpRequest(Type type, String name,
277277
Type[] outputs = fmts.stream().filter(fmt -> outIos.contains(fmt.itemIO()))
278278
.map(fmt -> fmt.type()).toArray(Type[]::new);
279279

280-
Type[] mappedInputs = Types.mapVarToTypes(inputs, typeVarAssigns);
281-
Type[] mappedOutputs = Types.mapVarToTypes(outputs, typeVarAssigns);
280+
Type[] mappedInputs = Types.unroll(inputs, typeVarAssigns);
281+
Type[] mappedOutputs = Types.unroll(outputs, typeVarAssigns);
282282

283283
final int numOutputs = mappedOutputs.length;
284284
if (numOutputs != 1) {

scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/convert/Conversions.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private static Optional<ConvertedOpInfo> postprocessFunction(OpInfo info,
198198
return Optional.empty();
199199
}
200200
// for functions, we only need a postconverter
201-
var fromOut = Nil.of(Types.mapVarToTypes(info.outputType(), vars));
201+
var fromOut = Nil.of(Types.unroll(info.outputType(), vars));
202202
var toOut = Nil.of(request.outType());
203203
RichOp<Function<?, ?>> postConverter = Ops.rich(env.op("engine.convert",
204204
hints).inType(fromOut).outType(toOut).function());
@@ -291,7 +291,7 @@ private static Optional<ConvertedOpInfo> postprocessConvertAndCopy(
291291
return Optional.empty();
292292
}
293293
try {
294-
var fromOut = Nil.of(Types.mapVarToTypes(info.outputType(), vars));
294+
var fromOut = Nil.of(Types.unroll(info.outputType(), vars));
295295
var toOut = Nil.of(request.outType());
296296
// First, we convert the output to the type the user requested
297297
RichOp<Function<?, ?>> postConverter = Ops.rich( //
@@ -354,7 +354,7 @@ private static Optional<ConvertedOpInfo> postprocessCopy(OpInfo info,
354354
return Optional.empty();
355355
}
356356
try {
357-
var fromOut = Nil.of(Types.mapVarToTypes(info.outputType(), vars));
357+
var fromOut = Nil.of(Types.unroll(info.outputType(), vars));
358358
var toOut = Nil.of(request.outType());
359359
// This is really just a placeholder.
360360
RichOp<Function<?, ?>> postConverter = Ops.rich( //
@@ -406,7 +406,7 @@ private static Optional<ConvertedOpInfo> postprocessCopy(OpInfo info,
406406
var source = Nil.of(from);
407407
// If the op parameter type has type variables that have been mapped
408408
// already, substitute those mappings in.
409-
var preDest = Types.mapVarToTypes(to, vars);
409+
var preDest = Types.unroll(to, vars);
410410
// Remaining type variables are unlikely to be matched directly. We thus
411411
// replace them with wildcards, bounded by the same bounds.
412412
var dest = wildcardVacuousTypeVars(preDest);
@@ -463,7 +463,7 @@ private static Nil<?> wildcardVacuousTypeVars(final Type t) {
463463
vars.put(from, to);
464464
}
465465
}
466-
return Nil.of(Types.mapVarToTypes(t, vars));
466+
return Nil.of(Types.unroll(t, vars));
467467
}
468468

469469
/**

scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/convert/ConvertedOpInfo.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ private static Type[] inTypes(List<Type> originalInputs,
367367
typeAssigns //
368368
);
369369
// Map type variables in T to Types inferred above
370-
inTypes[i] = Types.mapVarToTypes(inTypes[i], typeAssigns);
370+
inTypes[i] = Types.unroll(inTypes[i], typeAssigns);
371371
}
372372
return inTypes;
373373
}
@@ -398,7 +398,7 @@ private static Type outType( //
398398
vars //
399399
);
400400
// map type variables in T to Types inferred in Step 2a
401-
return Types.mapVarToTypes(outType, vars);
401+
return Types.unroll(outType, vars);
402402
}
403403

404404
@Override
@@ -431,7 +431,7 @@ private static Type mapAnys(Type opType, OpInfo info) {
431431
reqMap.put(key, infoMap.get(key));
432432
}
433433
}
434-
return Types.mapVarToTypes(raw, reqMap);
434+
return Types.unroll(raw, reqMap);
435435
}
436436

437437
/**
@@ -467,7 +467,7 @@ private double calculatePriority(OpEnvironment env) {
467467
List<Type> inputs = inputTypes();
468468
for (int i = 0; i < inputs.size(); i++) {
469469
var from = inputs.get(i);
470-
var to = Types.mapVarToTypes(originalInputs.get(i), typeVarAssigns);
470+
var to = Types.unroll(originalInputs.get(i), typeVarAssigns);
471471
penalty += determineLoss(env, Nil.of(from), Nil.of(to));
472472
}
473473

@@ -590,7 +590,7 @@ private static Type[] typesFromOpType(Class<?> opType, Method fMethod,
590590
GenericAssignability.inferTypeVariables(new Type[] {
591591
genericDeclaringClass }, new Type[] { superGenericClass }, map);
592592

593-
return Types.mapVarToTypes(types, map);
593+
return Types.unroll(types, map);
594594
}
595595

596596
/**

scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/InfoMatchingOpRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public InfoMatchingOpRequest(OpInfo info, Nil<?> specialType) {
8282

8383
private Type mappedType(Type t, Map<TypeVariable<?>, Type> map) {
8484
try {
85-
return Types.substituteTypeVars(t, map);
85+
return Types.unroll(t, map);
8686
}
8787
catch (Exception e) {
8888
return t;

scijava-types/src/main/java/org/scijava/types/Types.java

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -642,57 +642,6 @@ public static <T> T cast(final Object src, final Class<T> dest) {
642642
return result;
643643
}
644644

645-
public static Type substituteTypeVars(Type type,
646-
Map<TypeVariable<?>, Type> typeVarAssigns)
647-
{
648-
if (type == null || type instanceof Class) {
649-
return type;
650-
}
651-
652-
if (type instanceof ParameterizedType) {
653-
Class<?> raw = raw(type);
654-
Type[] typeParams = ((ParameterizedType) type).getActualTypeArguments();
655-
for (int i = 0; i < typeParams.length; i++) {
656-
typeParams[i] = TypeUtils.substituteTypeVariables(typeParams[i],
657-
typeVarAssigns);
658-
}
659-
return parameterize(raw, typeParams);
660-
}
661-
662-
if (type instanceof GenericArrayType) {}
663-
664-
if (type instanceof WildcardType) {
665-
return type;
666-
}
667-
668-
if (type instanceof TypeVariable) {
669-
return TypeUtils.substituteTypeVariables(type, typeVarAssigns);
670-
}
671-
672-
throw new IllegalStateException("found an unhandled type: " + type);
673-
}
674-
675-
/**
676-
* Map type vars in specified type list to types using the specified map. In
677-
* doing so, type vars mapping to other type vars will not be followed but
678-
* just replaced.
679-
*
680-
* @param typesToMap
681-
* @param typeVarAssigns
682-
*/
683-
public static Type[] mapVarToTypes(Type[] typesToMap,
684-
Map<TypeVariable<?>, Type> typeVarAssigns)
685-
{
686-
return Arrays.stream(typesToMap).map(type -> mapVarToTypes(type,
687-
typeVarAssigns)).toArray(Type[]::new);
688-
}
689-
690-
public static Type mapVarToTypes(Type typeToMap,
691-
Map<TypeVariable<?>, Type> typeVarAssigns)
692-
{
693-
return unroll(typeVarAssigns, typeToMap, false);
694-
}
695-
696645
/**
697646
* Converts the given string value to an enumeration constant of the specified
698647
* type.
@@ -911,6 +860,36 @@ public static Type superTypeOf(final Type type, final Class<?> searchClass) {
911860
return GenericTypeReflector.getExactSuperType(type, searchClass);
912861
}
913862

863+
/**
864+
* Map type vars in specified type list to types using the specified map. In
865+
* doing so, type vars mapping to other type vars will not be followed but
866+
* just replaced.
867+
*
868+
* @param typesToMap
869+
* @param typeVarAssigns
870+
*/
871+
public static Type[] unroll(Type[] typesToMap,
872+
Map<TypeVariable<?>, Type> typeVarAssigns)
873+
{
874+
return Arrays.stream(typesToMap)
875+
.map(type -> unroll(type, typeVarAssigns))
876+
.toArray(Type[]::new);
877+
}
878+
879+
/**
880+
* Map type vars in the specified type to a type using the specified map. In
881+
* doing so, type vars mapping to other type vars will not be followed but
882+
* just replaced.
883+
*
884+
* @param typeToMap
885+
* @param typeVarAssigns
886+
*/
887+
public static Type unroll(Type typeToMap,
888+
Map<TypeVariable<?>, Type> typeVarAssigns)
889+
{
890+
return unroll(typeVarAssigns, typeToMap, false);
891+
}
892+
914893
/**
915894
* Get a type representing {@code type} with variable assignments
916895
* "unrolled."
@@ -2361,7 +2340,7 @@ private static Type substituteTypeVariables(final Type type,
23612340
final Map<TypeVariable<?>, Type> typeVarAssigns)
23622341
{
23632342
if (type instanceof ParameterizedType) {
2364-
return substituteTypeVars(type, typeVarAssigns);
2343+
return unroll(type, typeVarAssigns);
23652344
}
23662345
if (type instanceof TypeVariable && typeVarAssigns != null) {
23672346
final Type replacementType = typeVarAssigns.get(type);
@@ -2374,7 +2353,7 @@ private static Type substituteTypeVariables(final Type type,
23742353
}
23752354
if (type instanceof GenericArrayType && typeVarAssigns != null) {
23762355
final GenericArrayType genArrType = (GenericArrayType) type;
2377-
final Type replacementType = substituteTypeVars(
2356+
final Type replacementType = unroll(
23782357
genArrType.getGenericComponentType(), typeVarAssigns);
23792358
return new GenericArrayTypeImpl(replacementType);
23802359
}

scijava-types/src/main/java/org/scijava/types/extract/SubTypeExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,6 @@ private static Type parameterizeViaSuperType(Class<?> cls,
103103
if (typeVars[i] instanceof TypeVariable) map.put(
104104
(TypeVariable<?>) typeVars[i], superClsTypeVars[i]);
105105
}
106-
return Types.mapVarToTypes(t, map);
106+
return Types.unroll(t, map);
107107
}
108108
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ private static void inferTypeVariables(ParameterizedType type, Type inferFrom,
455455
}
456456
else if (superInferFrom instanceof Class) {
457457
TypeVarAssigns typeVarAssigns = new TypeVarAssigns(typeMappings);
458-
Type mappedType = Types.mapVarToTypes(type, typeVarAssigns);
458+
Type mappedType = Types.unroll(type, typeVarAssigns);
459459
// Use isAssignable to attempt to infer the type variables present in type
460460
if (!Types.isAssignable(superInferFrom, mappedType, typeVarAssigns)) {
461461
throw new TypeInferenceException(inferFrom +

0 commit comments

Comments
 (0)