Interface ConstructorUtils
-
-
Method Summary
Static Methods Modifier and Type Method Description static java.util.List<javax.lang.model.element.ExecutableElement>filterDeclaredConstructors(javax.lang.model.type.TypeMirror type, java.util.function.Predicate<? super javax.lang.model.element.ExecutableElement>... constructorFilters)Filters and retrieves the list of declared constructors from the providedTypeMirror.static javax.lang.model.element.ExecutableElementfindConstructor(javax.lang.model.element.TypeElement type, java.lang.reflect.Type... parameterTypes)Finds a declared constructor in the specifiedTypeElementthat matches the given parameter types.static javax.lang.model.element.ExecutableElementfindConstructor(javax.lang.model.type.TypeMirror type, java.lang.reflect.Type... parameterTypes)Finds a declared constructor in the specifiedTypeMirrorthat matches the given parameter types.static java.util.List<javax.lang.model.element.ExecutableElement>findDeclaredConstructors(javax.lang.model.element.TypeElement type, java.util.function.Predicate<? super javax.lang.model.element.ExecutableElement>... constructorFilters)Retrieves and filters the list of declared constructors from the providedTypeElement.static java.util.List<javax.lang.model.element.ExecutableElement>findDeclaredConstructors(javax.lang.model.type.TypeMirror type, java.util.function.Predicate<? super javax.lang.model.element.ExecutableElement>... constructorFilters)Retrieves and filters the list of declared constructors from the providedTypeMirror.static java.util.List<javax.lang.model.element.ExecutableElement>getDeclaredConstructors(javax.lang.model.element.TypeElement type)Retrieves the list of declared constructors from the providedTypeElement.static java.util.List<javax.lang.model.element.ExecutableElement>getDeclaredConstructors(javax.lang.model.type.TypeMirror type)Retrieves the list of declared constructors from the providedTypeMirror.
-
-
-
Method Detail
-
getDeclaredConstructors
@Nonnull @Immutable static java.util.List<javax.lang.model.element.ExecutableElement> getDeclaredConstructors(javax.lang.model.element.TypeElement type)
Retrieves the list of declared constructors from the providedTypeElement.This method provides a null-safe way to obtain the constructors of a given type. If the input
typeisnull, an empty list is returned.Example Usage
TypeElement typeElement = ...; // Obtain a valid TypeElement List<ExecutableElement> constructors = getDeclaredConstructors(typeElement); if (!constructors.isEmpty()) { for (ExecutableElement constructor : constructors) { System.out.println("Constructor: " + constructor); } } else { System.out.println("No constructors found."); }This method is particularly useful when processing annotations during compilation, where it is necessary to inspect the constructors of a class without throwing exceptions for null inputs.
- Parameters:
type- theTypeElementrepresenting the type to retrieve constructors from- Returns:
- a
ListofExecutableElementobjects representing the declared constructors; nevernull, but may be empty if no constructors are found or if the input isnull
-
getDeclaredConstructors
@Nonnull @Immutable static java.util.List<javax.lang.model.element.ExecutableElement> getDeclaredConstructors(javax.lang.model.type.TypeMirror type)
Retrieves the list of declared constructors from the providedTypeMirror.This method provides a null-safe way to obtain the constructors of a given type. If the input
typeisnull, an empty list is returned.Example Usage
TypeMirror typeMirror = ...; // Obtain a valid TypeMirror List<ExecutableElement> constructors = getDeclaredConstructors(typeMirror); if (!constructors.isEmpty()) { for (ExecutableElement constructor : constructors) { System.out.println("Constructor: " + constructor); } } else { System.out.println("No constructors found."); }This method is particularly useful when processing annotations during compilation, where it is necessary to inspect the constructors of a class without throwing exceptions for null inputs.
- Parameters:
type- theTypeMirrorrepresenting the type to retrieve constructors from- Returns:
- a
ListofExecutableElementobjects representing the declared constructors; nevernull, but may be empty if no constructors are found or if the input isnull
-
findConstructor
@Nullable static javax.lang.model.element.ExecutableElement findConstructor(javax.lang.model.element.TypeElement type, java.lang.reflect.Type... parameterTypes)
Finds a declared constructor in the specifiedTypeElementthat matches the given parameter types.This method provides a null-safe way to locate a constructor based on its parameter types. If the input
typeisnull, or no matching constructor is found,nullis returned.Example Usage
TypeElement typeElement = ...; // Obtain a valid TypeElement Type[] paramTypes = new Type[] { String.class, int.class }; ExecutableElement constructor = findConstructor(typeElement, paramTypes); if (constructor != null) { System.out.println("Found constructor: " + constructor); } else { System.out.println("Constructor not found."); }- Parameters:
type- theTypeElementrepresenting the type to search for constructorsparameterTypes- the array ofTypeobjects representing the parameter types to match- Returns:
- the matched
ExecutableElementrepresenting the constructor; may benull
-
findConstructor
@Nullable static javax.lang.model.element.ExecutableElement findConstructor(javax.lang.model.type.TypeMirror type, java.lang.reflect.Type... parameterTypes)
Finds a declared constructor in the specifiedTypeMirrorthat matches the given parameter types.This method provides a null-safe way to locate a constructor based on its parameter types. If the input
typeisnull, or no matching constructor is found,nullis returned.Example Usage
TypeMirror typeMirror = ...; // Obtain a valid TypeMirror Type[] paramTypes = new Type[] { String.class, int.class }; ExecutableElement constructor = findConstructor(typeMirror, paramTypes); if (constructor != null) { System.out.println("Found constructor: " + constructor); } else { System.out.println("Constructor not found."); }- Parameters:
type- theTypeMirrorrepresenting the type to search for constructorsparameterTypes- the array ofTypeobjects representing the parameter types to match- Returns:
- the matched
ExecutableElementrepresenting the constructor; may benull
-
findDeclaredConstructors
@Nonnull @Immutable static java.util.List<javax.lang.model.element.ExecutableElement> findDeclaredConstructors(javax.lang.model.element.TypeElement type, java.util.function.Predicate<? super javax.lang.model.element.ExecutableElement>... constructorFilters)
Retrieves and filters the list of declared constructors from the providedTypeElement.This method provides a null-safe way to obtain the constructors of a given type. If the input
typeisnull, an empty list is returned. The provided filters can be used to selectively include only those constructors that match the criteria.Example Usage
TypeElement typeElement = ...; // Obtain a valid TypeElement // Get all declared constructors List<ExecutableElement> allConstructors = findDeclaredConstructors(typeElement); // Get only constructors with specific parameter types List<ExecutableElement> filteredConstructors = findDeclaredConstructors(typeElement, constructor -> matchParameterTypes(constructor, String.class, int.class)); if (!filteredConstructors.isEmpty()) { for (ExecutableElement constructor : filteredConstructors) { System.out.println("Matching Constructor: " + constructor); } } else { System.out.println("No matching constructors found."); }- Parameters:
type- theTypeElementrepresenting the type to retrieve constructors fromconstructorFilters- optional predicates to filter the constructors; if none are provided, all constructors are included- Returns:
- a
ListofExecutableElementobjects representing the filtered declared constructors; nevernull, but may be empty if no matching constructors are found or if the input isnull
-
findDeclaredConstructors
@Nonnull @Immutable static java.util.List<javax.lang.model.element.ExecutableElement> findDeclaredConstructors(javax.lang.model.type.TypeMirror type, java.util.function.Predicate<? super javax.lang.model.element.ExecutableElement>... constructorFilters)
Retrieves and filters the list of declared constructors from the providedTypeMirror.This method provides a null-safe way to obtain the constructors of a given type. If the input
typeisnull, an empty list is returned. The provided filters can be used to selectively include only those constructors that match the criteria.- Parameters:
type- theTypeMirrorrepresenting the type to retrieve constructors fromconstructorFilters- optional predicates to filter the constructors; if none are provided, all constructors are included- Returns:
- a
ListofExecutableElementobjects representing the filtered declared constructors; nevernull, but may be empty if no matching constructors are found or if the input isnull
-
filterDeclaredConstructors
@Nonnull @Immutable static java.util.List<javax.lang.model.element.ExecutableElement> filterDeclaredConstructors(javax.lang.model.type.TypeMirror type, java.util.function.Predicate<? super javax.lang.model.element.ExecutableElement>... constructorFilters)
Filters and retrieves the list of declared constructors from the providedTypeMirror.This method is responsible for extracting all declared constructors from the given type and applying the specified filters to narrow down the results. If the input
typeisnull, an empty list is returned.Example Usage
TypeMirror typeMirror = ...; // Obtain a valid TypeMirror // Get all declared constructors List<ExecutableElement> allConstructors = filterDeclaredConstructors(typeMirror); // Get only constructors with specific parameter types List<ExecutableElement> filteredConstructors = filterDeclaredConstructors(typeMirror, constructor -> matchParameterTypes(constructor, String.class, int.class)); if (!filteredConstructors.isEmpty()) { for (ExecutableElement constructor : filteredConstructors) { System.out.println("Matching Constructor: " + constructor); } } else { System.out.println("No matching constructors found."); }- Parameters:
type- theTypeMirrorrepresenting the type to retrieve constructors fromconstructorFilters- optional predicates to filter the constructors; if none are provided, all constructors are included- Returns:
- a
ListofExecutableElementobjects representing the filtered declared constructors; nevernull, but may be empty if no matching constructors are found or if the input isnull
-
-