Interface ConstructorUtils

  • All Superinterfaces:
    Utils

    public interface ConstructorUtils
    extends Utils
    The utils class for constructor
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    Constructor, ExecutableElement, ElementKind.CONSTRUCTOR
    • 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 provided TypeMirror.
      static javax.lang.model.element.ExecutableElement findConstructor​(javax.lang.model.element.TypeElement type, java.lang.reflect.Type... parameterTypes)
      Finds a declared constructor in the specified TypeElement that matches the given parameter types.
      static javax.lang.model.element.ExecutableElement findConstructor​(javax.lang.model.type.TypeMirror type, java.lang.reflect.Type... parameterTypes)
      Finds a declared constructor in the specified TypeMirror that 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 provided TypeElement.
      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 provided TypeMirror.
      static java.util.List<javax.lang.model.element.ExecutableElement> getDeclaredConstructors​(javax.lang.model.element.TypeElement type)
      Retrieves the list of declared constructors from the provided TypeElement.
      static java.util.List<javax.lang.model.element.ExecutableElement> getDeclaredConstructors​(javax.lang.model.type.TypeMirror type)
      Retrieves the list of declared constructors from the provided TypeMirror.
    • 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 provided TypeElement.

        This method provides a null-safe way to obtain the constructors of a given type. If the input type is null, 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 - the TypeElement representing the type to retrieve constructors from
        Returns:
        a List of ExecutableElement objects representing the declared constructors; never null, but may be empty if no constructors are found or if the input is null
      • 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 provided TypeMirror.

        This method provides a null-safe way to obtain the constructors of a given type. If the input type is null, 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 - the TypeMirror representing the type to retrieve constructors from
        Returns:
        a List of ExecutableElement objects representing the declared constructors; never null, but may be empty if no constructors are found or if the input is null
      • 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 specified TypeElement that matches the given parameter types.

        This method provides a null-safe way to locate a constructor based on its parameter types. If the input type is null, or no matching constructor is found, null is 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 - the TypeElement representing the type to search for constructors
        parameterTypes - the array of Type objects representing the parameter types to match
        Returns:
        the matched ExecutableElement representing the constructor; may be null
      • 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 specified TypeMirror that matches the given parameter types.

        This method provides a null-safe way to locate a constructor based on its parameter types. If the input type is null, or no matching constructor is found, null is 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 - the TypeMirror representing the type to search for constructors
        parameterTypes - the array of Type objects representing the parameter types to match
        Returns:
        the matched ExecutableElement representing the constructor; may be null
      • 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 provided TypeElement.

        This method provides a null-safe way to obtain the constructors of a given type. If the input type is null, 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 - the TypeElement representing the type to retrieve constructors from
        constructorFilters - optional predicates to filter the constructors; if none are provided, all constructors are included
        Returns:
        a List of ExecutableElement objects representing the filtered declared constructors; never null, but may be empty if no matching constructors are found or if the input is null
      • 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 provided TypeMirror.

        This method provides a null-safe way to obtain the constructors of a given type. If the input type is null, an empty list is returned. The provided filters can be used to selectively include only those constructors that match the criteria.

        Parameters:
        type - the TypeMirror representing the type to retrieve constructors from
        constructorFilters - optional predicates to filter the constructors; if none are provided, all constructors are included
        Returns:
        a List of ExecutableElement objects representing the filtered declared constructors; never null, but may be empty if no matching constructors are found or if the input is null
      • 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 provided TypeMirror.

        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 type is null, 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 - the TypeMirror representing the type to retrieve constructors from
        constructorFilters - optional predicates to filter the constructors; if none are provided, all constructors are included
        Returns:
        a List of ExecutableElement objects representing the filtered declared constructors; never null, but may be empty if no matching constructors are found or if the input is null