Class FieldUtils
java.lang.Object
ushiosan.jvm_utilities.lang.reflection.FieldUtils
Class with functionalities for handling class fields with reflection
-
Method Summary
Modifier and TypeMethodDescriptionexcludeAll(String @NotNull ... methods) Generates a filter to capture elements that are different from the names passed as parameters.static FieldIt searches inside a class (also inside the classes it inherits) and identifies the one that is mentioned inside the parameters of the method.static FieldIt searches inside a class (also inside the classes it inherits) and identifies the one that is mentioned inside the parameters of the method.static FieldfindFieldObj(@NotNull Object obj, @NotNull String field) It searches inside a class (also inside the classes it inherits) and identifies the one that is mentioned inside the parameters of the method.static FieldfindFieldObj(@NotNull Object obj, @NotNull String field, @NotNull ReflectionOpts<Field> opts) It searches inside a class (also inside the classes it inherits) and identifies the one that is mentioned inside the parameters of the method.getAllClassFields(@NotNull Class<?> cls) Returns all defined and undefined fields of a class (for this, this method accesses the inheritance tree to get all these fields)getAllClassFields(@NotNull Class<?> cls, @NotNull ReflectionOpts<Field> opts) Returns all defined and undefined fields of a class (for this, this method accesses the inheritance tree to get all these fields)regexMultipleOf(boolean inverted, String @NotNull ... regex) Generates a filter to capture the items that meet that constraint.regexMultipleOf(boolean inverted, Pattern @NotNull ... patterns) Generates a filter to capture the items that meet that constraint.regexMultipleOf(String @NotNull ... regex) Generates a filter to capture the items that meet that constraint.regexMultipleOf(Pattern @NotNull ... patterns) Generates a filter to capture the items that meet that constraint.Generates a filter to capture the elements that meet said restrictionGenerates a filter to capture the elements that meet said restrictionGenerates a filter to capture the elements that meet said restrictionGenerates a filter to capture the elements that meet said restrictionstatic <T extends AccessibleObject>
@NotNull Predicate<T>requireAnnotations(boolean inverted, Class<? extends Annotation> @NotNull ... annotations) Generates a filter to check if the object contains one or more annotations.static <T extends AccessibleObject>
@NotNull Predicate<T>requireAnnotations(Class<? extends Annotation> @NotNull ... annotations) Generates a filter to check if the object contains one or more annotations.Generates a filter to check if a field is valid within a classGenerates a filter to check if a method is valid inside a class (also checks if it is a getter)
-
Method Details
-
findField
public static Field findField(@NotNull @NotNull Class<?> cls, @NotNull @NotNull String field, @NotNull @NotNull ReflectionOpts<Field> opts) throws NoSuchFieldException It searches inside a class (also inside the classes it inherits) and identifies the one that is mentioned inside the parameters of the method. Java handles fields independently for each class, and you cannot access a field that is inside another class, but inherits from it. That is why a trick is performed to perform such a task.- Parameters:
cls- the class you want to inspectfield- the field you want to search foropts- reflection options- Returns:
- the class field you want to access
- Throws:
NoSuchFieldException- error if such field does not exist in the entire inheritance tree of the class- See Also:
-
findField
public static Field findField(@NotNull @NotNull Class<?> cls, @NotNull @NotNull String field) throws NoSuchFieldException It searches inside a class (also inside the classes it inherits) and identifies the one that is mentioned inside the parameters of the method. Java handles fields independently for each class, and you cannot access a field that is inside another class, but inherits from it. That is why a trick is performed to perform such a task.- Parameters:
cls- the class you want to inspectfield- the field you want to search for- Returns:
- the class field you want to access
- Throws:
NoSuchFieldException- Error if such field does not exist in the entire inheritance tree of the class
-
findFieldObj
public static Field findFieldObj(@NotNull @NotNull Object obj, @NotNull @NotNull String field, @NotNull @NotNull ReflectionOpts<Field> opts) throws NoSuchFieldException It searches inside a class (also inside the classes it inherits) and identifies the one that is mentioned inside the parameters of the method. Java handles fields independently for each class, and you cannot access a field that is inside another class, but inherits from it. That is why a trick is performed to perform such a task.- Parameters:
obj- the object you want to inspectfield- the field you want to search foropts- reflection options- Returns:
- the class field you want to access
- Throws:
NoSuchFieldException- Error if such field does not exist in the entire inheritance tree of the class- See Also:
-
findFieldObj
public static Field findFieldObj(@NotNull @NotNull Object obj, @NotNull @NotNull String field) throws NoSuchFieldException It searches inside a class (also inside the classes it inherits) and identifies the one that is mentioned inside the parameters of the method. Java handles fields independently for each class, and you cannot access a field that is inside another class, but inherits from it. That is why a trick is performed to perform such a task.- Parameters:
obj- the object you want to inspectfield- the field you want to search for- Returns:
- the class field you want to access
- Throws:
NoSuchFieldException- Error if such field does not exist in the entire inheritance tree of the class
-
getAllClassFields
public static Field @NotNull [] getAllClassFields(@NotNull @NotNull Class<?> cls, @NotNull @NotNull ReflectionOpts<Field> opts) Returns all defined and undefined fields of a class (for this, this method accesses the inheritance tree to get all these fields)- Parameters:
cls- the class you want to inspectopts- reflection options- Returns:
- all valid class fields
- See Also:
-
getAllClassFields
Returns all defined and undefined fields of a class (for this, this method accesses the inheritance tree to get all these fields)- Parameters:
cls- the class you want to inspect- Returns:
- all valid class fields
-
regexOf
@NotNull public static <T extends Member> @NotNull Predicate<T> regexOf(@NotNull @NotNull String regex, boolean inverted) Generates a filter to capture the elements that meet said restriction- Type Parameters:
T- generic member type- Parameters:
regex- regular expression to applyinverted- option to perform the action inverted- Returns:
- the filter instance with the desired behavior
-
regexOf
@NotNull public static <T extends Member> @NotNull Predicate<T> regexOf(@NotNull @NotNull String regex) Generates a filter to capture the elements that meet said restriction- Type Parameters:
T- generic member type- Parameters:
regex- regular expression to apply- Returns:
- the filter instance with the desired behavior
-
regexOf
@Contract(pure=true) @NotNull public static <T extends Member> @NotNull Predicate<T> regexOf(@NotNull @NotNull Pattern pattern, boolean inverted) Generates a filter to capture the elements that meet said restriction- Type Parameters:
T- generic member type- Parameters:
pattern- regular expression object to applyinverted- option to perform the action inverted- Returns:
- the filter instance with the desired behavior
-
regexOf
@NotNull public static <T extends Member> @NotNull Predicate<T> regexOf(@NotNull @NotNull Pattern pattern) Generates a filter to capture the elements that meet said restriction- Type Parameters:
T- generic member type- Parameters:
pattern- regular expression object to apply- Returns:
- the filter instance with the desired behavior
-
regexMultipleOf
@NotNull public static <T extends Member> @NotNull Predicate<T> regexMultipleOf(boolean inverted, String @NotNull ... regex) Generates a filter to capture the items that meet that constraint. The only difference with theregexOf(String)method is that it contains multiple constraints.- Type Parameters:
T- generic member type- Parameters:
inverted- option to perform the action invertedregex- regular expression to apply- Returns:
- the filter instance with the desired behavior
-
regexMultipleOf
@NotNull public static <T extends Member> @NotNull Predicate<T> regexMultipleOf(String @NotNull ... regex) Generates a filter to capture the items that meet that constraint. The only difference with theregexOf(String)method is that it contains multiple constraints.- Type Parameters:
T- generic member type- Parameters:
regex- regular expression to apply- Returns:
- the filter instance with the desired behavior
-
regexMultipleOf
@NotNull public static <T extends Member> @NotNull Predicate<T> regexMultipleOf(boolean inverted, Pattern @NotNull ... patterns) Generates a filter to capture the items that meet that constraint. The only difference with theregexOf(String)method is that it contains multiple constraints.- Type Parameters:
T- generic member type- Parameters:
inverted- option to perform the action invertedpatterns- regular expression object to apply- Returns:
- the filter instance with the desired behavior
-
regexMultipleOf
@NotNull public static <T extends Member> @NotNull Predicate<T> regexMultipleOf(Pattern @NotNull ... patterns) Generates a filter to capture the items that meet that constraint. The only difference with theregexOf(String)method is that it contains multiple constraints.- Type Parameters:
T- generic member type- Parameters:
patterns- regular expression object to apply- Returns:
- the filter instance with the desired behavior
-
excludeAll
@NotNull public static <T extends Member> @NotNull Predicate<T> excludeAll(String @NotNull ... methods) Generates a filter to capture elements that are different from the names passed as parameters.- Type Parameters:
T- generic member type- Parameters:
methods- all the names you want to exclude- Returns:
- the filter instance with the desired behavior
-
validField
Generates a filter to check if a field is valid within a class- Returns:
- the filter instance with the desired behavior
-
validGetterMethod
Generates a filter to check if a method is valid inside a class (also checks if it is a getter)- Returns:
- the filter instance with the desired behavior
-
requireAnnotations
@SafeVarargs @NotNull public static <T extends AccessibleObject> @NotNull Predicate<T> requireAnnotations(boolean inverted, Class<? extends Annotation> @NotNull ... annotations) Generates a filter to check if the object contains one or more annotations.- Type Parameters:
T- generic accessible type- Parameters:
inverted- option to perform the action invertedannotations- the annotations that are required- Returns:
- the filter instance with the desired behavior
-
requireAnnotations
@SafeVarargs @NotNull public static <T extends AccessibleObject> @NotNull Predicate<T> requireAnnotations(Class<? extends Annotation> @NotNull ... annotations) Generates a filter to check if the object contains one or more annotations.- Type Parameters:
T- generic accessible type- Parameters:
annotations- the annotations that are required- Returns:
- the filter instance with the desired behavior
-