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
| Constructor and Description |
|---|
DefaultBuilderProvider() |
| Modifier and Type | Method and Description |
|---|---|
protected BuilderInfo |
findBuilderInfo(TypeElement typeElement,
Elements elements,
Types types)
Find the
BuilderInfo for the given typeElement. |
BuilderInfo |
findBuilderInfo(TypeMirror type,
Elements elements,
Types types)
Find the builder information, if any, for the
type. |
protected Collection<ExecutableElement> |
findBuildMethods(TypeElement builderElement,
TypeElement typeElement,
Types types)
Searches for a build method for
typeElement within the builderElement. |
protected TypeElement |
getTypeElement(TypeMirror type)
Find the
TypeElement for the given TypeMirror. |
protected boolean |
isBuildMethod(ExecutableElement buildMethod,
TypeElement typeElement,
Types types)
Checks if the
buildMethod is a method that creates typeElement. |
protected boolean |
isPossibleBuilderCreationMethod(ExecutableElement method,
TypeElement typeElement,
Types types)
Checks if the
method is a possible builder creation method. |
protected boolean |
shouldIgnore(TypeElement typeElement)
Whether the
typeElement should be ignored, i.e. |
public BuilderInfo findBuilderInfo(TypeMirror type, Elements elements, Types types)
BuilderProvidertype.findBuilderInfo in interface BuilderProvidertype - the type for which a builder should be foundelements - the util elements that can be used for operations on program elementstypes - the util types that can be used for operations on TypeMirror(s)type 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 TypeElementTypeHierarchyErroneousException - if the TypeMirror is of kind TypeKind.ERRORprotected BuilderInfo findBuilderInfo(TypeElement typeElement, Elements elements, Types types)
BuilderInfo for the given typeElement.
The default implementation iterates over all the methods in typeElement and uses
isPossibleBuilderCreationMethod(ExecutableElement, TypeElement, Types) and
findBuildMethods(TypeElement, TypeElement, Types) 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 searchedelements - the util elements that can be used for operating on the type elementtypes - the util types that can be used for operation on TypeMirror(s)BuilderInfo or null if no builder was found for the type
findBuildMethods(TypeElement, TypeElement, Types)MoreThanOneBuilderCreationMethodException - if there are multiple builder creation methodsprotected boolean isPossibleBuilderCreationMethod(ExecutableElement method, TypeElement typeElement, Types types)
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 typeElementmethod - The method that needs to be checkedtypeElement - the enclosing element of the method, i.e. the type in which the method is located intypes - the util types that can be used for operations on TypeMirror(s)true if the method is a possible builder creation method, false otherwiseprotected Collection<ExecutableElement> findBuildMethods(TypeElement builderElement, TypeElement typeElement, Types types)
typeElement within the builderElement.
The default implementation iterates over each method in builderElement and uses
isBuildMethod(ExecutableElement, TypeElement, Types) 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, Types) 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 builttypes - the util types tat can be used for operations on TypeMirror(s)typeElement if it exists, or null if it does not
buildprotected boolean isBuildMethod(ExecutableElement buildMethod, TypeElement typeElement, Types types)
buildMethod is a method that creates typeElement.
The default implementation considers a method to be a build method if the following is satisfied:
typeElementbuildMethod - the method that should be checkedtypeElement - the type element that needs to be builttypes - the util types that can be used for operations on TypeMirror(s)true 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–2018. All rights reserved.