Class ReflectionDriver

java.lang.Object
nonapi.io.github.classgraph.reflection.ReflectionDriver
Direct Known Subclasses:
JVMDriverReflectionDriver, NarcissusReflectionDriver, StandardReflectionDriver

abstract class ReflectionDriver extends Object
Reflection driver
  • Field Details

  • Constructor Details

    • ReflectionDriver

      ReflectionDriver()
  • Method Details

    • findClass

      abstract Class<?> findClass(String className) throws Exception
      Find a class by name.
      Parameters:
      className - the class name
      Returns:
      the class reference
      Throws:
      Exception
    • getDeclaredMethods

      abstract Method[] getDeclaredMethods(Class<?> cls) throws Exception
      Get declared methods for class.
      Parameters:
      cls - the class
      Returns:
      the declared methods
      Throws:
      Exception
    • getDeclaredConstructors

      abstract <T> Constructor<T>[] getDeclaredConstructors(Class<T> cls) throws Exception
      Get declared constructors for class.
      Type Parameters:
      T - the generic type
      Parameters:
      cls - the class
      Returns:
      the declared constructors
      Throws:
      Exception
    • getDeclaredFields

      abstract Field[] getDeclaredFields(Class<?> cls) throws Exception
      Get declared fields for class.
      Parameters:
      cls - the class
      Returns:
      the declared fields
      Throws:
      Exception
    • getField

      abstract Object getField(Object object, Field field) throws Exception
      Get the value of a non-static field, boxing the value if necessary.
      Parameters:
      object - the object instance to get the field value from
      field - the non-static field
      Returns:
      the value of the field
      Throws:
      Exception
    • setField

      abstract void setField(Object object, Field field, Object value) throws Exception
      Set the value of a non-static field, unboxing the value if necessary.
      Parameters:
      object - the object instance to get the field value from
      field - the non-static field
      value - the value to set
      Throws:
      Exception
    • getStaticField

      abstract Object getStaticField(Field field) throws Exception
      Get the value of a static field, boxing the value if necessary.
      Parameters:
      field - the static field
      Returns:
      the static field
      Throws:
      Exception
    • setStaticField

      abstract void setStaticField(Field field, Object value) throws Exception
      Set the value of a static field, unboxing the value if necessary.
      Parameters:
      field - the static field
      value - the value to set
      Throws:
      Exception
    • invokeMethod

      abstract Object invokeMethod(Object object, Method method, Object... args) throws Exception
      Invoke a non-static method, boxing the result if necessary.
      Parameters:
      object - the object instance to invoke the method on
      method - the non-static method
      args - the method arguments (or new Object[0] if there are no args)
      Returns:
      the return value (possibly a boxed value)
      Throws:
      Exception
    • invokeStaticMethod

      abstract Object invokeStaticMethod(Method method, Object... args) throws Exception
      Invoke a static method, boxing the result if necessary.
      Parameters:
      method - the static method
      args - the method arguments (or new Object[0] if there are no args)
      Returns:
      the return value (possibly a boxed value)
      Throws:
      Exception
    • makeAccessible

      abstract boolean makeAccessible(Object instance, AccessibleObject fieldOrMethod)
      Make a field or method accessible.
      Parameters:
      instance - the object instance, or null if static.
      fieldOrMethod - the field or method.
      Returns:
      true if successful.
    • isAccessible

      boolean isAccessible(Object instance, AccessibleObject fieldOrMethod)
      Check whether a field or method is accessible.

      N.B. this is overridden in Narcissus driver to just return true, since everything is accessible to JNI.

      Parameters:
      instance - the object instance, or null if static.
      fieldOrMethod - the field or method.
      Returns:
      true if accessible.
    • findField

      protected Field findField(Class<?> cls, Object obj, String fieldName) throws Exception
      Get the field of the class that has a given field name.
      Parameters:
      cls - the class.
      obj - the object instance, or null for a static field.
      fieldName - The name of the field.
      Returns:
      The Field object for the requested field name, or null if no such field was found in the class.
      Throws:
      Exception - if the field could not be found
    • findStaticField

      protected Field findStaticField(Class<?> cls, String fieldName) throws Exception
      Get the static field of the class that has a given field name.
      Parameters:
      cls - the class.
      fieldName - The name of the field.
      Returns:
      The Field object for the requested field name, or null if no such field was found in the class.
      Throws:
      Exception - if the field could not be found
    • findInstanceField

      protected Field findInstanceField(Object obj, String fieldName) throws Exception
      Get the non-static field of the class that has a given field name.
      Parameters:
      obj - the object instance, or null for a static field.
      fieldName - The name of the field.
      Returns:
      The Field object for the requested field name, or null if no such field was found in the class.
      Throws:
      Exception - if the field could not be found
    • findMethod

      protected Method findMethod(Class<?> cls, Object obj, String methodName, Class<?>... paramTypes) throws Exception
      Get a method by name and parameter types.
      Parameters:
      cls - the class.
      obj - the object instance, or null for a static method.
      methodName - The name of the method.
      paramTypes - The types of the parameters of the method. For primitive-typed parameters, use e.g. Integer.TYPE.
      Returns:
      The Method object for the matching method, or null if no such method was found in the class.
      Throws:
      Exception - if the method could not be found.
    • findStaticMethod

      protected Method findStaticMethod(Class<?> cls, String methodName, Class<?>... paramTypes) throws Exception
      Get a static method by name and parameter types.
      Parameters:
      cls - the class.
      methodName - The name of the method.
      paramTypes - The types of the parameters of the method. For primitive-typed parameters, use e.g. Integer.TYPE.
      Returns:
      The Method object for the matching method, or null if no such method was found in the class.
      Throws:
      Exception - if the method could not be found.
    • findInstanceMethod

      protected Method findInstanceMethod(Object obj, String methodName, Class<?>... paramTypes) throws Exception
      Get a non-static method by name and parameter types.
      Parameters:
      obj - the object instance, or null for a static method.
      methodName - The name of the method.
      paramTypes - The types of the parameters of the method. For primitive-typed parameters, use e.g. Integer.TYPE.
      Returns:
      The Method object for the matching method, or null if no such method was found in the class.
      Throws:
      Exception - if the method could not be found.