Package io.jmix.core.common.util
Class ReflectionHelper
java.lang.Object
io.jmix.core.common.util.ReflectionHelper
Utility class to simplify work with Java reflection.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Field
static Method
findMethod
(Class<?> c, String name, Object... params) Searches for a method by its name and arguments.static <T> Class<T>
Load class by name, wrapping aClassNotFoundException
into unchecked exception.static Object
getFieldValue
(Object entity, String property) Useful to get real entity property value and avoid unfetched attribute exception.static Class<?>[]
getParamTypes
(Object... params) Constructs an array of argument types from an array of actual values.static <T> T
invokeMethod
(Object obj, String name, Object... params) Invokes a method by reflection.static Class<?>
Load class by name.static <T> T
newInstance
(Class<T> cls, Object... params) Instantiates an object by appropriate constructor.
-
Method Details
-
loadClass
Load class by name.- Parameters:
name
- class FQN or primitive type name- Returns:
- class instance
- Throws:
ClassNotFoundException
- if not found
-
getClass
Load class by name, wrapping aClassNotFoundException
into unchecked exception.- Parameters:
name
- class FQN- Returns:
- class instance
-
newInstance
Instantiates an object by appropriate constructor.- Parameters:
cls
- classparams
- constructor arguments- Returns:
- created object instance
- Throws:
NoSuchMethodException
- if the class has no constructor matching the given arguments
-
findMethod
Searches for a method by its name and arguments.- Parameters:
c
- classname
- method nameparams
- method arguments- Returns:
- method reference or null if a suitable method not found
-
invokeMethod
public static <T> T invokeMethod(Object obj, String name, Object... params) throws NoSuchMethodException Invokes a method by reflection.- Parameters:
obj
- object instancename
- method nameparams
- method arguments- Returns:
- method result
- Throws:
NoSuchMethodException
- if a suitable method not found
-
getParamTypes
Constructs an array of argument types from an array of actual values. Values can not contain nulls.- Parameters:
params
- arguments- Returns:
- the array of argument types
-
getFieldValue
Useful to get real entity property value and avoid unfetched attribute exception.- Returns:
- field value
-
findField
-