Package com.helger.commons.lang
Class GenericReflection
- java.lang.Object
-
- com.helger.commons.lang.GenericReflection
-
@Immutable public final class GenericReflection extends Object
This is a special helper class that provides many utility methods concerning the usage of reflection etc..- Author:
- Philip Helger
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <DATATYPE> Constructor<DATATYPE>findConstructor(DATATYPE aObj, Class<?>... aCtorArgs)static Class<?>[]getClassArray(Object... aObjs)Get an array with all the classes of the passed object array.static <DATATYPE> Class<DATATYPE>getClassFromName(ClassLoader aClassLoader, String sName)static <DATATYPE> Class<DATATYPE>getClassFromName(String sName)static <DATATYPE> Class<DATATYPE>getClassFromNameSafe(ClassLoader aClassLoader, String sName)Get the class of the given namestatic <DATATYPE> Class<DATATYPE>getClassFromNameSafe(String sName)Get the class of the given namestatic <RETURNTYPE>
RETURNTYPEinvokeMethod(Object aSrcObj, String sMethodName, Class<?>[] aArgClasses, Object[] aArgs)static <RETURNTYPE>
RETURNTYPEinvokeMethod(Object aSrcObj, String sMethodName, Object... aArgs)This method dynamically invokes the method with the given name on the given object.static <RETURNTYPE>
RETURNTYPEinvokeStaticMethod(Class<?> aClass, String sMethodName, Class<?>[] aArgClasses, Object[] aArgs)static <RETURNTYPE>
RETURNTYPEinvokeStaticMethod(Class<?> aClass, String sMethodName, Object... aArgs)static <RETURNTYPE>
RETURNTYPEinvokeStaticMethod(String sClassName, String sMethodName, Class<?>[] aArgClasses, Object[] aArgs)static <RETURNTYPE>
RETURNTYPEinvokeStaticMethod(String sClassName, String sMethodName, Object... aArgs)static <DATATYPE> DATATYPEnewInstance(DATATYPE aObj)Create a new instance of the class identified by the passed object.static <DATATYPE> DATATYPEnewInstance(Class<? extends DATATYPE> aClass)static <DATATYPE> DATATYPEnewInstance(ClassLoader aClassLoader, String sClassName, Class<? extends DATATYPE> aDesiredType)static <DATATYPE> DATATYPEnewInstance(String sClassName, Class<? extends DATATYPE> aDesiredType)static <DATATYPE> DATATYPEnewInstance(String sClassName, Class<? extends DATATYPE> aDesiredType, ClassLoader aClassLoaderToUse)static <SRCTYPE,DSTTYPE>
DSTTYPEuncheckedCast(SRCTYPE aObject)
-
-
-
Method Detail
-
uncheckedCast
public static <SRCTYPE,DSTTYPE> DSTTYPE uncheckedCast(@Nullable SRCTYPE aObject)
-
getClassFromName
@Nonnull public static <DATATYPE> Class<DATATYPE> getClassFromName(@Nonnull ClassLoader aClassLoader, @Nonnull String sName) throws ClassNotFoundException
- Throws:
ClassNotFoundException
-
getClassFromName
@Nonnull public static <DATATYPE> Class<DATATYPE> getClassFromName(@Nonnull String sName) throws ClassNotFoundException
- Throws:
ClassNotFoundException
-
getClassFromNameSafe
@Nullable public static <DATATYPE> Class<DATATYPE> getClassFromNameSafe(@Nonnull ClassLoader aClassLoader, @Nonnull String sName)
Get the class of the given name- Type Parameters:
DATATYPE- The return type- Parameters:
aClassLoader- The class loader to be used. May not benull.sName- The name to be resolved.- Returns:
nullif the class could not be resolved
-
getClassFromNameSafe
@Nullable public static <DATATYPE> Class<DATATYPE> getClassFromNameSafe(@Nonnull String sName)
Get the class of the given name- Type Parameters:
DATATYPE- The return type- Parameters:
sName- The name to be resolved.- Returns:
nullif the class could not be resolved
-
getClassArray
@Nonnull public static Class<?>[] getClassArray(@Nullable Object... aObjs)
Get an array with all the classes of the passed object array.- Parameters:
aObjs- The object array. May benull. No contained element may benull.- Returns:
- A non-
nullarray of classes.
-
invokeMethod
@Nullable public static <RETURNTYPE> RETURNTYPE invokeMethod(@Nonnull Object aSrcObj, @Nonnull String sMethodName, @Nullable Object... aArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
This method dynamically invokes the method with the given name on the given object.- Type Parameters:
RETURNTYPE- The method return type- Parameters:
aSrcObj- The source object on which the method is to be invoked. May not benull.sMethodName- The method to be invoked.aArgs- The arguments to be passed into the method. May benull. If notnull, the members of the array may not benullbecause otherwise the classes of the arguments cannot be determined and will throw an Exception!- Returns:
- The return value of the invoked method or
nullfor void methods. - Throws:
NoSuchMethodException- Thrown by reflectionIllegalAccessException- Thrown by reflectionInvocationTargetException- Thrown by reflection
-
invokeMethod
@Nullable public static <RETURNTYPE> RETURNTYPE invokeMethod(@Nonnull Object aSrcObj, @Nonnull String sMethodName, @Nullable Class<?>[] aArgClasses, @Nullable Object[] aArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
-
invokeStaticMethod
@Nullable public static <RETURNTYPE> RETURNTYPE invokeStaticMethod(@Nonnull String sClassName, @Nonnull String sMethodName, @Nullable Object... aArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
-
invokeStaticMethod
@Nullable public static <RETURNTYPE> RETURNTYPE invokeStaticMethod(@Nonnull Class<?> aClass, @Nonnull String sMethodName, @Nullable Object... aArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
-
invokeStaticMethod
@Nullable public static <RETURNTYPE> RETURNTYPE invokeStaticMethod(@Nonnull String sClassName, @Nonnull String sMethodName, @Nullable Class<?>[] aArgClasses, @Nullable Object[] aArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
-
invokeStaticMethod
@Nullable public static <RETURNTYPE> RETURNTYPE invokeStaticMethod(@Nonnull Class<?> aClass, @Nonnull String sMethodName, @Nullable Class<?>[] aArgClasses, @Nullable Object[] aArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
-
findConstructor
@Nonnull public static <DATATYPE> Constructor<DATATYPE> findConstructor(@Nonnull DATATYPE aObj, Class<?>... aCtorArgs) throws NoSuchMethodException
- Throws:
NoSuchMethodException
-
newInstance
@Nonnull public static <DATATYPE> DATATYPE newInstance(@Nonnull DATATYPE aObj) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException
Create a new instance of the class identified by the passed object. The default constructor will be invoked.- Type Parameters:
DATATYPE- The type of object to be created.- Parameters:
aObj- The object from which the class should be used.- Returns:
- A new instance of the object or an exception is thrown.
- Throws:
IllegalAccessException- Reflection exceptionNoSuchMethodException- Reflection exceptionInvocationTargetException- Reflection exceptionInstantiationException- Reflection exception
-
newInstance
@Nullable public static <DATATYPE> DATATYPE newInstance(@Nullable Class<? extends DATATYPE> aClass)
-
newInstance
@Nullable public static <DATATYPE> DATATYPE newInstance(@Nonnull ClassLoader aClassLoader, @Nullable String sClassName, @Nullable Class<? extends DATATYPE> aDesiredType)
-
newInstance
@Nullable public static <DATATYPE> DATATYPE newInstance(@Nullable String sClassName, @Nullable Class<? extends DATATYPE> aDesiredType)
-
-