Interface ElementUtils
-
- All Superinterfaces:
io.microsphere.util.Utils
public interface ElementUtils extends io.microsphere.util.Utils
The utility class forElement
- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
Element
,Enum
-
-
Method Summary
Static Methods Modifier and Type Method Description static <E extends javax.lang.model.element.Element>
java.util.List<E>filterElements(java.util.List<E> elements, java.util.function.Predicate<? super E>... elementPredicates)
Filters the provided list ofElement
objects based on the given array ofPredicate
conditions.static boolean
hasModifiers(javax.lang.model.element.Element member, javax.lang.model.element.Modifier... modifiers)
Checks whether the specifiedElement
has all of the specifiedModifier
s.static boolean
isClass(javax.lang.model.element.ElementKind kind)
Returnstrue
if the specifiedElementKind
represents a class-like element, including:ElementKind.CLASS
ElementKind.ENUM
ElementKind#RECORD
static boolean
isDeclaredType(javax.lang.model.element.ElementKind kind)
Returnstrue
if the specifiedElementKind
represents a declared type, which includes both class-like and interface-like elements.static boolean
isExecutable(javax.lang.model.element.ElementKind kind)
Returnstrue
if this is a kind of executable: eitherMETHOD
,CONSTRUCTOR
,STATIC_INIT
, orINSTANCE_INIT
.static boolean
isField(javax.lang.model.element.ElementKind kind)
Returnstrue
if this is a kind of field: eitherFIELD
orENUM_CONSTANT
.static boolean
isInitializer(javax.lang.model.element.ElementKind kind)
Returnstrue
if the specifiedElementKind
represents an initializer, eitherSTATIC_INIT
orINSTANCE_INIT
.static boolean
isInterface(javax.lang.model.element.ElementKind kind)
Returnstrue
if this is a kind of interface: eitherINTERFACE
orANNOTATION_TYPE
.static boolean
isMember(javax.lang.model.element.ElementKind kind)
Returnstrue
if the specifiedElementKind
represents a member element, which includes both field-like and executable-like elements.static boolean
isPublicNonStatic(javax.lang.model.element.Element member)
Checks whether the specifiedElement
is public and non-static.static boolean
isVariable(javax.lang.model.element.ElementKind kind)
Returnstrue
if the specifiedElementKind
represents a variable-like element, including:ElementKind.ENUM_CONSTANT
ElementKind.FIELD
ElementKind.PARAMETER
ElementKind.LOCAL_VARIABLE
ElementKind.EXCEPTION_PARAMETER
ElementKind.RESOURCE_VARIABLE
ElementKind#BINDING_VARIABLE
static boolean
matchesElementKind(javax.lang.model.element.Element member, javax.lang.model.element.ElementKind kind)
Checks whether the specifiedElement
matches the specifiedElementKind
.static boolean
matchesElementType(javax.lang.model.element.ElementKind elementKind, java.lang.annotation.ElementType elementType)
Checks whether the specifiedElementKind
matches the specifiedElementType
.static boolean
matchesElementType(javax.lang.model.element.ElementKind elementKind, java.lang.annotation.ElementType... elementTypes)
Checks whether the specifiedElementKind
matches any of the specifiedElementType
s.static boolean
matchesElementType(javax.lang.model.element.Element element, java.lang.annotation.ElementType... elementTypes)
Checks whether the specifiedElement
matches any of the specifiedElementType
s.static boolean
matchParameterTypeNames(java.util.List<? extends javax.lang.model.element.VariableElement> parameters, java.lang.CharSequence... parameterTypeNames)
Checks whether the parameter types of the given list ofVariableElement
parameters match the specified type names.static boolean
matchParameterTypes(java.util.List<? extends javax.lang.model.element.VariableElement> parameters, java.lang.reflect.Type... parameterTypes)
Checks whether the parameter types of the given list ofVariableElement
parameters match the specifiedtypes
.static boolean
matchParameterTypes(javax.lang.model.element.ExecutableElement executableElement, java.lang.reflect.Type... parameterTypes)
Checks whether the parameter types of the givenExecutableElement
match the specifiedtypes
.static javax.lang.model.element.ElementKind
toElementKind(java.lang.annotation.ElementType elementType)
Converts the specifiedElementType
to an equivalentElementKind
.
-
-
-
Method Detail
-
isClass
static boolean isClass(javax.lang.model.element.ElementKind kind)
Returnstrue
if the specifiedElementKind
represents a class-like element, including:ElementKind.CLASS
ElementKind.ENUM
ElementKind#RECORD
This method serves as a convenience wrapper around
ElementKind.isClass()
. It also guards against null input by returningfalse
when the argument is null.Example Usage
ElementKind classKind = ElementKind.CLASS; boolean result = isClass(classKind); // returns true ElementKind methodKind = ElementKind.METHOD; result = isClass(methodKind); // returns false result = isClass(null); // returns false
- Parameters:
kind
- the ElementKind to check, may be null- Returns:
true
if the kind is a class-like element,false
otherwise- See Also:
ElementKind.isClass()
-
isInterface
static boolean isInterface(javax.lang.model.element.ElementKind kind)
Returnstrue
if this is a kind of interface: eitherINTERFACE
orANNOTATION_TYPE
.This method serves as a convenience wrapper around
ElementKind.isInterface()
. It also guards against null input by returningfalse
when the argument is null.Example Usage
ElementKind interfaceKind = ElementKind.INTERFACE; boolean result = isInterface(interfaceKind); // returns true ElementKind annotationKind = ElementKind.ANNOTATION_TYPE; result = isInterface(annotationKind); // returns true ElementKind classKind = ElementKind.CLASS; result = isInterface(classKind); // returns false result = isInterface(null); // returns false
- Parameters:
kind
- the ElementKind to check, may be null- Returns:
true
if the kind is an interface-like element,false
otherwise- See Also:
ElementKind.isInterface()
-
isDeclaredType
static boolean isDeclaredType(javax.lang.model.element.ElementKind kind)
Returnstrue
if the specifiedElementKind
represents a declared type, which includes both class-like and interface-like elements.This method serves as a convenience wrapper that combines the checks for class and interface kinds. It also guards against null input by safely delegating to the respective methods.
Example Usage
ElementKind classKind = ElementKind.CLASS; boolean result = isDeclaredType(classKind); // returns true ElementKind interfaceKind = ElementKind.INTERFACE; result = isDeclaredType(interfaceKind); // returns true ElementKind methodKind = ElementKind.METHOD; result = isDeclaredType(methodKind); // returns false result = isDeclaredType(null); // returns false
- Parameters:
kind
- the ElementKind to check, may be null- Returns:
true
if the kind is a declared type,false
otherwise- See Also:
isClass(ElementKind)
,isInterface(ElementKind)
-
isField
static boolean isField(javax.lang.model.element.ElementKind kind)
Returnstrue
if this is a kind of field: eitherFIELD
orENUM_CONSTANT
.This method serves as a convenience wrapper around
ElementKind.isField()
. It also guards against null input by returningfalse
when the argument is null.Example Usage
ElementKind fieldKind = ElementKind.FIELD; boolean result = isField(fieldKind); // returns true ElementKind enumConstantKind = ElementKind.ENUM_CONSTANT; result = isField(enumConstantKind); // returns true ElementKind methodKind = ElementKind.METHOD; result = isField(methodKind); // returns false result = isField(null); // returns false
- Parameters:
kind
- the ElementKind to check, may be null- Returns:
true
if the kind is a field-like element,false
otherwise- See Also:
ElementKind.isField()
-
isExecutable
static boolean isExecutable(javax.lang.model.element.ElementKind kind)
Returnstrue
if this is a kind of executable: eitherMETHOD
,CONSTRUCTOR
,STATIC_INIT
, orINSTANCE_INIT
.This method serves as a convenience wrapper around
ElementKind#isExecutable()
. It also guards against null input by returningfalse
when the argument is null.Example Usage
ElementKind methodKind = ElementKind.METHOD; boolean result = isExecutable(methodKind); // returns true ElementKind constructorKind = ElementKind.CONSTRUCTOR; result = isExecutable(constructorKind); // returns true ElementKind staticInitKind = ElementKind.STATIC_INIT; result = isExecutable(staticInitKind); // returns true ElementKind instanceInitKind = ElementKind.INSTANCE_INIT; result = isExecutable(instanceInitKind); // returns true ElementKind classKind = ElementKind.CLASS; result = isExecutable(classKind); // returns false result = isExecutable(null); // returns false
- Parameters:
kind
- the ElementKind to check, may be null- Returns:
true
if the kind is an executable-like element,false
otherwise- See Also:
ElementKind#isExecutable()
-
isMember
static boolean isMember(javax.lang.model.element.ElementKind kind)
Returnstrue
if the specifiedElementKind
represents a member element, which includes both field-like and executable-like elements.This method serves as a convenience wrapper that combines the checks for field and executable kinds. It also guards against null input by safely delegating to the respective methods.
Example Usage
ElementKind fieldKind = ElementKind.FIELD; boolean result = isMember(fieldKind); // returns true ElementKind methodKind = ElementKind.METHOD; result = isMember(methodKind); // returns true ElementKind classKind = ElementKind.CLASS; result = isMember(classKind); // returns false result = isMember(null); // returns false
- Parameters:
kind
- the ElementKind to check, may be null- Returns:
true
if the kind is a member-like element,false
otherwise- See Also:
isField(ElementKind)
,isExecutable(ElementKind)
-
isInitializer
static boolean isInitializer(javax.lang.model.element.ElementKind kind)
Returnstrue
if the specifiedElementKind
represents an initializer, eitherSTATIC_INIT
orINSTANCE_INIT
.This method serves as a convenience wrapper around
ElementKind#isInitializer()
. It also guards against null input by returningfalse
when the argument is null.Example Usage
ElementKind staticInitKind = ElementKind.STATIC_INIT; boolean result = isInitializer(staticInitKind); // returns true ElementKind instanceInitKind = ElementKind.INSTANCE_INIT; result = isInitializer(instanceInitKind); // returns true ElementKind methodKind = ElementKind.METHOD; result = isInitializer(methodKind); // returns false result = isInitializer(null); // returns false
- Parameters:
kind
- the ElementKind to check, may be null- Returns:
true
if the kind is an initializer-like element,false
otherwise- See Also:
ElementKind#isInitializer()
-
isVariable
static boolean isVariable(javax.lang.model.element.ElementKind kind)
Returnstrue
if the specifiedElementKind
represents a variable-like element, including:ElementKind.ENUM_CONSTANT
ElementKind.FIELD
ElementKind.PARAMETER
ElementKind.LOCAL_VARIABLE
ElementKind.EXCEPTION_PARAMETER
ElementKind.RESOURCE_VARIABLE
ElementKind#BINDING_VARIABLE
This method serves as a convenience wrapper around
ElementKind#isVariable()
. It also guards against null input by returningfalse
when the argument is null.Example Usage
ElementKind enumConstantKind = ElementKind.ENUM_CONSTANT; boolean result = isVariable(enumConstantKind); // returns true ElementKind fieldKind = ElementKind.FIELD; result = isVariable(fieldKind); // returns true ElementKind parameterKind = ElementKind.PARAMETER; result = isVariable(parameterKind); // returns true ElementKind localVariableKind = ElementKind.LOCAL_VARIABLE; result = isVariable(localVariableKind); // returns true ElementKind exceptionParameterKind = ElementKind.EXCEPTION_PARAMETER; result = isVariable(exceptionParameterKind); // returns true ElementKind resourceVariableKind = ElementKind.RESOURCE_VARIABLE; result = isVariable(resourceVariableKind); // returns true ElementKind bindingVariableKind = ElementKind.BINDING_VARIABLE; result = isVariable(bindingVariableKind); // returns true ElementKind methodKind = ElementKind.METHOD; result = isVariable(methodKind); // returns false result = isVariable(null); // returns false
- Parameters:
kind
- the ElementKind to check, may be null- Returns:
true
if the kind is a variable-like element,false
otherwise- See Also:
ElementKind#isVariable()
-
toElementKind
static javax.lang.model.element.ElementKind toElementKind(java.lang.annotation.ElementType elementType)
Converts the specifiedElementType
to an equivalentElementKind
.This method maps the provided
ElementType
to a correspondingElementKind
. If the providedelementType
isnull
, this method returnsElementKind.OTHER
.Example Usage
ElementType typeElement = ElementType.TYPE; ElementKind result = toElementKind(typeElement); // returns ElementKind.CLASS ElementType typeUseElement = ElementType.TYPE_USE; result = toElementKind(typeUseElement); // returns ElementKind.CLASS ElementType fieldElement = ElementType.FIELD; result = toElementKind(fieldElement); // returns ElementKind.FIELD result = toElementKind(null); // returns ElementKind.OTHER
- Parameters:
elementType
- the ElementType to convert, may benull
- Returns:
- the corresponding ElementKind, never
null
-
matchesElementType
static boolean matchesElementType(javax.lang.model.element.ElementKind elementKind, java.lang.annotation.ElementType elementType)
Checks whether the specifiedElementKind
matches the specifiedElementType
.This method compares the provided
ElementKind
with the result of converting theElementType
to an equivalentElementKind
usingtoElementKind(ElementType)
. If either argument is null, the comparison will returnfalse
.Example Usage
ElementKind classKind = ElementKind.CLASS; ElementType typeElement = ElementType.TYPE; boolean result = matchesElementType(classKind, typeElement); // returns true ElementKind fieldKind = ElementKind.FIELD; ElementType typeUseElement = ElementType.TYPE_USE; result = matchesElementType(fieldKind, typeUseElement); // returns false result = matchesElementType(null, ElementType.TYPE); // returns false result = matchesElementType(ElementKind.CLASS, null); // returns false
- Parameters:
elementKind
- the ElementKind to check, may benull
elementType
- the ElementType to compare against, may benull
- Returns:
true
if the ElementKind matches the converted ElementType,false
otherwise- See Also:
toElementKind(ElementType)
-
matchesElementType
static boolean matchesElementType(javax.lang.model.element.ElementKind elementKind, java.lang.annotation.ElementType... elementTypes)
Checks whether the specifiedElementKind
matches any of the specifiedElementType
s.This method converts each
ElementType
to its correspondingElementKind
usingtoElementKind(ElementType)
, and then compares it with the providedElementKind
. If any match is found, the method returnstrue
.Example Usage
ElementKind classKind = ElementKind.CLASS; boolean result = matchesElementType(classKind, ElementType.TYPE, ElementType.METHOD); // returns true result = matchesElementType(classKind, ElementType.FIELD, ElementType.CONSTRUCTOR); // returns false result = matchesElementType(null, ElementType.TYPE); // returns false result = matchesElementType(ElementKind.FIELD, (ElementType[]) null); // returns false
- Parameters:
elementKind
- the ElementKind to check, may benull
elementTypes
- the array of ElementTypes to match against, may benull
- Returns:
true
if the ElementKind matches any of the converted ElementTypes,false
otherwise- See Also:
toElementKind(ElementType)
,matchesElementType(ElementKind, ElementType)
-
matchesElementType
static boolean matchesElementType(javax.lang.model.element.Element element, java.lang.annotation.ElementType... elementTypes)
Checks whether the specifiedElement
matches any of the specifiedElementType
s.This method determines if the provided
Element
has a kind that matches any of the convertedElementKind
values derived from the givenElementType
s. It delegates tomatchesElementType(ElementKind, ElementType...)
for the actual comparison.Example Usage
Element element = ...; // an Element of kind CLASS boolean result = matchesElementType(element, ElementType.TYPE, ElementType.TYPE_USE); // returns true result = matchesElementType(element, ElementType.FIELD, ElementType.METHOD); // returns false result = matchesElementType(null, ElementType.TYPE); // returns false result = matchesElementType(element, (ElementType[]) null); // returns false
- Parameters:
element
- the Element to check, may benull
elementTypes
- the array of ElementTypes to match against, may benull
- Returns:
true
if the element matches any of the converted ElementTypes,false
otherwise- See Also:
matchesElementType(ElementKind, ElementType...)
-
matchesElementKind
static boolean matchesElementKind(javax.lang.model.element.Element member, javax.lang.model.element.ElementKind kind)
Checks whether the specifiedElement
matches the specifiedElementKind
.This method returns
false
if either the element or the kind isnull
. Otherwise, it compares the kind of the element with the providedElementKind
.Example Usage
Element element = ...; // an Element of kind METHOD ElementKind methodKind = ElementKind.METHOD; boolean result = matchesElementKind(element, methodKind); // returns true result = matchesElementKind(null, methodKind); // returns false result = matchesElementKind(element, null); // returns false
- Parameters:
member
- the Element to check, may benull
kind
- the ElementKind to match, may benull
- Returns:
true
if the element is not null and its kind matches the specified kind; otherwise,false
-
isPublicNonStatic
static boolean isPublicNonStatic(javax.lang.model.element.Element member)
Checks whether the specifiedElement
is public and non-static.This method verifies if the provided element has the
Modifier.PUBLIC
modifier and does not have theModifier.STATIC
modifier.Example Usage
Element methodElement = ...; // an Element with PUBLIC and non-STATIC modifiers boolean result = isPublicNonStatic(methodElement); // returns true Element staticMethodElement = ...; // an Element with PUBLIC and STATIC modifiers result = isPublicNonStatic(staticMethodElement); // returns false result = isPublicNonStatic(null); // returns false
- Parameters:
member
- the Element to check, may be null- Returns:
true
if the element is public and non-static; otherwise,false
-
hasModifiers
static boolean hasModifiers(javax.lang.model.element.Element member, javax.lang.model.element.Modifier... modifiers)
Checks whether the specifiedElement
has all of the specifiedModifier
s.This method returns
false
if the element isnull
or if the modifiers array isnull
. Otherwise, it verifies that the element contains all the provided modifiers.Example Usage
Element methodElement = ...; // an Element with PUBLIC and STATIC modifiers boolean result = hasModifiers(methodElement, Modifier.PUBLIC, Modifier.STATIC); // returns true result = hasModifiers(methodElement, Modifier.PRIVATE); // returns false result = hasModifiers(null, Modifier.PUBLIC); // returns false result = hasModifiers(methodElement, (Modifier[]) null); // returns false
- Parameters:
member
- theElement
to check, may benull
modifiers
- the array ofModifier
s to match, may benull
- Returns:
true
if the element is not null and contains all specified modifiers; otherwise,false
-
filterElements
@Nonnull @Immutable static <E extends javax.lang.model.element.Element> java.util.List<E> filterElements(java.util.List<E> elements, java.util.function.Predicate<? super E>... elementPredicates)
Filters the provided list ofElement
objects based on the given array ofPredicate
conditions.This method applies a logical AND combination of all provided predicates to filter the elements. If the input list is empty or the predicates array is null or empty, an empty list is returned.
Example Usage
List<Element> elements = ...; // a list of Elements // Filter public and static methods List<Element> filtered = filterElements(elements, element -> ElementUtils.hasModifiers(element, Modifier.PUBLIC), element -> ElementUtils.hasModifiers(element, Modifier.STATIC) ); // Returns an empty list if no elements match filtered = filterElements(elements, element -> ElementUtils.hasModifiers(element, Modifier.PRIVATE) ); // Returns an empty list if input is null or predicates are null filtered = filterElements(null, (Predicate[]) null); // returns empty list
- Type Parameters:
E
- The type of the elements, which must be a subclass ofElement
- Parameters:
elements
- The list of elements to be filtered, may benull
elementPredicates
- An array of predicates used to filter the elements, may benull
- Returns:
- A filtered list of elements that match all the provided predicates. Returns an empty list if no elements match, or if the input list or predicate array is invalid.
-
matchParameterTypes
static boolean matchParameterTypes(javax.lang.model.element.ExecutableElement executableElement, java.lang.reflect.Type... parameterTypes)
Checks whether the parameter types of the givenExecutableElement
match the specifiedtypes
.This method compares the types of the parameters declared in the executable element with the provided expected types. It uses
TypeUtils.isSameType(Element, CharSequence)
to perform type comparison.Example Usage
ExecutableElement methodElement = ...; // an executable element with parameters boolean result = matchParameterTypes(methodElement, String.class, int.class); // returns true if parameter types match result = matchParameterTypes(null, String.class); // returns false result = matchParameterTypes(methodElement, (Type[]) null); // returns false
- Parameters:
executableElement
- the executable element whose parameters are to be checked, may benull
parameterTypes
- the expected parameter types, may benull
- Returns:
true
if the parameter types match;false
otherwise
-
matchParameterTypes
static boolean matchParameterTypes(java.util.List<? extends javax.lang.model.element.VariableElement> parameters, java.lang.reflect.Type... parameterTypes)
Checks whether the parameter types of the given list ofVariableElement
parameters match the specifiedtypes
.This method compares each parameter's type with the corresponding expected type by their fully qualified type names. It uses
TypeUtils.isSameType(Element, CharSequence)
to perform the type comparison.Example Usage
List<? extends VariableElement> parameters = executableElement.getParameters(); boolean result = matchParameterTypes(parameters, String.class, int.class); // returns true if types match result = matchParameterTypes(null, String.class); // returns false result = matchParameterTypes(parameters, (Type[]) null); // returns false
- Parameters:
parameters
- the list of variable elements representing the parameters, may benull
parameterTypes
- the expected parameter types, may benull
- Returns:
true
if all parameter types match their corresponding expected types; otherwise,false
-
matchParameterTypeNames
static boolean matchParameterTypeNames(java.util.List<? extends javax.lang.model.element.VariableElement> parameters, java.lang.CharSequence... parameterTypeNames)
Checks whether the parameter types of the given list ofVariableElement
parameters match the specified type names.This method compares each parameter's type with the corresponding expected type name using
TypeUtils.isSameType(Element, CharSequence)
. It ensures that both the number of parameters and their respective types match the provided array of type names.Example Usage
List<? extends VariableElement> parameters = executableElement.getParameters(); // Check if the parameters match String and int boolean result = matchParameterTypeNames(parameters, "java.lang.String", "int"); // returns true if match // Returns false if either parameter list or type names are null result = matchParameterTypeNames(null, "java.lang.String"); // returns false result = matchParameterTypeNames(parameters, (CharSequence[]) null); // returns false // Returns false if the size of parameters and type names do not match result = matchParameterTypeNames(Arrays.asList(param1, param2), "java.lang.String"); // returns false
- Parameters:
parameters
- the list of variable elements representing the parameters, may benull
parameterTypeNames
- the expected fully qualified type names of the parameters, may benull
- Returns:
true
if all parameter types match their corresponding type names; otherwise,false
-
-