Class ReflectUtil


  • public final class ReflectUtil
    extends Object
    Utility methods for using Java reflection.
    • Method Detail

      • findConstructor

        public static <T> @Nullable Constructor<T> findConstructor​(Class<T> clazz,
                                                                   Class<?>... params)
        Tries to find a constructor that is able to accept parameters of the given types. First tries to find the constructor matching the exact parameter types. If such a constructor does not exist, tries to find any (the first match of arbitrary order) constructor that is able to accept the parameter types by means of auto-boxing, i.e. a Constructor(int) would be returned for the parameters class Integer.class.

        Returns null if no such constructor could be found.

        Type Parameters:
        T - the class type
        Parameters:
        clazz - the class which should be scanned for constructors
        params - the types of the constructor arguments
        Returns:
        A constructor that is able of accepting parameters of the specified types, null if such a constructor could not be found.
      • findMethod

        public static @Nullable Method findMethod​(Class<?> clazz,
                                                  String name,
                                                  Class<?>... params)
        Tries to find a method of the given name that is able to accept parameters of the given types. First tries to find the method matching the exact parameter types. If such a method does not exist, tries to find any (the first match of arbitrary order) method that is able to accept the parameter types by means of auto-boxing, i.e. a Method(int) would be returned for the parameters class Integer.class.

        Returns null if no such method could be found.

        Parameters:
        clazz - the class which should be scanned for methods
        name - the name of the method
        params - the types of the method arguments
        Returns:
        A method that is able to accept parameters of the specified types, null if such a method could not be found.
      • findMatchingMethod

        public static @Nullable Method findMatchingMethod​(Class<?> clazz,
                                                          String name,
                                                          @Nullable Object... args)
        See findMethod(Class, String, Class...). This variation does not require the types of input parameters, but can handle the actual objects, which should be passed to the method.
        Parameters:
        clazz - the class which should be scanned for methods
        name - the name of the method
        args - the objects that should be passed to the method, may contain nulls
        Returns:
        A method that is able to accept of the specified objects, null if such a method could not be found.
      • findMethodRT

        public static @Nullable Method findMethodRT​(Class<?> clazz,
                                                    String name,
                                                    @Nullable Class<?> returnType,
                                                    Class<?>... params)
        See findMethod(Class, String, Class...). This variation allows to additionally narrow the method by specifying its return type.
        Parameters:
        clazz - the class which should be scanned for methods
        name - the name of the method
        returnType - the type of the returned object, if null, the return type will be ignored
        params - the types of the method arguments
        Returns:
        A method that is able to accept of the specified objects, null if such a method could not be found.