From 6dd64d84b3aa9578087e7dec4a4567d3f9f4e0b1 Mon Sep 17 00:00:00 2001 From: Gabriel Selzer Date: Mon, 18 Oct 2021 14:09:11 -0500 Subject: [PATCH] Remove MethodParameterMemberParser hack --- .../engine/matcher/impl/OpAdaptationInfo.java | 2 +- .../ops/engine/matcher/impl/OpClassInfo.java | 2 +- .../ops/engine/matcher/impl/OpFieldInfo.java | 4 +++- .../ops/engine/matcher/impl/OpMethodInfo.java | 9 +++++---- .../ops/engine/simplify/SimplifiedOpInfo.java | 2 +- .../struct/ClassOpDependencyMemberParser.java | 3 ++- .../struct/ClassParameterMemberParser.java | 2 +- .../struct/FieldParameterMemberParser.java | 4 ++-- .../struct/MethodOpDependencyMemberParser.java | 2 +- .../struct/MethodParameterMemberParser.java | 16 +++------------- .../engine/struct/OpRetypingMemberParser.java | 7 ++++--- .../java/org/scijava/struct/MemberParser.java | 3 ++- .../main/java/org/scijava/struct/Structs.java | 5 +++-- 13 files changed, 29 insertions(+), 32 deletions(-) diff --git a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpAdaptationInfo.java b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpAdaptationInfo.java index 9f76b8077..d552487fb 100644 --- a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpAdaptationInfo.java +++ b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpAdaptationInfo.java @@ -59,7 +59,7 @@ public OpAdaptationInfo(OpInfo srcInfo, Type type, List fmts = FunctionalParameters.findFunctionalMethodTypes(type); RetypingRequest r = new RetypingRequest(srcInfo.struct(), fmts); - struct = Structs.from(r, problems, new OpRetypingMemberParser()); + struct = Structs.from(r, type, problems, new OpRetypingMemberParser()); try { OpUtils.checkHasSingleOutput(struct); } diff --git a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpClassInfo.java b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpClassInfo.java index 1b856a27b..f7fd1ac5d 100644 --- a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpClassInfo.java +++ b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpClassInfo.java @@ -77,7 +77,7 @@ public OpClassInfo(final Class opClass, final String version, final Hints hin this.names = Arrays.asList(names); List problems = new ArrayList<>(); try { - struct = Structs.from(opClass, problems, new ClassParameterMemberParser(), new ClassOpDependencyMemberParser()); + struct = Structs.from(opClass, opClass, problems, new ClassParameterMemberParser(), new ClassOpDependencyMemberParser()); OpUtils.checkHasSingleOutput(struct); } catch (ValidityException e) { validityException = e; diff --git a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpFieldInfo.java b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpFieldInfo.java index b298c1a2a..543b16a0e 100644 --- a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpFieldInfo.java +++ b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpFieldInfo.java @@ -48,6 +48,7 @@ import org.scijava.struct.StructInstance; import org.scijava.struct.Structs; import org.scijava.struct.ValidityException; +import org.scijava.types.Types; /** * Metadata about an op implementation defined as a field. @@ -100,7 +101,8 @@ public OpFieldInfo(final Object instance, final Field field, final String versio // NB: Subclassing a collection and inheriting its fields is NOT // ALLOWED! try { - struct = Structs.from(field, problems, new FieldParameterMemberParser()); + Type structType = Types.fieldType(field, field.getDeclaringClass()); + struct = Structs.from(field, structType, problems, new FieldParameterMemberParser()); // struct = ParameterStructs.structOf(field); OpUtils.checkHasSingleOutput(struct); // NB: Contextual parameters not supported for now. diff --git a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpMethodInfo.java b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpMethodInfo.java index 42638823a..1a7d4f568 100644 --- a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpMethodInfo.java +++ b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/impl/OpMethodInfo.java @@ -104,18 +104,19 @@ public OpMethodInfo(final Method method, final Class opType, final String ver checkModifiers(method, problems); this.opType = findOpType(method, opType, problems); - this.struct = generateStruct(method, problems, new MethodParameterMemberParser(opType), new MethodOpDependencyMemberParser()); + this.struct = generateStruct(method, opType, problems, new MethodParameterMemberParser(), new MethodOpDependencyMemberParser()); validityException = problems.isEmpty() ? null : new ValidityException( problems); } @SafeVarargs - private Struct generateStruct(Method m, List problems, - MemberParser> ... memberParsers) + private Struct generateStruct(Method m, Type structType, + List problems, + MemberParser>... memberParsers) { try { - return Structs.from(m, problems, memberParsers); + return Structs.from(m, structType, problems, memberParsers); } catch (IllegalArgumentException e) { problems.add(new ValidityProblem(e)); return null; diff --git a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/simplify/SimplifiedOpInfo.java b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/simplify/SimplifiedOpInfo.java index 476273bdf..6c4583d1a 100644 --- a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/simplify/SimplifiedOpInfo.java +++ b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/simplify/SimplifiedOpInfo.java @@ -75,7 +75,7 @@ public SimplifiedOpInfo(OpInfo info, SimplificationMetadata metadata, double pri this.opType = SimplificationUtils.retypeOpType(info.opType(), inputTypes, outputType); RetypingRequest r = new RetypingRequest(info.struct(), fmts); - this.struct = Structs.from(r, problems, new OpRetypingMemberParser()); + this.struct = Structs.from(r, opType, problems, new OpRetypingMemberParser()); this.priority = priority; this.hints = srcInfo.declaredHints().plus(Simplification.FORBIDDEN); diff --git a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/ClassOpDependencyMemberParser.java b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/ClassOpDependencyMemberParser.java index e08f82a56..08aa3a672 100644 --- a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/ClassOpDependencyMemberParser.java +++ b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/ClassOpDependencyMemberParser.java @@ -3,6 +3,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; @@ -17,7 +18,7 @@ public class ClassOpDependencyMemberParser implements { @Override - public List> parse(Class source) + public List> parse(Class source, Type structType) throws ValidityException { if (source == null) return null; diff --git a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/ClassParameterMemberParser.java b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/ClassParameterMemberParser.java index a5ee79b08..d88ce931e 100644 --- a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/ClassParameterMemberParser.java +++ b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/ClassParameterMemberParser.java @@ -19,7 +19,7 @@ public class ClassParameterMemberParser implements { @Override - public List> parse(Class source) + public List> parse(Class source, Type structType) throws ValidityException { if (source == null) return null; diff --git a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/FieldParameterMemberParser.java b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/FieldParameterMemberParser.java index 63f4989a4..68ad4ebeb 100644 --- a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/FieldParameterMemberParser.java +++ b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/FieldParameterMemberParser.java @@ -17,7 +17,7 @@ public class FieldParameterMemberParser implements { @Override - public List> parse(Field source) + public List> parse(Field source, Type structType) throws ValidityException { if (source == null) return null; @@ -32,7 +32,7 @@ public List> parse(Field source) org.scijava.struct.Structs.checkModifiers(source.toString() + ": ", problems, source .getModifiers(), false, Modifier.FINAL); - FunctionalParameters.parseFunctionalParameters(items, problems, fieldType, + FunctionalParameters.parseFunctionalParameters(items, problems, structType, paramData); // Fail if there were any problems. if (!problems.isEmpty()) { diff --git a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/MethodOpDependencyMemberParser.java b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/MethodOpDependencyMemberParser.java index be402509a..2389cee17 100644 --- a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/MethodOpDependencyMemberParser.java +++ b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/MethodOpDependencyMemberParser.java @@ -26,7 +26,7 @@ public class MethodOpDependencyMemberParser implements private static final Map methodDocMap = new ConcurrentHashMap<>(); @Override - public List> parse(Method source) + public List> parse(Method source, Type structType) throws ValidityException { if (source == null) return null; diff --git a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/MethodParameterMemberParser.java b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/MethodParameterMemberParser.java index 357dceb71..1bb7b071c 100644 --- a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/MethodParameterMemberParser.java +++ b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/MethodParameterMemberParser.java @@ -16,18 +16,8 @@ public class MethodParameterMemberParser implements MemberParser> { - private final Class opType; - - /** - * HACK: We need the opType here to determine the functional type. - * @param opType - */ - public MethodParameterMemberParser(Class opType) { - this.opType = opType; - } - @Override - public List> parse(Method source) + public List> parse(Method source, Type structType) throws ValidityException { if (source == null) return null; @@ -43,11 +33,11 @@ public List> parse(Method source) // Determine functional type Type functionalType; try { - functionalType = OpMethodUtils.getOpMethodType(opType, source); + functionalType = OpMethodUtils.getOpMethodType(Types.raw(structType), source); } catch (IllegalArgumentException e) { problems.add(new ValidityProblem(e.getMessage())); - functionalType = Types.parameterizeRaw(opType); + functionalType = Types.parameterizeRaw(Types.raw(structType)); } // Parse method level @Parameter annotations. diff --git a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/OpRetypingMemberParser.java b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/OpRetypingMemberParser.java index 00a053778..2ad0c71b4 100644 --- a/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/OpRetypingMemberParser.java +++ b/scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/OpRetypingMemberParser.java @@ -1,6 +1,7 @@ package org.scijava.ops.engine.struct; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; @@ -37,7 +38,7 @@ public class OpRetypingMemberParser implements * output of this {@link SimplifiedOpInfo} */ @Override - public List> parse(RetypingRequest source) + public List> parse(RetypingRequest source, Type structType) throws ValidityException { List newFmts = source.newFmts(); @@ -58,10 +59,10 @@ else if (m.isOutput()) { return newMembers; } - public List> parse(Struct s, List newFmts) + public List> parse(Struct s, List newFmts, Type structType) throws ValidityException { - return parse(new RetypingRequest(s, newFmts)); + return parse(new RetypingRequest(s, newFmts), structType); } } diff --git a/scijava/scijava-struct/src/main/java/org/scijava/struct/MemberParser.java b/scijava/scijava-struct/src/main/java/org/scijava/struct/MemberParser.java index 0afbb6ecc..09520a9dc 100644 --- a/scijava/scijava-struct/src/main/java/org/scijava/struct/MemberParser.java +++ b/scijava/scijava-struct/src/main/java/org/scijava/struct/MemberParser.java @@ -18,10 +18,11 @@ public interface MemberParser> { * returned * * @param source the {@link Object} to parse + * @param structType TODO * @return a {@link List} of {@link Member}s parsed from {@code source} * @throws {@link ValidityException} when the source of the {@link Member}s is * improperly declared, formatted */ - List parse(S source) throws ValidityException; + List parse(S source, Type structType) throws ValidityException; } diff --git a/scijava/scijava-struct/src/main/java/org/scijava/struct/Structs.java b/scijava/scijava-struct/src/main/java/org/scijava/struct/Structs.java index ab9795b4d..c811ea495 100644 --- a/scijava/scijava-struct/src/main/java/org/scijava/struct/Structs.java +++ b/scijava/scijava-struct/src/main/java/org/scijava/struct/Structs.java @@ -2,6 +2,7 @@ package org.scijava.struct; import java.lang.reflect.Modifier; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; import java.util.function.Supplier; @@ -30,13 +31,13 @@ public static StructInstance expand( } @SafeVarargs - public static Struct from(S source, List problems, + public static Struct from(S source, Type structType, List problems, MemberParser>... parsers) { List> members = new ArrayList<>(); for (MemberParser> p : parsers) { try { - members.addAll(p.parse(source)); + members.addAll(p.parse(source, structType)); } catch (ValidityException e) { problems.addAll(e.problems());