public class DefaultBuilderProvider extends Object implements BuilderProvider
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
Modifier and Type | Field and Description |
---|---|
protected Elements |
elementUtils |
protected Types |
typeUtils |
Constructor and Description |
---|
DefaultBuilderProvider() |
Modifier and Type | Method and Description |
---|---|
protected BuilderInfo |
findBuilderInfo(TypeElement typeElement)
Find the
BuilderInfo for the given typeElement . |
BuilderInfo |
findBuilderInfo(TypeMirror type)
Find the builder information, if any, for the
type . |
protected Collection<ExecutableElement> |
findBuildMethods(TypeElement builderElement,
TypeElement typeElement)
Searches for a build method for
typeElement within the builderElement . |
protected TypeElement |
getTypeElement(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(ExecutableElement buildMethod,
TypeElement typeElement)
Checks if the
buildMethod is a method that creates typeElement . |
protected boolean |
isPossibleBuilderCreationMethod(ExecutableElement method,
TypeElement typeElement)
Checks if the
method is a possible builder creation method. |
protected boolean |
shouldIgnore(TypeElement typeElement)
Whether the
typeElement should be ignored, i.e. not used in inspection. |
public void init(MapStructProcessingEnvironment processingEnvironment)
BuilderProvider
init
in interface BuilderProvider
processingEnvironment
- environment for facilitiespublic BuilderInfo findBuilderInfo(TypeMirror type)
BuilderProvider
type
.findBuilderInfo
in interface BuilderProvider
type
- the type for which a builder should be foundtype
if it exists, or null
if there is no builderprotected TypeElement getTypeElement(TypeMirror type)
TypeElement
for the given TypeMirror
.type
- for which the TypeElement
needs to be found/null
if the TypeMirror
is not a DeclaredType
and the declared type element is not TypeElement
TypeHierarchyErroneousException
- if the TypeMirror
is of kind TypeKind.ERROR
protected BuilderInfo findBuilderInfo(TypeElement typeElement)
BuilderInfo
for the given typeElement
.
The default implementation iterates over all the methods in typeElement
and uses
isPossibleBuilderCreationMethod(ExecutableElement, TypeElement)
and
findBuildMethods(TypeElement, TypeElement)
to create the
BuilderInfo
.
The default implementation uses shouldIgnore(TypeElement)
to check if the
typeElement
should be ignored.
In case there are multiple BuilderInfo
then a MoreThanOneBuilderCreationMethodException
is
thrown.
typeElement
- the type element for which a builder searchedBuilderInfo
or null
if no builder was found for the type
findBuildMethods(TypeElement, TypeElement)
MoreThanOneBuilderCreationMethodException
- if there are multiple builder creation methodsprotected boolean isPossibleBuilderCreationMethod(ExecutableElement method, TypeElement typeElement)
method
is a possible builder creation method.
The default implementation considers a method as a possible creation method if the following is satisfied:
public static
methodmethod
is not the same as the typeElement
method
- The method that needs to be checkedtypeElement
- the enclosing element of the method, i.e. the type in which the method is located intrue
if the method
is a possible builder creation method, false
otherwiseprotected Collection<ExecutableElement> findBuildMethods(TypeElement builderElement, TypeElement typeElement)
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.
builderElement
- the element for the buildertypeElement
- the element for the type that is being builttypeElement
if it exists, or null
if it does not
build
protected boolean isBuildMethod(ExecutableElement buildMethod, TypeElement typeElement)
buildMethod
is a method that creates typeElement
.
The default implementation considers a method to be a build method if the following is satisfied:
typeElement
buildMethod
- the method that should be checkedtypeElement
- the type element that needs to be builttrue
if the buildMethod
is a build method for typeElement
, false
otherwiseprotected boolean shouldIgnore(TypeElement typeElement)
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
typeElement
- that needs to be checkedtrue
if the element should be ignored, false
otherwiseCopyright © 2012-2021 MapStruct Authors; All rights reserved. Released under the Apache Software License 2.0.