Skip to content

Commit 695a86a

Browse files
Treiblesschorlewiedenm
authored andcommitted
Javadoc
1 parent 1e716a6 commit 695a86a

3 files changed

Lines changed: 99 additions & 16 deletions

File tree

‎src/main/java/org/scijava/param/ParameterStructs.java‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.List;
1818
import java.util.Map;
1919
import java.util.Set;
20+
import java.util.function.Function;
2021
import java.util.stream.Collectors;
2122

2223
import org.scijava.struct.ItemIO;
@@ -34,29 +35,68 @@
3435
* Utility functions for working with {@link org.scijava.param} classes.
3536
*
3637
* @author Curtis Rueden
38+
* @author David Kolb
3739
*/
3840
public final class ParameterStructs {
3941

42+
/**
43+
* Convenience method to call {@link #structOf(Class)} dot {@link Struct#createInstance(Object)}
44+
*
45+
* @param object
46+
* @return
47+
* @throws ValidityException
48+
*/
4049
public static <C> StructInstance<C> create(final C object)
4150
throws ValidityException
4251
{
4352
return structOf(object.getClass()).createInstance(object);
4453
}
4554

55+
/**
56+
* Convenience method to () -> parse(type)
57+
*
58+
* @param type
59+
* @return
60+
* @throws ValidityException
61+
*/
4662
public static Struct structOf(final Class<?> type)
4763
throws ValidityException
4864
{
4965
final List<Member<?>> items = parse(type);
5066
return () -> items;
5167
}
5268

69+
/**
70+
* Convenience method to () -> parse(field)
71+
*
72+
* @param field
73+
* @return
74+
* @throws ValidityException
75+
*/
5376
public static Struct structOf(final Field field)
5477
throws ValidityException
5578
{
5679
final List<Member<?>> items = parse(field);
5780
return () -> items;
5881
}
5982

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+
*/
60100
public static List<Member<?>> parse(final Class<?> type)
61101
throws ValidityException
62102
{
@@ -84,6 +124,14 @@ public static List<Member<?>> parse(final Class<?> type)
84124
return items;
85125
}
86126

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+
*/
87135
public static List<Member<?>> parse(final Field field) throws ValidityException {
88136
Class<?> c = field.getDeclaringClass();
89137
if (c == null || field == null) return null;
@@ -166,6 +214,20 @@ private static void parseFieldParameters (final ArrayList<Member<?>> items, fina
166214
}
167215
}
168216

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+
*/
169231
private static Parameter[] synthesizeParameterAnnotations(final List<FunctionalMethodType> fmts) {
170232
List<Parameter> params = new ArrayList<>();
171233

@@ -206,12 +268,26 @@ private static Parameter[] synthesizeParameterAnnotations(final List<FunctionalM
206268
return params.toArray(new Parameter[params.size()]);
207269
}
208270

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+
*/
209282
private static boolean resolveItemIOAuto(Parameter[] annotations, List<FunctionalMethodType> fmts, final ArrayList<ValidityProblem> problems) {
210283
boolean dirty = false;
211284
int i = 0;
212285
for (Parameter anno : annotations) {
213286
FunctionalMethodType fmt = fmts.get(i);
214287
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)
215291
ItemIO io = (ItemIO) AnnotationUtils.mutateAnnotationInstance(anno, Parameter.ITEMIO_FIELD_NAME, fmt.itemIO());
216292
assert io.equals(ItemIO.AUTO);
217293
// if the ItemIO is explicitly specified, we can check if it matches the inferred ItemIO from the functional method

‎src/main/java/org/scijava/struct/ItemIO.java‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,30 @@
3636

3737
/**
3838
* Defines the input/output type of a module.
39-
* <p>
40-
* Choices are:
41-
* </p>
42-
* <ul>
43-
* <li>INPUT: item is an input for the module.</li>
44-
* <li>OUTPUT: item is an output for the module.</li>
45-
* <li>BOTH: item is both an input and an output for the module. This type is
46-
* used to indicate an object that is mutated somehow during execution.</li>
47-
* <li>AUTO: marker to specify that one of the above should be inferred automatically
48-
* during structification. I.e. the type can be inferred from the functional method
49-
* signature of a functional type. The return type will always be ItemIO.Output and
50-
* method parameters should be ItemIO.Input. If a method parameter is ItemIO.Both,
51-
* it should be annotated with {@link Mutable}.</li>
52-
* </ul>
39+
*
5340
* @author Curtis Rueden
5441
* @author David Kolb
5542
*/
5643
public enum ItemIO {
57-
INPUT, OUTPUT, BOTH, AUTO
44+
/**
45+
* The item is an input for the module.
46+
*/
47+
INPUT,
48+
/**
49+
* The item is an output for the module.
50+
*/
51+
OUTPUT,
52+
/**
53+
* The item is both an input and an output for the module. This type is
54+
* used to indicate an object that is mutated somehow during execution.
55+
*/
56+
BOTH,
57+
/**
58+
* Marker to specify that one of the above should be inferred automatically
59+
* during structification. I.e. the type can be inferred from the functional method
60+
* signature of a functional type. The return type will always be ItemIO.Output and
61+
* method parameters should be ItemIO.Input. If a method parameter is ItemIO.Both,
62+
* it should be annotated with {@link Mutable}.
63+
*/
64+
AUTO
5865
}

‎src/main/java/org/scijava/util/AnnotationUtils.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static Parameter[] parameters(final AnnotatedElement element) {
7575
* Changes the annotation value for the given key of the given annotation to
7676
* newValue and returns the previous value.
7777
*
78-
* Taken and modified from: https://stackoverflow.com/a/28118436
78+
* Taken and modified (only renaming) from: https://stackoverflow.com/a/28118436
7979
*
8080
* @param instance the annotation to mutate
8181
* @param key the key of the field to mutate

0 commit comments

Comments
 (0)