Class ReflectUtils

  • All Implemented Interfaces:

    
    public final class ReflectUtils
    
                        

    Utility class to ease Reflection

    • Constructor Detail

      • ReflectUtils

        ReflectUtils()
    • Method Detail

      • allocateInstance

         static <T> T allocateInstance(@NonNull() Class<T> clazz)

        Creates new class instance without using a constructor

        Parameters:
        clazz - Class
      • invokeConstructorWithArgs

         static <T> T invokeConstructorWithArgs(@NonNull() Class<T> clazz, Array<Object> args)

        Attempts to find and invoke the constructor of class T matching the specified arguments

        Parameters:
        clazz - T.
        args - The arguments to invoke the constructor with.
      • getMethodByArgs

        @NonNull() static Method getMethodByArgs(@NonNull() Class<out Object> clazz, @NonNull() String methodName, Array<Object> args)

        Attempts to find and invoke the method matching the specified arguments Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Method to improve performance drastically

        Parameters:
        clazz - The class
        methodName - The name of the method
        args - The arguments to invoke the method with.
      • invokeMethod

        @Nullable() static Object invokeMethod(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String methodName, Array<Object> args)

        Attempts to find and invoke the method matching the specified arguments Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Method to improve performance drastically

        Parameters:
        clazz - The class holding the method
        instance - The instance of the class to invoke the method on or null to invoke static method
        methodName - The name of the method
        args - The arguments to invoke the method with.
      • invokeMethod

        @Nullable() static Object invokeMethod(@NonNull() Object instance, @NonNull() String methodName, Array<Object> args)

        Attempts to find and invoke the method matching the specified arguments Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Method to improve performance drastically

        Parameters:
        instance - The instance of the class to invoke the method on
        methodName - The name of the method
        args - The arguments to invoke the method with.
      • getField

        @Nullable() static Object getField(@NonNull() Object instance, @NonNull() String fieldName)

        Gets a field declared in the class. Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Field to improve performance drastically

        Parameters:
        instance - Instance of the class where the field is located.
        fieldName - Name of the field.
      • getField

        @Nullable() static Object getField(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String fieldName)

        Gets a field declared in the class. Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Field to improve performance drastically

        Parameters:
        clazz - Class where the field is located.
        instance - Instance of the clazz or null to get static field
        fieldName - Name of the field.
      • setField

         static void setField(@NonNull() Object instance, @NonNull() String fieldName, @Nullable() Object v)

        Override a field of a class. Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Field to improve performance drastically

        Parameters:
        instance - Instance of the class where the field is located.
        fieldName - Name of the field.
        v - Value to store.
      • setField

         static void setField(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String fieldName, @Nullable() Object v)

        Override a field of a class. Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Field to improve performance drastically

        Parameters:
        clazz - Class where the field is located.
        instance - Instance of the clazz or null to set static field.
        fieldName - Name of the field.
        v - Value to store.
      • setFinalField

         static void setFinalField(@Nullable() Object instance, @NonNull() String fieldName, @Nullable() Object v)

        Override a final field of a class. WARNING: If this field is of a primitive type, setting it may have no effect as the compiler will inline final primitives. Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Field to improve performance drastically

        Parameters:
        instance - Instance of the clazz or null to set static field.
        fieldName - Name of the field.
        v - Value to store.
      • setFinalField

         static void setFinalField(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String fieldName, @Nullable() Object v)

        Override a final field of a class. WARNING: If this field is of a primitive type, setting it may have no effect as the compiler will inline final primitives. Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Field to improve performance drastically

        Parameters:
        clazz - Class where the field is located.
        instance - Instance of the clazz or null to set static field.
        fieldName - Name of the field.
        v - Value to store.