java.lang.Object
org.klojang.util.InvokeMethods
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <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.- Parameters:
arrayType
- the array class (not the class of its elements!)length
- the desired length of the array- Returns:
- the array
-
getArrayLength
Returns the length of the provided 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
Sets the element at the specified index.- Parameters:
array
- the arrayidx
- the array indexvalue
- the value
-