Class InvokeMethods

java.lang.Object
org.klojang.util.InvokeMethods

public final class InvokeMethods extends Object
Dynamic invocation utility methods. These methods are not meant to be used in application-level software. They very thinly wrap methods from the java.lang.invoke package and don't perform any null checks, type checks, range checks, etc.
  • Constructor Details

    • InvokeMethods

      public InvokeMethods()
  • Method Details

    • newInstance

      public static <T> T newInstance(Class<T> clazz) throws InvokeException
      Returns a new instance of the specified class using its no-arg constructor. The NoSuchMethodException thrown if there is no such constructor is converted to an InvokeException.
      Type Parameters:
      T - the type of the returned instance
      Parameters:
      clazz - the class to instantiate
      Returns:
      the instance
      Throws:
      InvokeException - if the class does not have a no-arg constructor
    • newInstance

      public static <T> T newInstance(Class<T> clazz, int arg0) throws InvokeException
      Returns a new instance of the specified class using the constructor that takes a single int argument. The NoSuchMethodException thrown if there is no such constructor is converted to an InvokeException.
      Type Parameters:
      T - the type of the returned instance
      Parameters:
      clazz - the class
      arg0 - the constructor argument
      Returns:
      the instance
      Throws:
      InvokeException - if the class does not have such a constructor
    • newArray

      public static Object newArray(Class<?> arrayType, int length)
      Returns a new array with the specified length.
      Parameters:
      arrayType - the array class (not the class of its elements!)
      length - the desired length of the array
      Returns:
      the array
    • getArrayLength

      public static int getArrayLength(Object array)
      Returns the length of the provided array.
      Parameters:
      array - the array
      Returns:
      its length
    • getArrayElement

      public static <T> T getArrayElement(Object array, int index)
      Returns the array element at the specified index.
      Type Parameters:
      T - the type of the array elements
      Parameters:
      array - the array
      index - the array index
      Returns:
      the array element
    • setArrayElement

      public static void setArrayElement(Object array, int idx, Object value)
      Sets the element at the specified index.
      Parameters:
      array - the array
      idx - the array index
      value - the value