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.ElementKind;
|
||||||
import javax.lang.model.element.TypeElement;
|
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.Accessibility;
|
||||||
import org.mapstruct.ap.internal.model.common.Type;
|
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 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 TypeElement mapperElement;
|
||||||
private DecoratedWithPrism decoratorPrism;
|
private DecoratedWithPrism decoratorPrism;
|
||||||
private List<MappingMethod> methods;
|
|
||||||
private Options options;
|
|
||||||
private VersionInformation versionInformation;
|
|
||||||
private boolean hasDelegateConstructor;
|
private boolean hasDelegateConstructor;
|
||||||
private String implName;
|
private String implName;
|
||||||
private String implPackage;
|
private String implPackage;
|
||||||
private SortedSet<Type> extraImportedTypes;
|
|
||||||
|
|
||||||
public Builder elementUtils(Elements elementUtils) {
|
public Builder() {
|
||||||
this.elementUtils = elementUtils;
|
super( Builder.class );
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder typeFactory(TypeFactory typeFactory) {
|
|
||||||
this.typeFactory = typeFactory;
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder mapperElement(TypeElement mapperElement) {
|
public Builder mapperElement(TypeElement mapperElement) {
|
||||||
@ -61,21 +49,6 @@ public class Decorator extends GeneratedType {
|
|||||||
return this;
|
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) {
|
public Builder hasDelegateConstructor(boolean hasDelegateConstructor) {
|
||||||
this.hasDelegateConstructor = hasDelegateConstructor;
|
this.hasDelegateConstructor = hasDelegateConstructor;
|
||||||
return this;
|
return this;
|
||||||
@ -91,20 +64,15 @@ public class Decorator extends GeneratedType {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder extraImports(SortedSet<Type> extraImportedTypes) {
|
|
||||||
this.extraImportedTypes = extraImportedTypes;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Decorator build() {
|
public Decorator build() {
|
||||||
String implementationName = implName.replace( Mapper.CLASS_NAME_PLACEHOLDER,
|
String implementationName = implName.replace( Mapper.CLASS_NAME_PLACEHOLDER,
|
||||||
Mapper.getFlatName( mapperElement ) );
|
Mapper.getFlatName( mapperElement ) );
|
||||||
|
|
||||||
Type decoratorType = typeFactory.getType( decoratorPrism.value() );
|
Type decoratorType = typeFactory.getType( decoratorPrism.value() );
|
||||||
DecoratorConstructor decoratorConstructor = new DecoratorConstructor(
|
DecoratorConstructor decoratorConstructor = new DecoratorConstructor(
|
||||||
implementationName,
|
implementationName,
|
||||||
implementationName + "_",
|
implementationName + "_",
|
||||||
hasDelegateConstructor );
|
hasDelegateConstructor );
|
||||||
|
|
||||||
|
|
||||||
String elementPackage = elementUtils.getPackageOf( mapperElement ).getQualifiedName().toString();
|
String elementPackage = elementUtils.getPackageOf( mapperElement ).getQualifiedName().toString();
|
||||||
|
@ -12,6 +12,7 @@ import java.util.SortedSet;
|
|||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
|
||||||
import javax.lang.model.type.TypeKind;
|
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.Accessibility;
|
||||||
import org.mapstruct.ap.internal.model.common.ModelElement;
|
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";
|
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 packageName;
|
||||||
private final String name;
|
private final String name;
|
||||||
private final String superClassName;
|
private final String superClassName;
|
||||||
|
@ -12,7 +12,6 @@ import java.util.SortedSet;
|
|||||||
import javax.lang.model.element.Element;
|
import javax.lang.model.element.Element;
|
||||||
import javax.lang.model.element.ElementKind;
|
import javax.lang.model.element.ElementKind;
|
||||||
import javax.lang.model.element.TypeElement;
|
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.Accessibility;
|
||||||
import org.mapstruct.ap.internal.model.common.Type;
|
import org.mapstruct.ap.internal.model.common.Type;
|
||||||
@ -33,6 +32,86 @@ public class Mapper extends GeneratedType {
|
|||||||
static final String DEFAULT_IMPLEMENTATION_CLASS = CLASS_NAME_PLACEHOLDER + "Impl";
|
static final String DEFAULT_IMPLEMENTATION_CLASS = CLASS_NAME_PLACEHOLDER + "Impl";
|
||||||
static final String DEFAULT_IMPLEMENTATION_PACKAGE = PACKAGE_NAME_PLACEHOLDER;
|
static final String DEFAULT_IMPLEMENTATION_PACKAGE = PACKAGE_NAME_PLACEHOLDER;
|
||||||
|
|
||||||
|
public static class Builder extends GeneratedTypeBuilder<Builder> {
|
||||||
|
|
||||||
|
private TypeElement element;
|
||||||
|
private List<Field> fields;
|
||||||
|
private Set<SupportingConstructorFragment> fragments;
|
||||||
|
|
||||||
|
private Decorator decorator;
|
||||||
|
private String implName;
|
||||||
|
private boolean customName;
|
||||||
|
private String implPackage;
|
||||||
|
private boolean customPackage;
|
||||||
|
|
||||||
|
public Builder() {
|
||||||
|
super( Builder.class );
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder element(TypeElement element) {
|
||||||
|
this.element = element;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder fields(List<Field> fields) {
|
||||||
|
this.fields = fields;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder constructorFragments(Set<SupportingConstructorFragment> fragments) {
|
||||||
|
this.fragments = fragments;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder decorator(Decorator decorator) {
|
||||||
|
this.decorator = decorator;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder implName(String implName) {
|
||||||
|
this.implName = implName;
|
||||||
|
this.customName = !DEFAULT_IMPLEMENTATION_CLASS.equals( this.implName );
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder implPackage(String implPackage) {
|
||||||
|
this.implPackage = implPackage;
|
||||||
|
this.customPackage = !DEFAULT_IMPLEMENTATION_PACKAGE.equals( this.implPackage );
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mapper build() {
|
||||||
|
String implementationName = implName.replace( CLASS_NAME_PLACEHOLDER, getFlatName( element ) ) +
|
||||||
|
( decorator == null ? "" : "_" );
|
||||||
|
|
||||||
|
String elementPackage = elementUtils.getPackageOf( element ).getQualifiedName().toString();
|
||||||
|
String packageName = implPackage.replace( PACKAGE_NAME_PLACEHOLDER, elementPackage );
|
||||||
|
Constructor constructor = null;
|
||||||
|
if ( !fragments.isEmpty() ) {
|
||||||
|
constructor = new NoArgumentConstructor( implementationName, fragments );
|
||||||
|
}
|
||||||
|
return new Mapper(
|
||||||
|
typeFactory,
|
||||||
|
packageName,
|
||||||
|
implementationName,
|
||||||
|
element.getKind() != ElementKind.INTERFACE ? element.getSimpleName().toString() : null,
|
||||||
|
elementPackage,
|
||||||
|
element.getKind() == ElementKind.INTERFACE ? element.getSimpleName().toString() : null,
|
||||||
|
customPackage,
|
||||||
|
customName,
|
||||||
|
methods,
|
||||||
|
options,
|
||||||
|
versionInformation,
|
||||||
|
Accessibility.fromModifiers( element.getModifiers() ),
|
||||||
|
fields,
|
||||||
|
constructor,
|
||||||
|
decorator,
|
||||||
|
extraImportedTypes
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private final boolean customPackage;
|
private final boolean customPackage;
|
||||||
private final boolean customImplName;
|
private final boolean customImplName;
|
||||||
private Decorator decorator;
|
private Decorator decorator;
|
||||||
@ -65,118 +144,6 @@ public class Mapper extends GeneratedType {
|
|||||||
this.decorator = decorator;
|
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 mappingMethods(List<MappingMethod> mappingMethods) {
|
|
||||||
this.mappingMethods = mappingMethods;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder fields(List<Field> fields) {
|
|
||||||
this.fields = fields;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder constructorFragments(Set<SupportingConstructorFragment> fragments) {
|
|
||||||
this.fragments = fragments;
|
|
||||||
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 );
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder implPackage(String implPackage) {
|
|
||||||
this.implPackage = implPackage;
|
|
||||||
this.customPackage = !DEFAULT_IMPLEMENTATION_PACKAGE.equals( this.implPackage );
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Mapper build() {
|
|
||||||
String implementationName = implName.replace( CLASS_NAME_PLACEHOLDER, getFlatName( element ) ) +
|
|
||||||
( decorator == null ? "" : "_" );
|
|
||||||
|
|
||||||
String elementPackage = elementUtils.getPackageOf( element ).getQualifiedName().toString();
|
|
||||||
String packageName = implPackage.replace( PACKAGE_NAME_PLACEHOLDER, elementPackage );
|
|
||||||
Constructor constructor = null;
|
|
||||||
if ( !fragments.isEmpty() ) {
|
|
||||||
constructor = new NoArgumentConstructor( implementationName, fragments );
|
|
||||||
}
|
|
||||||
return new Mapper(
|
|
||||||
typeFactory,
|
|
||||||
packageName,
|
|
||||||
implementationName,
|
|
||||||
element.getKind() != ElementKind.INTERFACE ? element.getSimpleName().toString() : null,
|
|
||||||
elementPackage,
|
|
||||||
element.getKind() == ElementKind.INTERFACE ? element.getSimpleName().toString() : null,
|
|
||||||
customPackage,
|
|
||||||
customName,
|
|
||||||
mappingMethods,
|
|
||||||
options,
|
|
||||||
versionInformation,
|
|
||||||
Accessibility.fromModifiers( element.getModifiers() ),
|
|
||||||
fields,
|
|
||||||
constructor,
|
|
||||||
decorator,
|
|
||||||
extraImportedTypes
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public Decorator getDecorator() {
|
public Decorator getDecorator() {
|
||||||
return decorator;
|
return decorator;
|
||||||
}
|
}
|
||||||
|
@ -162,7 +162,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
|
|
||||||
Mapper mapper = new Mapper.Builder()
|
Mapper mapper = new Mapper.Builder()
|
||||||
.element( element )
|
.element( element )
|
||||||
.mappingMethods( mappingMethods )
|
.methods( mappingMethods )
|
||||||
.fields( fields )
|
.fields( fields )
|
||||||
.constructorFragments( constructorFragments )
|
.constructorFragments( constructorFragments )
|
||||||
.options( options )
|
.options( options )
|
||||||
|
Loading…
x
Reference in New Issue
Block a user