Class DefaultBuilderProvider

  • All Implemented Interfaces:
    BuilderProvider
    Direct Known Subclasses:
    ImmutablesBuilderProvider

    public class DefaultBuilderProvider
    extends Object
    implements BuilderProvider
    Default implementation of BuilderProvider. The default builder provider considers all public static parameterless methods of a TypeMirror as potential builder creation methods. For each potential builder creation method checks in the return type of the method if there exists a method that returns the initial TypeMirror if such a combination is found the BuilderInfo is created with those 2 methods. Example:
    
     public class Person {
    
         private final String firstName;
         private final String lastName;
    
         private Person(String firstName, String lastName) {
             this.firstName = firstName;
             this.lastName = lastName;
         }
    
         //getters
    
         public static Builder builder() {
             return new Builder();
         }
    
         public static class Builder {
    
             private String firstName;
             private String lastName;
    
             private Builder() {}
    
             //fluent setters
    
             public Person create() {
                 return new Person( firstName, lastName );
             }
         }
     }
     
    In the example above, when searching for a builder for the Person type. The Person#builder method would be a builder creation candidate. Then the return type of Person#builder, Builder, is investigated for a parameterless method that returns Person. When Builder#create is found the BuilderInfo is created with the Person#builder as a builder creation method and Builder#create as a build method.

    IMPORTANT: Types from the java and javax packages are excluded from inspection

    Author:
    Filip Hrisafov
    • Field Detail

      • elementUtils

        protected Elements elementUtils
      • typeUtils

        protected Types typeUtils
    • Constructor Detail

      • DefaultBuilderProvider

        public DefaultBuilderProvider()
    • Method Detail

      • init

        public void init​(MapStructProcessingEnvironment processingEnvironment)
        Description copied from interface: BuilderProvider
        Initializes the builder provider with the MapStruct processing environment.
        Specified by:
        init in interface BuilderProvider
        Parameters:
        processingEnvironment - environment for facilities
      • findBuilderInfo

        public BuilderInfo findBuilderInfo​(TypeMirror type)
        Description copied from interface: BuilderProvider
        Find the builder information, if any, for the type.
        Specified by:
        findBuilderInfo in interface BuilderProvider
        Parameters:
        type - the type for which a builder should be found
        Returns:
        the builder info for the type if it exists, or null if there is no builder
      • isPossibleBuilderCreationMethod

        protected boolean isPossibleBuilderCreationMethod​(ExecutableElement method,
                                                          TypeElement typeElement)
        Checks if the method is a possible builder creation method.

        The default implementation considers a method as a possible creation method if the following is satisfied:

        • The method has no parameters
        • It is a public static method
        • The return type of the method is not the same as the typeElement
        Parameters:
        method - The method that needs to be checked
        typeElement - the enclosing element of the method, i.e. the type in which the method is located in
        Returns:
        true if the method is a possible builder creation method, false otherwise
      • findBuildMethods

        protected Collection<ExecutableElement> findBuildMethods​(TypeElement builderElement,
                                                                 TypeElement typeElement)
        Searches for a build method for typeElement within the builderElement.

        The default implementation iterates over each method in builderElement and uses isBuildMethod(ExecutableElement, TypeElement) to check if the method is a build method for typeElement.

        The default implementation uses shouldIgnore(TypeElement) to check if the builderElement should be ignored, i.e. not checked for build elements.

        If there are multiple methods that satisfy isBuildMethod(ExecutableElement, TypeElement) and one of those methods is names build that that method would be considered as a build method.

        Parameters:
        builderElement - the element for the builder
        typeElement - the element for the type that is being built
        Returns:
        the build method for the typeElement if it exists, or null if it does not build
      • isBuildMethod

        protected boolean isBuildMethod​(ExecutableElement buildMethod,
                                        TypeElement typeElement)
        Checks if the buildMethod is a method that creates typeElement.

        The default implementation considers a method to be a build method if the following is satisfied:

        • The method has no parameters
        • The method is public
        • The return type of method is assignable to the typeElement
        Parameters:
        buildMethod - the method that should be checked
        typeElement - the type element that needs to be built
        Returns:
        true if the buildMethod is a build method for typeElement, false otherwise
      • shouldIgnore

        protected boolean shouldIgnore​(TypeElement typeElement)
        Whether the typeElement should be ignored, i.e. not used in inspection.

        The default implementations ignores null elements and elements that belong to the java and javax packages

        Parameters:
        typeElement - that needs to be checked
        Returns:
        true if the element should be ignored, false otherwise