mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#1649 Improvement: builder for Mapper/Decorator/GeneratedType
This commit is contained in:
parent
2977c2e614
commit
b651ad34b5
@ -11,7 +11,6 @@ import java.util.SortedSet;
|
||||
|
||||
import javax.lang.model.element.ElementKind;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.util.Elements;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Accessibility;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
@ -27,28 +26,17 @@ import org.mapstruct.ap.internal.version.VersionInformation;
|
||||
*/
|
||||
public class Decorator extends GeneratedType {
|
||||
|
||||
public static class Builder {
|
||||
public static class Builder extends GeneratedTypeBuilder<Builder> {
|
||||
|
||||
private Elements elementUtils;
|
||||
private TypeFactory typeFactory;
|
||||
private TypeElement mapperElement;
|
||||
private DecoratedWithPrism decoratorPrism;
|
||||
private List<MappingMethod> methods;
|
||||
private Options options;
|
||||
private VersionInformation versionInformation;
|
||||
|
||||
private boolean hasDelegateConstructor;
|
||||
private String implName;
|
||||
private String implPackage;
|
||||
private SortedSet<Type> extraImportedTypes;
|
||||
|
||||
public Builder elementUtils(Elements elementUtils) {
|
||||
this.elementUtils = elementUtils;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder typeFactory(TypeFactory typeFactory) {
|
||||
this.typeFactory = typeFactory;
|
||||
return this;
|
||||
public Builder() {
|
||||
super( Builder.class );
|
||||
}
|
||||
|
||||
public Builder mapperElement(TypeElement mapperElement) {
|
||||
@ -61,21 +49,6 @@ public class Decorator extends GeneratedType {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder methods(List<MappingMethod> methods) {
|
||||
this.methods = methods;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder options(Options options) {
|
||||
this.options = options;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder versionInformation(VersionInformation versionInformation) {
|
||||
this.versionInformation = versionInformation;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder hasDelegateConstructor(boolean hasDelegateConstructor) {
|
||||
this.hasDelegateConstructor = hasDelegateConstructor;
|
||||
return this;
|
||||
@ -91,11 +64,6 @@ public class Decorator extends GeneratedType {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder extraImports(SortedSet<Type> extraImportedTypes) {
|
||||
this.extraImportedTypes = extraImportedTypes;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Decorator build() {
|
||||
String implementationName = implName.replace( Mapper.CLASS_NAME_PLACEHOLDER,
|
||||
Mapper.getFlatName( mapperElement ) );
|
||||
|
@ -12,6 +12,7 @@ import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import javax.lang.model.type.TypeKind;
|
||||
import javax.lang.model.util.Elements;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Accessibility;
|
||||
import org.mapstruct.ap.internal.model.common.ModelElement;
|
||||
@ -30,6 +31,53 @@ public abstract class GeneratedType extends ModelElement {
|
||||
|
||||
private static final String JAVA_LANG_PACKAGE = "java.lang";
|
||||
|
||||
protected abstract static class GeneratedTypeBuilder<T extends GeneratedTypeBuilder> {
|
||||
|
||||
private T myself;
|
||||
protected TypeFactory typeFactory;
|
||||
protected Elements elementUtils;
|
||||
protected Options options;
|
||||
protected VersionInformation versionInformation;
|
||||
protected SortedSet<Type> extraImportedTypes;
|
||||
|
||||
protected List<MappingMethod> methods;
|
||||
|
||||
GeneratedTypeBuilder(Class<T> selfType) {
|
||||
myself = selfType.cast( this );
|
||||
}
|
||||
|
||||
public T elementUtils(Elements elementUtils) {
|
||||
this.elementUtils = elementUtils;
|
||||
return myself;
|
||||
}
|
||||
|
||||
public T typeFactory(TypeFactory typeFactory) {
|
||||
this.typeFactory = typeFactory;
|
||||
return myself;
|
||||
}
|
||||
|
||||
public T options(Options options) {
|
||||
this.options = options;
|
||||
return myself;
|
||||
}
|
||||
|
||||
public T versionInformation(VersionInformation versionInformation) {
|
||||
this.versionInformation = versionInformation;
|
||||
return myself;
|
||||
}
|
||||
|
||||
public T extraImports(SortedSet<Type> extraImportedTypes) {
|
||||
this.extraImportedTypes = extraImportedTypes;
|
||||
return myself;
|
||||
}
|
||||
|
||||
public T methods(List<MappingMethod> methods) {
|
||||
this.methods = methods;
|
||||
return myself;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final String packageName;
|
||||
private final String name;
|
||||
private final String superClassName;
|
||||
|
@ -12,7 +12,6 @@ import java.util.SortedSet;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.ElementKind;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.util.Elements;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Accessibility;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
@ -33,63 +32,24 @@ public class Mapper extends GeneratedType {
|
||||
static final String DEFAULT_IMPLEMENTATION_CLASS = CLASS_NAME_PLACEHOLDER + "Impl";
|
||||
static final String DEFAULT_IMPLEMENTATION_PACKAGE = PACKAGE_NAME_PLACEHOLDER;
|
||||
|
||||
private final boolean customPackage;
|
||||
private final boolean customImplName;
|
||||
private Decorator decorator;
|
||||
public static class Builder extends GeneratedTypeBuilder<Builder> {
|
||||
|
||||
@SuppressWarnings( "checkstyle:parameternumber" )
|
||||
private Mapper(TypeFactory typeFactory, String packageName, String name, String superClassName,
|
||||
String interfacePackage, String interfaceName, boolean customPackage, boolean customImplName,
|
||||
List<MappingMethod> methods, Options options, VersionInformation versionInformation,
|
||||
Accessibility accessibility, List<Field> fields, Constructor constructor,
|
||||
Decorator decorator, SortedSet<Type> extraImportedTypes ) {
|
||||
|
||||
super(
|
||||
typeFactory,
|
||||
packageName,
|
||||
name,
|
||||
superClassName,
|
||||
interfacePackage,
|
||||
interfaceName,
|
||||
methods,
|
||||
fields,
|
||||
options,
|
||||
versionInformation,
|
||||
accessibility,
|
||||
extraImportedTypes,
|
||||
constructor
|
||||
);
|
||||
this.customPackage = customPackage;
|
||||
this.customImplName = customImplName;
|
||||
|
||||
this.decorator = decorator;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
private TypeFactory typeFactory;
|
||||
private TypeElement element;
|
||||
private List<MappingMethod> mappingMethods;
|
||||
private List<Field> fields;
|
||||
private Set<SupportingConstructorFragment> fragments;
|
||||
private SortedSet<Type> extraImportedTypes;
|
||||
|
||||
private Elements elementUtils;
|
||||
private Options options;
|
||||
private VersionInformation versionInformation;
|
||||
private Decorator decorator;
|
||||
private String implName;
|
||||
private boolean customName;
|
||||
private String implPackage;
|
||||
private boolean customPackage;
|
||||
|
||||
public Builder element(TypeElement element) {
|
||||
this.element = element;
|
||||
return this;
|
||||
public Builder() {
|
||||
super( Builder.class );
|
||||
}
|
||||
|
||||
public Builder mappingMethods(List<MappingMethod> mappingMethods) {
|
||||
this.mappingMethods = mappingMethods;
|
||||
public Builder element(TypeElement element) {
|
||||
this.element = element;
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -103,36 +63,11 @@ public class Mapper extends GeneratedType {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder options(Options options) {
|
||||
this.options = options;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder versionInformation(VersionInformation versionInformation) {
|
||||
this.versionInformation = versionInformation;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder typeFactory(TypeFactory typeFactory) {
|
||||
this.typeFactory = typeFactory;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder elementUtils(Elements elementUtils) {
|
||||
this.elementUtils = elementUtils;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder decorator(Decorator decorator) {
|
||||
this.decorator = decorator;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder extraImports(SortedSet<Type> extraImportedTypes) {
|
||||
this.extraImportedTypes = extraImportedTypes;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder implName(String implName) {
|
||||
this.implName = implName;
|
||||
this.customName = !DEFAULT_IMPLEMENTATION_CLASS.equals( this.implName );
|
||||
@ -164,7 +99,7 @@ public class Mapper extends GeneratedType {
|
||||
element.getKind() == ElementKind.INTERFACE ? element.getSimpleName().toString() : null,
|
||||
customPackage,
|
||||
customName,
|
||||
mappingMethods,
|
||||
methods,
|
||||
options,
|
||||
versionInformation,
|
||||
Accessibility.fromModifiers( element.getModifiers() ),
|
||||
@ -177,6 +112,38 @@ public class Mapper extends GeneratedType {
|
||||
|
||||
}
|
||||
|
||||
private final boolean customPackage;
|
||||
private final boolean customImplName;
|
||||
private Decorator decorator;
|
||||
|
||||
@SuppressWarnings( "checkstyle:parameternumber" )
|
||||
private Mapper(TypeFactory typeFactory, String packageName, String name, String superClassName,
|
||||
String interfacePackage, String interfaceName, boolean customPackage, boolean customImplName,
|
||||
List<MappingMethod> methods, Options options, VersionInformation versionInformation,
|
||||
Accessibility accessibility, List<Field> fields, Constructor constructor,
|
||||
Decorator decorator, SortedSet<Type> extraImportedTypes ) {
|
||||
|
||||
super(
|
||||
typeFactory,
|
||||
packageName,
|
||||
name,
|
||||
superClassName,
|
||||
interfacePackage,
|
||||
interfaceName,
|
||||
methods,
|
||||
fields,
|
||||
options,
|
||||
versionInformation,
|
||||
accessibility,
|
||||
extraImportedTypes,
|
||||
constructor
|
||||
);
|
||||
this.customPackage = customPackage;
|
||||
this.customImplName = customImplName;
|
||||
|
||||
this.decorator = decorator;
|
||||
}
|
||||
|
||||
public Decorator getDecorator() {
|
||||
return decorator;
|
||||
}
|
||||
|
@ -162,7 +162,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
|
||||
Mapper mapper = new Mapper.Builder()
|
||||
.element( element )
|
||||
.mappingMethods( mappingMethods )
|
||||
.methods( mappingMethods )
|
||||
.fields( fields )
|
||||
.constructorFragments( constructorFragments )
|
||||
.options( options )
|
||||
|
Loading…
x
Reference in New Issue
Block a user