Class ReflectionUtils

java.lang.Object
nl.colorize.util.ReflectionUtils

public final class ReflectionUtils extends Object
Various utility and convenience methods for working with reflection. Unless stated otherwise, all methods in this class will throw unchecked exceptions if an error occurs while attempting to locate, access, or call elements. This prevents using code from having to deal with the several types of checked exceptions thrown by the reflection APIs. Also, in some cases methods may throw SecurityException if the environment in which the application is used does not allow certain forms of reflection.
  • Method Details

    • getProperty

      public static Object getProperty(Object subject, String propertyName)
      Accesses the object's property with the specified name using reflection, and returns its value.
      Throws:
      IllegalArgumentException - if the object does not have a property with that name.
      SecurityException - if the property is private or protected, and the environment does not allow access to non-public properties.
    • getPropertyNames

      public static Set<String> getPropertyNames(Class<?> forClass)
      Returns the names of the properties defined by the specified class. This does include private and protected properties, but does not include any properties defined by parent classes, or static or transient properties.
    • getPropertyTypes

      public static Map<String,Class<?>> getPropertyTypes(Class<?> forClass)
      Returns the names and types of the properties defined by the specified class. This does include private and protected properties, but does not include any properties defined by parent classes, or static or transient properties.

      This method does not retrieve the actual property values for an instance of the class. Use getProperties(Object) for that purpose.

    • getProperties

      public static Map<String,Object> getProperties(Object subject)
      Accesses an object's properties using reflection. The properties are obtained using the approach described in getProperty(Object, String). This does include private and protected properties, but does not include any properties defined by parent classes, or static or transient properties.
    • setProperty

      public static void setProperty(Object subject, String propertyName, Object value)
      Accesses the object's property with the specified name using reflection, and updates its value.
      Throws:
      IllegalArgumentException - if the object does not have a property with that name, or if the property's type and type of value don't match.
      SecurityException - if the property is private or protected, and the environment does not allow access to non-public properties.
    • getPropertyComparator

      public static <T> Comparator<T> getPropertyComparator(String propertyName)
      Returns a comparator that compares objects based on the value of one of their properties. Property access follows the approach used by getProperty(Object, String), including the exceptions that might be thrown when trying to access the property.
    • callMethod

      public static Object callMethod(Object subject, String methodName, Object... args)
      Calls the object's method with the specified name using reflection. The types of the arguments passed to the method (args) are also used to find the method with the requested parameter types.
      Throws:
      RuntimeException - if the calling the method results in an exception.
      IllegalArgumentException - if no method with that name exists, or if the number of parameter or the parameter type don't match.
    • getMethodsWithAnnotation

      public static List<Method> getMethodsWithAnnotation(Object obj, Class<? extends Annotation> type)
      Returns a list containing all of an object's methods that are marked with the specified annotation.
    • getFieldsWithAnnotation

      public static List<Field> getFieldsWithAnnotation(Object obj, Class<? extends Annotation> type)
      Returns a list containing all of an object's fields that are marked with the specified annotation.