Class GenericReflection

java.lang.Object
com.helger.base.reflection.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

    Modifier and Type
    Method
    Description
    static <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 using Class.forName(String).
    static <DATATYPE> @Nullable Class<DATATYPE>
    getClassFromNameSafe(@NonNull ClassLoader aClassLoader, @NonNull String sName)
    Get the class of the given name
    static <DATATYPE> @Nullable Class<DATATYPE>
    getClassFromNameSafe(@NonNull String sName)
    Get the class of the given name
    static <RETURNTYPE>
    @Nullable RETURNTYPE
    invokeMethod(@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 RETURNTYPE
    invokeMethod(@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 RETURNTYPE
    invokeStaticMethod(@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 RETURNTYPE
    invokeStaticMethod(@NonNull Class<?> aClass, @NonNull String sMethodName, @Nullable Object... aArgs)
    Dynamically invoke a static method on the given class.
    static <RETURNTYPE>
    @Nullable RETURNTYPE
    invokeStaticMethod(@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 RETURNTYPE
    invokeStaticMethod(@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 DATATYPE
    newInstance(@NonNull DATATYPE aObj)
    Create a new instance of the class identified by the passed object.
    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.
    static <DATATYPE> @Nullable DATATYPE
    newInstance(@Nullable Class<? extends DATATYPE> aClass)
    Create a new instance of the given class using its default constructor.
    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.
    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.
    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.
    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.
    static <SRCTYPE, DSTTYPE>
    DSTTYPE
    uncheckedCast(@Nullable SRCTYPE aObject)
    Perform an unchecked cast from one type to another.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 type
      DSTTYPE - The destination type
      Parameters:
      aObject - The object to be cast. May be null.
      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 be null.
      sName - The fully qualified class name to be resolved. May not be null.
      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 using Class.forName(String).
      Type Parameters:
      DATATYPE - The return type
      Parameters:
      sName - The fully qualified class name to be resolved. May not be null.
      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 be null.
      sName - The name to be resolved.
      Returns:
      null if the class could not be resolved
    • getClassFromNameSafe

      public static <DATATYPE> @Nullable 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:
      null if the class could not be resolved
    • getClassArray

      public static @NonNull Class<?>[] getClassArray(@Nullable Object... aObjs)
      Get an array with all the classes of the passed object array.
      Parameters:
      aObjs - The object array. May be null. No contained element may be null.
      Returns:
      A non-null array 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 be null.
      sMethodName - The method to be invoked.
      aArgs - The arguments to be passed into the method. May be null . If not null, the members of the array may not be null because otherwise the classes of the arguments cannot be determined and will throw an Exception!
      Returns:
      The return value of the invoked method or null for void methods.
      Throws:
      NoSuchMethodException - Thrown by reflection
      IllegalAccessException - Thrown by reflection
      InvocationTargetException - 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 be null.
      sMethodName - The method to be invoked. May not be null.
      aArgClasses - The classes of the arguments. May be null.
      aArgs - The arguments to be passed into the method. May be null.
      Returns:
      The return value of the invoked method or null for void methods.
      Throws:
      NoSuchMethodException - Thrown by reflection
      IllegalAccessException - Thrown by reflection
      InvocationTargetException - 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 be null.
      sMethodName - The method to be invoked. May not be null.
      aArgs - The arguments to be passed into the method. May be null.
      Returns:
      The return value of the invoked method or null for void methods.
      Throws:
      NoSuchMethodException - Thrown by reflection
      IllegalAccessException - Thrown by reflection
      InvocationTargetException - Thrown by reflection
      ClassNotFoundException - 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 be null.
      sMethodName - The method to be invoked. May not be null.
      aArgs - The arguments to be passed into the method. May be null.
      Returns:
      The return value of the invoked method or null for void methods.
      Throws:
      NoSuchMethodException - Thrown by reflection
      IllegalAccessException - Thrown by reflection
      InvocationTargetException - 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 be null.
      sMethodName - The method to be invoked. May not be null.
      aArgClasses - The classes of the arguments. May be null.
      aArgs - The arguments to be passed into the method. May be null.
      Returns:
      The return value of the invoked method or null for void methods.
      Throws:
      NoSuchMethodException - Thrown by reflection
      IllegalAccessException - Thrown by reflection
      InvocationTargetException - Thrown by reflection
      ClassNotFoundException - 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 be null.
      sMethodName - The method to be invoked. May not be null.
      aArgClasses - The classes of the arguments. May be null.
      aArgs - The arguments to be passed into the method. May be null.
      Returns:
      The return value of the invoked method or null for void methods.
      Throws:
      NoSuchMethodException - Thrown by reflection
      IllegalAccessException - Thrown by reflection
      InvocationTargetException - 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 be null.
      aCtorArgs - The constructor parameter types. May be null or 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 exception
      NoSuchMethodException - Reflection exception
      InvocationTargetException - Reflection exception
      InstantiationException - Reflection exception
    • newInstance

      public static <DATATYPE> @Nullable DATATYPE newInstance(@Nullable Class<? extends DATATYPE> aClass)
      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 be null.
      Returns:
      A new instance of the class or null if instantiation failed or the class is null.
    • 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 be null.
      sClassName - The fully qualified class name. May be null.
      aDesiredType - The desired type to cast to. May be null.
      Returns:
      A new instance of the class or null if 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 be null.
      aDesiredType - The desired type to cast to. May be null.
      Returns:
      A new instance of the class or null if 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 be null.
      aDesiredType - The desired type to cast to. May be null.
      aExHdl - The exception handler to use. May be null.
      Returns:
      A new instance of the class or null if 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 be null.
      aDesiredType - The desired type to cast to. May be null.
      aClassLoaderToUse - The class loader to use. May be null.
      Returns:
      A new instance of the class or null if 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 be null.
      aDesiredType - The desired type to cast to. May be null.
      aClassLoaderToUse - The class loader to use. May be null.
      aExHdl - The exception handler to use. May be null.
      Returns:
      A new instance of the class or null if instantiation failed.