Class Permit

java.lang.Object
host.anzo.commons.processors.utils.Permit

public class Permit extends Object
  • Method Details

    • setAccessible

      public static <T extends AccessibleObject> T setAccessible(T accessor)
      Set the accessible flag of the given accessible object to true.

      This method is used to set the accessible flag of a field, method, or constructor without having to declare the checked exception IllegalAccessException.

      Parameters:
      accessor - the accessible object to set the accessible flag of
      Returns:
      the given accessible object
    • getMethod

      public static Method getMethod(Class<?> c, String mName, Class<?>... parameterTypes) throws NoSuchMethodException
      Gets a method declared in the given class or any of its superclasses.

      This method will traverse the class hierarchy and find the given method, regardless of whether it is declared as public, private, protected, or default (package-private).

      Parameters:
      c - the class to search for the method
      mName - the name of the method to search for
      parameterTypes - the parameter types of the method to search for
      Returns:
      the method found, or null if no such method exists
      Throws:
      NoSuchMethodException - if the method does not exist
    • permissiveGetMethod

      @Nullable public static @Nullable Method permissiveGetMethod(Class<?> c, String mName, Class<?>... parameterTypes)
      Like getMethod(Class, String, Class[]), but won't throw an exception if the method doesn't exist.

      Instead, this method will return null if the method doesn't exist.

      Parameters:
      c - the class to search for the method
      mName - the name of the method to search for
      parameterTypes - the parameter types of the method to search for
      Returns:
      the method found, or null if no such method exists
    • getField

      public static Field getField(Class<?> c, String fName) throws NoSuchFieldException
      Retrieves a field with the given name from the specified class or its superclasses.

      This method searches for a field with the specified name in the given class and, if not found, traverses the superclass hierarchy to find the field. Once found, it sets the field to be accessible and returns it.

      Parameters:
      c - the class to search for the field
      fName - the name of the field to search for
      Returns:
      the found field with accessible set to true
      Throws:
      NoSuchFieldException - if the field does not exist in the given class or its superclasses
    • permissiveGetField

      @Nullable public static @Nullable Field permissiveGetField(Class<?> c, String fName)
      Retrieves a field with the given name from the specified class or its superclasses.

      This method searches for a field with the specified name in the given class and, if not found, traverses the superclass hierarchy to find the field. Once found, it sets the field to be accessible and returns it. If no field is found, it returns null.

      Parameters:
      c - the class to search for the field
      fName - the name of the field to search for
      Returns:
      the found field with accessible set to true, or null if the field does not exist
    • permissiveReadField

      @Nullable public static <T> T permissiveReadField(Class<T> type, Field f, Object instance)
      Like
      invalid reference
      #readField(Class, Field, Object)
      , but won't throw an exception if the field doesn't exist or is not accessible.

      Instead, this method will return null if the field doesn't exist or if any exception occurs while trying to read the field.

      Parameters:
      type - the type of the object to be returned
      f - the field to read
      instance - the object containing the field
      Returns:
      the value of the field, or null if the field doesn't exist or any exception occurs
    • getConstructor

      public static <T> Constructor<T> getConstructor(@NotNull @NotNull Class<T> c, Class<?>... parameterTypes) throws NoSuchMethodException
      Finds a constructor with the given parameter types and makes it accessible.

      This method is like Class.getDeclaredConstructor(Class[]), but sets the accessible flag of the constructor to true before returning it.

      Parameters:
      c - the class to search for the constructor
      parameterTypes - the parameter types of the constructor to search for
      Returns:
      the found constructor with accessible set to true
      Throws:
      NoSuchMethodException - if no such constructor exists
    • handleReflectionDebug

      public static void handleReflectionDebug(@NotNull @NotNull Throwable t, Throwable initError)
      Handles a reflection-related exception by printing out a helpful message about the exception and, if given, an exception that occurred while setting up reflection.
      Parameters:
      t - the exception to handle
      initError - the exception that occurred while setting up reflection, or null if no such exception occurred
    • invoke

      public static Object invoke(Method m, Object receiver, Object... args) throws IllegalAccessException, InvocationTargetException
      Throws:
      IllegalAccessException
      InvocationTargetException
    • invoke

      public static Object invoke(Throwable initError, Method m, Object receiver, Object... args) throws IllegalAccessException, InvocationTargetException
      Invokes the specified method on the given receiver with the provided arguments.

      This method attempts to call the specified method using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown.

      Parameters:
      initError - the optional exception that occurred while setting up reflection
      m - the method to invoke
      receiver - the object on which to invoke the method
      args - the arguments to pass to the method
      Returns:
      the result of the method invocation
      Throws:
      IllegalAccessException - if the method is not accessible
      InvocationTargetException - if the underlying method throws an exception
    • invokeSneaky

      public static Object invokeSneaky(Method m, Object receiver, Object... args)
    • invokeSneaky

      @Nullable public static @Nullable Object invokeSneaky(Throwable initError, Method m, Object receiver, Object... args)
      Invokes the specified method on the given receiver with the provided arguments.

      This method attempts to call the specified method using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown. If the exception is a NoClassDefFoundError or a NullPointerException, the exception is ignored and null is returned.

      Parameters:
      initError - the optional exception that occurred while setting up reflection
      m - the method to invoke
      receiver - the object on which to invoke the method
      args - the arguments to pass to the method
      Returns:
      the result of the method invocation, or null if a NoClassDefFoundError or a NullPointerException was thrown
      Throws:
      IllegalAccessException - if the method is not accessible
      InvocationTargetException - if the underlying method throws an exception
    • newInstance

      @NotNull public static <T> T newInstance(Constructor<T> c, Object... args) throws IllegalAccessException, InvocationTargetException, InstantiationException
      Invokes the specified constructor with the given arguments.

      This method attempts to call the specified constructor using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown.

      Parameters:
      c - the constructor to invoke
      args - the arguments to pass to the constructor
      Returns:
      the result of the constructor invocation
      Throws:
      IllegalAccessException - if the constructor is not accessible
      InvocationTargetException - if the underlying constructor throws an exception
      InstantiationException - if the underlying constructor throws an exception
    • newInstance

      @NotNull public static <T> T newInstance(Throwable initError, Constructor<T> c, Object... args) throws IllegalAccessException, InvocationTargetException, InstantiationException
      Invokes the specified constructor with the given arguments.

      This method attempts to call the specified constructor using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown.

      Parameters:
      initError - the optional exception that occurred while setting up reflection
      c - the constructor to invoke
      args - the arguments to pass to the constructor
      Returns:
      the result of the constructor invocation
      Throws:
      IllegalAccessException - if the constructor is not accessible
      InvocationTargetException - if the underlying constructor throws an exception
      InstantiationException - if the underlying constructor throws an exception
    • newInstanceSneaky

      public static <T> T newInstanceSneaky(Constructor<T> c, Object... args)
      Invokes the specified constructor with the given arguments.

      This method attempts to call the specified constructor using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown. If the exception is a NoClassDefFoundError or a NullPointerException, the exception is ignored and null is returned.

      Parameters:
      c - the constructor to invoke
      args - the arguments to pass to the constructor
      Returns:
      the result of the constructor invocation, or null if a NoClassDefFoundError or a NullPointerException was thrown
    • newInstanceSneaky

      @Nullable public static <T> T newInstanceSneaky(Throwable initError, Constructor<T> c, Object... args)
      Invokes the specified constructor with the given arguments.

      This method attempts to call the specified constructor using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown. If the exception is a NoClassDefFoundError or a NullPointerException, the exception is ignored and null is returned.

      Parameters:
      initError - the optional exception that occurred while setting up reflection
      c - the constructor to invoke
      args - the arguments to pass to the constructor
      Returns:
      the result of the constructor invocation, or null if a NoClassDefFoundError or a NullPointerException was thrown
    • get

      public static <T> T get(Field f, Object receiver) throws IllegalAccessException
      Retrieves the value of the given field from the given receiver.

      This method attempts to call the specified field using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown.

      Parameters:
      f - the field to retrieve
      receiver - the object from which to retrieve the field
      Returns:
      the value of the field
      Throws:
      IllegalAccessException - if the field is not accessible
    • set

      public static void set(Field f, Object receiver, Object newValue) throws IllegalAccessException
      Set the value of the field to newValue.
      Parameters:
      f - the field to set
      receiver - the object in which to set the field
      newValue - the value to set
      Throws:
      IllegalAccessException - if the field is not accessible
    • reportReflectionProblem

      public static void reportReflectionProblem(Throwable initError, String msg)
      Reports a reflection-related problem by printing out a message to the error stream.

      This method logs a specified message indicating a reflection issue and, if provided, also logs an exception that occurred during the setup of reflection.

      Parameters:
      initError - the exception that occurred while setting up reflection, or null if no such exception occurred
      msg - the message describing the reflection problem
    • sneakyThrow

      public static RuntimeException sneakyThrow(Throwable t)
      Wraps a Throwable in a RuntimeException, but only if it isn't already a RuntimeException. This is useful for wrapping exceptions that you want to propagate up the call stack without having to declare them in the method signature.

      If the given Throwable is already a RuntimeException, this method simply returns it as is. Otherwise, it wraps it in a RuntimeException and returns that.

      Parameters:
      t - the exception to wrap
      Returns:
      the wrapped exception
      Throws:
      NullPointerException - if the given exception is null