Class ReflectionUtils


  • public class ReflectionUtils
    extends Object
    Utility methods to work with reflection entities
    Author:
    andrea del bene
    • Constructor Detail

      • ReflectionUtils

        public ReflectionUtils()
    • Method Detail

      • isParameterAnnotatedWith

        public static boolean isParameterAnnotatedWith​(int i,
                                                       Method method,
                                                       Class<? extends Annotation> targetAnnotation)
        Check if a parameter is annotated with a given annotation.
        Parameters:
        i - method parameter index.
        method - the method the parameter belongs to.
        targetAnnotation - the annotation type we want to check for.
        Returns:
        true if the method parameter is annotated with the given annotation, false otherwise.
      • getAnnotationParam

        public static Annotation getAnnotationParam​(int i,
                                                    Method method)
        Return the annotation (annotated with an AnnotatedParam) used with a given method parameter.
        Parameters:
        i - method parameter index.
        method - the method the parameter belongs to.
        Returns:
        the annotation if any is found, null otherwise.
        See Also:
        AnnotatedParam
      • findAnnotation

        public static <T extends Annotation> T findAnnotation​(Annotation[] parameterAnnotations,
                                                              Class<T> targetAnnotation)
        Utility method to find if an annotation type is present in an array of annotations.
        Parameters:
        parameterAnnotations - the array of annotations we will look in.
        targetAnnotation - the type of annotation we are looking for.
        Returns:
        the first occurrence of the targetAnnotation found in the array, null if no occurrence was found.
      • findMethodParameterAnnotation

        public static <T extends Annotation> T findMethodParameterAnnotation​(Method ownerMethod,
                                                                             int paramIndex,
                                                                             Class<T> targetAnnotation)
        Return the specified annotation for the method parameter at a given position (see paramIndex).
        Parameters:
        ownerMethod - the parameter's method
        paramIndex - the parameter index
        targetAnnotation - the annotation type to search for
        Returns:
        return an instance of the given annotation type, or null if the method parameter it's not annotated.
      • findMethod

        public static Method findMethod​(Class<?> clazz,
                                        String name,
                                        Class<?>... parameterTypes)
        Safely search for a method with a given signature (name + parameter types) on a given class.
        Parameters:
        clazz - the target class
        name - method name
        parameterTypes - method's parameters types.
        Returns:
        the method corresponding to the given signature, null if such a method doesn't exist.
      • invokeMethod

        public static <T> T invokeMethod​(Object target,
                                         String name,
                                         Class<?>... parameterTypes)
        Safely invoke a method with the given signature (name + parameter types) on the given target object.
        Parameters:
        target - the target object
        name - method name
        parameterTypes - method's parameters types.
        Returns:
        the value returned by the method invocation.
      • getAnnotationField

        public static <T> T getAnnotationField​(Annotation annotation,
                                               String fieldName,
                                               T defaultValue)
        Get the value of an annotation field.
        Parameters:
        annotation - the target annotation
        fieldName - the field name
        defaultValue - the default value to return
        Returns:
        the value of the field or the default value if the filed has no value.
      • getElementsClasses

        public static List<Class<?>> getElementsClasses​(Collection<?> collection)
        Extract the list of types of every element of a given collection.
        Parameters:
        collection - the collection in input.
        Returns:
        the list containing the type of list elements.