|
17 | 17 | import java.util.List; |
18 | 18 | import java.util.Map; |
19 | 19 | import java.util.Set; |
| 20 | +import java.util.function.Function; |
20 | 21 | import java.util.stream.Collectors; |
21 | 22 |
|
22 | 23 | import org.scijava.struct.ItemIO; |
|
34 | 35 | * Utility functions for working with {@link org.scijava.param} classes. |
35 | 36 | * |
36 | 37 | * @author Curtis Rueden |
| 38 | + * @author David Kolb |
37 | 39 | */ |
38 | 40 | public final class ParameterStructs { |
39 | 41 |
|
| 42 | + /** |
| 43 | + * Convenience method to call {@link #structOf(Class)} dot {@link Struct#createInstance(Object)} |
| 44 | + * |
| 45 | + * @param object |
| 46 | + * @return |
| 47 | + * @throws ValidityException |
| 48 | + */ |
40 | 49 | public static <C> StructInstance<C> create(final C object) |
41 | 50 | throws ValidityException |
42 | 51 | { |
43 | 52 | return structOf(object.getClass()).createInstance(object); |
44 | 53 | } |
45 | 54 |
|
| 55 | + /** |
| 56 | + * Convenience method to () -> parse(type) |
| 57 | + * |
| 58 | + * @param type |
| 59 | + * @return |
| 60 | + * @throws ValidityException |
| 61 | + */ |
46 | 62 | public static Struct structOf(final Class<?> type) |
47 | 63 | throws ValidityException |
48 | 64 | { |
49 | 65 | final List<Member<?>> items = parse(type); |
50 | 66 | return () -> items; |
51 | 67 | } |
52 | 68 |
|
| 69 | + /** |
| 70 | + * Convenience method to () -> parse(field) |
| 71 | + * |
| 72 | + * @param field |
| 73 | + * @return |
| 74 | + * @throws ValidityException |
| 75 | + */ |
53 | 76 | public static Struct structOf(final Field field) |
54 | 77 | throws ValidityException |
55 | 78 | { |
56 | 79 | final List<Member<?>> items = parse(field); |
57 | 80 | return () -> items; |
58 | 81 | } |
59 | 82 |
|
| 83 | + /** |
| 84 | + * Parses the specified functional class for @{@link Parameter} annotations. This consists of the following steps: |
| 85 | + * <br><br> |
| 86 | + * 1) First annotations on the class level are checked. These annotate the signature (parameters and return type) of |
| 87 | + * the specified functional class (or subtype of one). The annotations re expected to be in the following order: |
| 88 | + * parameters, return type. |
| 89 | + * <br> |
| 90 | + * E.g. a {@link Function} may be annotated with two @{@link Parameter} annotations, where the first annotation will |
| 91 | + * annotate the parameter and the second the return type of the functional method {@link Function#apply(Object)} of |
| 92 | + * {@link Function}. |
| 93 | + * <br><br> |
| 94 | + * 2) Second, annotations on the fields of the specified class are checked. |
| 95 | + * |
| 96 | + * @param type the class to parse, is expected to contain some functional interface in its hierarchy |
| 97 | + * @return list of identified member instances |
| 98 | + * @throws ValidityException if there are problems during parsing |
| 99 | + */ |
60 | 100 | public static List<Member<?>> parse(final Class<?> type) |
61 | 101 | throws ValidityException |
62 | 102 | { |
@@ -84,6 +124,14 @@ public static List<Member<?>> parse(final Class<?> type) |
84 | 124 | return items; |
85 | 125 | } |
86 | 126 |
|
| 127 | + /** |
| 128 | + * Parses the specified field for @{@link Parameter} annotations. Has the same behavior as the first parsing step |
| 129 | + * of {@link #parse(Class)}. |
| 130 | + * |
| 131 | + * @param field the field to parse, is expected to contain some functional interface in the hierarchy of its type |
| 132 | + * @return list of identified member instances |
| 133 | + * @throws ValidityException if there are problems during parsing |
| 134 | + */ |
87 | 135 | public static List<Member<?>> parse(final Field field) throws ValidityException { |
88 | 136 | Class<?> c = field.getDeclaringClass(); |
89 | 137 | if (c == null || field == null) return null; |
@@ -166,6 +214,20 @@ private static void parseFieldParameters (final ArrayList<Member<?>> items, fina |
166 | 214 | } |
167 | 215 | } |
168 | 216 |
|
| 217 | + /** |
| 218 | + * Create new instances of {@link Parameter} annotations having default names (key) and the {@link ItemIO} |
| 219 | + * from the specified list of {@link FunctionalMethodType}s. Default names will be:<br><br> |
| 220 | + * 'mutable{index}' for {@link ItemIO#BOTH}<br> |
| 221 | + * 'input{index}' for {@link ItemIO#INPUT}<br> |
| 222 | + * 'output{index}' for {@link ItemIO#OUTPUT}<br><br> |
| 223 | + * with {index} being counted individually. |
| 224 | + * |
| 225 | + * This is used to infer the annotations for {@link FunctionalParameterMember}s if the {@link Parameter} is not |
| 226 | + * explicitly specified by a user and should thus be inferred from the functional method type. |
| 227 | + * |
| 228 | + * @param fmts |
| 229 | + * @return |
| 230 | + */ |
169 | 231 | private static Parameter[] synthesizeParameterAnnotations(final List<FunctionalMethodType> fmts) { |
170 | 232 | List<Parameter> params = new ArrayList<>(); |
171 | 233 |
|
@@ -206,12 +268,26 @@ private static Parameter[] synthesizeParameterAnnotations(final List<FunctionalM |
206 | 268 | return params.toArray(new Parameter[params.size()]); |
207 | 269 | } |
208 | 270 |
|
| 271 | + /** |
| 272 | + * Mutates {@link ItemIO#AUTO} in the specified annotations array by replacing it with the inferred {@link ItemIO} |
| 273 | + * from the specified {@link FunctionalMethodType}s. Also checks if the user defined {@link ItemIO} matches the |
| 274 | + * inferred one if its different from AUTO and logs the errors in the specified problems list. It is expected that |
| 275 | + * the order of annotations matches the order of specified {@link FunctionalMethodType}s. |
| 276 | + * |
| 277 | + * @param annotations the {@link Parameter} annotations to mutate |
| 278 | + * @param fmts inferred method types from the functional method |
| 279 | + * @param problems list to record problems |
| 280 | + * @return true if new problems got added to the problems list |
| 281 | + */ |
209 | 282 | private static boolean resolveItemIOAuto(Parameter[] annotations, List<FunctionalMethodType> fmts, final ArrayList<ValidityProblem> problems) { |
210 | 283 | boolean dirty = false; |
211 | 284 | int i = 0; |
212 | 285 | for (Parameter anno : annotations) { |
213 | 286 | FunctionalMethodType fmt = fmts.get(i); |
214 | 287 | if (anno.itemIO().equals(ItemIO.AUTO)) { |
| 288 | + // NB: Mutating the annotation should be fine here, as the functional signature can't change dynamically. |
| 289 | + // Hence, the inferred ITemIO should stay valid. (And for now we do not need information about AUTO after |
| 290 | + // this point) |
215 | 291 | ItemIO io = (ItemIO) AnnotationUtils.mutateAnnotationInstance(anno, Parameter.ITEMIO_FIELD_NAME, fmt.itemIO()); |
216 | 292 | assert io.equals(ItemIO.AUTO); |
217 | 293 | // if the ItemIO is explicitly specified, we can check if it matches the inferred ItemIO from the functional method |
|
0 commit comments