java.lang.Object
net.luis.utils.util.unsafe.reflection.ReflectionHelper
Helper class for reflection-related operations.
Provides methods to:
Provides methods to:
- check if a class has a constructor, method or field
- get constructors, methods and fields
- invoke constructors, methods and fields
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final org.apache.logging.log4j.Logger
The logger for this class.
Used for logging errors and warnings.private static final String
Constant for the system property 'reflection.exceptions.log'.private static final String
Constant for the system property 'reflection.exceptions.throw'.
If this property istrue
, exceptions will be thrown when an error occurs. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Private constructor to prevent instantiation.
This is a static helper class. -
Method Summary
Modifier and TypeMethodDescriptionGets the value from the field with the given name from the given class and instance.Gets the value of the given field from the given instance.
Exceptions will not be logged or thrown by default.static @Nullable Class
<?> getClassForName
(@NotNull String className) Gets the class for the given name.static <T> @NotNull Optional
<Constructor<T>> getConstructor
(@NotNull Class<T> clazz, Class<?> @Nullable ... parameters) Gets the constructor from the given class with the given parameters.
Exceptions will not be logged or thrown by default.Gets the field from the given class with the given name.
Exceptions will not be logged or thrown by default.Gets the method from the given class with the given name and parameters.
Exceptions will not be logged or thrown by default.getSimpleNames
(Class<?> @Nullable ... classes) Maps the given classes to their simple names.
If the given array is null or empty, an empty list will be returned.getSimpleNames
(Object @Nullable ... objects) Maps the given objects to their simple names by converting them to their classes first.
If the given array is null or empty, an empty list will be returned.private static void
handleException
(@NotNull Exception e) Handles the given exception.
If the system property 'reflection.exceptions.log' istrue
, the exception will be logged.
If the system property 'reflection.exceptions.throw' istrue
, the exception will be thrown.static boolean
hasConstructor
(@NotNull Class<?> clazz, Class<?> @Nullable ... parameters) Check if the given class has a constructor with the given parameters.
Exceptions which occur during reflection will be ignored.static <T> boolean
hasConstructor
(@NotNull Class<T> clazz, @Nullable Predicate<Constructor<T>> predicate, Class<?> @Nullable ... parameters) Check if the given class has a constructor with the given parameters.
Exceptions which occur during reflection will be ignored.static boolean
Check if the given class has a field with the given name.
Exceptions which occur during reflection will be ignored.static boolean
Check if the given class has a field with the given name.
Exceptions which occur during reflection will be ignored.static boolean
hasInterface
(@NotNull Class<?> clazz, @NotNull Class<?> iface) Check if the given class is an instance of the given interface.static boolean
Check if the given class has a method with the given name and parameters.
Exceptions which occur during reflection will be ignored.static boolean
hasMethod
(@NotNull Class<?> clazz, @NotNull String name, @Nullable Predicate<Method> predicate, Class<?> @Nullable ... parameters) Check if the given class has a method with the given name and parameters.
Exceptions which occur during reflection will be ignored.invoke
(@NotNull Class<?> clazz, @NotNull String name, @Nullable Object instance, Object @Nullable ... parameters) Invokes the method with the given name and parameters on the given instance.Invokes the given method with the given parameters on the given instance.static <T> @NotNull Optional
<T> newInstance
(@NotNull Class<T> clazz, Object @Nullable ... parameters) Creates a new instance from the given class and parameters.static <T> @NotNull Optional
<T> newInstance
(@NotNull Constructor<T> constructor, Object @Nullable ... parameters) Creates a new instance from the given constructor and parameters.
Exceptions will not be logged or thrown by default.static void
set
(@NotNull Class<?> clazz, @NotNull String name, @Nullable Object instance, @Nullable Object value) Sets the value of the field with the given name in the given class and instance to the given value.static void
Sets the value of the given field in the given instance to the given value.
Exceptions will not be logged or thrown by default.
-
Field Details
-
LOGGER
private static final org.apache.logging.log4j.Logger LOGGERThe logger for this class.
Used for logging errors and warnings. -
REFLECTION_EXCEPTIONS_THROW
Constant for the system property 'reflection.exceptions.throw'.
If this property istrue
, exceptions will be thrown when an error occurs.- See Also:
-
REFLECTION_EXCEPTIONS_LOG
Constant for the system property 'reflection.exceptions.log'.
If this property is
true
, exceptions will be logged when an error occurs.
If the property isfalse
, the logging is not completely disabled
only exceptions will not be logged.
- See Also:
-
-
Constructor Details
-
ReflectionHelper
private ReflectionHelper()Private constructor to prevent instantiation.
This is a static helper class.
-
-
Method Details
-
getClassForName
Gets the class for the given name.- Parameters:
className
- The name of the class- Returns:
- The class for the given name or null if the class could not be found
- Throws:
NullPointerException
- If the given class name is nullRuntimeException
- If the class could not be found and throwing exceptions is enabled
-
hasInterface
public static boolean hasInterface(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull Class<?> iface) Check if the given class is an instance of the given interface.- Parameters:
clazz
- The class to checkiface
- The interface to check- Returns:
- True if the given class is an instance of the given interface, otherwise false
- Throws:
NullPointerException
- If the given class or interface is null
-
getConstructor
@NotNull public static <T> @NotNull Optional<Constructor<T>> getConstructor(@NotNull @NotNull Class<T> clazz, Class<?> @Nullable ... parameters) Gets the constructor from the given class with the given parameters.
Exceptions will not be logged or thrown by default.- Type Parameters:
T
- The type of the class- Parameters:
clazz
- The class to get the constructor fromparameters
- The parameters of the constructor- Returns:
- The constructor as an optional or an empty optional if the constructor could not be found
- Throws:
NullPointerException
- If the given class is null- See Also:
-
hasConstructor
public static boolean hasConstructor(@NotNull @NotNull Class<?> clazz, Class<?> @Nullable ... parameters) Check if the given class has a constructor with the given parameters.
Exceptions which occur during reflection will be ignored.- Parameters:
clazz
- The class to checkparameters
- The parameters of the constructor- Returns:
- True if the given class has a constructor with the given parameters, otherwise false
- Throws:
NullPointerException
- If the given class is null- See Also:
-
hasConstructor
public static <T> boolean hasConstructor(@NotNull @NotNull Class<T> clazz, @Nullable @Nullable Predicate<Constructor<T>> predicate, Class<?> @Nullable ... parameters) Check if the given class has a constructor with the given parameters.
Exceptions which occur during reflection will be ignored.- Type Parameters:
T
- The type of the class- Parameters:
clazz
- The class to checkpredicate
- A predicate with an additional condition the constructor must meetparameters
- The parameters of the constructor- Returns:
- True if the given class has a constructor with the given parameters, otherwise false
- Throws:
NullPointerException
- If the given class is null
-
newInstance
@NotNull public static <T> @NotNull Optional<T> newInstance(@NotNull @NotNull Constructor<T> constructor, Object @Nullable ... parameters) Creates a new instance from the given constructor and parameters.
Exceptions will not be logged or thrown by default.- Type Parameters:
T
- The type of the instance- Parameters:
constructor
- The constructor to create the instance fromparameters
- The parameters of the constructor- Returns:
- The new instance as an optional or an empty optional if the instance could not be created
- Throws:
NullPointerException
- If the given constructor is null- See Also:
-
newInstance
@NotNull public static <T> @NotNull Optional<T> newInstance(@NotNull @NotNull Class<T> clazz, Object @Nullable ... parameters) Creates a new instance from the given class and parameters.- Type Parameters:
T
- The type of the instance- Parameters:
clazz
- The class to create the instance fromparameters
- The parameters of the constructor- Returns:
- The new instance as an optional or an empty optional if the instance could not be created
- Throws:
NullPointerException
- If the given class is nullIllegalStateException
- If no constructor for the given parameters could be found- See Also:
-
getMethod
@NotNull public static @NotNull Optional<Method> getMethod(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, Class<?> @Nullable ... parameters) Gets the method from the given class with the given name and parameters.
Exceptions will not be logged or thrown by default.- Parameters:
clazz
- The class to get the method fromname
- The name of the methodparameters
- The parameters of the method- Returns:
- The method as an optional or an empty optional if the method could not be found
- Throws:
NullPointerException
- If the given class or name is null- See Also:
-
hasMethod
public static boolean hasMethod(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, Class<?> @Nullable ... parameters) Check if the given class has a method with the given name and parameters.
Exceptions which occur during reflection will be ignored.- Parameters:
clazz
- The class to check for the methodname
- The name of the methodparameters
- The parameters of the method- Returns:
- True if the given class has a method with the given name and parameters, otherwise false
- Throws:
NullPointerException
- If the given class or name is null
-
hasMethod
public static boolean hasMethod(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @Nullable @Nullable Predicate<Method> predicate, Class<?> @Nullable ... parameters) Check if the given class has a method with the given name and parameters.
Exceptions which occur during reflection will be ignored.- Parameters:
clazz
- The class to check for the methodname
- The name of the methodpredicate
- A predicate with an additional condition the method must meetparameters
- The parameters of the method- Returns:
- True if the given class has a method with the given name and parameters, otherwise false
- Throws:
NullPointerException
- If the given class or name is null
-
invoke
@NotNull public static @NotNull Optional<Object> invoke(@NotNull @NotNull Method method, @Nullable @Nullable Object instance, Object @Nullable ... parameters) Invokes the given method with the given parameters on the given instance.
If the invocation is successful, the return value will be returned.
If the invocation fails, the return value will be null.
Exceptions will not be logged or thrown by default.
- Parameters:
method
- The method to invokeinstance
- The instance to invoke the method onparameters
- The parameters of the method- Returns:
- The return value of the method as an optional or an empty optional if the invocation failed
- Throws:
NullPointerException
- If the given method is null- See Also:
-
invoke
@NotNull public static @NotNull Optional<Object> invoke(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @Nullable @Nullable Object instance, Object @Nullable ... parameters) Invokes the method with the given name and parameters on the given instance.
If the invocation is successful, the return value will be returned.
If the invocation fails, the return value will be null.
- Parameters:
clazz
- The class which contains the methodname
- The name of the methodinstance
- The instance to invoke the method onparameters
- The parameters of the method- Returns:
- The return value of the method as an optional or an empty optional if the invocation failed
- Throws:
NullPointerException
- If the given class or name is nullIllegalStateException
- If no method for the given name and parameters could be found- See Also:
-
getField
@NotNull public static @NotNull Optional<Field> getField(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name) Gets the field from the given class with the given name.
Exceptions will not be logged or thrown by default.- Parameters:
clazz
- The class to get the field fromname
- The name of the field- Returns:
- The field as an optional or an empty optional if the field could not be found
- Throws:
NullPointerException
- If the given class or name is null- See Also:
-
hasField
Check if the given class has a field with the given name.
Exceptions which occur during reflection will be ignored.- Parameters:
clazz
- The class to check for the fieldname
- The name of the field- Returns:
- True if the given class has a field with the given name, otherwise false
- Throws:
NullPointerException
- If the given class or name is null
-
hasField
public static boolean hasField(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @Nullable @Nullable Predicate<Field> predicate) Check if the given class has a field with the given name.
Exceptions which occur during reflection will be ignored.- Parameters:
clazz
- The class to check for the fieldname
- The name of the fieldpredicate
- A predicate with an additional condition the field must meet- Returns:
- True if the given class has a field with the given name, otherwise false
- Throws:
NullPointerException
- If the given class or name is null
-
get
@NotNull public static @NotNull Optional<Object> get(@NotNull @NotNull Field field, @Nullable @Nullable Object instance) Gets the value of the given field from the given instance.
Exceptions will not be logged or thrown by default.- Parameters:
field
- The field to get the value frominstance
- The instance to get the value from- Returns:
- The value of the field as an optional or an empty optional if the value could not be determined
- Throws:
NullPointerException
- If the given field is null- See Also:
-
get
@NotNull public static @NotNull Optional<Object> get(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @Nullable @Nullable Object instance) Gets the value from the field with the given name from the given class and instance.- Parameters:
clazz
- The class to get the field fromname
- The name of the fieldinstance
- The instance to get the value from- Returns:
- The value of the field as an optional or an empty optional if the value could not be determined
- Throws:
NullPointerException
- If the given class or name is nullIllegalStateException
- If no field with the given name could be found- See Also:
-
set
public static void set(@NotNull @NotNull Field field, @Nullable @Nullable Object instance, @Nullable @Nullable Object value) Sets the value of the given field in the given instance to the given value.
Exceptions will not be logged or thrown by default.- Parameters:
field
- The field to set the value toinstance
- The instance to set the value tovalue
- The value to set- Throws:
NullPointerException
- If the given field is null- See Also:
-
set
public static void set(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @Nullable @Nullable Object instance, @Nullable @Nullable Object value) Sets the value of the field with the given name in the given class and instance to the given value.- Parameters:
clazz
- The class to set the field inname
- The name of the fieldinstance
- The instance to set the value tovalue
- The value to set- Throws:
NullPointerException
- If the given class or name is nullIllegalStateException
- If no field with the given name could be found- See Also:
-
handleException
Handles the given exception.
If the system property 'reflection.exceptions.log' istrue
, the exception will be logged.
If the system property 'reflection.exceptions.throw' istrue
, the exception will be thrown.- Parameters:
e
- The exception to handle
-
getSimpleNames
Maps the given classes to their simple names.
If the given array is null or empty, an empty list will be returned.- Parameters:
classes
- The list of classes- Returns:
- A list containing the simple names
-
getSimpleNames
Maps the given objects to their simple names by converting them to their classes first.
If the given array is null or empty, an empty list will be returned.- Parameters:
objects
- The list of objects- Returns:
- A list containing the simple names
-