Class ReflectionUtils

java.lang.Object
io.fluentlenium.utils.ReflectionUtils

public final class ReflectionUtils extends Object
Utility class for reflection.
  • Method Details

    • wrapPrimitive

      public static <T> Class<T> wrapPrimitive (Class<T> clazz)
      Wrap given class to it's primitive class if it's matching a primitive class.
      Type Parameters:
      T - type of class
      Parameters:
      clazz - primitive class or not
      Returns:
      class or primitive class
    • toClass

      public static Class<?>[] toClass (Object... array)
      Converts an array of Object into an array of Class objects.

      If any of these objects is null, a null element will be inserted into the array.

      This method returns null for a null input array.

      Parameters:
      array - an Object array
      Returns:
      a Class array, null if null array input
    • toArgs

      public static Object[] toArgs (Function<Class<?>,Object> valueSupplier, Class<?>... array)
      Converts an array of values provided by an array of Class and Function supplying value for each class into an array of Object
      Parameters:
      valueSupplier - supplier of values for each class
      array - array of class
      Returns:
      array of values
    • getDefault

      public static <T> T getDefault (Class<T> type)
      Get default value for given type.
      Type Parameters:
      T - type of value
      Parameters:
      type - type of value to get the default
      Returns:
      default value
    • getConstructor

      public static <T> Constructor<T> getConstructor (Class<T> cls, Class<?>... argsTypes) throws NoSuchMethodException
      Retrieve the constructor of a class for given argument types.
      Type Parameters:
      T - type to retrieve the constructor from
      Parameters:
      cls - class to retrieve the constructor from
      argsTypes - argument types
      Returns:
      matching constructor for given argument values
      Throws:
      NoSuchMethodException - if a matching method is not found.
    • getConstructorOptional

      public static <T> Constructor<T> getConstructorOptional (int mandatoryCount, Class<T> cls, Class<?>... argsTypes) throws NoSuchMethodException
      Retrieve the constructor of a class for given optional argument types, considering mandatory values at the beginning of the given types.
      Type Parameters:
      T - type to retrieve the constructor from
      Parameters:
      mandatoryCount - number of mandatory arguments at the beginning of the given arguments
      cls - class to retrieve the constructor from
      argsTypes - argument types
      Returns:
      matching constructor for given optional argument values
      Throws:
      NoSuchMethodException - if a matching method is not found.
    • newInstance

      public static <T> T newInstance (Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
      Creates a new instance matching possible constructors with provided args.
      Type Parameters:
      T - type of the instance
      Parameters:
      cls - class to instantiate
      args - arguments of the constructor
      Returns:
      new instance
      Throws:
      NoSuchMethodException - if a matching method is not found.
      IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
      InstantiationException - if the class that declares the underlying constructor represents an abstract class.
      InvocationTargetException - if the underlying constructor throws an exception.
    • newInstanceOptionalArgs

      public static <T> T newInstanceOptionalArgs (Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
      Creates a new instance by trying every possible constructors with provided args.
      Type Parameters:
      T - type of the instance
      Parameters:
      cls - class to instantiate
      args - arguments of the constructor
      Returns:
      new instance
      Throws:
      NoSuchMethodException - if a matching method is not found.
      IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
      InstantiationException - if the class that declares the underlying constructor represents an abstract class.
      InvocationTargetException - if the underlying constructor throws an exception.
    • newInstanceOptionalArgs

      public static <T> T newInstanceOptionalArgs (int mandatoryCount, Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
      Creates a new instance by trying every possible constructors with provided args.
      Type Parameters:
      T - type of the instance
      Parameters:
      mandatoryCount - count of mandatory arguments
      cls - class to instantiate
      args - arguments of the constructor
      Returns:
      new instance
      Throws:
      NoSuchMethodException - if a matching method is not found.
      IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
      InstantiationException - if the class that declares the underlying constructor represents an abstract class.
      InvocationTargetException - if the underlying constructor throws an exception.
    • invoke

      public static Object invoke (Method method, Object obj, Object... args) throws InvocationTargetException, IllegalAccessException
      Invoke the method event if not accessible.
      Parameters:
      method - method to invoke
      obj - object to invoke
      args - arguments of the method
      Returns:
      return value from the method invocation
      Throws:
      IllegalAccessException - if this Method object is enforcing Java language access control and the underlying method is inaccessible.
      InvocationTargetException - if the underlying method throws an exception.
      See Also:
    • get

      public static Object get (Field field, Object obj) throws IllegalAccessException
      Get the field value even if not accessible.
      Parameters:
      field - field to get
      obj - instance to get
      Returns:
      field value
      Throws:
      IllegalAccessException - if this Field object is enforcing Java language access control and the underlying field is inaccessible.
      See Also:
    • set

      public static void set (Field field, Object obj, Object value) throws IllegalAccessException
      Set the field even if not accessible.
      Parameters:
      field - field to set
      obj - instance to set
      value - value of the field to set
      Throws:
      IllegalAccessException - if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
      See Also:
    • getFirstGenericType

      public static Class<?> getFirstGenericType (Field field)
      Retrieve the first generic type of the field type.
      Parameters:
      field - field to analyze
      Returns:
      first generic type, or null if no generic type is found
    • getMethod

      public static Method getMethod (Class<?> declaringClass, String name, Class... types)
      Get public method by name from the declaring class.
      Parameters:
      declaringClass - declaring class
      name - method name
      types - argument types
      Returns:
      the public method by the specified name
      Throws:
      IllegalArgumentException - when there is no method found with the specified name