Class ReflectionHelper


  • public class ReflectionHelper
    extends Object
    Convenience methods on java reflection API.
    • Constructor Detail

      • ReflectionHelper

        public ReflectionHelper()
    • Method Detail

      • invoke

        public static Object invoke​(Object target,
                                    Class<?> clazz,
                                    String methodName,
                                    Class<?>[] parameterTypes,
                                    Object... args)
        Convenience wrapper to reflection method invoke API. Invoke the method and hide checked exceptions.
        Parameters:
        target - object to invoke the method on (or null for static methods)
        clazz - class name
        methodName - method name
        parameterTypes - parameter types to resolve method name
        args - actual arguments
        Returns:
        invocation result or null
        Throws:
        IllegalArgumentException - if method not found
        IllegalStateException - for InvocationTargetException (exception in invoked method)
      • invokeNoException

        public static Object invokeNoException​(Object target,
                                               String className,
                                               ClassLoader cl,
                                               String methodName,
                                               Class<?>[] parameterTypes,
                                               Object... args)
        Convenience wrapper to reflection method invoke API. Invoke the method and swallow exceptions due to missing methods. Use this method if you have multiple framework support and the method may not exist in current version.
        Parameters:
        target - object to invoke the method on (or null for static methods)
        clazz - className name
        cl - Classloader to load the class
        methodName - method name
        parameterTypes - parameter types to resolve method name
        args - actual arguments
        Returns:
        invocation result or null
        Throws:
        IllegalStateException - for InvocationTargetException (exception in invoked method)
      • invoke

        public static Object invoke​(Object target,
                                    String methodName)
        Convenience wrapper to reflection method invoke API. Invoke the method and hide checked exceptions.
        Parameters:
        target - object to invoke the method on (or null for static methods)
        methodName - method name
        Returns:
        invocation result or null
        Throws:
        IllegalArgumentException - if method not found
        IllegalStateException - for InvocationTargetException (exception in invoked method)
      • get

        public static Object get​(Object target,
                                 String fieldName)
        Convenience wrapper to reflection field access API. Get field value and hide checked exceptions. Field class is set by
        Parameters:
        target - object to get field value (or null for static methods)
        fieldName - field name
        Returns:
        field value
        Throws:
        IllegalArgumentException - if field not found
      • get

        public static Object get​(Object target,
                                 Class<?> clazz,
                                 String fieldName)
        Convenience wrapper to reflection field access API. Get field value and hide checked exceptions.
        Parameters:
        target - object to get field value (or null for static methods)
        clazz - class name
        fieldName - field name
        Returns:
        field value
        Throws:
        IllegalArgumentException - if field not found
      • getNoException

        public static Object getNoException​(Object target,
                                            Class<?> clazz,
                                            String fieldName)
        Convenience wrapper to reflection field access API. Get field value and swallow exceptions. Use this method if you have multiple framework support and the field may not exist in current version.
        Parameters:
        target - object to get field value (or null for static methods)
        clazz - class name
        fieldName - field name
        Returns:
        field value or null if an exception
      • getNoException

        public static Object getNoException​(Object target,
                                            String className,
                                            ClassLoader cl,
                                            String fieldName)
        Convenience wrapper to reflection field access API. Get field value and swallow exceptions. Use this method if you have multiple framework support and the field may not exist in current version.
        Parameters:
        target - object to get field value (or null for static methods)
        className - class name
        cl - class loader to load the target class
        fieldName - field name
        Returns:
        field value or null if an exception
      • set

        public static void set​(Object target,
                               Class<?> clazz,
                               String fieldName,
                               Object value)
        Convenience wrapper to reflection field access API. Set field value and hide checked exceptions.
        Parameters:
        target - object to get field value (or null for static methods)
        clazz - class name
        fieldName - field name
        value - field value
        Throws:
        IllegalArgumentException - if field not found