Class Narcissus

java.lang.Object
io.github.toolfactory.narcissus.Narcissus

public class Narcissus extends Object
Narcissus reflection library.
  • Field Details

  • Constructor Details

    • Narcissus

      public Narcissus()
  • Method Details

    • findClassInternal

      private static Class<?> findClassInternal(String classNameInternal)
    • findMethodInternal

      private static Method findMethodInternal(Class<?> cls, String methodName, String sig, boolean isStatic)
    • findFieldInternal

      private static Field findFieldInternal(Class<?> cls, String fieldName, String sig, boolean isStatic)
    • findClass

      public static Class<?> findClass(String className)
      Finds a class by name (e.g. "com.xyz.MyClass") using the current classloader or the system classloader, ignoring visibility and bypassing security checks. Finds array classes if the class name is of the form "com.xyz.MyClass[][]".
      Parameters:
      className - the class name
      Returns:
      the class reference
    • allocateInstance

      public static Object allocateInstance(Class<?> cls)
      Allocate an object instance, without calling any constructor. (For internal use.)
      Parameters:
      cls - the class to instantiate
      Returns:
      the new object instance
    • sneakyThrow

      public static void sneakyThrow(Throwable throwable)
      Throw a Throwable without requiring a throws declaration. (For internal use.)
      Parameters:
      throwable - the Throwable to throw
    • forAllMethods

      private static void forAllMethods(Class<?> cls, Narcissus.MethodIterator methodIter)
      Iterate through all methods in the given class, ignoring visibility and bypassing security checks. Also iterates up through superclasses, to collect all methods of the class and its superclasses.
      Parameters:
      cls - the class
    • findMethod

      public static Method findMethod(Class<?> cls, String methodName, Class<?>... paramTypes) throws NoSuchMethodException
      Find a method by name and parameter types in the given class, ignoring visibility and bypassing security checks.
      Parameters:
      cls - the class
      methodName - the method name.
      paramTypes - the parameter types of the method.
      Returns:
      the Method
      Throws:
      NoSuchMethodException - if the class does not contain a method of the given name
    • findConstructor

      public static Constructor<?> findConstructor(Class<?> cls, Class<?>... paramTypes) throws NoSuchMethodException
      Find a constructor by parameter types in the given class, ignoring visibility and bypassing security checks.
      Parameters:
      cls - the class
      paramTypes - the parameter types of the constructor.
      Returns:
      the Method
      Throws:
      NoSuchMethodException - if the class does not contain a constructor of the given name
    • enumerateMethods

      public static List<Method> enumerateMethods(Class<?> cls)
      Enumerate all methods in the given class, ignoring visibility and bypassing security checks. Also iterates up through superclasses, to collect all methods of the class and its superclasses.
      Parameters:
      cls - the class
      Returns:
      a list of Method objects representing all methods declared by the class or a superclass.
    • findField

      public static Field findField(Class<?> cls, String fieldName) throws NoSuchFieldException
      Find a field by name in the given class, ignoring visibility and bypassing security checks.
      Parameters:
      cls - the class
      fieldName - the field name.
      Returns:
      the Field
      Throws:
      NoSuchFieldException - if the class does not contain a field of the given name
    • enumerateFields

      public static List<Field> enumerateFields(Class<?> cls)
      Enumerate all fields in the given class, ignoring visibility and bypassing security checks. Also iterates up through superclasses, to collect all fields of the class and its superclasses.
      Parameters:
      cls - the class
      Returns:
      a list of Field objects representing all fields declared by the class or a superclass.
    • getDeclaredMethods

      public static Method[] getDeclaredMethods(Class<?> cls)
      Get declared methods, ignoring visibility and bypassing security checks.
      Parameters:
      cls - the class
      Returns:
      the declared methods
    • getDeclaredConstructors

      public static <T> Constructor<T>[] getDeclaredConstructors(Class<T> cls)
      Get declared constructors, ignoring visibility and bypassing security checks.
      Type Parameters:
      T - the generic type
      Parameters:
      cls - the class
      Returns:
      the declared constructors
    • getDeclaredFields

      public static Field[] getDeclaredFields(Class<?> cls)
      Get declared fields, ignoring visibility and bypassing security checks.
      Parameters:
      cls - the class
      Returns:
      the declared fields
    • getIntField

      public static int getIntField(Object object, Field field)
      Get the value of an int field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance to get the field value from
      field - the non-static field
      Returns:
      the int value of the field
    • getLongField

      public static long getLongField(Object object, Field field)
      Get the value of a long field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance to get the field value from
      field - the non-static field
      Returns:
      the long value of the field
    • getShortField

      public static short getShortField(Object object, Field field)
      Get the value of a short field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance to get the field value from
      field - the non-static field
      Returns:
      the short value of the field
    • getCharField

      public static char getCharField(Object object, Field field)
      Get the value of a char field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance to get the field value from
      field - the non-static field
      Returns:
      the char value of the field
    • getBooleanField

      public static boolean getBooleanField(Object object, Field field)
      Get the value of a boolean field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance to get the field value from
      field - the non-static field
      Returns:
      the boolean value of the field
    • getByteField

      public static byte getByteField(Object object, Field field)
      Get the value of a byte field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance to get the field value from
      field - the non-static field
      Returns:
      the byte value of the field
    • getFloatField

      public static float getFloatField(Object object, Field field)
      Get the value of a float field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance to get the field value from
      field - the non-static field
      Returns:
      the float value of the field.
    • getDoubleField

      public static double getDoubleField(Object object, Field field)
      Get the value of a double field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance to get the field value from
      field - the non-static field
      Returns:
      the double value of the field
    • getObjectField

      public static Object getObjectField(Object object, Field field)
      Get the value of an object field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance to get the field value from
      field - the non-static field
      Returns:
      the value of the field
    • getField

      public static Object getField(Object object, Field field)
      Get the value of an object field, ignoring visibility and bypassing security checks, 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
    • setIntField

      public static void setIntField(Object object, Field field, int val)
      Set the value of an int field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance in which to set the field value
      field - the non-static field
      val - the int value to set
    • setLongField

      public static void setLongField(Object object, Field field, long val)
      Set the value of a long field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance in which to set the field value
      field - the non-static field
      val - the long value to set
    • setShortField

      public static void setShortField(Object object, Field field, short val)
      Set the value of a short field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance in which to set the field value
      field - the non-static field
      val - the short value to set
    • setCharField

      public static void setCharField(Object object, Field field, char val)
      Set the value of a char field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance in which to set the field value
      field - the non-static field
      val - the char value to set
    • setBooleanField

      public static void setBooleanField(Object object, Field field, boolean val)
      Set the value of a boolean field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance in which to set the field value
      field - the non-static field
      val - the boolean value to set
    • setByteField

      public static void setByteField(Object object, Field field, byte val)
      Set the value of a byte field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance in which to set the field value
      field - the non-static field
      val - the byte value to set
    • setFloatField

      public static void setFloatField(Object object, Field field, float val)
      Set the value of a float field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance in which to set the field value
      field - the non-static field
      val - the float value to set
    • setDoubleField

      public static void setDoubleField(Object object, Field field, double val)
      Set the value of a double field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance in which to set the field value
      field - the non-static field
      val - the double value to set
    • setObjectField

      public static void setObjectField(Object object, Field field, Object val)
      Set the value of a object field, ignoring visibility and bypassing security checks.
      Parameters:
      object - the object instance in which to set the field value
      field - the non-static field
      val - the value to set
    • setField

      public static void setField(Object object, Field field, Object val)
      Set the value of an object field, ignoring visibility and bypassing security checks, unboxing the passed value if necessary.
      Parameters:
      object - the object instance in which to set the field value
      field - the non-static field
      val - the value to set
    • getStaticIntField

      public static int getStaticIntField(Field field)
      Get the value of a static int field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      Returns:
      the int value of the static field
    • getStaticLongField

      public static long getStaticLongField(Field field)
      Get the value of a static long field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      Returns:
      the static long field
    • getStaticShortField

      public static short getStaticShortField(Field field)
      Get the value of a static short field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      Returns:
      the static short field
    • getStaticCharField

      public static char getStaticCharField(Field field)
      Get the value of a static char field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      Returns:
      the static char field
    • getStaticBooleanField

      public static boolean getStaticBooleanField(Field field)
      Get the value of a static boolean field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      Returns:
      the static boolean field
    • getStaticByteField

      public static byte getStaticByteField(Field field)
      Get the value of a static byte field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      Returns:
      the static byte field
    • getStaticFloatField

      public static float getStaticFloatField(Field field)
      Get the value of a static float field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      Returns:
      the static float field
    • getStaticDoubleField

      public static double getStaticDoubleField(Field field)
      Get the value of a static double field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      Returns:
      the static double field
    • getStaticObjectField

      public static Object getStaticObjectField(Field field)
      Get the value of a static object field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      Returns:
      the static object field
    • getStaticField

      public static Object getStaticField(Field field)
      Get the value of a static field, ignoring visibility and bypassing security checks, boxing the value if necessary.
      Parameters:
      field - the static field
      Returns:
      the static field
    • setStaticIntField

      public static void setStaticIntField(Field field, int val)
      Set the value of a static int field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      val - the int value to set
    • setStaticLongField

      public static void setStaticLongField(Field field, long val)
      Set the value of a static long field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      val - the long value to set
    • setStaticShortField

      public static void setStaticShortField(Field field, short val)
      Set the value of a static short field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      val - the short value to set
    • setStaticCharField

      public static void setStaticCharField(Field field, char val)
      Set the value of a static char field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      val - the char value to set
    • setStaticBooleanField

      public static void setStaticBooleanField(Field field, boolean val)
      Set the value of a static boolean field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      val - the boolean value to set
    • setStaticByteField

      public static void setStaticByteField(Field field, byte val)
      Set the value of a static byte field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      val - the byte value to set
    • setStaticFloatField

      public static void setStaticFloatField(Field field, float val)
      Set the value of a static float field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      val - the float value to set
    • setStaticDoubleField

      public static void setStaticDoubleField(Field field, double val)
      Set the value of a static double field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      val - the double value to set
    • setStaticObjectField

      public static void setStaticObjectField(Field field, Object val)
      Set the value of a static object field, ignoring visibility and bypassing security checks.
      Parameters:
      field - the static field
      val - the value to set
    • setStaticField

      public static void setStaticField(Field field, Object val)
      Set the value of a static field, ignoring visibility and bypassing security checks, unboxing the passed value if necessary.
      Parameters:
      field - the static field
      val - the value to set
    • invokeVoidMethod

      public static void invokeVoidMethod(Object object, Method method, Object... args)
      Invoke a non-static void-return-type method, ignoring visibility and bypassing security checks.
      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)
    • invokeIntMethod

      public static int invokeIntMethod(Object object, Method method, Object... args)
      Invoke a non-static int-return-type method, ignoring visibility and bypassing security checks.
      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 int return value
    • invokeLongMethod

      public static long invokeLongMethod(Object object, Method method, Object... args)
      Invoke a non-static long-return-type method, ignoring visibility and bypassing security checks.
      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 long return value
    • invokeShortMethod

      public static short invokeShortMethod(Object object, Method method, Object... args)
      Invoke a non-static short-return-type method, ignoring visibility and bypassing security checks.
      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 short return value
    • invokeCharMethod

      public static char invokeCharMethod(Object object, Method method, Object... args)
      Invoke a non-static char-return-type method, ignoring visibility and bypassing security checks.
      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 char return value
    • invokeBooleanMethod

      public static boolean invokeBooleanMethod(Object object, Method method, Object... args)
      Invoke a non-static boolean-return-type method, ignoring visibility and bypassing security checks.
      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 boolean return value
    • invokeByteMethod

      public static byte invokeByteMethod(Object object, Method method, Object... args)
      Invoke a non-static byte-return-type method, ignoring visibility and bypassing security checks.
      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 byte return value
    • invokeFloatMethod

      public static float invokeFloatMethod(Object object, Method method, Object... args)
      Invoke a non-static float-return-type method, ignoring visibility and bypassing security checks.
      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 float return value
    • invokeDoubleMethod

      public static double invokeDoubleMethod(Object object, Method method, Object... args)
      Invoke a non-static double-return-type method, ignoring visibility and bypassing security checks.
      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 double return value
    • invokeObjectMethod

      public static Object invokeObjectMethod(Object object, Method method, Object... args)
      Invoke a non-static Object-return-type method, ignoring visibility and bypassing security checks.
      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
    • invokeMethod

      public static Object invokeMethod(Object object, Method method, Object... args)
      Invoke a non-static Object-return-type method, ignoring visibility and bypassing security checks, 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)
    • invokeStaticVoidMethod

      public static void invokeStaticVoidMethod(Method method, Object... args)
      Invoke a static void-return-type method, ignoring visibility and bypassing security checks.
      Parameters:
      method - the static method
      args - the method arguments (or new Object[0] if there are no args)
    • invokeStaticIntMethod

      public static int invokeStaticIntMethod(Method method, Object... args)
      Invoke a static int-return-type method, ignoring visibility and bypassing security checks.
      Parameters:
      method - the static method
      args - the method arguments (or new Object[0] if there are no args)
      Returns:
      the int return value
    • invokeStaticLongMethod

      public static long invokeStaticLongMethod(Method method, Object... args)
      Invoke a static long-return-type method, ignoring visibility and bypassing security checks.
      Parameters:
      method - the static method
      args - the method arguments (or new Object[0] if there are no args)
      Returns:
      the long return value
    • invokeStaticShortMethod

      public static short invokeStaticShortMethod(Method method, Object... args)
      Invoke a static short-return-type method, ignoring visibility and bypassing security checks.
      Parameters:
      method - the static method
      args - the method arguments (or new Object[0] if there are no args)
      Returns:
      the short return value
    • invokeStaticCharMethod

      public static char invokeStaticCharMethod(Method method, Object... args)
      Invoke a static char-return-type method, ignoring visibility and bypassing security checks.
      Parameters:
      method - the static method
      args - the method arguments (or new Object[0] if there are no args)
      Returns:
      the char return value
    • invokeStaticBooleanMethod

      public static boolean invokeStaticBooleanMethod(Method method, Object... args)
      Invoke a static boolean-return-type method, ignoring visibility and bypassing security checks.
      Parameters:
      method - the static method
      args - the method arguments (or new Object[0] if there are no args)
      Returns:
      the boolean return value
    • invokeStaticByteMethod

      public static byte invokeStaticByteMethod(Method method, Object... args)
      Invoke a static byte-return-type method, ignoring visibility and bypassing security checks.
      Parameters:
      method - the static method
      args - the method arguments (or new Object[0] if there are no args)
      Returns:
      the byte return value
    • invokeStaticFloatMethod

      public static float invokeStaticFloatMethod(Method method, Object... args)
      Invoke a static float-return-type method, ignoring visibility and bypassing security checks.
      Parameters:
      method - the static method
      args - the method arguments (or new Object[0] if there are no args)
      Returns:
      the float return value
    • invokeStaticDoubleMethod

      public static double invokeStaticDoubleMethod(Method method, Object... args)
      Invoke a static double-return-type method, ignoring visibility and bypassing security checks.
      Parameters:
      method - the static method
      args - the method arguments (or new Object[0] if there are no args)
      Returns:
      the double return value
    • invokeStaticObjectMethod

      public static Object invokeStaticObjectMethod(Method method, Object... args)
      Invoke a static Object-return-type method, ignoring visibility and bypassing security checks.
      Parameters:
      method - the static method
      args - the method arguments (or new Object[0] if there are no args)
      Returns:
      the return value
    • invokeStaticMethod

      public static Object invokeStaticMethod(Method method, Object... args)
      Invoke a static Object-return-type method, ignoring visibility and bypassing security checks, 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)