Class ReflectionUtil
- Since:
- 2.1
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
callMethod
(String identifier, Object... arguments) Calls the method identified by the given identifier, which has the format "<full class name>::<method signature>
", with the given arguments.static <T> Optional<T>
callMethodOptional
(String identifier, Object... arguments) Calls the method identified by the given identifier.static <T> T
CombinesgetClass(String)
,getField(String, Object)
, andcallMethod(String, Object[])
into a single method.static Class<?>
Returns theClass
object for the given class name.static Class<?>[]
Returns the array ofClass
objects for a given type signature list.static Class<?>
Returns theClass
object for a given type signature.static <T> T
Reads the field identified by the given identifier, which has the format "<full class name>#<field name>
".static <T> Optional<T>
getFieldOptional
(String identifier, Object instance) Reads the field identified by the given identifier.static String[]
getIntegerFieldNames
(Class<?> cl, String prefix, int lowest, int length, boolean readableNames) Gets the names of allpublic static int
(possibly alsofinal
) fields starting with prefix of the given class.static String
getMethodSignature
(Class<?> returnType, Class<?>[] parameterTypes) Returns the method type signature of a method with the given returnType and parameterTypes.static String
Returns the method type signature of the givenMethod
.static <T> Optional<T>
getOptional
(String identifier, Object... arguments) The same asget(String, Object[])
, except that an emptyOptional
is returned if an exception is thrown.static String
getSignatureOfClass
(Class<?> cl) Returns the type signature string of the given type, for example "I
" or "Ljava/lang/String;
".static Class<?>[]
getTypesFromObjectArray
(Object[] array) Returns an array containing the types of each object in the given array.static boolean
Checks whether the given method has the given name and parameterTypes.
-
Method Details
-
getMethodSignature
Returns the method type signature of the givenMethod
.- Parameters:
m
- The method- Returns:
- The type signature of the given method
-
getMethodSignature
Returns the method type signature of a method with the given returnType and parameterTypes.- Parameters:
returnType
- The return typeparameterTypes
- The parameter types- Returns:
- The type signature of a method with the given types
- See Also:
-
getClassesForSignature
Returns the array ofClass
objects for a given type signature list.For example, "
ILjava/lang/String;
" returns an array containing theClass
representing the primitiveint
type and theClass
representingString
.The given string may also be a method type signature as returned by
getMethodSignature(Class, Class[])
. The return type signature is ignored.- Parameters:
sig
- The type signature list- Returns:
- The array of class objects
- Throws:
ClassNotFoundException
- If a class in the given signature was not found- Since:
- 2.12.0
- See Also:
-
getSignatureOfClass
Returns the type signature string of the given type, for example "I
" or "Ljava/lang/String;
". The inverse operation ofgetClassForTypeSignature(String)
.- Parameters:
cl
- The class- Returns:
- The type signature
- See Also:
-
getClassForTypeSignature
Returns theClass
object for a given type signature. The inverse operation ofgetSignatureOfClass(Class)
.For example, "
I
" returns theClass
representing the primitiveint
type, or "Ljava/lang/String;
" returns theString
class object.- Parameters:
sig
- The type signature- Returns:
- The class object
- Throws:
ClassNotFoundException
- If no class could be found that is represented by the given signature- Since:
- 2.12.0
- See Also:
-
isMethod
Checks whether the given method has the given name and parameterTypes. A parameter type of the given method may also be a superclass of a given expected parameter type.- Parameters:
m
- The methodname
- The method name to compare toparameterTypes
- The parameter types to compare to- Returns:
true
if the given method has the given name and parameterTypes
-
getTypesFromObjectArray
Returns an array containing the types of each object in the given array. If an element in the given array isnull
, the returned array will contain thevoid
class at its position.- Parameters:
array
- The object array- Returns:
- The array of types
- Since:
- 2.7
-
getIntegerFieldNames
public static String[] getIntegerFieldNames(Class<?> cl, String prefix, int lowest, int length, boolean readableNames) throws IllegalAccessException Gets the names of allpublic static int
(possibly alsofinal
) fields starting with prefix of the given class.The returned array contains all names without the prefix. The index of each string is the value of the integer field minus lowest. If an index is outside of the created string array with the given length, the name is not included in the array.
If readableNames is
true
, any_
characters in the field names are converted to spaces, and all characters are set to lowercase, except the first character of each word.- Parameters:
cl
- The classprefix
- The prefixlowest
- The lowest integer valuelength
- The length of the returned arrayreadableNames
- Whether to transform field names- Returns:
- The string array
- Throws:
IllegalAccessException
- If a field is inaccessible- Since:
- 2.8
-
getClass
Returns theClass
object for the given class name.Similar to
Class.forName(String)
, except that no exception is thrown when the class is not found, returningnull
instead.- Parameters:
name
- The class name- Returns:
- The
Class
, ornull
if the class was not found - Since:
- 2.12.0
-
getField
public static <T> T getField(String identifier, Object instance) throws ReflectiveOperationException Reads the field identified by the given identifier, which has the format "<full class name>#<field name>
".The instance is the instance object of which the field is read, if the field is not static. This method allows accessing private fields.
For example,
getField("java.lang.String#hash", "")
returns0
(the initial value).- Parameters:
identifier
- The field identifierinstance
- The instance- Returns:
- The value of the field (may be
null
) - Throws:
ReflectiveOperationException
- If the class or field does not exist, or field access failedIllegalArgumentException
- If instance is not an instance of the class in the identifierNullPointerException
- If instance isnull
, but the field is non-static- Since:
- 2.12.0
- See Also:
-
getFieldOptional
Reads the field identified by the given identifier. The same asgetField(String, Object)
, except that an emptyOptional
is returned if an exception is thrown.- Parameters:
identifier
- The field identifierinstance
- The instance- Returns:
- An
Optional
containing the value of the field (may benull
), or an emptyOptional
if the field could not be accessed - Since:
- 2.12.0
- See Also:
-
callMethod
public static <T> T callMethod(String identifier, Object... arguments) throws ReflectiveOperationException Calls the method identified by the given identifier, which has the format "<full class name>::<method signature>
", with the given arguments.If the target method is non-static, the first argument of arguments is taken as the instance object used to call the method (and not explicitly passed to the method). This method allows accessing private methods.
The method part of the identifier (the part after the
::
) consists of the method name, followed by a parameter type signature of the same format asgetClassesForSignature(java.lang.String)
accepts, enclosed in parentheses (any return type after the closing parenthesis is ignored). If the method has no parameters, the parameter type signature parentheses may be omitted. If the method name equals the special string "<init>
", the constructor of the class is called.For example,
callMethod("java.lang.String::length", "example")
returns7
(the length of the string).- Parameters:
identifier
- The method identifierarguments
- The arguments to pass to the method, including an object instance- Returns:
- The return value of the method (may be
null
, for example for void methods) - Throws:
ReflectiveOperationException
- If the class or method does not exist, method access failed, or the method threw an exceptionIllegalArgumentException
- If instance is not an instance of the class in the identifier, or the given arguments do not match the method parameter listNullPointerException
- If instance isnull
, but the method is non-static- Since:
- 2.12.0
- See Also:
-
callMethodOptional
Calls the method identified by the given identifier. The same ascallMethod(String, Object[])
, except that an emptyOptional
is returned if an exception is thrown.- Parameters:
identifier
- The method identifierarguments
- The arguments to pass to the method, including an object instance- Returns:
- An
Optional
containing the return value of the method (may benull
), or an emptyOptional
if the method could not be called - Since:
- 2.12.0
- See Also:
-
get
CombinesgetClass(String)
,getField(String, Object)
, andcallMethod(String, Object[])
into a single method. Which of the three methods is called is determined by the identifier format.The meaning of arguments depends on the identifier. If the identifier is a method, arguments has the meaning as defined by
callMethod(String, Object[])
. If it is a field, an optional first argument is used as the object instance passed togetField(String, Object)
. If the identifier is a class, arguments is ignored.- Parameters:
identifier
- The identifierarguments
- The arguments- Returns:
- The value of the identified object
- Throws:
ReflectiveOperationException
- If the class, field, or method does not exist, field or method access failed, or the method threw an exceptionIllegalArgumentException
- If instance is not an instance of the class in the identifier, or the given arguments do not match the method parameter listNullPointerException
- If instance isnull
, but the field or method is non-static- Since:
- 2.12.0
- See Also:
-
getOptional
The same asget(String, Object[])
, except that an emptyOptional
is returned if an exception is thrown.- Parameters:
identifier
- The identifierarguments
- The arguments- Returns:
- An
Optional
containing the value of the identified object, or an emptyOptional
if the value could not be retrieved - Since:
- 2.12.0
-