Class ReflectionUtils

java.lang.Object
com.thecsdev.common.util.ReflectionUtils

public final class ReflectionUtils extends Object
Utility methods for java.lang.reflect.
  • Method Details

    • getAllDeclaredFields

      @NotNull public static final @NotNull Field[] getAllDeclaredFields(@Nullable @Nullable Class<?> clazz)
      Retrieves all declared Fields from the specified Class and its superclasses, up to (but not including) Object. This method ensures that no duplicate fields are included in the result.
      Parameters:
      clazz - The Class from which to retrieve Fields.
      Returns:
      An array of Field objects representing all declared fields in the Class hierarchy, without duplicates. The Fields also include private, protected, and package-private members of the classes.
    • constructorMatchesArgs

      public static boolean constructorMatchesArgs(Constructor<?> constructor, Object... args)
      Compares a Constructor's parameter types against an Object array, checking if the arguments match the constructor.
      Parameters:
      constructor - The Constructor to check.
      args - The arguments for said Constructor.
      Returns:
      The result of the comparison. true if it's a match.
      API Note:
      Depends on Guava - com.google.*.
    • createClassInstance

      public static <T> T createClassInstance(Class<T> type, Object... params) throws NullPointerException
      Creates an instance of a Class using the first constructor that matches the given parameters. Pass no parameters to use a constructor that does not take any arguments. Non-primitive argument types may be assigned null.

      The constructor must also be accessible. If a matching and accessible constructor cannot be found, null is returned.
      Type Parameters:
      T - The Class type.
      Parameters:
      type - The Class type.
      params - The Class constructor parameters.
      Throws:
      NullPointerException - If an argument is null.
      API Note:
      Depends on Guava - com.google.*.
    • isMethodOverridden

      @Experimental public static boolean isMethodOverridden(Class<?> topClass, String methodName, Class<?> returnType, Class<?>... paramTypes) throws NullPointerException
      Checks if the given Class overrides a method with the specified signature.
      Parameters:
      topClass - Class to start checking from.
      methodName - Name of the method.
      returnType - Expected return type.
      paramTypes - Expected parameter types.
      Returns:
      true if the method is overridden in the Class hierarchy, false otherwise.
      Throws:
      NullPointerException - If an argument is null.
      API Note:
      This does not account for interfaces yet!