Class DefaultAccessorNamingStrategy

    • 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 Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String getCollectionGetterName​(java.lang.String property)
      Returns the getter name of the given collection property.
      java.lang.String getElementName​(javax.lang.model.element.ExecutableElement adderMethod)
      Adder methods are used to add elements to collections on a target bean.
      MethodType getMethodType​(javax.lang.model.element.ExecutableElement method)
      Returns the type of the given method.
      java.lang.String getPropertyName​(javax.lang.model.element.ExecutableElement getterOrSetterMethod)
      Analyzes the method (getter or setter) and derives the property name.
      protected static java.lang.String getQualifiedName​(javax.lang.model.type.TypeMirror type)
      Helper method, to obtain the fully qualified name of a type.
      void init​(MapStructProcessingEnvironment processingEnvironment)
      Initializes the accessor naming strategy with the MapStruct processing environment.
      boolean isAdderMethod​(javax.lang.model.element.ExecutableElement method)
      Returns true when the ExecutableElement is an adder method.
      private boolean isAdderWithUpperCase4thCharacter​(javax.lang.model.element.ExecutableElement method)
      Checks that the method is an adder with an upper case 4th character.
      protected boolean isFluentSetter​(javax.lang.model.element.ExecutableElement method)  
      boolean isGetterMethod​(javax.lang.model.element.ExecutableElement method)
      Returns true when the ExecutableElement is a getter method.
      boolean isPresenceCheckMethod​(javax.lang.model.element.ExecutableElement method)
      Returns true when the ExecutableElement is a presence check method that checks if the corresponding property is present (e.g.
      boolean isSetterMethod​(javax.lang.model.element.ExecutableElement method)
      Returns true when the ExecutableElement is a setter method.
      • 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

      • DefaultAccessorNamingStrategy

        public DefaultAccessorNamingStrategy()
    • Method Detail

      • isGetterMethod

        public boolean isGetterMethod​(javax.lang.model.element.ExecutableElement method)
        Returns true when the ExecutableElement is a getter method. A method is a getter when it has no parameters, starts with 'get' and the return type is any type other than void, OR the getter starts with 'is' and the type returned is a primitive or the wrapper for boolean. NOTE: the latter does strictly not comply to the bean convention. The remainder of the name is supposed to reflect the property name.

        The calling MapStruct code guarantees that the given method has no arguments.

        Parameters:
        method - to be analyzed
        Returns:
        true when the method is a getter.
      • isSetterMethod

        public boolean isSetterMethod​(javax.lang.model.element.ExecutableElement method)
        Returns true when the ExecutableElement is a setter method. A setter starts with 'set'. The remainder of the name is supposed to reflect the property name.

        The calling MapStruct code guarantees that there's only one argument.

        Parameters:
        method - to be analyzed
        Returns:
        true when the method is a setter.
      • isFluentSetter

        protected boolean isFluentSetter​(javax.lang.model.element.ExecutableElement method)
      • isAdderWithUpperCase4thCharacter

        private boolean isAdderWithUpperCase4thCharacter​(javax.lang.model.element.ExecutableElement method)
        Checks that the method is an adder with an upper case 4th character. The reason for this is that methods such as address(String address) are considered as setter and addName(String name) too. We need to make sure that addName is considered as an adder and address is considered as a setter.
        Parameters:
        method - the method that needs to be checked
        Returns:
        true if the method is an adder with an upper case 4h character, false otherwise
      • isAdderMethod

        public boolean isAdderMethod​(javax.lang.model.element.ExecutableElement method)
        Returns true when the ExecutableElement is an adder method. An adder method starts with 'add'. The remainder of the name is supposed to reflect the singular property name (as opposed to plural) of its corresponding property. For example: property "children", but "addChild". See also getElementName(ExecutableElement).

        The calling MapStruct code guarantees there's only one argument.

        Parameters:
        method - to be analyzed
        Returns:
        true when the method is an adder method.
      • isPresenceCheckMethod

        public boolean isPresenceCheckMethod​(javax.lang.model.element.ExecutableElement method)
        Returns true when the ExecutableElement is a presence check method that checks if the corresponding property is present (e.g. not null, not nil, ..). A presence check method method starts with 'has'. The remainder of the name is supposed to reflect the property name.

        The calling MapStruct code guarantees there's no argument and that the return type is boolean or a Boolean

        Parameters:
        method - to be analyzed
        Returns:
        true when the method is a presence check method.
      • getPropertyName

        public java.lang.String getPropertyName​(javax.lang.model.element.ExecutableElement getterOrSetterMethod)
        Analyzes the method (getter or setter) and derives the property name. See isGetterMethod(ExecutableElement) isSetterMethod(ExecutableElement). The first three ('get' / 'set' scenario) characters are removed from the simple name, or the first 2 characters ('is' scenario). From the remainder the first character is made into small case (to counter camel casing) and the result forms the property name.
        Specified by:
        getPropertyName in interface AccessorNamingStrategy
        Parameters:
        getterOrSetterMethod - getter or setter method.
        Returns:
        the property name.
      • getElementName

        public java.lang.String getElementName​(javax.lang.model.element.ExecutableElement adderMethod)
        Adder methods are used to add elements to collections on a target bean. A typical use case is JPA. The convention is that the element name will be equal to the remainder of the add method. Example: 'addElement' element name will be 'element'.
        Specified by:
        getElementName in interface AccessorNamingStrategy
        Parameters:
        adderMethod - getter or setter method.
        Returns:
        the property name.
      • getQualifiedName

        protected static java.lang.String getQualifiedName​(javax.lang.model.type.TypeMirror type)
        Helper method, to obtain the fully qualified name of a type.
        Parameters:
        type - input type
        Returns:
        fully qualified name of type when the type is a DeclaredType, null when otherwise.
      • getCollectionGetterName

        public java.lang.String getCollectionGetterName​(java.lang.String property)
        Description copied from interface: AccessorNamingStrategy
        Returns the getter name of the given collection property.

        The default implementation will e.g. return "getItems" for "items".

        Specified by:
        getCollectionGetterName in interface AccessorNamingStrategy
        Parameters:
        property - to be getterOrSetterMethod.
        Returns:
        getter name for collection properties