Class Reflect

java.lang.Object
one.tranic.t.utils.Reflect

public class Reflect extends Object
Utility class containing reflection-related methods for manipulating and inspecting classes and fields.
  • Constructor Details

    • Reflect

      public Reflect()
  • Method Details

    • enforceType

      public static void enforceType(@NotNull @NotNull Class<?> field, @NotNull @NotNull Class<?> value)
    • setNotNullStaticField

      public static void setNotNullStaticField(@NotNull @NotNull Class<?> targetClass, @NotNull @NotNull String fieldName, @NotNull @NotNull Object value) throws NoSuchFieldException, IllegalAccessException
      Sets the value of a static field in the specified class if the field's current value is null.
      Parameters:
      targetClass - the class containing the static field. Must not be null.
      fieldName - the name of the static field to set. Must not be null.
      value - the value to assign to the static field. Must not be null.
      Throws:
      NoSuchFieldException - if the field with the specified name does not exist in the class.
      IllegalAccessException - if the field cannot be accessed or modified due to security restrictions.
      IllegalArgumentException - if the field is not static.
    • setNotNullStaticFieldL

      public static void setNotNullStaticFieldL(@NotNull @NotNull Class<?> targetClass, @NotNull @NotNull String fieldName, @NotNull @NotNull Object value) throws NoSuchFieldException, IllegalAccessException
      Sets a non-null static field on the specified class with the provided value. This method ensures that the field being set is not null and matches the given field name and type. If the field does not exist or cannot be accessed, an exception is thrown.
      Parameters:
      targetClass - the class containing the static field to be set
      fieldName - the name of the static field to be modified
      value - the value to assign to the static field, which must not be null
      Throws:
      NoSuchFieldException - if the specified field does not exist
      IllegalAccessException - if the field is not accessible
    • setNotNullStaticFieldL

      public static void setNotNullStaticFieldL(@NotNull MethodHandles.Lookup lookup, @NotNull @NotNull Class<?> targetClass, @NotNull @NotNull String fieldName, @NotNull @NotNull Object value) throws NoSuchFieldException, IllegalAccessException
      Sets a static field of the specified class to a non-null value if it is currently null.
      Parameters:
      lookup - the MethodHandles.Lookup instance to perform reflective operations
      targetClass - the class containing the static field to set
      fieldName - the name of the static field
      value - the non-null value to set the field to
      Throws:
      NoSuchFieldException - if the specified field does not exist
      IllegalAccessException - if accessing the field is not permitted
    • setStaticField

      public static void setStaticField(@NotNull @NotNull Class<?> targetClass, @NotNull @NotNull String fieldName, @NotNull @NotNull Object value) throws NoSuchFieldException, IllegalAccessException
      Sets the value of a static field in the specified class using reflection.
      Parameters:
      targetClass - the class containing the static field. Must not be null.
      fieldName - the name of the static field to set. Must not be null.
      value - the value to assign to the static field. Must not be null.
      Throws:
      NoSuchFieldException - if the field with the specified name does not exist in the class.
      IllegalAccessException - if the field cannot be accessed or modified due to security restrictions.
      IllegalArgumentException - if the field is not static.
    • setStaticFieldL

      public static void setStaticFieldL(@NotNull @NotNull Class<?> targetClass, @NotNull @NotNull String fieldName, @NotNull @NotNull Object value) throws NoSuchFieldException, IllegalAccessException
      Sets the value of a static field in the specified class using reflection.
      Parameters:
      targetClass - the class containing the static field. Must not be null.
      fieldName - the name of the static field to modify. Must not be null.
      value - the value to assign to the static field. Must not be null.
      Throws:
      NoSuchFieldException - if the field with the specified name does not exist in the class.
      IllegalAccessException - if the field cannot be accessed or modified due to security restrictions.
    • setStaticFieldL

      public static void setStaticFieldL(@NotNull MethodHandles.Lookup lookup, @NotNull @NotNull Class<?> targetClass, @NotNull @NotNull String fieldName, @NotNull @NotNull Object value) throws NoSuchFieldException, IllegalAccessException
      Sets the value of a static field in the specified class.
      Parameters:
      lookup - the MethodHandles.Lookup object used to access the field
      targetClass - the Class object representing the class containing the static field
      fieldName - the name of the static field to be set
      value - the value to set to the static field
      Throws:
      NoSuchFieldException - if the specified field does not exist in the target class
      IllegalAccessException - if access to the field is denied
    • setFieldL

      public static void setFieldL(@NotNull @NotNull Object targetObject, @NotNull @NotNull String fieldName, @NotNull @NotNull Object value) throws NoSuchFieldException, IllegalAccessException
      Sets the value of a field in an object instance using reflection.
      Parameters:
      targetObject - the object instance containing the field to modify. Must not be null.
      fieldName - the name of the field to modify. Must not be null.
      value - the value to assign to the field. Must not be null.
      Throws:
      NoSuchFieldException - if the field with the specified name does not exist in the class.
      IllegalAccessException - if the field cannot be accessed or modified due to security restrictions.
    • setFieldL

      public static void setFieldL(@NotNull MethodHandles.Lookup lookup, @NotNull @NotNull Object targetObject, @NotNull @NotNull String fieldName, @NotNull @NotNull Object value) throws NoSuchFieldException, IllegalAccessException
      Sets the value of the specified field on the given target object.
      Parameters:
      lookup - the MethodHandles.Lookup object used to perform the operation
      targetObject - the target object whose field value is to be set
      fieldName - the name of the field to be set
      value - the new value to be assigned to the field
      Throws:
      NoSuchFieldException - if the specified field does not exist
      IllegalAccessException - if the field is not accessible
    • setField

      public static void setField(@NotNull @NotNull Object targetObject, @NotNull @NotNull String fieldName, @NotNull @NotNull Object value) throws NoSuchFieldException, IllegalAccessException
      Sets the value of a specified field in the given target object.
      Parameters:
      targetObject - the object whose field is to be modified. Must not be null.
      fieldName - the name of the field to modify. Must not be null.
      value - the new value to set for the specified field. Must not be null.
      Throws:
      NoSuchFieldException - if the specified field is not found in the target object's class.
      IllegalAccessException - if the field cannot be accessed or modified due to security restrictions.
    • getStaticField

      public static Object getStaticField(@NotNull @NotNull Class<?> targetClass, @NotNull @NotNull String fieldName) throws IllegalAccessException, NoSuchFieldException
      Retrieves the value of a static field from the specified class using reflection.
      Parameters:
      targetClass - the class containing the static field. Must not be null.
      fieldName - the name of the static field to retrieve. Must not be null.
      Returns:
      the value of the specified static field.
      Throws:
      NoSuchFieldException - if the field with the specified name does not exist in the class.
      IllegalAccessException - if the field cannot be accessed due to security restrictions.
      IllegalArgumentException - if the field is not static.
    • getField

      public static Object getField(@NotNull @NotNull Object target, @NotNull @NotNull String fieldName, @NotNull @NotNull Object arg) throws NoSuchFieldException, IllegalAccessException
      Retrieves the value of a specified field from an object instance using reflection.
      Parameters:
      target - the object instance containing the field. Must not be null.
      fieldName - the name of the field to retrieve. Must not be null.
      arg - the instance on which to retrieve the value. Must not be null.
      Returns:
      the value of the specified field.
      Throws:
      NoSuchFieldException - if the field with the specified name does not exist in the object's class.
      IllegalAccessException - if the field cannot be accessed due to security restrictions.
    • getField

      public static Object getField(@NotNull @NotNull Object targetObject, @NotNull @NotNull String fieldName, @NotNull @NotNull Class<?> fieldType, @NotNull @NotNull Object arg) throws NoSuchFieldException, IllegalAccessException
      Retrieves the value of the specified field in the given target object using reflection.
      Parameters:
      targetObject - the object instance from which the field value is retrieved. Must not be null.
      fieldName - the name of the field to retrieve. Must not be null.
      fieldType - the type of the field to retrieve. Must not be null.
      arg - an argument specifying additional context for accessing the field. Must not be null.
      Returns:
      the value of the specified field.
      Throws:
      NoSuchFieldException - if the field with the specified name does not exist in the class.
      IllegalAccessException - if the field cannot be accessed due to security restrictions.
    • getField

      public static Object getField(@NotNull MethodHandles.Lookup lookup, @NotNull @NotNull Object targetObject, @NotNull @NotNull String fieldName, @NotNull @NotNull Class<?> fieldType, @NotNull @NotNull Object arg) throws NoSuchFieldException, IllegalAccessException
      Retrieves the value of a field in an object instance using MethodHandles for reflection.
      Parameters:
      lookup - a MethodHandles.Lookup instance for accessing the field. Must not be null.
      targetObject - the object instance containing the field to retrieve. Must not be null.
      fieldName - the name of the field to retrieve. Must not be null.
      fieldType - the type of the field to retrieve. Must not be null.
      arg - the argument required by the field getter. Must not be null.
      Returns:
      the value of the specified field.
      Throws:
      NoSuchFieldException - if the field with the specified name does not exist in the class.
      IllegalAccessException - if the field cannot be accessed due to security restrictions.
    • getStaticField

      public static Object getStaticField(@NotNull @NotNull Class<?> targetClass, @NotNull @NotNull String fieldName, @NotNull @NotNull Class<?> fieldType) throws NoSuchFieldException, IllegalAccessException
      Retrieves the value of a static field from the specified class using reflection and method handles.
      Parameters:
      targetClass - the class containing the static field. Must not be null.
      fieldName - the name of the static field to retrieve. Must not be null.
      fieldType - the type of the static field to retrieve. Must not be null.
      Returns:
      the value of the specified static field.
      Throws:
      NoSuchFieldException - if the field with the specified name does not exist in the class.
      IllegalAccessException - if the field cannot be accessed due to security restrictions.
    • getStaticField

      public static Object getStaticField(@NotNull MethodHandles.Lookup lookup, @NotNull @NotNull Class<?> targetClass, @NotNull @NotNull String fieldName, @NotNull @NotNull Class<?> fieldType) throws NoSuchFieldException, IllegalAccessException
      Retrieves the value of a static field from the specified class using MethodHandles.
      Parameters:
      lookup - a MethodHandles.Lookup instance for accessing the field. Must not be null.
      targetClass - the class containing the static field. Must not be null.
      fieldName - the name of the static field to retrieve. Must not be null.
      fieldType - the type of the static field. Must not be null.
      Returns:
      the value of the specified static field.
      Throws:
      NoSuchFieldException - if the field with the specified name does not exist in the class.
      IllegalAccessException - if the field cannot be accessed due to security restrictions.
    • getLookup

      public static MethodHandles.Lookup getLookup(@NotNull @NotNull Class<?> targetClass) throws IllegalAccessException
      Retrieves a MethodHandles.Lookup instance for performing operations on the specified class.
      Parameters:
      targetClass - the class for which the MethodHandles.Lookup instance is created. Must not be null.
      Returns:
      a MethodHandles.Lookup instance for accessing private or otherwise inaccessible members of the specified class.
      Throws:
      IllegalAccessException - if the attempt to create the MethodHandles.Lookup instance violates security rules.
    • hasClass

      public static boolean hasClass(@NotNull @NotNull String className)
      Checks whether a class with the specified fully qualified name exists in the classpath.
      Parameters:
      className - the fully qualified name of the class to check.
      Returns:
      true if the class exists; false otherwise.
    • getClass

      @Nullable public static @Nullable Class<?> getClass(String className)
      Retrieves a Class object associated with the fully qualified name of a class.
      Parameters:
      className - the fully qualified name of the class to retrieve.
      Returns:
      the Class object for the given name, or null if the class cannot be found.