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 aTypeMirroras 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 initialTypeMirrorif such a combination is found theBuilderInfois 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 ); } } }Persontype. ThePerson#buildermethod would be a builder creation candidate. Then the return type ofPerson#builder,Builder, is investigated for a parameterless method that returnsPerson. WhenBuilder#createis found theBuilderInfois created with thePerson#builderas a builder creation method andBuilder#createas a build method.IMPORTANT: Types from the
javaandjavaxpackages are excluded from inspection- Author:
- Filip Hrisafov
-
-
Field Summary
Fields Modifier and Type Field Description protected ElementselementUtilsprotected TypestypeUtils
-
Constructor Summary
Constructors Constructor Description DefaultBuilderProvider()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected BuilderInfofindBuilderInfo(TypeElement typeElement)Find theBuilderInfofor the giventypeElement.protected BuilderInfofindBuilderInfo(TypeElement typeElement, boolean checkParent)BuilderInfofindBuilderInfo(TypeMirror type)Find the builder information, if any, for thetype.protected Collection<ExecutableElement>findBuildMethods(TypeElement builderElement, TypeElement typeElement)Searches for a build method fortypeElementwithin thebuilderElement.protected TypeElementgetTypeElement(TypeMirror type)Find theTypeElementfor the givenTypeMirror.voidinit(MapStructProcessingEnvironment processingEnvironment)Initializes the builder provider with the MapStruct processing environment.protected booleanisBuildMethod(ExecutableElement buildMethod, TypeElement typeElement)Deprecated.protected booleanisBuildMethod(ExecutableElement buildMethod, DeclaredType builderType, TypeElement typeElement)Checks if thebuildMethodis a method that creates thetypeElementas a member of thebuilderType.protected booleanisPossibleBuilderCreationMethod(ExecutableElement method, TypeElement typeElement)Checks if themethodis a possible builder creation method.protected booleanshouldIgnore(TypeElement typeElement)Whether thetypeElementshould be ignored, i.e. not used in inspection.
-
-
-
Method Detail
-
init
public void init(MapStructProcessingEnvironment processingEnvironment)
Description copied from interface:BuilderProviderInitializes the builder provider with the MapStruct processing environment.- Specified by:
initin interfaceBuilderProvider- Parameters:
processingEnvironment- environment for facilities
-
findBuilderInfo
public BuilderInfo findBuilderInfo(TypeMirror type)
Description copied from interface:BuilderProviderFind the builder information, if any, for thetype.- Specified by:
findBuilderInfoin interfaceBuilderProvider- Parameters:
type- the type for which a builder should be found- Returns:
- the builder info for the
typeif it exists, ornullif there is no builder
-
getTypeElement
protected TypeElement getTypeElement(TypeMirror type)
Find theTypeElementfor the givenTypeMirror.- Parameters:
type- for which theTypeElementneeds to be found/- Returns:
- the type element or
nullif theTypeMirroris not aDeclaredTypeand the declared type element is notTypeElement - Throws:
TypeHierarchyErroneousException- if theTypeMirroris of kindTypeKind.ERROR
-
findBuilderInfo
protected BuilderInfo findBuilderInfo(TypeElement typeElement)
Find theBuilderInfofor the giventypeElement.The default implementation iterates over all the methods in
typeElementand usesisPossibleBuilderCreationMethod(ExecutableElement, TypeElement)andfindBuildMethods(TypeElement, TypeElement)to create theBuilderInfo.The default implementation uses
shouldIgnore(TypeElement)to check if thetypeElementshould be ignored.In case there are multiple
BuilderInfothen aMoreThanOneBuilderCreationMethodExceptionis thrown.- Parameters:
typeElement- the type element for which a builder searched- Returns:
- the
BuilderInfoornullif 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 themethodis 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 staticmethod - The return type of the
methodis 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:
trueif themethodis a possible builder creation method,falseotherwise
-
findBuildMethods
protected Collection<ExecutableElement> findBuildMethods(TypeElement builderElement, TypeElement typeElement)
Searches for a build method fortypeElementwithin thebuilderElement.The default implementation iterates over each method in
builderElementand usesisBuildMethod(ExecutableElement, DeclaredType, TypeElement)to check if the method is a build method fortypeElement.The default implementation uses
shouldIgnore(TypeElement)to check if thebuilderElementshould 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
typeElementif it exists, ornullif 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 thebuildMethodis a method that creates thetypeElementas 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 thebuildMethodis located intypeElement- the type element that needs to be built- Returns:
trueif thebuildMethodis a build method fortypeElement,falseotherwise
-
shouldIgnore
protected boolean shouldIgnore(TypeElement typeElement)
Whether thetypeElementshould be ignored, i.e. not used in inspection.The default implementations ignores
nullelements and elements that belong to thejavaandjavaxpackages- Parameters:
typeElement- that needs to be checked- Returns:
trueif the element should be ignored,falseotherwise
-
-