Class ReflectionUtils

java.lang.Object
nz.org.riskscape.ReflectionUtils

public class ReflectionUtils extends Object
  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

    • getAnnotatedFields

      public static List<Field> getAnnotatedFields(Class<?> parameterClass, Class<? extends Annotation> annotationClass)
      Returns:
      all fields annotated with the desired annotation in the class hierarchy of the given class
    • getInterfaces

      public static List<Class<?>> getInterfaces(Class<?> clazz)
      Returns:
      all interfaces that a class implements by following the class hierarchy up to object.

      It does not include interfaces that are inherited by implemented interfaces. E.g. class foo implements bar. interface bar extends baz, getInterfaces(Foo.class) returns bar, but not baz.

    • findCommonAncestorOfType

      public static Optional<Class<?>> findCommonAncestorOfType(Class<?> lhs, Class<?> rhs, Class<?> lookFor)

      Search through both classes hierarchies, looking for the most recent ancestor that is either equal to lookFor or an ancestor that implements lookFor. See ReflectionUtilsTest for examples.

      Parameters:
      lhs - a type to search through
      rhs - another type to search through
      lookFor - either a parent class in the shared ancestry, or an interface a shared ancestor should implement.
      Returns:
      empty if lhs and rhs have no common ancestor of lookFor. If they have a common ancestor that is either exactly lookFor, or implements lookFor, then the ancestor is returned.
    • findImplementingClass

      public static <T> Class<? extends T> findImplementingClass(Class<T> iface, Class<? extends T> search, ReflectionUtils.SearchOptions... optionsArray)

      Search through a class's hierarchy for an interface, returning the class or interface that implemented it. See ReflectionUtilsTest for more details.

      Type Parameters:
      T - The type of the interface we're looking for
      Parameters:
      iface - the class instance for the interface we're looking for
      search - where to start searching from
      optionsArray - search options
      Returns:
      The implementing class. Will never return null, but can throw a RiskscapeException if the interface is implemented multiple times or is not found given the ReflectionUtils.SearchOptions.
    • newInstance

      public static <T> T newInstance(Class<T> clazz)

      Calls clazz.newInstance, wrapping the checked exceptions in an unchecked runtime exception

    • newInstance

      public static <T, E extends Exception> T newInstance(Class<T> clazz, Function<Exception,E> constructor) throws E

      Calls clazz.newInstance, wrapping the checked exceptions in an exception provided from constructor

      Throws:
      E extends Exception
    • setField

      public static void setField(Field field, Object instance, Object value)

      Calls field.set(instance, value), wrapping the checked exception in an unchecked runtime exception

      Also ensures that the field is set to accessible before attempting to set the value.

    • getPublicField

      public static Optional<Field> getPublicField(Class<?> clazz, String fieldName)

      Very simple public field lookup method that returns empty if no such field exists

    • findParameterClass

      public static <T> Class<T> findParameterClass(Class<?> subclass)
    • findParameterClass

      public static <T> Class<T> findParameterClass(Class<?> subclass, int parameterIndex)

      Extracts the parameterized type (The T from MyClass<T>) from a parameterized subclass (e.g. Foo extends MyClass<Bar>). Normally, java's generics are runtime only, but subclasses keep their declared parameter types in their class metadata, and these are available in the reflection API - this method will extract a parameterized type and return it, as well as fudging the casting.

      Parameters:
      subclass - a class that declares type arguments
      parameterIndex - the index of the declared type argument