Package com.github.toolarium.common.util
Class ReflectionUtil
java.lang.Object
com.github.toolarium.common.util.ReflectionUtil
Defines the reflection utility.
-
Method Summary
Modifier and TypeMethodDescription<T> ObjectcallMethod(Object classInstance, String className, String methodName, Class<?>[] paramType, Object[] param) Calls the given method on the given instancevoidDisable access warnings<T> Class<T> getClassObject(String className) Load a class by given namestatic ReflectionUtilGet the instance<T> MethodGets the defined method backbooleanisClassAvailable(String className) Check if the given class exists<T> TnewInstance(Class<T> clazz) Create a new instance of a given class
-
Method Details
-
getInstance
Get the instance- Returns:
- the instance
-
isClassAvailable
Check if the given class exists- Parameters:
className- the name of the class to check- Returns:
- true if the class exists; otherwise false
- Throws:
IllegalArgumentException- In case of an invalid class name
-
getClassObject
public <T> Class<T> getClassObject(String className) throws IllegalArgumentException, ClassNotFoundException Load a class by given name- Type Parameters:
T- the generic type- Parameters:
className- the name of the class- Returns:
- the loaded class
- Throws:
IllegalArgumentException- In case of an invalid class nameClassNotFoundException- in case of error
-
newInstance
public <T> T newInstance(Class<T> clazz) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException Create a new instance of a given class- Type Parameters:
T- the generic type- Parameters:
clazz- the class- Returns:
- the instance
- Throws:
IllegalArgumentException- In case of an invalid class nameSecurityException- in case of errorInvocationTargetException- in case of errorIllegalArgumentException- in case of errorIllegalAccessException- in case of errorInstantiationException- in case of error
-
getMethod
public <T> Method getMethod(String className, String methodName, Class<?>[] paramType) throws SecurityException, NoSuchMethodException, ClassNotFoundException Gets the defined method back- Type Parameters:
T- the generic type- Parameters:
className- the class namemethodName- the method nameparamType- the parameter type- Returns:
- the method
- Throws:
IllegalArgumentException- In case of an invalid class or method nameSecurityException- in case of errorNoSuchMethodException- in case of errorClassNotFoundException- in case of error
-
callMethod
public <T> Object callMethod(Object classInstance, String className, String methodName, Class<?>[] paramType, Object[] param) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException Calls the given method on the given instance- Type Parameters:
T- the generic type- Parameters:
classInstance- the instance of the class where the method should be calledclassName- the name of the classmethodName- the method nameparamType- the parameter typeparam- the parameter values- Returns:
- the result
- Throws:
SecurityException- in case of errorNoSuchMethodException- in case of errorInvocationTargetException- in case of errorIllegalAccessException- in case of errorIllegalArgumentException- in case of errorClassNotFoundException- in case of error
-
disableAccessWarnings
public void disableAccessWarnings()Disable access warnings
-