Package com.helger.base.reflection
Class GenericReflection
java.lang.Object
com.helger.base.reflection.GenericReflection
This is a special helper class that provides many utility methods concerning the usage of
reflection etc..
- Author:
- Philip Helger
-
Method Summary
Modifier and TypeMethodDescriptionstatic <DATATYPE> @NonNull Constructor<DATATYPE> findConstructor(@NonNull DATATYPE aObj, Class<?>... aCtorArgs) Find the constructor of the class of the given object matching the specified parameter types.static @NonNull Class<?>[]getClassArray(@Nullable Object... aObjs) Get an array with all the classes of the passed object array.static <DATATYPE> @NonNull Class<DATATYPE> getClassFromName(@NonNull ClassLoader aClassLoader, @NonNull String sName) Get the class of the given name using the specified class loader.static <DATATYPE> @NonNull Class<DATATYPE> getClassFromName(@NonNull String sName) Get the class of the given name usingClass.forName(String).static <DATATYPE> @Nullable Class<DATATYPE> getClassFromNameSafe(@NonNull ClassLoader aClassLoader, @NonNull String sName) Get the class of the given namestatic <DATATYPE> @Nullable Class<DATATYPE> getClassFromNameSafe(@NonNull String sName) Get the class of the given namestatic <RETURNTYPE>
@Nullable RETURNTYPEinvokeMethod(@NonNull Object aSrcObj, @NonNull String sMethodName, @Nullable Class<?>[] aArgClasses, @Nullable Object[] aArgs) Dynamically invoke a method on the given object, with explicit argument class types.static <RETURNTYPE>
@Nullable RETURNTYPEinvokeMethod(@NonNull Object aSrcObj, @NonNull String sMethodName, @Nullable Object... aArgs) This method dynamically invokes the method with the given name on the given object.static <RETURNTYPE>
@Nullable RETURNTYPEinvokeStaticMethod(@NonNull Class<?> aClass, @NonNull String sMethodName, @Nullable Class<?>[] aArgClasses, @Nullable Object[] aArgs) Dynamically invoke a static method on the given class, with explicit argument class types.static <RETURNTYPE>
@Nullable RETURNTYPEinvokeStaticMethod(@NonNull Class<?> aClass, @NonNull String sMethodName, @Nullable Object... aArgs) Dynamically invoke a static method on the given class.static <RETURNTYPE>
@Nullable RETURNTYPEinvokeStaticMethod(@NonNull String sClassName, @NonNull String sMethodName, @Nullable Class<?>[] aArgClasses, @Nullable Object[] aArgs) Dynamically invoke a static method identified by the class name and method name, with explicit argument class types.static <RETURNTYPE>
@Nullable RETURNTYPEinvokeStaticMethod(@NonNull String sClassName, @NonNull String sMethodName, @Nullable Object... aArgs) Dynamically invoke a static method identified by the class name and method name.static <DATATYPE> @NonNull DATATYPEnewInstance(@NonNull DATATYPE aObj) Create a new instance of the class identified by the passed object.static <DATATYPE> @Nullable DATATYPEnewInstance(@NonNull ClassLoader aClassLoader, @Nullable String sClassName, @Nullable Class<? extends DATATYPE> aDesiredType) Create a new instance of the class identified by the given name, using the specified class loader and casting to the desired type.static <DATATYPE> @Nullable DATATYPEnewInstance(@Nullable Class<? extends DATATYPE> aClass) Create a new instance of the given class using its default constructor.static <DATATYPE> @Nullable DATATYPEnewInstance(@Nullable String sClassName, @Nullable Class<? extends DATATYPE> aDesiredType) Create a new instance of the class identified by the given name, casting to the desired type.static <DATATYPE> @Nullable DATATYPEnewInstance(@Nullable String sClassName, @Nullable Class<? extends DATATYPE> aDesiredType, @Nullable ClassLoader aClassLoaderToUse) Create a new instance of the class identified by the given name, using the specified class loader and casting to the desired type.static <DATATYPE> @Nullable DATATYPEnewInstance(@Nullable String sClassName, @Nullable Class<? extends DATATYPE> aDesiredType, @Nullable ClassLoader aClassLoaderToUse, @Nullable Consumer<Exception> aExHdl) Create a new instance of the class identified by the given name, using the specified class loader and casting to the desired type, with a custom exception handler.static <DATATYPE> @Nullable DATATYPEnewInstance(@Nullable String sClassName, @Nullable Class<? extends DATATYPE> aDesiredType, @Nullable Consumer<Exception> aExHdl) Create a new instance of the class identified by the given name, casting to the desired type, with a custom exception handler.static <SRCTYPE,DSTTYPE>
DSTTYPEuncheckedCast(@Nullable SRCTYPE aObject) Perform an unchecked cast from one type to another.
-
Method Details
-
uncheckedCast
@UsedInGeneratedCode public static <SRCTYPE,DSTTYPE> DSTTYPE uncheckedCast(@Nullable SRCTYPE aObject) Perform an unchecked cast from one type to another. This avoids unchecked cast warnings in calling code.- Type Parameters:
SRCTYPE- The source typeDSTTYPE- The destination type- Parameters:
aObject- The object to be cast. May benull.- Returns:
- The casted object. May be
null.
-
getClassFromName
public static <DATATYPE> @NonNull Class<DATATYPE> getClassFromName(@NonNull ClassLoader aClassLoader, @NonNull String sName) throws ClassNotFoundException Get the class of the given name using the specified class loader.- Type Parameters:
DATATYPE- The return type- Parameters:
aClassLoader- The class loader to be used. May not benull.sName- The fully qualified class name to be resolved. May not benull.- Returns:
- The resolved class. Never
null. - Throws:
ClassNotFoundException- If the class could not be found.
-
getClassFromName
public static <DATATYPE> @NonNull Class<DATATYPE> getClassFromName(@NonNull String sName) throws ClassNotFoundException Get the class of the given name usingClass.forName(String).- Type Parameters:
DATATYPE- The return type- Parameters:
sName- The fully qualified class name to be resolved. May not benull.- Returns:
- The resolved class. Never
null. - Throws:
ClassNotFoundException- If the class could not be found.
-
getClassFromNameSafe
public static <DATATYPE> @Nullable 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
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
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
public static <RETURNTYPE> @Nullable 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
public static <RETURNTYPE> @Nullable RETURNTYPE invokeMethod(@NonNull Object aSrcObj, @NonNull String sMethodName, @Nullable Class<?>[] aArgClasses, @Nullable Object[] aArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException Dynamically invoke a method on the given object, with explicit argument class types.- 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. May not benull.aArgClasses- The classes of the arguments. May benull.aArgs- The arguments to be passed into the method. May benull.- Returns:
- The return value of the invoked method or
nullfor void methods. - Throws:
NoSuchMethodException- Thrown by reflectionIllegalAccessException- Thrown by reflectionInvocationTargetException- Thrown by reflection
-
invokeStaticMethod
public static <RETURNTYPE> @Nullable RETURNTYPE invokeStaticMethod(@NonNull String sClassName, @NonNull String sMethodName, @Nullable Object... aArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException Dynamically invoke a static method identified by the class name and method name.- Type Parameters:
RETURNTYPE- The method return type- Parameters:
sClassName- The fully qualified class name. May not benull.sMethodName- The method to be invoked. May not benull.aArgs- The arguments to be passed into the method. May benull.- Returns:
- The return value of the invoked method or
nullfor void methods. - Throws:
NoSuchMethodException- Thrown by reflectionIllegalAccessException- Thrown by reflectionInvocationTargetException- Thrown by reflectionClassNotFoundException- If the class could not be found
-
invokeStaticMethod
public static <RETURNTYPE> @Nullable RETURNTYPE invokeStaticMethod(@NonNull Class<?> aClass, @NonNull String sMethodName, @Nullable Object... aArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException Dynamically invoke a static method on the given class.- Type Parameters:
RETURNTYPE- The method return type- Parameters:
aClass- The class on which the static method is to be invoked. May not benull.sMethodName- The method to be invoked. May not benull.aArgs- The arguments to be passed into the method. May benull.- Returns:
- The return value of the invoked method or
nullfor void methods. - Throws:
NoSuchMethodException- Thrown by reflectionIllegalAccessException- Thrown by reflectionInvocationTargetException- Thrown by reflection
-
invokeStaticMethod
public static <RETURNTYPE> @Nullable RETURNTYPE invokeStaticMethod(@NonNull String sClassName, @NonNull String sMethodName, @Nullable Class<?>[] aArgClasses, @Nullable Object[] aArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException Dynamically invoke a static method identified by the class name and method name, with explicit argument class types.- Type Parameters:
RETURNTYPE- The method return type- Parameters:
sClassName- The fully qualified class name. May not benull.sMethodName- The method to be invoked. May not benull.aArgClasses- The classes of the arguments. May benull.aArgs- The arguments to be passed into the method. May benull.- Returns:
- The return value of the invoked method or
nullfor void methods. - Throws:
NoSuchMethodException- Thrown by reflectionIllegalAccessException- Thrown by reflectionInvocationTargetException- Thrown by reflectionClassNotFoundException- If the class could not be found
-
invokeStaticMethod
public static <RETURNTYPE> @Nullable RETURNTYPE invokeStaticMethod(@NonNull Class<?> aClass, @NonNull String sMethodName, @Nullable Class<?>[] aArgClasses, @Nullable Object[] aArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException Dynamically invoke a static method on the given class, with explicit argument class types.- Type Parameters:
RETURNTYPE- The method return type- Parameters:
aClass- The class on which the static method is to be invoked. May not benull.sMethodName- The method to be invoked. May not benull.aArgClasses- The classes of the arguments. May benull.aArgs- The arguments to be passed into the method. May benull.- Returns:
- The return value of the invoked method or
nullfor void methods. - Throws:
NoSuchMethodException- Thrown by reflectionIllegalAccessException- Thrown by reflectionInvocationTargetException- Thrown by reflection
-
findConstructor
public static <DATATYPE> @NonNull Constructor<DATATYPE> findConstructor(@NonNull DATATYPE aObj, Class<?>... aCtorArgs) throws NoSuchMethodException Find the constructor of the class of the given object matching the specified parameter types.- Type Parameters:
DATATYPE- The object type- Parameters:
aObj- The object whose class is used to find the constructor. May not benull.aCtorArgs- The constructor parameter types. May benullor empty for the default constructor.- Returns:
- The matching constructor. Never
null. - Throws:
NoSuchMethodException- If no matching constructor is found.
-
newInstance
public static <DATATYPE> @NonNull 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
Create a new instance of the given class using its default constructor. Errors are logged but not thrown.- Type Parameters:
DATATYPE- The type of object to be created- Parameters:
aClass- The class to instantiate. May benull.- Returns:
- A new instance of the class or
nullif instantiation failed or the class isnull.
-
newInstance
public static <DATATYPE> @Nullable DATATYPE newInstance(@NonNull ClassLoader aClassLoader, @Nullable String sClassName, @Nullable Class<? extends DATATYPE> aDesiredType) Create a new instance of the class identified by the given name, using the specified class loader and casting to the desired type. Errors are logged but not thrown.- Type Parameters:
DATATYPE- The type of object to be created- Parameters:
aClassLoader- The class loader to use. May not benull.sClassName- The fully qualified class name. May benull.aDesiredType- The desired type to cast to. May benull.- Returns:
- A new instance of the class or
nullif instantiation failed.
-
newInstance
public static <DATATYPE> @Nullable DATATYPE newInstance(@Nullable String sClassName, @Nullable Class<? extends DATATYPE> aDesiredType) Create a new instance of the class identified by the given name, casting to the desired type. Errors are logged but not thrown.- Type Parameters:
DATATYPE- The type of object to be created- Parameters:
sClassName- The fully qualified class name. May benull.aDesiredType- The desired type to cast to. May benull.- Returns:
- A new instance of the class or
nullif instantiation failed.
-
newInstance
public static <DATATYPE> @Nullable DATATYPE newInstance(@Nullable String sClassName, @Nullable Class<? extends DATATYPE> aDesiredType, @Nullable Consumer<Exception> aExHdl) Create a new instance of the class identified by the given name, casting to the desired type, with a custom exception handler.- Type Parameters:
DATATYPE- The type of object to be created- Parameters:
sClassName- The fully qualified class name. May benull.aDesiredType- The desired type to cast to. May benull.aExHdl- The exception handler to use. May benull.- Returns:
- A new instance of the class or
nullif instantiation failed.
-
newInstance
public static <DATATYPE> @Nullable DATATYPE newInstance(@Nullable String sClassName, @Nullable Class<? extends DATATYPE> aDesiredType, @Nullable ClassLoader aClassLoaderToUse) Create a new instance of the class identified by the given name, using the specified class loader and casting to the desired type. Errors are logged but not thrown.- Type Parameters:
DATATYPE- The type of object to be created- Parameters:
sClassName- The fully qualified class name. May benull.aDesiredType- The desired type to cast to. May benull.aClassLoaderToUse- The class loader to use. May benull.- Returns:
- A new instance of the class or
nullif instantiation failed.
-
newInstance
public static <DATATYPE> @Nullable DATATYPE newInstance(@Nullable String sClassName, @Nullable Class<? extends DATATYPE> aDesiredType, @Nullable ClassLoader aClassLoaderToUse, @Nullable Consumer<Exception> aExHdl) Create a new instance of the class identified by the given name, using the specified class loader and casting to the desired type, with a custom exception handler.- Type Parameters:
DATATYPE- The type of object to be created- Parameters:
sClassName- The fully qualified class name. May benull.aDesiredType- The desired type to cast to. May benull.aClassLoaderToUse- The class loader to use. May benull.aExHdl- The exception handler to use. May benull.- Returns:
- A new instance of the class or
nullif instantiation failed.
-