Package com.jidesoft.utils
Class ReflectionUtils
- java.lang.Object
-
- com.jidesoft.utils.ReflectionUtils
-
public class ReflectionUtils extends java.lang.Object
Utility class to use reflection to call methods.
-
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
call(java.lang.Object thisObject, java.lang.String methodName)
Helper method to call a no argument, no return method.static java.lang.Object
callAny(java.lang.Object thisObject, java.lang.String methodName, java.lang.Class<?>[] argTypes, java.lang.Object[] args)
Helper method to call a multi-argument method having a return.static java.lang.Object
callAny(java.lang.Object thisObject, java.lang.String methodName, java.lang.Object[] args)
Helper method to call a multi-argument method having a return.static java.lang.Object
callAnyWithoutException(java.lang.Object thisObject, java.lang.Class<?> objectClass, java.lang.String methodName, java.lang.Class<?>[] argTypes, java.lang.Object[] args)
Helper method to call a multi-argument method having a return without throwing an exception.static java.lang.Object
callConstructor(java.lang.Class<?> clazz, java.lang.Class<?>[] argTypes, java.lang.Object[] args)
Helper method to call a constructor.static java.lang.Object
callGet(java.lang.Object thisObject, java.lang.String methodName)
Helper method to call a get method with no argument.static void
callSet(java.lang.Object thisObject, java.lang.String methodName, java.lang.Object value)
Helper method to call a set boolean method.static void
callSetBoolean(java.lang.Object thisObject, java.lang.String methodName, boolean value)
Helper method to call a set boolean method.static void
callSetInt(java.lang.Object thisObject, java.lang.String methodName, int value)
Helper method to call a set boolean method.static java.lang.Object
callStatic(java.lang.Class<?> thisClass, java.lang.String methodName, java.lang.Class<?>[] argTypes, java.lang.Object[] args)
Helper method to call a multi-argument static class method having a return.static java.lang.Object
createInstance(java.lang.String className, java.lang.Class<?>[] types, java.lang.Object[] args)
Instantiate an object based on the class name.static boolean
isSubClassOf(java.lang.Object o, java.lang.String className)
Checks if the instance o is an instance of a subclass of the designated class name.
-
-
-
Method Detail
-
callSetBoolean
public static void callSetBoolean(java.lang.Object thisObject, java.lang.String methodName, boolean value) throws java.lang.Exception
Helper method to call a set boolean method.- Parameters:
thisObject
- the instancemethodName
- the method namevalue
- true or false- Throws:
java.lang.Exception
- if the method is not found or invocation to the method fails.
-
callSetInt
public static void callSetInt(java.lang.Object thisObject, java.lang.String methodName, int value) throws java.lang.Exception
Helper method to call a set boolean method.- Parameters:
thisObject
- the instancemethodName
- the method namevalue
- the value- Throws:
java.lang.Exception
- if the method is not found or invocation to the method fails.
-
callSet
public static void callSet(java.lang.Object thisObject, java.lang.String methodName, java.lang.Object value) throws java.lang.Exception
Helper method to call a set boolean method.- Parameters:
thisObject
- the instancemethodName
- the method namevalue
- the value- Throws:
java.lang.Exception
- if the method is not found or invocation to the method fails.
-
callGet
public static java.lang.Object callGet(java.lang.Object thisObject, java.lang.String methodName) throws java.lang.Exception
Helper method to call a get method with no argument.- Parameters:
thisObject
- the instancemethodName
- the method name- Returns:
- the value the method returns.
- Throws:
java.lang.Exception
- if the method is not found or invocation to the method fails.
-
call
public static void call(java.lang.Object thisObject, java.lang.String methodName) throws java.lang.Exception
Helper method to call a no argument, no return method.- Parameters:
thisObject
- the instancemethodName
- the method name- Throws:
java.lang.Exception
- if the method is not found or invocation to the method fails.
-
callConstructor
public static java.lang.Object callConstructor(java.lang.Class<?> clazz, java.lang.Class<?>[] argTypes, java.lang.Object[] args) throws java.lang.Exception
Helper method to call a constructor.- Parameters:
clazz
- the classargTypes
- argument Classes for constructor lookup. Must not be null.args
- the argument array- Returns:
- the value the method returns.
- Throws:
java.lang.Exception
- if the method is not found or invocation to the method fails.
-
callAny
public static java.lang.Object callAny(java.lang.Object thisObject, java.lang.String methodName, java.lang.Object[] args) throws java.lang.Exception
Helper method to call a multi-argument method having a return. The class types will be derived from the input values. This call is usually successful with primitive types or Strings as arguments, but care should be used with other kinds of values. The constructor lookup is not polymorphic. CallscallAny(Object, methodName, argTypes, args)
.- Parameters:
thisObject
- the instancemethodName
- the method nameargs
- the argument array, must not contain null.- Returns:
- the value the method returns.
- Throws:
java.lang.Exception
- if the method is not found or invocation to the method fails.
-
callAny
public static java.lang.Object callAny(java.lang.Object thisObject, java.lang.String methodName, java.lang.Class<?>[] argTypes, java.lang.Object[] args) throws java.lang.Exception
Helper method to call a multi-argument method having a return.- Parameters:
thisObject
- the instancemethodName
- the method nameargTypes
- argument Classes for constructor lookup. Must not be null.args
- the argument array- Returns:
- the value the method returns.
- Throws:
java.lang.Exception
- if the method is not found or invocation to the method fails.
-
callAnyWithoutException
public static java.lang.Object callAnyWithoutException(java.lang.Object thisObject, java.lang.Class<?> objectClass, java.lang.String methodName, java.lang.Class<?>[] argTypes, java.lang.Object[] args)
Helper method to call a multi-argument method having a return without throwing an exception.- Parameters:
thisObject
- the instanceobjectClass
- the class which could find the method namemethodName
- the method nameargTypes
- argument Classes for constructor lookup. Must not be null.args
- the argument array- Returns:
- the value the method returns.
-
callStatic
public static java.lang.Object callStatic(java.lang.Class<?> thisClass, java.lang.String methodName, java.lang.Class<?>[] argTypes, java.lang.Object[] args) throws java.lang.Exception
Helper method to call a multi-argument static class method having a return.- Parameters:
thisClass
- the classmethodName
- the method nameargTypes
- argument Classes for constructor lookup. Must not be null.args
- the argument array- Returns:
- the value the method returns.
- Throws:
java.lang.Exception
- if the method is not found or invocation to the method fails.
-
createInstance
public static java.lang.Object createInstance(java.lang.String className, java.lang.Class<?>[] types, java.lang.Object[] args)
Instantiate an object based on the class name.- Parameters:
className
- the class nametypes
- the class types for the constructorargs
- the constructor values- Returns:
- the object instance. null if any exception occurs.
-
isSubClassOf
public static boolean isSubClassOf(java.lang.Object o, java.lang.String className)
Checks if the instance o is an instance of a subclass of the designated class name.- Parameters:
o
- the instance to checkclassName
- the class name to check- Returns:
- true if the instance o is an instance of a subclass of the class name. Otherwise false.
- Since:
- 3.4.9
-
-