Module xaos.tools

Class Reflections


  • public class Reflections
    extends Object
    Utility methods using the Java Reflection API.
    Author:
    claudio.rosati@esss.se
    • Method Detail

      • getField

        public static Field getField​(Class<?> clazz,
                                     String name)
                              throws NoSuchFieldException
        Searches for a field in the given class and all of its super classes. If the field cannot be found NoSuchFieldException is thrown.
        Parameters:
        clazz - Class to start the search for the field.
        name - Name of the field.
        Returns:
        The found field.
        Throws:
        NoSuchFieldException - If the field cannot be found in the class hierarchy starting from the given class.
      • getFieldValue

        public static Object getFieldValue​(Field field,
                                           Object object)
                                    throws RuntimeException
        Read a field value. If the field is not accessible, it will be set to be accessible.
        Parameters:
        field - The Field description.
        object - Instance in which the field value should be read.
        Returns:
        The value of the field.
        Throws:
        RuntimeException - If the field doesn't exist or cannot be read.
      • getFieldValue

        public static Object getFieldValue​(Object object,
                                           String name)
                                    throws RuntimeException
        Read a field value. If the field is not accessible, it will be set to be accessible.
        Parameters:
        object - Instance in which the field value should be read.
        name - Name of the field who's value should be read.
        Returns:
        The value of the field.
        Throws:
        RuntimeException - If the field doesn't exist or cannot be read.
      • getMethod

        public static Method getMethod​(Class<?> clazz,
                                       String name,
                                       Class<?>... parameterTypes)
                                throws NoSuchMethodException
        Searches for a method in the given class and all of its super classes.If the method cannot be found NoSuchMethodException is thrown.
        Parameters:
        clazz - Class to start the search for the method.
        name - Name of the method.
        parameterTypes - The parameters type array.
        Returns:
        The found method.
        Throws:
        NoSuchMethodException - If the method cannot be found in the class hierarchy starting from the given class.
      • invokeMethod

        public static Object invokeMethod​(Method method,
                                          Object object,
                                          Object... parameters)
                                   throws RuntimeException
        Invokes a method on a given object. If the method is not accessible, it will be set to be accessible.
        Parameters:
        method - The Method descriptor.
        object - Instance whose method must be invoked onto.
        parameters - The parameters array.
        Returns:
        The returned value from the method invocation.
        Throws:
        RuntimeException - If the method doesn't exist or cannot be invoked.
      • invokeMethod

        public static Object invokeMethod​(Object object,
                                          String name)
                                   throws RuntimeException
        Invokes a no-parameter method on a given object. If the method is not accessible, it will be set to be accessible.
        Parameters:
        object - Instance whose method must be invoked onto.
        name - The name of the method to be invoked.
        Returns:
        The returned value from the method invocation.
        Throws:
        RuntimeException - If the method doesn't exist or cannot be invoked.
      • setFieldValue

        public static void setFieldValue​(Field field,
                                         Object object,
                                         Object value)
                                  throws RuntimeException
        Set a field to a given value. If the field is not accessible, it will be set to be accessible.
        Parameters:
        object - Instance in which the field must be set to the given value.
        field - The Field descriptor.
        value - The value to be set.
        Throws:
        RuntimeException - If the field doesn't exist or cannot be set.
      • setFieldValue

        public static void setFieldValue​(Object object,
                                         String name,
                                         Object value)
                                  throws RuntimeException
        Set a field to a given value. If the field is not accessible, it will be set to be accessible.
        Parameters:
        object - Instance in which the field must be set to the given value.
        name - Name of the field who's value should be set.
        value - The value to be set.
        Throws:
        RuntimeException - If the field doesn't exist or cannot be set.