Skip to content

Commit

Permalink
Remove MethodParameterMemberParser hack
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer authored and ctrueden committed Apr 13, 2022
1 parent c1e6eec commit d0d8479
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public OpAdaptationInfo(OpInfo srcInfo, Type type,
List<FunctionalMethodType> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public OpClassInfo(final Class<?> opClass, final String version, final Hints hin
this.names = Arrays.asList(names);
List<ValidityProblem> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidityProblem> problems,
MemberParser<Method, ? extends Member<?>> ... memberParsers)
private Struct generateStruct(Method m, Type structType,
List<ValidityProblem> problems,
MemberParser<Method, ? extends Member<?>>... 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,7 +18,7 @@ public class ClassOpDependencyMemberParser implements
{

@Override
public List<FieldOpDependencyMember<?>> parse(Class<?> source)
public List<FieldOpDependencyMember<?>> parse(Class<?> source, Type structType)
throws ValidityException
{
if (source == null) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ClassParameterMemberParser implements
{

@Override
public List<SynthesizedParameterMember<?>> parse(Class<?> source)
public List<SynthesizedParameterMember<?>> parse(Class<?> source, Type structType)
throws ValidityException
{
if (source == null) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class FieldParameterMemberParser implements
{

@Override
public List<SynthesizedParameterMember<?>> parse(Field source)
public List<SynthesizedParameterMember<?>> parse(Field source, Type structType)
throws ValidityException
{
if (source == null) return null;
Expand All @@ -32,7 +32,7 @@ public List<SynthesizedParameterMember<?>> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class MethodOpDependencyMemberParser implements
private static final Map<Method, MethodJavadoc> methodDocMap = new ConcurrentHashMap<>();

@Override
public List<MethodParameterOpDependencyMember<?>> parse(Method source)
public List<MethodParameterOpDependencyMember<?>> parse(Method source, Type structType)
throws ValidityException
{
if (source == null) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,8 @@
public class MethodParameterMemberParser implements
MemberParser<Method, SynthesizedParameterMember<?>>
{
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<SynthesizedParameterMember<?>> parse(Method source)
public List<SynthesizedParameterMember<?>> parse(Method source, Type structType)
throws ValidityException
{
if (source == null) return null;
Expand All @@ -43,11 +33,11 @@ public List<SynthesizedParameterMember<?>> 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

package org.scijava.ops.engine.struct;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -37,7 +38,7 @@ public class OpRetypingMemberParser implements
* output of this {@link SimplifiedOpInfo}
*/
@Override
public List<Member<?>> parse(RetypingRequest source)
public List<Member<?>> parse(RetypingRequest source, Type structType)
throws ValidityException
{
List<FunctionalMethodType> newFmts = source.newFmts();
Expand All @@ -58,10 +59,10 @@ else if (m.isOutput()) {
return newMembers;
}

public List<Member<?>> parse(Struct s, List<FunctionalMethodType> newFmts)
public List<Member<?>> parse(Struct s, List<FunctionalMethodType> newFmts, Type structType)
throws ValidityException
{
return parse(new RetypingRequest(s, newFmts));
return parse(new RetypingRequest(s, newFmts), structType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ public interface MemberParser<S, M extends Member<?>> {
* 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<M> parse(S source) throws ValidityException;
List<M> parse(S source, Type structType) throws ValidityException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -30,13 +31,13 @@ public static <T> StructInstance<T> expand(
}

@SafeVarargs
public static <S> Struct from(S source, List<ValidityProblem> problems,
public static <S> Struct from(S source, Type structType, List<ValidityProblem> problems,
MemberParser<S, ? extends Member<?>>... parsers)
{
List<Member<?>> members = new ArrayList<>();
for (MemberParser<S, ? extends Member<?>> p : parsers) {
try {
members.addAll(p.parse(source));
members.addAll(p.parse(source, structType));
}
catch (ValidityException e) {
problems.addAll(e.problems());
Expand Down

0 comments on commit d0d8479

Please sign in to comment.