Class DefaultBuilderProvider
- java.lang.Object
-
- org.mapstruct.ap.spi.DefaultBuilderProvider
-
- All Implemented Interfaces:
BuilderProvider
- Direct Known Subclasses:
ImmutablesBuilderProvider
public class DefaultBuilderProvider extends Object implements BuilderProvider
Default implementation ofBuilderProvider
. The default builder provider considers all public static parameterless methods of aTypeMirror
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 initialTypeMirror
if such a combination is found theBuilderInfo
is created with those 2 methods. Example:
In the example above, when searching for a builder for thepublic 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 ); } } }
Person
type. ThePerson#builder
method would be a builder creation candidate. Then the return type ofPerson#builder
,Builder
, is investigated for a parameterless method that returnsPerson
. WhenBuilder#create
is found theBuilderInfo
is created with thePerson#builder
as a builder creation method andBuilder#create
as a build method.IMPORTANT: Types from the
java
andjavax
packages are excluded from inspection- Author:
- Filip Hrisafov
-
-
Field Summary
Fields Modifier and Type Field Description protected Elements
elementUtils
protected Types
typeUtils
-
Constructor Summary
Constructors Constructor Description DefaultBuilderProvider()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected BuilderInfo
findBuilderInfo(TypeElement typeElement)
Find theBuilderInfo
for the giventypeElement
.protected BuilderInfo
findBuilderInfo(TypeElement typeElement, boolean checkParent)
BuilderInfo
findBuilderInfo(TypeMirror type)
Find the builder information, if any, for thetype
.protected Collection<ExecutableElement>
findBuildMethods(TypeElement builderElement, TypeElement typeElement)
Searches for a build method fortypeElement
within thebuilderElement
.protected TypeElement
getTypeElement(TypeMirror type)
Find theTypeElement
for the givenTypeMirror
.void
init(MapStructProcessingEnvironment processingEnvironment)
Initializes the builder provider with the MapStruct processing environment.protected boolean
isBuildMethod(ExecutableElement buildMethod, TypeElement typeElement)
Deprecated.protected boolean
isBuildMethod(ExecutableElement buildMethod, DeclaredType builderType, TypeElement typeElement)
Checks if thebuildMethod
is a method that creates thetypeElement
as a member of thebuilderType
.protected boolean
isPossibleBuilderCreationMethod(ExecutableElement method, TypeElement typeElement)
Checks if themethod
is a possible builder creation method.protected boolean
shouldIgnore(TypeElement typeElement)
Whether thetypeElement
should be ignored, i.e. not used in inspection.
-
-
-
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 interfaceBuilderProvider
- Parameters:
processingEnvironment
- environment for facilities
-
findBuilderInfo
public BuilderInfo findBuilderInfo(TypeMirror type)
Description copied from interface:BuilderProvider
Find the builder information, if any, for thetype
.- Specified by:
findBuilderInfo
in interfaceBuilderProvider
- Parameters:
type
- the type for which a builder should be found- Returns:
- the builder info for the
type
if it exists, ornull
if there is no builder
-
getTypeElement
protected TypeElement getTypeElement(TypeMirror type)
Find theTypeElement
for the givenTypeMirror
.- Parameters:
type
- for which theTypeElement
needs to be found/- Returns:
- the type element or
null
if theTypeMirror
is not aDeclaredType
and the declared type element is notTypeElement
- Throws:
TypeHierarchyErroneousException
- if theTypeMirror
is of kindTypeKind.ERROR
-
findBuilderInfo
protected BuilderInfo findBuilderInfo(TypeElement typeElement)
Find theBuilderInfo
for the giventypeElement
.The default implementation iterates over all the methods in
typeElement
and usesisPossibleBuilderCreationMethod(ExecutableElement, TypeElement)
andfindBuildMethods(TypeElement, TypeElement)
to create theBuilderInfo
.The default implementation uses
shouldIgnore(TypeElement)
to check if thetypeElement
should be ignored.In case there are multiple
BuilderInfo
then aMoreThanOneBuilderCreationMethodException
is thrown.- Parameters:
typeElement
- the type element for which a builder searched- Returns:
- the
BuilderInfo
ornull
if no builder was found for the typefindBuildMethods(TypeElement, TypeElement)
- Throws:
MoreThanOneBuilderCreationMethodException
- if there are multiple builder creation methods
-
findBuilderInfo
protected BuilderInfo findBuilderInfo(TypeElement typeElement, boolean checkParent)
-
isPossibleBuilderCreationMethod
protected boolean isPossibleBuilderCreationMethod(ExecutableElement method, TypeElement typeElement)
Checks if themethod
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 thetypeElement
- Parameters:
method
- The method that needs to be checkedtypeElement
- the enclosing element of the method, i.e. the type in which the method is located in- Returns:
true
if themethod
is a possible builder creation method,false
otherwise
-
findBuildMethods
protected Collection<ExecutableElement> findBuildMethods(TypeElement builderElement, TypeElement typeElement)
Searches for a build method fortypeElement
within thebuilderElement
.The default implementation iterates over each method in
builderElement
and usesisBuildMethod(ExecutableElement, DeclaredType, TypeElement)
to check if the method is a build method fortypeElement
.The default implementation uses
shouldIgnore(TypeElement)
to check if thebuilderElement
should be ignored, i.e. not checked for build elements.- Parameters:
builderElement
- the element for the buildertypeElement
- the element for the type that is being built- Returns:
- the build method for the
typeElement
if it exists, ornull
if it does notbuild
-
isBuildMethod
@Deprecated protected boolean isBuildMethod(ExecutableElement buildMethod, TypeElement typeElement)
Deprecated.
-
isBuildMethod
protected boolean isBuildMethod(ExecutableElement buildMethod, DeclaredType builderType, TypeElement typeElement)
Checks if thebuildMethod
is a method that creates thetypeElement
as a member of thebuilderType
.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 checkedbuilderType
- the type of the builder in which thebuildMethod
is located intypeElement
- the type element that needs to be built- Returns:
true
if thebuildMethod
is a build method fortypeElement
,false
otherwise
-
shouldIgnore
protected boolean shouldIgnore(TypeElement typeElement)
Whether thetypeElement
should be ignored, i.e. not used in inspection.The default implementations ignores
null
elements and elements that belong to thejava
andjavax
packages- Parameters:
typeElement
- that needs to be checked- Returns:
true
if the element should be ignored,false
otherwise
-
-