Class ReflectUtil

java.lang.Object
org.dellroad.stuff.java.ReflectUtil

public final class ReflectUtil extends Object
Reflection utility methods.
  • Method Details

    • propertyNameFromGetterMethod

      public static String propertyNameFromGetterMethod(Method method)
      Get the Java bean name corresponding to the given Java bean property getter method.

      The method does not need to be public.

      Does not support indexed bean properties.

      Parameters:
      method - getter method
      Returns:
      corresponding bean property name, or null if method is not a valid Java bean getter method
      Throws:
      IllegalArgumentException - if method is null
    • propertyNameFromGetterMethodName

      public static String propertyNameFromGetterMethodName(String methodName)
      Get the Java bean name corresponding to the given Java bean property getter method name.

      This method assumes that if the name starts with "is", then the caller has already verified any corresponding method has return type boolean.

      Parameters:
      methodName - getter method name
      Returns:
      corresponding bean property name, or null if methodName is not a value property getter method name
      Throws:
      IllegalArgumentException - if methodName is null
    • findPropertySetters

      public static Map<String,List<Method>> findPropertySetters(Class<?> type, String methodPrefix)
      Find all setter methods in the given class whose name starts with the given prefix.

      A setter method is any public method taking exactly one parameter. Multiple setter methods could have the same name, if they have a different parameter type and/or return type, though method will filter out bridge methods.

      Parameters:
      type - type to introspect
      methodPrefix - setter method name prefix (typically "set")
      Returns:
      mapping from property name to setter methods, with those having narrower parameter types first
      Throws:
      IllegalArgumentException - if either parameter is null
    • instantiate

      public static <T> T instantiate(Class<T> type)
      Instantiate the given class using its zero-arg constructor.
      Type Parameters:
      T - new object type
      Parameters:
      type - type to instantiate
      Returns:
      new instance
      Throws:
      RuntimeException - if construction fails
      IllegalArgumentException - if type is null
    • instantiate

      public static <T> T instantiate(Constructor<T> constructor, Object... params)
      Instantiate the given class using the given constructor.
      Type Parameters:
      T - new object type
      Parameters:
      constructor - constructor to invoke
      params - constructor parameters
      Returns:
      method's return value
      Throws:
      RuntimeException - if construction fails
      IllegalArgumentException - if either parameter is null
    • invoke

      public static Object invoke(Method method, Object target, Object... params)
      Invoke the given method, rethrowing any checked exceptions.
      Parameters:
      method - method to invoke
      target - target object, or null if method is static
      params - constructor parameters
      Returns:
      method's return value
      Throws:
      RuntimeException - if invocation fails
      IllegalArgumentException - if method or params is null
    • sortByType

      public static void sortByType(List<? extends Class<?>> list)
      Sort the given list so narrower types always appear before wider types.

      This is a stable sort. Algorithm is O(n2) however.

      Parameters:
      list - list of types
    • sortByType

      public static <T> void sortByType(List<T> list, Function<? super T,? extends Class<?>> typer)
      Sort the given list so items with narrower types always appear before items with wider types.

      This is a stable sort. Algorithm is O(n2) however.

      Parameters:
      list - list of items
      typer - associates a type with each item
    • getClassComparator

      public static Comparator<Class<?>> getClassComparator()
      Get a comparator that partially orders types where narrower types sort first.

      Non-comparable types compare as equal, so they will not reorder under a stable sort.

      Returns:
      stable ordering of types narrowest first
    • getClassComparator

      public static Comparator<Class<?>> getClassComparator(boolean incomparableEqual)
      Get a comparator that calculates which type is narrower, if possible.

      Non-comparable types will compare as equal if incomparableEqual is true, or else generate an IllegalArgumentException.

      Warning: use this comparator for simple comparisons only; it will not work for sorting because it is not transitive for "equality"; use sortByType() instead.

      Parameters:
      incomparableEqual - true to return zero for incomparable classes, false to throw IllegalArgumentException
      Returns:
      ordering of types narrowest first