Class ReflectionUtils

java.lang.Object
nonapi.io.github.classgraph.reflection.ReflectionUtils

public final class ReflectionUtils extends Object
Reflection utility methods that can be used by ClassLoaderHandlers.
  • Field Details

    • reflectionDriver

      public ReflectionDriver reflectionDriver
      The reflection driver to use.
    • accessControllerClass

      private Class<?> accessControllerClass
    • privilegedActionClass

      private Class<?> privilegedActionClass
    • accessControllerDoPrivileged

      private Method accessControllerDoPrivileged
  • Constructor Details

  • Method Details

    • getFieldVal

      public Object getFieldVal(boolean throwException, Object obj, Field field) throws IllegalArgumentException
      Get the value of the field in the class of the given object or any of its superclasses. If an exception is thrown while trying to read the field, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null object, returns null unless throwException is true, then throws IllegalArgumentException.
      Parameters:
      throwException - If true, throw an exception if the field value could not be read.
      obj - The object.
      field - The field.
      Returns:
      The field value.
      Throws:
      IllegalArgumentException - If the field value could not be read.
    • getFieldVal

      public Object getFieldVal(boolean throwException, Object obj, String fieldName) throws IllegalArgumentException
      Get the value of the named field in the class of the given object or any of its superclasses. If an exception is thrown while trying to read the field, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null object, returns null unless throwException is true, then throws IllegalArgumentException.
      Parameters:
      throwException - If true, throw an exception if the field value could not be read.
      obj - The object.
      fieldName - The field name.
      Returns:
      The field value.
      Throws:
      IllegalArgumentException - If the field value could not be read.
    • getStaticFieldVal

      public Object getStaticFieldVal(boolean throwException, Class<?> cls, String fieldName) throws IllegalArgumentException
      Get the value of the named field in the given class or any of its superclasses. If an exception is thrown while trying to read the field value, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null class reference, returns null unless throwException is true, then throws IllegalArgumentException.
      Parameters:
      throwException - If true, throw an exception if the field value could not be read.
      cls - The class.
      fieldName - The field name.
      Returns:
      The field value.
      Throws:
      IllegalArgumentException - If the field value could not be read.
    • invokeMethod

      public Object invokeMethod(boolean throwException, Object obj, String methodName) throws IllegalArgumentException
      Invoke the named method in the given object or its superclasses. If an exception is thrown while trying to call the method, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null object, returns null unless throwException is true, then throws IllegalArgumentException.
      Parameters:
      throwException - If true, throw an exception if the field value could not be read.
      obj - The object.
      methodName - The method name.
      Returns:
      The result of the method invocation.
      Throws:
      IllegalArgumentException - If the method could not be invoked.
    • invokeMethod

      public Object invokeMethod(boolean throwException, Object obj, String methodName, Class<?> argType, Object param) throws IllegalArgumentException
      Invoke the named method in the given object or its superclasses. If an exception is thrown while trying to call the method, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null object, returns null unless throwException is true, then throws IllegalArgumentException.
      Parameters:
      throwException - Whether to throw an exception on failure.
      obj - The object.
      methodName - The method name.
      argType - The type of the method argument.
      param - The parameter value to use when invoking the method.
      Returns:
      The result of the method invocation.
      Throws:
      IllegalArgumentException - If the method could not be invoked.
    • invokeStaticMethod

      public Object invokeStaticMethod(boolean throwException, Class<?> cls, String methodName) throws IllegalArgumentException
      Invoke the named method. If an exception is thrown while trying to call the method, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null class reference, returns null unless throwException is true, then throws IllegalArgumentException.
      Parameters:
      throwException - Whether to throw an exception on failure.
      cls - The class.
      methodName - The method name.
      Returns:
      The result of the method invocation.
      Throws:
      IllegalArgumentException - If the method could not be invoked.
    • invokeStaticMethod

      public Object invokeStaticMethod(boolean throwException, Class<?> cls, String methodName, Class<?> argType, Object param) throws IllegalArgumentException
      Invoke the named method. If an exception is thrown while trying to call the method, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null class reference, returns null unless throwException is true, then throws IllegalArgumentException.
      Parameters:
      throwException - Whether to throw an exception on failure.
      cls - The class.
      methodName - The method name.
      argType - The type of the method argument.
      param - The parameter value to use when invoking the method.
      Returns:
      The result of the method invocation.
      Throws:
      IllegalArgumentException - If the method could not be invoked.
    • classForNameOrNull

      public Class<?> classForNameOrNull(String className)
      Call Class.forName(className), but return null if any exception is thrown.
      Parameters:
      className - The class name to load.
      Returns:
      The class of the requested name, or null if an exception was thrown while trying to load the class.
    • staticMethodForNameOrNull

      public Method staticMethodForNameOrNull(String className, String staticMethodName)
      Get a method by name, but return null if any exception is thrown.
      Parameters:
      className - The class name to load.
      Returns:
      The class of the requested name, or null if an exception was thrown while trying to load the class.
    • doPrivileged

      public <T> T doPrivileged(Callable<T> callable) throws Throwable
      Call a method in the AccessController.doPrivileged(PrivilegedAction) context, using reflection, if possible (AccessController is deprecated in JDK 17).
      Throws:
      Throwable