Interface ElementUtils
-
-
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 ofElementobjects based on the given array ofPredicateconditions.static booleanhasModifiers(javax.lang.model.element.Element member, javax.lang.model.element.Modifier... modifiers)Checks whether the specifiedElementhas all of the specifiedModifiers.static booleanisClass(javax.lang.model.element.ElementKind kind)Returnstrueif the specifiedElementKindrepresents a class-like element, including:ElementKind.CLASSElementKind.ENUMElementKind#RECORDstatic booleanisDeclaredType(javax.lang.model.element.ElementKind kind)Returnstrueif the specifiedElementKindrepresents a declared type, which includes both class-like and interface-like elements.static booleanisExecutable(javax.lang.model.element.ElementKind kind)Returnstrueif this is a kind of executable: eitherMETHOD,CONSTRUCTOR,STATIC_INIT, orINSTANCE_INIT.static booleanisField(javax.lang.model.element.ElementKind kind)Returnstrueif this is a kind of field: eitherFIELDorENUM_CONSTANT.static booleanisInitializer(javax.lang.model.element.ElementKind kind)Returnstrueif the specifiedElementKindrepresents an initializer, eitherSTATIC_INITorINSTANCE_INIT.static booleanisInterface(javax.lang.model.element.ElementKind kind)Returnstrueif this is a kind of interface: eitherINTERFACEorANNOTATION_TYPE.static booleanisMember(javax.lang.model.element.ElementKind kind)Returnstrueif the specifiedElementKindrepresents a member element, which includes both field-like and executable-like elements.static booleanisPublicNonStatic(javax.lang.model.element.Element member)Checks whether the specifiedElementis public and non-static.static booleanisVariable(javax.lang.model.element.ElementKind kind)Returnstrueif the specifiedElementKindrepresents a variable-like element, including:ElementKind.ENUM_CONSTANTElementKind.FIELDElementKind.PARAMETERElementKind.LOCAL_VARIABLEElementKind.EXCEPTION_PARAMETERElementKind.RESOURCE_VARIABLEElementKind#BINDING_VARIABLEstatic booleanmatchesElementKind(javax.lang.model.element.Element member, javax.lang.model.element.ElementKind kind)Checks whether the specifiedElementmatches the specifiedElementKind.static booleanmatchesElementType(javax.lang.model.element.ElementKind elementKind, java.lang.annotation.ElementType elementType)Checks whether the specifiedElementKindmatches the specifiedElementType.static booleanmatchesElementType(javax.lang.model.element.ElementKind elementKind, java.lang.annotation.ElementType... elementTypes)Checks whether the specifiedElementKindmatches any of the specifiedElementTypes.static booleanmatchesElementType(javax.lang.model.element.Element element, java.lang.annotation.ElementType... elementTypes)Checks whether the specifiedElementmatches any of the specifiedElementTypes.static booleanmatchParameterTypeNames(java.util.List<? extends javax.lang.model.element.VariableElement> parameters, java.lang.CharSequence... parameterTypeNames)Checks whether the parameter types of the given list ofVariableElementparameters match the specified type names.static booleanmatchParameterTypes(java.util.List<? extends javax.lang.model.element.VariableElement> parameters, java.lang.reflect.Type... parameterTypes)Checks whether the parameter types of the given list ofVariableElementparameters match the specifiedtypes.static booleanmatchParameterTypes(javax.lang.model.element.ExecutableElement executableElement, java.lang.reflect.Type... parameterTypes)Checks whether the parameter types of the givenExecutableElementmatch the specifiedtypes.static javax.lang.model.element.ElementKindtoElementKind(java.lang.annotation.ElementType elementType)Converts the specifiedElementTypeto an equivalentElementKind.
-
-
-
Method Detail
-
isClass
static boolean isClass(javax.lang.model.element.ElementKind kind)
Returnstrueif the specifiedElementKindrepresents a class-like element, including:ElementKind.CLASSElementKind.ENUMElementKind#RECORD
This method serves as a convenience wrapper around
ElementKind.isClass(). It also guards against null input by returningfalsewhen 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:
trueif the kind is a class-like element,falseotherwise- See Also:
ElementKind.isClass()
-
isInterface
static boolean isInterface(javax.lang.model.element.ElementKind kind)
Returnstrueif this is a kind of interface: eitherINTERFACEorANNOTATION_TYPE.This method serves as a convenience wrapper around
ElementKind.isInterface(). It also guards against null input by returningfalsewhen 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:
trueif the kind is an interface-like element,falseotherwise- See Also:
ElementKind.isInterface()
-
isDeclaredType
static boolean isDeclaredType(javax.lang.model.element.ElementKind kind)
Returnstrueif the specifiedElementKindrepresents 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:
trueif the kind is a declared type,falseotherwise- See Also:
isClass(ElementKind),isInterface(ElementKind)
-
isField
static boolean isField(javax.lang.model.element.ElementKind kind)
Returnstrueif this is a kind of field: eitherFIELDorENUM_CONSTANT.This method serves as a convenience wrapper around
ElementKind.isField(). It also guards against null input by returningfalsewhen 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:
trueif the kind is a field-like element,falseotherwise- See Also:
ElementKind.isField()
-
isExecutable
static boolean isExecutable(javax.lang.model.element.ElementKind kind)
Returnstrueif 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 returningfalsewhen 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:
trueif the kind is an executable-like element,falseotherwise- See Also:
ElementKind#isExecutable()
-
isMember
static boolean isMember(javax.lang.model.element.ElementKind kind)
Returnstrueif the specifiedElementKindrepresents 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:
trueif the kind is a member-like element,falseotherwise- See Also:
isField(ElementKind),isExecutable(ElementKind)
-
isInitializer
static boolean isInitializer(javax.lang.model.element.ElementKind kind)
Returnstrueif the specifiedElementKindrepresents an initializer, eitherSTATIC_INITorINSTANCE_INIT.This method serves as a convenience wrapper around
ElementKind#isInitializer(). It also guards against null input by returningfalsewhen 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:
trueif the kind is an initializer-like element,falseotherwise- See Also:
ElementKind#isInitializer()
-
isVariable
static boolean isVariable(javax.lang.model.element.ElementKind kind)
Returnstrueif the specifiedElementKindrepresents a variable-like element, including:ElementKind.ENUM_CONSTANTElementKind.FIELDElementKind.PARAMETERElementKind.LOCAL_VARIABLEElementKind.EXCEPTION_PARAMETERElementKind.RESOURCE_VARIABLEElementKind#BINDING_VARIABLE
This method serves as a convenience wrapper around
ElementKind#isVariable(). It also guards against null input by returningfalsewhen 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:
trueif the kind is a variable-like element,falseotherwise- See Also:
ElementKind#isVariable()
-
toElementKind
static javax.lang.model.element.ElementKind toElementKind(java.lang.annotation.ElementType elementType)
Converts the specifiedElementTypeto an equivalentElementKind.This method maps the provided
ElementTypeto a correspondingElementKind. If the providedelementTypeisnull, 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 specifiedElementKindmatches the specifiedElementType.This method compares the provided
ElementKindwith the result of converting theElementTypeto an equivalentElementKindusingtoElementKind(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 benullelementType- the ElementType to compare against, may benull- Returns:
trueif the ElementKind matches the converted ElementType,falseotherwise- See Also:
toElementKind(ElementType)
-
matchesElementType
static boolean matchesElementType(javax.lang.model.element.ElementKind elementKind, java.lang.annotation.ElementType... elementTypes)Checks whether the specifiedElementKindmatches any of the specifiedElementTypes.This method converts each
ElementTypeto its correspondingElementKindusingtoElementKind(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 benullelementTypes- the array of ElementTypes to match against, may benull- Returns:
trueif the ElementKind matches any of the converted ElementTypes,falseotherwise- 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 specifiedElementmatches any of the specifiedElementTypes.This method determines if the provided
Elementhas a kind that matches any of the convertedElementKindvalues derived from the givenElementTypes. 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 benullelementTypes- the array of ElementTypes to match against, may benull- Returns:
trueif the element matches any of the converted ElementTypes,falseotherwise- See Also:
matchesElementType(ElementKind, ElementType...)
-
matchesElementKind
static boolean matchesElementKind(javax.lang.model.element.Element member, javax.lang.model.element.ElementKind kind)Checks whether the specifiedElementmatches the specifiedElementKind.This method returns
falseif 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 benullkind- the ElementKind to match, may benull- Returns:
trueif 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 specifiedElementis public and non-static.This method verifies if the provided element has the
Modifier.PUBLICmodifier and does not have theModifier.STATICmodifier.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:
trueif 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 specifiedElementhas all of the specifiedModifiers.This method returns
falseif the element isnullor 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- theElementto check, may benullmodifiers- the array ofModifiers to match, may benull- Returns:
trueif 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 ofElementobjects based on the given array ofPredicateconditions.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 benullelementPredicates- 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 givenExecutableElementmatch 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 benullparameterTypes- the expected parameter types, may benull- Returns:
trueif the parameter types match;falseotherwise
-
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 ofVariableElementparameters 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 benullparameterTypes- the expected parameter types, may benull- Returns:
trueif 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 ofVariableElementparameters 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 benullparameterTypeNames- the expected fully qualified type names of the parameters, may benull- Returns:
trueif all parameter types match their corresponding type names; otherwise,false
-
-