Class DefaultBuilderProvider

  • All Implemented Interfaces:
    BuilderProvider
    Direct Known Subclasses:
    ImmutablesBuilderProvider

    public class DefaultBuilderProvider
    extends java.lang.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

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected javax.lang.model.util.Elements elementUtils  
      private static java.util.regex.Pattern JAVA_JAVAX_PACKAGE  
      protected javax.lang.model.util.Types typeUtils  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected BuilderInfo findBuilderInfo​(javax.lang.model.element.TypeElement typeElement)
      Find the BuilderInfo for the given typeElement.
      BuilderInfo findBuilderInfo​(javax.lang.model.type.TypeMirror type)
      Find the builder information, if any, for the type.
      protected java.util.Collection<javax.lang.model.element.ExecutableElement> findBuildMethods​(javax.lang.model.element.TypeElement builderElement, javax.lang.model.element.TypeElement typeElement)
      Searches for a build method for typeElement within the builderElement.
      protected javax.lang.model.element.TypeElement getTypeElement​(javax.lang.model.type.TypeMirror type)
      Find the TypeElement for the given TypeMirror.
      void init​(MapStructProcessingEnvironment processingEnvironment)
      Initializes the builder provider with the MapStruct processing environment.
      protected boolean isBuildMethod​(javax.lang.model.element.ExecutableElement buildMethod, javax.lang.model.element.TypeElement typeElement)
      Checks if the buildMethod is a method that creates typeElement.
      protected boolean isPossibleBuilderCreationMethod​(javax.lang.model.element.ExecutableElement method, javax.lang.model.element.TypeElement typeElement)
      Checks if the method is a possible builder creation method.
      protected boolean shouldIgnore​(javax.lang.model.element.TypeElement typeElement)
      Whether the typeElement should be ignored, i.e.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • JAVA_JAVAX_PACKAGE

        private static final java.util.regex.Pattern JAVA_JAVAX_PACKAGE
      • elementUtils

        protected javax.lang.model.util.Elements elementUtils
      • typeUtils

        protected javax.lang.model.util.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​(javax.lang.model.type.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
      • getTypeElement

        protected javax.lang.model.element.TypeElement getTypeElement​(javax.lang.model.type.TypeMirror type)
        Find the TypeElement for the given TypeMirror.
        Parameters:
        type - for which the TypeElement needs to be found/
        Returns:
        the type element or null if the TypeMirror is not a DeclaredType and the declared type element is not TypeElement
        Throws:
        TypeHierarchyErroneousException - if the TypeMirror is of kind TypeKind.ERROR
      • isPossibleBuilderCreationMethod

        protected boolean isPossibleBuilderCreationMethod​(javax.lang.model.element.ExecutableElement method,
                                                          javax.lang.model.element.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 java.util.Collection<javax.lang.model.element.ExecutableElement> findBuildMethods​(javax.lang.model.element.TypeElement builderElement,
                                                                                                    javax.lang.model.element.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​(javax.lang.model.element.ExecutableElement buildMethod,
                                        javax.lang.model.element.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​(javax.lang.model.element.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