java.lang.Object
net.luis.utils.util.unsafe.reflection.ReflectionUtils
Utility class for reflection-related operations.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Private constructor to prevent instantiation.
This is a static helper class. -
Method Summary
Modifier and TypeMethodDescriptionprivate static @NotNull Object
Finds the value for the given parameter.
The value will be searched by its type, and if the type is ambiguous, by its name.
An exception will be thrown if no value for the parameter could be found.static Object @NotNull []
findParameters
(@NotNull Executable executable, Object @Nullable ... values) Finds the parameters in the correct order for the given executable.
The parameters will be searched by their type, and if the type is ambiguous, by their name.static Object @NotNull []
findParameters
(@NotNull Executable executable, @Nullable List<Pair<Object, String>> values) Finds the parameters in the correct order for the given executable.
The parameters will be searched by their type, and if the type is ambiguous, by their name.getAnnotatedField
(@NotNull Class<?> clazz, @NotNull String name, @NotNull Class<? extends Annotation> annotation) Gets the field with the given name from the given class which is annotated with the given annotation.
If the field does not exist or is not annotated with the given annotation, an empty optional will be returned.getAnnotatedFields
(@NotNull Class<?> clazz, @NotNull Class<? extends Annotation> annotation) Gets all fields from the given class that are annotated with the given annotation.getAnnotatedMethod
(@NotNull Class<?> clazz, @NotNull String name, @NotNull Class<? extends Annotation> annotation, Class<?> @Nullable ... parameters) Gets the method with the given name from the given class which is annotated with the given annotation.getAnnotatedMethods
(@NotNull Class<?> clazz, @NotNull Class<? extends Annotation> annotation) Gets all methods from the given class that are annotated with the given annotation.getFieldsForType
(@NotNull Class<?> clazz, @Nullable Class<?> type) Gets all fields from the given class for the given type.
If the given type is null, an empty list will be returned.getMethodForName
(@NotNull Class<?> clazz, @Nullable String name) Gets the method with the given name from the given class.
If the given name is null or there are multiple methods with the given name, an empty optional will be returned.getMethodsForName
(@NotNull Class<?> clazz, @Nullable String name) Gets all methods of the given class for the given name (case-sensitive).
If the given name is null, all methods of the given class will be returned.
The methods will be returned in the order of their parameter count.static @NotNull String
getRawName
(@NotNull Method method, String @Nullable ... prefixes) Gets the raw name of the given method.
The raw name is the name of the method without prefixes like "get", "set", "is" or "has".
-
Constructor Details
-
ReflectionUtils
private ReflectionUtils()Private constructor to prevent instantiation.
This is a static helper class.
-
-
Method Details
-
getRawName
@NotNull public static @NotNull String getRawName(@NotNull @NotNull Method method, String @Nullable ... prefixes) Gets the raw name of the given method.
The raw name is the name of the method without prefixes like "get", "set", "is" or "has".- Parameters:
method
- The method to get the raw name ofprefixes
- Additional prefixes to remove- Returns:
- The raw name of the given method or
Method.getName()
as fallback - Throws:
NullPointerException
- If the given method is null
-
getAnnotatedMethods
@NotNull public static @NotNull List<Method> getAnnotatedMethods(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull Class<? extends Annotation> annotation) Gets all methods from the given class that are annotated with the given annotation.- Parameters:
clazz
- The class in which the methods should beannotation
- The annotation which should be present on the methods- Returns:
- A list of all methods
- Throws:
NullPointerException
- If the given class or annotation is null
-
getAnnotatedMethod
@NotNull public static @NotNull Optional<Method> getAnnotatedMethod(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @NotNull @NotNull Class<? extends Annotation> annotation, Class<?> @Nullable ... parameters) Gets the method with the given name from the given class which is annotated with the given annotation.- Parameters:
clazz
- The class in which the method should bename
- The name of the methodannotation
- The annotation which should be present on the methodparameters
- The parameters of the method- Returns:
- An optional containing the method or an empty optional if the method was not found
- Throws:
NullPointerException
- If the given class, name or annotation is null
-
getMethodsForName
@NotNull public static @NotNull List<Method> getMethodsForName(@NotNull @NotNull Class<?> clazz, @Nullable @Nullable String name) Gets all methods of the given class for the given name (case-sensitive).
If the given name is null, all methods of the given class will be returned.
The methods will be returned in the order of their parameter count.- Parameters:
clazz
- The class to get the methods ofname
- The name of the methods to get- Returns:
- A list of all methods from the given class for the given name
- Throws:
NullPointerException
- If the given class is null
-
getMethodForName
@NotNull public static @NotNull Optional<Method> getMethodForName(@NotNull @NotNull Class<?> clazz, @Nullable @Nullable String name) Gets the method with the given name from the given class.
If the given name is null or there are multiple methods with the given name, an empty optional will be returned.- Parameters:
clazz
- The class to get the method fromname
- The name of the method- Returns:
- An optional containing the method or an empty optional if the method was not found
- Throws:
NullPointerException
- If the given class is null
-
findParameters
public static Object @NotNull [] findParameters(@NotNull @NotNull Executable executable, Object @Nullable ... values) Finds the parameters in the correct order for the given executable.
The parameters will be searched by their type, and if the type is ambiguous, by their name.
In the case the compiler has not included the parameter names,
and the types are ambiguous, an exception will be thrown.
If the given executable has no parameters, an empty array will be returned.
- Parameters:
executable
- The executable to find the parameters forvalues
- The possible values for the parameters- Returns:
- An array of the found parameters in the correct order
- Throws:
NullPointerException
- If the given executable is nullIllegalArgumentException
- If the type of the parameter is ambiguous and the name is not presentIllegalArgumentException
- If no value for a parameter could be found- See Also:
-
findParameters
public static Object @NotNull [] findParameters(@NotNull @NotNull Executable executable, @Nullable @Nullable List<Pair<Object, String>> values) Finds the parameters in the correct order for the given executable.
The parameters will be searched by their type, and if the type is ambiguous, by their name.
In the case the compiler has not included the parameter names,
and the types are ambiguous, an exception will be thrown.
If the given executable has no parameters, an empty array will be returned.
- Parameters:
executable
- The executable to find the parameters forvalues
- The possible values for the parameters- Returns:
- An array of the found parameters in the correct order
- Throws:
NullPointerException
- If the given executable is nullIllegalArgumentException
- If the type of the parameter is ambiguous and the name is not presentIllegalArgumentException
- If no value for a parameter could be found- See Also:
-
getAnnotatedFields
@NotNull public static @NotNull List<Field> getAnnotatedFields(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull Class<? extends Annotation> annotation) Gets all fields from the given class that are annotated with the given annotation.- Parameters:
clazz
- The class in which the fields should beannotation
- The annotation which should be present on the fields- Returns:
- A list of all fields
- Throws:
NullPointerException
- If the given class or annotation is null
-
getAnnotatedField
@NotNull public static @NotNull Optional<Field> getAnnotatedField(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String name, @NotNull @NotNull Class<? extends Annotation> annotation) Gets the field with the given name from the given class which is annotated with the given annotation.
If the field does not exist or is not annotated with the given annotation, an empty optional will be returned.- Parameters:
clazz
- The class in which the field should bename
- The name of the fieldannotation
- The annotation which should be present on the field- Returns:
- An optional containing the field or an empty optional if the field was not found
- Throws:
NullPointerException
- If the given class, name or annotation is null
-
getFieldsForType
@NotNull public static @NotNull List<Field> getFieldsForType(@NotNull @NotNull Class<?> clazz, @Nullable @Nullable Class<?> type) Gets all fields from the given class for the given type.
If the given type is null, an empty list will be returned.- Parameters:
clazz
- The class to get the fields oftype
- The type of the fields to get- Returns:
- A list of all fields from the given class for the given type
- Throws:
NullPointerException
- If the given class is null
-
findParameter
@NotNull private static @NotNull Object findParameter(@NotNull @NotNull Parameter parameter, @NotNull @NotNull List<Pair<Object, String>> values) Finds the value for the given parameter.
The value will be searched by its type, and if the type is ambiguous, by its name.
An exception will be thrown if no value for the parameter could be found.- Parameters:
parameter
- The parameter to find the value forvalues
- The possible values for the parameter- Returns:
- The value found for the given parameter
- Throws:
NullPointerException
- If the given parameter or values are nullIllegalArgumentException
- If the type of the parameter is ambiguous and the name is not presentIllegalStateException
- If no value for the parameter could be found
-