Interface FieldUtils
-
- All Superinterfaces:
Utils
public interface FieldUtils extends Utils
A utility interface for working with fields in the context of Java annotation processing.This interface provides a collection of static methods to retrieve, filter, and inspect
fields
within a class or interface. It supports operations such as:- Retrieving fields by name
- Finding fields with specific modifiers (e.g., static, public)
- Filtering fields based on custom predicates
- Checking if a field is an enum constant or a non-static field
- Getting all declared or non-static fields from a type or element
Example Usage
// Get a specific field by name VariableElement field = FieldUtils.getDeclaredField(element, "myField"); // Get all non-static fields List<VariableElement> nonStaticFields = FieldUtils.getNonStaticFields(type); // Find a field by name in the current class and its superclasses VariableElement fieldInHierarchy = FieldUtils.findField(type, "myField"); // Check if a field is non-static boolean isNonStatic = FieldUtils.isNonStaticField(field); // Get all fields, including those from superclasses List<VariableElement> allFields = FieldUtils.getAllDeclaredFields(element);
- Since:
- 1.0.0
- Author:
- Mercy
-
-
Method Summary
Static Methods Modifier and Type Method Description static boolean
equalsFieldName(javax.lang.model.element.VariableElement field, java.lang.CharSequence fieldName)
Checks if the simple name of the given field matches the specified field name.static java.util.List<javax.lang.model.element.VariableElement>
filterDeclaredFields(javax.lang.model.type.TypeMirror type, boolean includeHierarchicalTypes, java.util.function.Predicate<? super javax.lang.model.element.VariableElement>... fieldFilters)
Filters and retrieves the declared fields from the given type based on the provided criteria.static java.util.List<javax.lang.model.element.VariableElement>
findAllDeclaredFields(javax.lang.model.element.Element element, java.util.function.Predicate<? super javax.lang.model.element.VariableElement>... fieldFilters)
Retrieves all declared fields from the given element, including those from hierarchical types (e.g., superclasses), after applying the provided filters.static java.util.List<javax.lang.model.element.VariableElement>
findAllDeclaredFields(javax.lang.model.type.TypeMirror type, java.util.function.Predicate<? super javax.lang.model.element.VariableElement>... fieldFilters)
Retrieves all declared fields from the given type, including those from hierarchical types (e.g., superclasses), after applying the provided filters.static java.util.List<javax.lang.model.element.VariableElement>
findDeclaredFields(javax.lang.model.element.Element element, java.util.function.Predicate<? super javax.lang.model.element.VariableElement>... fieldFilters)
Retrieves the declared fields from the given element after applying the provided filters.static java.util.List<javax.lang.model.element.VariableElement>
findDeclaredFields(javax.lang.model.type.TypeMirror type, java.util.function.Predicate<? super javax.lang.model.element.VariableElement>... fieldFilters)
Retrieves the declared fields from the given type after applying the provided filters.static javax.lang.model.element.VariableElement
findField(javax.lang.model.element.Element element, java.lang.String fieldName)
Retrieves the first matching field with the specified name from the given element.static javax.lang.model.element.VariableElement
findField(javax.lang.model.type.TypeMirror type, java.lang.String fieldName)
Retrieves the first matching field with the specified name from the given type.static java.util.List<javax.lang.model.element.VariableElement>
getAllDeclaredFields(javax.lang.model.element.Element element)
Retrieves all declared fields from the given element, including those from hierarchical types (e.g., superclasses), without any filtering.static java.util.List<javax.lang.model.element.VariableElement>
getAllDeclaredFields(javax.lang.model.type.TypeMirror type)
Retrieves all declared fields from the given type, including those from hierarchical types (e.g., superclasses), without any filtering.static java.util.List<javax.lang.model.element.VariableElement>
getAllNonStaticFields(javax.lang.model.element.Element element)
Retrieves all non-static fields from the given element, including those from superclasses and interfaces.static java.util.List<javax.lang.model.element.VariableElement>
getAllNonStaticFields(javax.lang.model.type.TypeMirror type)
Retrieves all non-static fields from the given type, including those from hierarchical types (e.g., superclasses).static javax.lang.model.element.VariableElement
getDeclaredField(javax.lang.model.element.Element element, java.lang.String fieldName)
Retrieves the declared field with the specified name from the given element.static javax.lang.model.element.VariableElement
getDeclaredField(javax.lang.model.type.TypeMirror type, java.lang.String fieldName)
Retrieves the declared field with the specified name from the given type.static java.util.List<javax.lang.model.element.VariableElement>
getDeclaredFields(javax.lang.model.element.Element element)
Retrieves all declared fields from the given element without any filtering.static java.util.List<javax.lang.model.element.VariableElement>
getDeclaredFields(javax.lang.model.type.TypeMirror type)
Retrieves all declared fields from the given type without any filtering.static java.util.List<javax.lang.model.element.VariableElement>
getNonStaticFields(javax.lang.model.element.Element element)
Retrieves all declared non-static fields from the given element.static java.util.List<javax.lang.model.element.VariableElement>
getNonStaticFields(javax.lang.model.type.TypeMirror type)
Retrieves all declared non-static fields from the given type.static boolean
isEnumMemberField(javax.lang.model.element.VariableElement field)
Determines whether the given field is an enum member field.static boolean
isField(javax.lang.model.element.VariableElement field)
Checks if the given element is a field or an enum constant.static boolean
isField(javax.lang.model.element.VariableElement field, javax.lang.model.element.Modifier... modifiers)
Checks if the given element is a field or an enum constant, and also has all the specified modifiers.static boolean
isNonStaticField(javax.lang.model.element.VariableElement field)
Checks if the given field is a non-static field.
-
-
-
Method Detail
-
getDeclaredField
@Nullable static javax.lang.model.element.VariableElement getDeclaredField(javax.lang.model.element.Element element, java.lang.String fieldName)
Retrieves the declared field with the specified name from the given element.If the provided
Element
is null, this method returns null. It's typically used to retrieve a specific field from a class or interface element.Example Usage
Element element = ...; // A class or interface element String fieldName = "myField"; VariableElement field = getDeclaredField(element, fieldName); if (field != null) { System.out.println("Found field: " + field.getSimpleName()); } else { System.out.println("Field not found."); }
- Parameters:
element
- the element to search for the field; if null, null is returnedfieldName
- the name of the field to find- Returns:
- the VariableElement representing the declared field, or null if not found
-
getDeclaredField
@Nullable static javax.lang.model.element.VariableElement getDeclaredField(javax.lang.model.type.TypeMirror type, java.lang.String fieldName)
Retrieves the declared field with the specified name from the given type.If the provided
TypeMirror
is null, this method returns null. It searches for a field with the exact specified name in the given type, and returns the first match found.Example Usage
TypeMirror type = element.asType(); // A valid type from an element String fieldName = "myField"; VariableElement field = getDeclaredField(type, fieldName); if (field != null) { System.out.println("Found field: " + field.getSimpleName()); } else { System.out.println("Field not found."); }
- Parameters:
type
- the type to search for the field; if null, null is returnedfieldName
- the name of the field to find- Returns:
- the VariableElement representing the declared field, or null if not found
-
getDeclaredFields
@Nonnull @Immutable static java.util.List<javax.lang.model.element.VariableElement> getDeclaredFields(javax.lang.model.element.Element element)
Retrieves all declared fields from the given element without any filtering.If the provided
Element
is null, this method returns an empty list. It's typically used to get all fields declared in a class or interface, excluding fields from superclasses or interfaces.Example Usage
Element element = ...; // A class or interface element List<VariableElement> fields = getDeclaredFields(element); for (VariableElement field : fields) { System.out.println("Field: " + field.getSimpleName()); }
- Parameters:
element
- the element to retrieve declared fields from- Returns:
- a list of VariableElement objects representing the declared fields, or an empty list if the element is null or no fields are found
-
getDeclaredFields
@Nonnull @Immutable static java.util.List<javax.lang.model.element.VariableElement> getDeclaredFields(javax.lang.model.type.TypeMirror type)
Retrieves all declared fields from the given type without any filtering.If the provided
TypeMirror
is null, this method returns an empty list. It's typically used to get all fields declared directly within a class or interface, excluding fields from superclasses or interfaces.Example Usage
TypeMirror type = element.asType(); // A valid type from an element List<VariableElement> fields = getDeclaredFields(type); for (VariableElement field : fields) { System.out.println("Declared field: " + field.getSimpleName()); }
- Parameters:
type
- the type to retrieve declared fields from- Returns:
- a list of VariableElement objects representing the declared fields, or an empty list if the type is null or no fields are found
-
getAllDeclaredFields
@Nonnull @Immutable static java.util.List<javax.lang.model.element.VariableElement> getAllDeclaredFields(javax.lang.model.element.Element element)
Retrieves all declared fields from the given element, including those from hierarchical types (e.g., superclasses), without any filtering.If the provided
Element
is null, this method returns an empty list. It's typically used to get all fields declared directly within a class or interface, as well as those inherited from superclasses.Example Usage
Element element = ...; // A class or interface element List<VariableElement> fields = getAllDeclaredFields(element); for (VariableElement field : fields) { System.out.println("Declared field (including hierarchical): " + field.getSimpleName()); }
- Parameters:
element
- the element to retrieve all declared fields from- Returns:
- a list of VariableElement objects representing all declared fields, or an empty list if the element is null or no fields are found
-
getAllDeclaredFields
@Nonnull @Immutable static java.util.List<javax.lang.model.element.VariableElement> getAllDeclaredFields(javax.lang.model.type.TypeMirror type)
Retrieves all declared fields from the given type, including those from hierarchical types (e.g., superclasses), without any filtering.If the provided
TypeMirror
is null, this method returns an empty list. It's typically used to get all fields declared directly within a class or interface, as well as those inherited from superclasses.Example Usage
TypeMirror type = element.asType(); // A valid type from an element List<VariableElement> fields = getAllDeclaredFields(type); for (VariableElement field : fields) { System.out.println("Declared field (including hierarchical): " + field.getSimpleName()); }
- Parameters:
type
- the type to retrieve all declared fields from- Returns:
- a list of VariableElement objects representing all declared fields, or an empty list if the type is null or no fields are found
-
findField
@Nullable static javax.lang.model.element.VariableElement findField(javax.lang.model.element.Element element, java.lang.String fieldName)
Retrieves the first matching field with the specified name from the given element.If the provided
Element
is null, this method returns null. It searches for a field with the exact specified name in the given element and returns the first match found.Example Usage
Element element = ...; // A valid class or interface element String fieldName = "myField"; VariableElement field = findField(element, fieldName); if (field != null) { System.out.println("Found field: " + field.getSimpleName()); } else { System.out.println("Field not found."); }
- Parameters:
element
- the element to search for the field; if null, null is returnedfieldName
- the name of the field to find- Returns:
- the VariableElement representing the first matching field, or null if not found
-
findField
@Nullable static javax.lang.model.element.VariableElement findField(javax.lang.model.type.TypeMirror type, java.lang.String fieldName)
Retrieves the first matching field with the specified name from the given type.This method searches for a field with the exact specified name in the given type, including all hierarchical types (e.g., superclasses), and returns the first match found.
Example Usage
TypeMirror type = element.asType(); // A valid type from an element String fieldName = "myField"; VariableElement field = findField(type, fieldName); if (field != null) { System.out.println("Found field: " + field.getSimpleName()); } else { System.out.println("Field not found."); }
- Parameters:
type
- the type to search for fields; if null, null is returnedfieldName
- the name of the field to find- Returns:
- the VariableElement representing the first matching field, or null if not found
-
findDeclaredFields
@Nonnull @Immutable static java.util.List<javax.lang.model.element.VariableElement> findDeclaredFields(javax.lang.model.element.Element element, java.util.function.Predicate<? super javax.lang.model.element.VariableElement>... fieldFilters)
Retrieves the declared fields from the given element after applying the provided filters.If the provided
Element
is null, this method returns an empty list. It searches for fields directly declared in the given element (excluding fields from superclasses or interfaces) and applies the specified filters to narrow down the results.Example Usage
Element element = ...; // A class or interface element List<VariableElement> allFields = findDeclaredFields(element); // Get all declared fields // Get only non-static fields List<VariableElement> nonStaticFields = findDeclaredFields(element, FieldUtils::isNonStaticField); // Get fields matching a specific name String fieldName = "myField"; List<VariableElement> matchingFields = findDeclaredFields(element, field -> fieldName.equals(field.getSimpleName().toString())); // Get fields with multiple filters (e.g., non-static and public) List<VariableElement> publicNonStaticFields = findDeclaredFields(element, field -> field.getModifiers().contains(Modifier.PUBLIC), FieldUtils::isNonStaticField );
- Parameters:
element
- the element to retrieve declared fields from; if null, an empty list is returnedfieldFilters
- the predicates used to filter the fields; optional- Returns:
- a list of VariableElement objects representing the filtered declared fields
-
findDeclaredFields
@Nonnull @Immutable static java.util.List<javax.lang.model.element.VariableElement> findDeclaredFields(javax.lang.model.type.TypeMirror type, java.util.function.Predicate<? super javax.lang.model.element.VariableElement>... fieldFilters)
Retrieves the declared fields from the given type after applying the provided filters.This method searches for fields directly declared in the given type, excluding fields from superclasses or interfaces. It allows filtering the fields using the provided predicates to narrow down the results.
Example Usage
TypeMirror type = element.asType(); // A valid type from an element // Get all declared fields List<VariableElement> allFields = findDeclaredFields(type); // Get only non-static fields List<VariableElement> nonStaticFields = findDeclaredFields(type, FieldUtils::isNonStaticField); // Get fields matching a specific name String fieldName = "myField"; List<VariableElement> matchingFields = findDeclaredFields(type, field -> "myField".equals(field.getSimpleName().toString())); // Get fields with multiple filters (e.g., non-static and public) List<VariableElement> publicNonStaticFields = findDeclaredFields(type, field -> field.getModifiers().contains(Modifier.PUBLIC), FieldUtils::isNonStaticField );
- Parameters:
type
- the type to retrieve declared fields from; if null, an empty list is returnedfieldFilters
- the predicates used to filter the fields- Returns:
- a list of VariableElement objects representing the filtered declared fields
-
findAllDeclaredFields
@Nonnull @Immutable static java.util.List<javax.lang.model.element.VariableElement> findAllDeclaredFields(javax.lang.model.element.Element element, java.util.function.Predicate<? super javax.lang.model.element.VariableElement>... fieldFilters)
Retrieves all declared fields from the given element, including those from hierarchical types (e.g., superclasses), after applying the provided filters.This method processes the given element and searches for all fields declared directly within it as well as those inherited from superclasses. The fields can be filtered using one or more predicates to narrow down the results.
Example Usage
Element element = ...; // A valid class or interface element // Get all declared fields (including from superclasses) List<VariableElement> allFields = findAllDeclaredFields(element); // Get only non-static fields List<VariableElement> nonStaticFields = findAllDeclaredFields(element, FieldUtils::isNonStaticField); // Get fields matching a specific name String fieldName = "myField"; List<VariableElement> matchingFields = findAllDeclaredFields(element, field -> "myField".equals(field.getSimpleName().toString())); // Get fields with multiple filters (e.g., non-static and public) List<VariableElement> publicNonStaticFields = findAllDeclaredFields(element, field -> field.getModifiers().contains(Modifier.PUBLIC), FieldUtils::isNonStaticField );
- Parameters:
element
- the element to retrieve all declared fields from; if null, an empty list is returnedfieldFilters
- the predicates used to filter the fields; optional- Returns:
- a list of VariableElement objects representing the filtered declared fields
-
findAllDeclaredFields
@Nonnull @Immutable static java.util.List<javax.lang.model.element.VariableElement> findAllDeclaredFields(javax.lang.model.type.TypeMirror type, java.util.function.Predicate<? super javax.lang.model.element.VariableElement>... fieldFilters)
Retrieves all declared fields from the given type, including those from hierarchical types (e.g., superclasses), after applying the provided filters.This method searches for fields directly declared in the given type as well as those inherited from superclasses. It allows filtering the fields using the provided predicates to narrow down the results.
Example Usage
TypeMirror type = element.asType(); // A valid type from an element // Get all declared fields (including hierarchical) List<VariableElement> allFields = findAllDeclaredFields(type); // Get only non-static fields List<VariableElement> nonStaticFields = findAllDeclaredFields(type, FieldUtils::isNonStaticField); // Get fields matching a specific name String fieldName = "myField"; List<VariableElement> matchingFields = findAllDeclaredFields(type, field -> "myField".equals(field.getSimpleName().toString())); // Get fields with multiple filters (e.g., non-static and public) List<VariableElement> publicNonStaticFields = findAllDeclaredFields(type, field -> field.getModifiers().contains(Modifier.PUBLIC), FieldUtils::isNonStaticField );
- Parameters:
type
- the type to retrieve all declared fields from; if null, an empty list is returnedfieldFilters
- the predicates used to filter the fields- Returns:
- a list of VariableElement objects representing the filtered declared fields
-
filterDeclaredFields
@Nonnull @Immutable static java.util.List<javax.lang.model.element.VariableElement> filterDeclaredFields(javax.lang.model.type.TypeMirror type, boolean includeHierarchicalTypes, java.util.function.Predicate<? super javax.lang.model.element.VariableElement>... fieldFilters)
Filters and retrieves the declared fields from the given type based on the provided criteria.This method is used to retrieve fields declared in the given type, optionally including fields from its superclasses or interfaces. The fields can be further filtered using one or more predicates to narrow down the results.
Example Usage
TypeMirror type = element.asType(); // A valid type from an element // Get all declared fields (excluding hierarchical) List<VariableElement> declaredFields = filterDeclaredFields(type, false); // Get all declared fields including hierarchical ones List<VariableElement> allFields = filterDeclaredFields(type, true); // Get only non-static fields List<VariableElement> nonStaticFields = filterDeclaredFields(type, false, FieldUtils::isNonStaticField); // Get fields matching a specific name String fieldName = "myField"; List<VariableElement> matchingFields = filterDeclaredFields(type, false, field -> "myField".equals(field.getSimpleName().toString())); // Get fields with multiple filters (e.g., non-static and public) List<VariableElement> publicNonStaticFields = filterDeclaredFields(type, true, field -> field.getModifiers().contains(Modifier.PUBLIC), FieldUtils::isNonStaticField );
- Parameters:
type
- the type to retrieve declared fields from; if null, an empty list is returnedincludeHierarchicalTypes
- whether to include fields from hierarchical types (e.g., superclasses)fieldFilters
- the predicates used to filter the fields; optional- Returns:
- a list of VariableElement objects representing the filtered declared fields
-
isEnumMemberField
static boolean isEnumMemberField(javax.lang.model.element.VariableElement field)
Determines whether the given field is an enum member field.An enum member field is typically a public static final field that represents a constant within an enum declaration. This method checks if the field's enclosing element is an enum and if the field's kind matches
ElementKind.ENUM_CONSTANT
.Example Usage
VariableElement field = ...; // A valid field element boolean isEnumMember = FieldUtils.isEnumMemberField(field); if (isEnumMember) { System.out.println("The field is an enum member field."); } else { System.out.println("The field is not an enum member field."); }
- Parameters:
field
- the field to check; may be null- Returns:
- true if the field is an enum member field, false otherwise
-
isNonStaticField
static boolean isNonStaticField(javax.lang.model.element.VariableElement field)
Checks if the given field is a non-static field.This method verifies whether the provided
VariableElement
represents a field that is not declared with thestatic
modifier. It first checks if the element is a valid field (including enum constants) using theisField(VariableElement)
method, and then ensures that the field does not have the static modifier.Example Usage
VariableElement field = ...; // A valid field element boolean isNonStatic = FieldUtils.isNonStaticField(field); if (isNonStatic) { System.out.println("The field is non-static."); } else { System.out.println("The field is static or not a valid field."); }
- Parameters:
field
- the VariableElement to check; may be null- Returns:
- true if the field is a valid field (as per
isField(VariableElement)
) and does not have the 'static' modifier, false otherwise
-
isField
static boolean isField(javax.lang.model.element.VariableElement field)
Checks if the given element is a field or an enum constant.This method determines whether the provided
VariableElement
represents a valid field or an enum constant. It returnstrue
if the element's kind is eitherElementKind.FIELD
orElementKind.ENUM_CONSTANT
.Example Usage
VariableElement field = ...; // A valid field element boolean isValidField = FieldUtils.isField(field); if (isValidField) { System.out.println("The element is a valid field or enum constant."); } else { System.out.println("The element is not a field or enum constant."); }
- Parameters:
field
- the VariableElement to check; may be null- Returns:
- true if the element is a field (
ElementKind.FIELD
) or an enum constant (ElementKind.ENUM_CONSTANT
), false otherwise
-
isField
static boolean isField(javax.lang.model.element.VariableElement field, javax.lang.model.element.Modifier... modifiers)
Checks if the given element is a field or an enum constant, and also has all the specified modifiers.This method extends the
isField(VariableElement)
method by additionally verifying that the field has all of the specified modifiers. It returnstrue
only if the element is a valid field (including enum constants) and contains all the provided modifiers.Example Usage
VariableElement field = ...; // A valid field element // Check if it's a public static field boolean isPublicStaticField = FieldUtils.isField(field, Modifier.PUBLIC, Modifier.STATIC); if (isPublicStaticField) { System.out.println("The field is a public static field."); } else { System.out.println("The field is not a public static field."); } // Check if it's a private field boolean isPrivateField = FieldUtils.isField(field, Modifier.PRIVATE); if (isPrivateField) { System.out.println("The field is a private field."); } else { System.out.println("The field is not a private field."); }
- Parameters:
field
- the VariableElement to check; may be nullmodifiers
- the modifiers to match (e.g., Modifier.PUBLIC, Modifier.STATIC)- Returns:
- true if the element is a field (
ElementKind.FIELD
) or an enum constant (ElementKind.ENUM_CONSTANT
), and it has all of the specified modifiers, false otherwise
-
getNonStaticFields
@Nonnull @Immutable static java.util.List<javax.lang.model.element.VariableElement> getNonStaticFields(javax.lang.model.type.TypeMirror type)
Retrieves all declared non-static fields from the given type.This method returns a list of fields that are declared directly within the given type and are not marked as static. It does not include fields from superclasses or interfaces. If the provided
TypeMirror
is null, this method returns an empty list.Example Usage
TypeMirror type = element.asType(); // A valid type from an element List<VariableElement> nonStaticFields = FieldUtils.getNonStaticFields(type); for (VariableElement field : nonStaticFields) { System.out.println("Non-static field: " + field.getSimpleName()); }
- Parameters:
type
- the type to search for non-static fields; if null, an empty list is returned- Returns:
- a list of VariableElement objects representing the non-static fields, or an empty list if the type is null or no non-static fields are found
-
getNonStaticFields
@Nonnull @Immutable static java.util.List<javax.lang.model.element.VariableElement> getNonStaticFields(javax.lang.model.element.Element element)
Retrieves all declared non-static fields from the given element.This method processes the provided element and retrieves all fields directly declared within it that are not marked as static. It excludes fields from superclasses or interfaces. If the provided element is null, this method returns an empty list.
Example Usage
Element element = ...; // A valid class or interface element List<VariableElement> nonStaticFields = FieldUtils.getNonStaticFields(element); for (VariableElement field : nonStaticFields) { System.out.println("Non-static field: " + field.getSimpleName()); } // Handling null case List<VariableElement> safeList = FieldUtils.getNonStaticFields(null); System.out.println(safeList.isEmpty()); // true
- Parameters:
element
- the element to search for non-static fields; if null, an empty list is returned- Returns:
- a list of VariableElement objects representing the non-static fields, or an empty list if the element is null or no non-static fields are found
-
getAllNonStaticFields
@Nonnull @Immutable static java.util.List<javax.lang.model.element.VariableElement> getAllNonStaticFields(javax.lang.model.type.TypeMirror type)
Retrieves all non-static fields from the given type, including those from hierarchical types (e.g., superclasses).This method searches for all fields declared directly within the given type, as well as those inherited from its superclasses or interfaces, and filters out only the non-static fields. If the provided
TypeMirror
is null, this method returns an empty list.Example Usage
TypeMirror type = element.asType(); // A valid type from an element List<VariableElement> nonStaticFields = FieldUtils.getAllNonStaticFields(type); for (VariableElement field : nonStaticFields) { System.out.println("Non-static field (including hierarchical): " + field.getSimpleName()); } // Handling null case List<VariableElement> safeList = FieldUtils.getAllNonStaticFields(null); System.out.println(safeList.isEmpty()); // true
- Parameters:
type
- the type to retrieve all non-static fields from; if null, an empty list is returned- Returns:
- a list of VariableElement objects representing all non-static fields, or an empty list if the type is null or no non-static fields are found
-
getAllNonStaticFields
@Nonnull @Immutable static java.util.List<javax.lang.model.element.VariableElement> getAllNonStaticFields(javax.lang.model.element.Element element)
Retrieves all non-static fields from the given element, including those from superclasses and interfaces.This method processes the provided element and retrieves all fields declared directly within it, as well as those inherited from superclasses or interfaces. It filters out only the non-static fields. If the provided element is null, this method returns an empty list.
Example Usage
Element element = ...; // A valid class or interface element List<VariableElement> nonStaticFields = FieldUtils.getAllNonStaticFields(element); for (VariableElement field : nonStaticFields) { System.out.println("Non-static field: " + field.getSimpleName()); } // Handling null case List<VariableElement> safeList = FieldUtils.getAllNonStaticFields(null); System.out.println(safeList.isEmpty()); // true
- Parameters:
element
- the element to retrieve all non-static fields from; if null, an empty list is returned- Returns:
- a list of VariableElement objects representing all non-static fields, or an empty list if the element is null or no non-static fields are found
-
equalsFieldName
static boolean equalsFieldName(javax.lang.model.element.VariableElement field, java.lang.CharSequence fieldName)
Checks if the simple name of the given field matches the specified field name.This method ensures both the
VariableElement
and the field name are non-null before comparing their string representations for equality.Example Usage
VariableElement field = ...; // A valid field element CharSequence fieldName = "myField"; boolean isMatch = equalsFieldName(field, fieldName); if (isMatch) { System.out.println("Field name matches: " + fieldName); } else { System.out.println("Field name does not match."); }
- Parameters:
field
- the VariableElement representing the field; may be nullfieldName
- the CharSequence representing the expected field name; may be null- Returns:
- true if both the field and fieldName are non-null and their string representations match, false otherwise
-
-