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 code. They very thinly wrap methods from the java.lang.invoke package and they do not perform any null checks, type checks, range checks, etc. These checks are still necessary but are left to higher-level code.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static Object
    arrayOf(Class<?> elementType, int length)
    Returns a new array with the specified type of elements and with the specified length.
    static <T> T
    getArrayElement(Object array, int index)
    Returns the array element at the specified index.
    static int
    Returns the length of the provided array.
    static Object
    newArray(Class<?> arrayType, int length)
    Returns a new array with the specified length.
    static <T> T
    newInstance(Class<T> clazz)
    Returns a new instance of the specified class using its no-arg constructor.
    static <T> T
    newInstance(Class<T> clazz, int arg0)
    Returns a new instance of the specified class using the constructor that takes a single int argument.
    static void
    setArrayElement(Object array, int idx, Object value)
    Sets the element at the specified index.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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. Higher-level code must check that the provided class does in fact represent an array type and that the specified length is not negative
      Parameters:
      arrayType - the array class
      length - the length of the outermost array
      Returns:
      the array
    • arrayOf

      public static Object arrayOf(Class<?> elementType, int length)
      Returns a new array with the specified type of elements and with the specified length. The provided type may be an array type, but the returned array will then be an array of that array type. For example, if the provided type is String[][].class, and the provided length is 100, then this method will return new String[100][][].
      Parameters:
      elementType - the type of the elements in the array.
      length - the length of the array
      Returns:
      the array
    • getArrayLength

      public static int getArrayLength(Object array)
      Returns the length of the provided array. Higher-level code must check that the provided object is in fact represent an 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