11
22package org .scijava .param ;
33
4- import java .lang .annotation .Annotation ;
54import java .lang .reflect .AnnotatedElement ;
6- import java .lang .reflect .AnnotatedType ;
75import java .lang .reflect .Field ;
86import java .lang .reflect .Method ;
97import java .lang .reflect .Modifier ;
@@ -153,6 +151,44 @@ public static List<Member<?>> parse(final Field field) throws ValidityException
153151
154152 return items ;
155153 }
154+
155+ /**
156+ * Returns a list of {@link FunctionalMethodType}s describing the input and output
157+ * types of the functional method of the specified functional type. In doing so,
158+ * the return type of the method will me marked as {@link ItemIO#OUTPUT} and the
159+ * all method parameters as {@link ItemIO#OUTPUT}, except for parameters annotated
160+ * with {@link Mutable} which will be marked as {@link ItemIO#BOTH}. If the specified
161+ * type does not have a functional method in its hierarchy, null will be
162+ * returned.<br>
163+ * The order will be the following: method parameters from left to right, return type
164+ *
165+ * @param functionalType
166+ * @return
167+ */
168+ public static List <FunctionalMethodType > findFunctionalMethodTypes (Type functionalType ) {
169+ Method functionalMethod = findFunctionalMethod (Types .raw (functionalType ));
170+ if (functionalMethod == null ) return null ;
171+
172+ Type paramfunctionalType = functionalType ;
173+ if (functionalType instanceof Class ) {
174+ paramfunctionalType = Types .parameterizeRaw ((Class <?>) functionalType );
175+ }
176+
177+ List <FunctionalMethodType > out = new ArrayList <>();
178+ int i = 0 ;
179+ for (Type t : Types .getExactParameterTypes (functionalMethod , paramfunctionalType )) {
180+ boolean isMutable = AnnotationUtils .getMethodParameterAnnotation (functionalMethod , i , Mutable .class ) != null ;
181+ out .add (new FunctionalMethodType (t , isMutable ? ItemIO .BOTH : ItemIO .INPUT ));
182+ i ++;
183+ }
184+
185+ Type returnType = Types .getExactReturnType (functionalMethod , paramfunctionalType );
186+ if (!returnType .equals (void .class )) {
187+ out .add (new FunctionalMethodType (returnType , ItemIO .OUTPUT ));
188+ }
189+
190+ return out ;
191+ }
156192
157193 // -- Helper methods --
158194
@@ -391,7 +427,7 @@ private static Class<?> findParametersDeclaration(final Class<?> type) {
391427 * @param type
392428 * @return
393429 */
394- public static Class <?> findFunctionalInterface (Class <?> type ) {
430+ private static Class <?> findFunctionalInterface (Class <?> type ) {
395431 if (type == null ) return null ;
396432 if (type .getAnnotation (FunctionalInterface .class ) != null ) return type ;
397433 for (Class <?> iface : type .getInterfaces ()) {
@@ -401,7 +437,7 @@ public static Class<?> findFunctionalInterface(Class<?> type) {
401437 return findFunctionalInterface (type .getSuperclass ());
402438 }
403439
404- public static boolean checkValidity (Parameter param , String name ,
440+ private static boolean checkValidity (Parameter param , String name ,
405441 Class <?> type , boolean isFinal , Set <String > names ,
406442 ArrayList <ValidityProblem > problems )
407443 {
@@ -434,64 +470,6 @@ public static boolean checkValidity(Parameter param, String name,
434470 return valid ;
435471 }
436472
437- /**
438- * Returns a list of {@link FunctionalMethodType}s describing the input and output
439- * types of the functional method of the specified functional type. In doing so,
440- * the return type of the method will me marked as {@link ItemIO#OUTPUT} and the
441- * all method parameters as {@link ItemIO#OUTPUT}, except for parameters annotated
442- * with {@link Mutable} which will be marked as {@link ItemIO#BOTH}. If the specified
443- * type does not have a functional method in its hierarchy, null will be
444- * returned.<br>
445- * The order will be the following: method parameters from left to right, return type
446- *
447- * @param functionalType
448- * @return
449- */
450- public static List <FunctionalMethodType > findFunctionalMethodTypes (Type functionalType ) {
451- Method functionalMethod = findFunctionalMethod (Types .raw (functionalType ));
452- if (functionalMethod == null ) return null ;
453-
454- Type paramfunctionalType = functionalType ;
455- if (functionalType instanceof Class ) {
456- paramfunctionalType = Types .parameterizeRaw ((Class <?>) functionalType );
457- }
458-
459- List <FunctionalMethodType > out = new ArrayList <>();
460- int i = 0 ;
461- for (Type t : Types .getExactParameterTypes (functionalMethod , paramfunctionalType )) {
462- boolean isMutable = getMethodParameterAnnotation (functionalMethod , i , Mutable .class ) != null ;
463- out .add (new FunctionalMethodType (t , isMutable ? ItemIO .BOTH : ItemIO .INPUT ));
464- i ++;
465- }
466-
467- Type returnType = Types .getExactReturnType (functionalMethod , paramfunctionalType );
468- if (!returnType .equals (void .class )) {
469- out .add (new FunctionalMethodType (returnType , ItemIO .OUTPUT ));
470- }
471-
472- return out ;
473- }
474-
475- /**
476- * Attempt to retrieve the specified annotation from the i'th parameter
477- * of the specified method. This method will only find annotations with:
478- * <pre>@Target(ElementType.TYPE_USE)</pre>
479- * If the ElementType is different or no annotation with specified type
480- * is present, null is returned.
481- *
482- * @param method
483- * @param i
484- * @param annotationClass
485- * @return
486- */
487- public static <A extends Annotation > A getMethodParameterAnnotation (Method method , int i , Class <A > annotationClass ) {
488- AnnotatedType [] params = method .getAnnotatedParameterTypes ();
489- if (i >= params .length ) {
490- return null ;
491- }
492- return params [i ].getAnnotation (annotationClass );
493- }
494-
495473 /**
496474 * Attempts to find the single functional method of the specified
497475 * class, by scanning the for functional interfaces. If there
@@ -500,7 +478,7 @@ public static <A extends Annotation> A getMethodParameterAnnotation(Method metho
500478 * @param cls
501479 * @return
502480 */
503- public static Method findFunctionalMethod (Class <?> cls ) {
481+ private static Method findFunctionalMethod (Class <?> cls ) {
504482 Class <?> iFace = findFunctionalInterface (cls );
505483 if (iFace == null ) {
506484 return null ;
0 commit comments