Package one.tranic.t.utils
Class Reflect
java.lang.Object
one.tranic.t.utils.Reflect
Utility class containing reflection-related methods for manipulating and inspecting classes and fields.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
enforceType
(@NotNull Class<?> field, @NotNull Class<?> value) static @Nullable Class<?>
Retrieves aClass
object associated with the fully qualified name of a class.static Object
getField
(@NotNull Object targetObject, @NotNull String fieldName, @NotNull Class<?> fieldType, @NotNull Object arg) Retrieves the value of the specified field in the given target object using reflection.static Object
Retrieves the value of a specified field from an object instance using reflection.static Object
getField
(MethodHandles.Lookup lookup, @NotNull Object targetObject, @NotNull String fieldName, @NotNull Class<?> fieldType, @NotNull Object arg) Retrieves the value of a field in an object instance usingMethodHandles
for reflection.static MethodHandles.Lookup
Retrieves aMethodHandles.Lookup
instance for performing operations on the specified class.static Object
getStaticField
(@NotNull Class<?> targetClass, @NotNull String fieldName) Retrieves the value of a static field from the specified class using reflection.static Object
getStaticField
(@NotNull Class<?> targetClass, @NotNull String fieldName, @NotNull Class<?> fieldType) Retrieves the value of a static field from the specified class using reflection and method handles.static Object
getStaticField
(MethodHandles.Lookup lookup, @NotNull Class<?> targetClass, @NotNull String fieldName, @NotNull Class<?> fieldType) Retrieves the value of a static field from the specified class usingMethodHandles
.static boolean
Checks whether a class with the specified fully qualified name exists in the classpath.static void
Sets the value of a specified field in the given target object.static void
Sets the value of a field in an object instance using reflection.static void
setFieldL
(MethodHandles.Lookup lookup, @NotNull Object targetObject, @NotNull String fieldName, @NotNull Object value) Sets the value of the specified field on the given target object.static void
setNotNullStaticField
(@NotNull Class<?> targetClass, @NotNull String fieldName, @NotNull Object value) Sets the value of a static field in the specified class if the field's current value is null.static void
setNotNullStaticFieldL
(@NotNull Class<?> targetClass, @NotNull String fieldName, @NotNull Object value) Sets a non-null static field on the specified class with the provided value.static void
setNotNullStaticFieldL
(MethodHandles.Lookup lookup, @NotNull Class<?> targetClass, @NotNull String fieldName, @NotNull Object value) Sets a static field of the specified class to a non-null value if it is currently null.static void
setStaticField
(@NotNull Class<?> targetClass, @NotNull String fieldName, @NotNull Object value) Sets the value of a static field in the specified class using reflection.static void
setStaticFieldL
(@NotNull Class<?> targetClass, @NotNull String fieldName, @NotNull Object value) Sets the value of a static field in the specified class using reflection.static void
setStaticFieldL
(MethodHandles.Lookup lookup, @NotNull Class<?> targetClass, @NotNull String fieldName, @NotNull Object value) Sets the value of a static field in the specified class.
-
Constructor Details
-
Reflect
public Reflect()
-
-
Method Details
-
enforceType
-
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 setfieldName
- the name of the static field to be modifiedvalue
- the value to assign to the static field, which must not be null- Throws:
NoSuchFieldException
- if the specified field does not existIllegalAccessException
- 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 operationstargetClass
- the class containing the static field to setfieldName
- the name of the static fieldvalue
- the non-null value to set the field to- Throws:
NoSuchFieldException
- if the specified field does not existIllegalAccessException
- 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 fieldtargetClass
- the Class object representing the class containing the static fieldfieldName
- the name of the static field to be setvalue
- the value to set to the static field- Throws:
NoSuchFieldException
- if the specified field does not exist in the target classIllegalAccessException
- 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
- theMethodHandles.Lookup
object used to perform the operationtargetObject
- the target object whose field value is to be setfieldName
- the name of the field to be setvalue
- the new value to be assigned to the field- Throws:
NoSuchFieldException
- if the specified field does not existIllegalAccessException
- 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 usingMethodHandles
for reflection.- Parameters:
lookup
- aMethodHandles.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 usingMethodHandles
.- Parameters:
lookup
- aMethodHandles.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 aMethodHandles.Lookup
instance for performing operations on the specified class.- Parameters:
targetClass
- the class for which theMethodHandles.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 theMethodHandles.Lookup
instance violates security rules.
-
hasClass
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
Retrieves aClass
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, ornull
if the class cannot be found.
-