Class InvokeMethods
java.lang.Object
org.klojang.util.InvokeMethods
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
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
getArrayLength
(Object array) Returns the length of the provided array.static Object
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 singleint
argument.static void
setArrayElement
(Object array, int idx, Object value) Sets the element at the specified index.
-
Constructor Details
-
InvokeMethods
public InvokeMethods()
-
-
Method Details
-
newInstance
Returns a new instance of the specified class using its no-arg constructor. TheNoSuchMethodException
, thrown if there is no such constructor, is converted to anInvokeException
.- 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
Returns a new instance of the specified class using the constructor that takes a singleint
argument. TheNoSuchMethodException
, thrown if there is no such constructor, is converted to anInvokeException
.- Type Parameters:
T
- the type of the returned instance- Parameters:
clazz
- the classarg0
- the constructor argument- Returns:
- the instance
- Throws:
InvokeException
- if the class does not have such a constructor
-
newArray
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 classlength
- the length of the outermost array- Returns:
- the array
-
arrayOf
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 isString[][].class
, and the provided length is 100, then this method will returnnew String[100][][]
.- Parameters:
elementType
- the type of the elements in the array.length
- the length of the array- Returns:
- the array
-
getArrayLength
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
Returns the array element at the specified index.- Type Parameters:
T
- the type of the array elements- Parameters:
array
- the arrayindex
- the array index- Returns:
- the array element
-
setArrayElement
-