Skip to content

Commit b651ad3

Browse files
authored
mapstruct#1649 Improvement: builder for Mapper/Decorator/GeneratedType
1 parent 2977c2e commit b651ad3

4 files changed

Lines changed: 96 additions & 113 deletions

File tree

processor/src/main/java/org/mapstruct/ap/internal/model/Decorator.java

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import javax.lang.model.element.ElementKind;
1313
import javax.lang.model.element.TypeElement;
14-
import javax.lang.model.util.Elements;
1514

1615
import org.mapstruct.ap.internal.model.common.Accessibility;
1716
import org.mapstruct.ap.internal.model.common.Type;
@@ -27,28 +26,17 @@
2726
*/
2827
public class Decorator extends GeneratedType {
2928

30-
public static class Builder {
29+
public static class Builder extends GeneratedTypeBuilder<Builder> {
3130

32-
private Elements elementUtils;
33-
private TypeFactory typeFactory;
3431
private TypeElement mapperElement;
3532
private DecoratedWithPrism decoratorPrism;
36-
private List<MappingMethod> methods;
37-
private Options options;
38-
private VersionInformation versionInformation;
33+
3934
private boolean hasDelegateConstructor;
4035
private String implName;
4136
private String implPackage;
42-
private SortedSet<Type> extraImportedTypes;
4337

44-
public Builder elementUtils(Elements elementUtils) {
45-
this.elementUtils = elementUtils;
46-
return this;
47-
}
48-
49-
public Builder typeFactory(TypeFactory typeFactory) {
50-
this.typeFactory = typeFactory;
51-
return this;
38+
public Builder() {
39+
super( Builder.class );
5240
}
5341

5442
public Builder mapperElement(TypeElement mapperElement) {
@@ -61,21 +49,6 @@ public Builder decoratorPrism(DecoratedWithPrism decoratorPrism) {
6149
return this;
6250
}
6351

64-
public Builder methods(List<MappingMethod> methods) {
65-
this.methods = methods;
66-
return this;
67-
}
68-
69-
public Builder options(Options options) {
70-
this.options = options;
71-
return this;
72-
}
73-
74-
public Builder versionInformation(VersionInformation versionInformation) {
75-
this.versionInformation = versionInformation;
76-
return this;
77-
}
78-
7952
public Builder hasDelegateConstructor(boolean hasDelegateConstructor) {
8053
this.hasDelegateConstructor = hasDelegateConstructor;
8154
return this;
@@ -91,20 +64,15 @@ public Builder implPackage(String implPackage) {
9164
return this;
9265
}
9366

94-
public Builder extraImports(SortedSet<Type> extraImportedTypes) {
95-
this.extraImportedTypes = extraImportedTypes;
96-
return this;
97-
}
98-
9967
public Decorator build() {
10068
String implementationName = implName.replace( Mapper.CLASS_NAME_PLACEHOLDER,
101-
Mapper.getFlatName( mapperElement ) );
69+
Mapper.getFlatName( mapperElement ) );
10270

10371
Type decoratorType = typeFactory.getType( decoratorPrism.value() );
10472
DecoratorConstructor decoratorConstructor = new DecoratorConstructor(
105-
implementationName,
106-
implementationName + "_",
107-
hasDelegateConstructor );
73+
implementationName,
74+
implementationName + "_",
75+
hasDelegateConstructor );
10876

10977

11078
String elementPackage = elementUtils.getPackageOf( mapperElement ).getQualifiedName().toString();

processor/src/main/java/org/mapstruct/ap/internal/model/GeneratedType.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.TreeSet;
1313

1414
import javax.lang.model.type.TypeKind;
15+
import javax.lang.model.util.Elements;
1516

1617
import org.mapstruct.ap.internal.model.common.Accessibility;
1718
import org.mapstruct.ap.internal.model.common.ModelElement;
@@ -30,6 +31,53 @@ public abstract class GeneratedType extends ModelElement {
3031

3132
private static final String JAVA_LANG_PACKAGE = "java.lang";
3233

34+
protected abstract static class GeneratedTypeBuilder<T extends GeneratedTypeBuilder> {
35+
36+
private T myself;
37+
protected TypeFactory typeFactory;
38+
protected Elements elementUtils;
39+
protected Options options;
40+
protected VersionInformation versionInformation;
41+
protected SortedSet<Type> extraImportedTypes;
42+
43+
protected List<MappingMethod> methods;
44+
45+
GeneratedTypeBuilder(Class<T> selfType) {
46+
myself = selfType.cast( this );
47+
}
48+
49+
public T elementUtils(Elements elementUtils) {
50+
this.elementUtils = elementUtils;
51+
return myself;
52+
}
53+
54+
public T typeFactory(TypeFactory typeFactory) {
55+
this.typeFactory = typeFactory;
56+
return myself;
57+
}
58+
59+
public T options(Options options) {
60+
this.options = options;
61+
return myself;
62+
}
63+
64+
public T versionInformation(VersionInformation versionInformation) {
65+
this.versionInformation = versionInformation;
66+
return myself;
67+
}
68+
69+
public T extraImports(SortedSet<Type> extraImportedTypes) {
70+
this.extraImportedTypes = extraImportedTypes;
71+
return myself;
72+
}
73+
74+
public T methods(List<MappingMethod> methods) {
75+
this.methods = methods;
76+
return myself;
77+
}
78+
79+
}
80+
3381
private final String packageName;
3482
private final String name;
3583
private final String superClassName;

processor/src/main/java/org/mapstruct/ap/internal/model/Mapper.java

Lines changed: 39 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import javax.lang.model.element.Element;
1313
import javax.lang.model.element.ElementKind;
1414
import javax.lang.model.element.TypeElement;
15-
import javax.lang.model.util.Elements;
1615

1716
import org.mapstruct.ap.internal.model.common.Accessibility;
1817
import org.mapstruct.ap.internal.model.common.Type;
@@ -33,63 +32,24 @@ public class Mapper extends GeneratedType {
3332
static final String DEFAULT_IMPLEMENTATION_CLASS = CLASS_NAME_PLACEHOLDER + "Impl";
3433
static final String DEFAULT_IMPLEMENTATION_PACKAGE = PACKAGE_NAME_PLACEHOLDER;
3534

36-
private final boolean customPackage;
37-
private final boolean customImplName;
38-
private Decorator decorator;
39-
40-
@SuppressWarnings( "checkstyle:parameternumber" )
41-
private Mapper(TypeFactory typeFactory, String packageName, String name, String superClassName,
42-
String interfacePackage, String interfaceName, boolean customPackage, boolean customImplName,
43-
List<MappingMethod> methods, Options options, VersionInformation versionInformation,
44-
Accessibility accessibility, List<Field> fields, Constructor constructor,
45-
Decorator decorator, SortedSet<Type> extraImportedTypes ) {
46-
47-
super(
48-
typeFactory,
49-
packageName,
50-
name,
51-
superClassName,
52-
interfacePackage,
53-
interfaceName,
54-
methods,
55-
fields,
56-
options,
57-
versionInformation,
58-
accessibility,
59-
extraImportedTypes,
60-
constructor
61-
);
62-
this.customPackage = customPackage;
63-
this.customImplName = customImplName;
64-
65-
this.decorator = decorator;
66-
}
67-
68-
public static class Builder {
35+
public static class Builder extends GeneratedTypeBuilder<Builder> {
6936

70-
private TypeFactory typeFactory;
7137
private TypeElement element;
72-
private List<MappingMethod> mappingMethods;
7338
private List<Field> fields;
7439
private Set<SupportingConstructorFragment> fragments;
75-
private SortedSet<Type> extraImportedTypes;
7640

77-
private Elements elementUtils;
78-
private Options options;
79-
private VersionInformation versionInformation;
8041
private Decorator decorator;
8142
private String implName;
8243
private boolean customName;
8344
private String implPackage;
8445
private boolean customPackage;
8546

86-
public Builder element(TypeElement element) {
87-
this.element = element;
88-
return this;
47+
public Builder() {
48+
super( Builder.class );
8949
}
9050

91-
public Builder mappingMethods(List<MappingMethod> mappingMethods) {
92-
this.mappingMethods = mappingMethods;
51+
public Builder element(TypeElement element) {
52+
this.element = element;
9353
return this;
9454
}
9555

@@ -103,36 +63,11 @@ public Builder constructorFragments(Set<SupportingConstructorFragment> fragment
10363
return this;
10464
}
10565

106-
public Builder options(Options options) {
107-
this.options = options;
108-
return this;
109-
}
110-
111-
public Builder versionInformation(VersionInformation versionInformation) {
112-
this.versionInformation = versionInformation;
113-
return this;
114-
}
115-
116-
public Builder typeFactory(TypeFactory typeFactory) {
117-
this.typeFactory = typeFactory;
118-
return this;
119-
}
120-
121-
public Builder elementUtils(Elements elementUtils) {
122-
this.elementUtils = elementUtils;
123-
return this;
124-
}
125-
12666
public Builder decorator(Decorator decorator) {
12767
this.decorator = decorator;
12868
return this;
12969
}
13070

131-
public Builder extraImports(SortedSet<Type> extraImportedTypes) {
132-
this.extraImportedTypes = extraImportedTypes;
133-
return this;
134-
}
135-
13671
public Builder implName(String implName) {
13772
this.implName = implName;
13873
this.customName = !DEFAULT_IMPLEMENTATION_CLASS.equals( this.implName );
@@ -147,7 +82,7 @@ public Builder implPackage(String implPackage) {
14782

14883
public Mapper build() {
14984
String implementationName = implName.replace( CLASS_NAME_PLACEHOLDER, getFlatName( element ) ) +
150-
( decorator == null ? "" : "_" );
85+
( decorator == null ? "" : "_" );
15186

15287
String elementPackage = elementUtils.getPackageOf( element ).getQualifiedName().toString();
15388
String packageName = implPackage.replace( PACKAGE_NAME_PLACEHOLDER, elementPackage );
@@ -164,7 +99,7 @@ public Mapper build() {
16499
element.getKind() == ElementKind.INTERFACE ? element.getSimpleName().toString() : null,
165100
customPackage,
166101
customName,
167-
mappingMethods,
102+
methods,
168103
options,
169104
versionInformation,
170105
Accessibility.fromModifiers( element.getModifiers() ),
@@ -177,6 +112,38 @@ public Mapper build() {
177112

178113
}
179114

115+
private final boolean customPackage;
116+
private final boolean customImplName;
117+
private Decorator decorator;
118+
119+
@SuppressWarnings( "checkstyle:parameternumber" )
120+
private Mapper(TypeFactory typeFactory, String packageName, String name, String superClassName,
121+
String interfacePackage, String interfaceName, boolean customPackage, boolean customImplName,
122+
List<MappingMethod> methods, Options options, VersionInformation versionInformation,
123+
Accessibility accessibility, List<Field> fields, Constructor constructor,
124+
Decorator decorator, SortedSet<Type> extraImportedTypes ) {
125+
126+
super(
127+
typeFactory,
128+
packageName,
129+
name,
130+
superClassName,
131+
interfacePackage,
132+
interfaceName,
133+
methods,
134+
fields,
135+
options,
136+
versionInformation,
137+
accessibility,
138+
extraImportedTypes,
139+
constructor
140+
);
141+
this.customPackage = customPackage;
142+
this.customImplName = customImplName;
143+
144+
this.decorator = decorator;
145+
}
146+
180147
public Decorator getDecorator() {
181148
return decorator;
182149
}

processor/src/main/java/org/mapstruct/ap/internal/processor/MapperCreationProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private Mapper getMapper(TypeElement element, MapperConfiguration mapperConfig,
162162

163163
Mapper mapper = new Mapper.Builder()
164164
.element( element )
165-
.mappingMethods( mappingMethods )
165+
.methods( mappingMethods )
166166
.fields( fields )
167167
.constructorFragments( constructorFragments )
168168
.options( options )

0 commit comments

Comments
 (0)