Class DefaultBuilderProvider
- All Implemented Interfaces:
BuilderProvider
- Direct Known Subclasses:
ImmutablesBuilderProvider
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected BuilderInfofindBuilderInfo(TypeElement typeElement) Find theBuilderInfofor the giventypeElement.protected BuilderInfofindBuilderInfo(TypeElement typeElement, boolean checkParent) findBuilderInfo(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.
-
Field Details
-
elementUtils
-
typeUtils
-
-
Constructor Details
-
DefaultBuilderProvider
public DefaultBuilderProvider()
-
-
Method Details
-
init
Description copied from interface:BuilderProviderInitializes the builder provider with the MapStruct processing environment.- Specified by:
initin interfaceBuilderProvider- Parameters:
processingEnvironment- environment for facilities
-
findBuilderInfo
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
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
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
-
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.- See Also:
-
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
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
-
isBuildMethod(ExecutableElement, DeclaredType, TypeElement)instead