Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

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;
Expand All @@ -27,28 +26,17 @@
*/
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) {
Expand All @@ -61,21 +49,6 @@ public Builder decoratorPrism(DecoratedWithPrism decoratorPrism) {
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;
Expand All @@ -91,20 +64,15 @@ public Builder implPackage(String implPackage) {
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 ) );
Mapper.getFlatName( mapperElement ) );

Type decoratorType = typeFactory.getType( decoratorPrism.value() );
DecoratorConstructor decoratorConstructor = new DecoratorConstructor(
implementationName,
implementationName + "_",
hasDelegateConstructor );
implementationName,
implementationName + "_",
hasDelegateConstructor );


String elementPackage = elementUtils.getPackageOf( mapperElement ).getQualifiedName().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand All @@ -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;
Expand Down
111 changes: 39 additions & 72 deletions processor/src/main/java/org/mapstruct/ap/internal/model/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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;
Expand All @@ -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;

@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 {
public static class Builder extends GeneratedTypeBuilder<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;
}

Expand All @@ -103,36 +63,11 @@ public Builder constructorFragments(Set<SupportingConstructorFragment> fragment
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 );
Expand All @@ -147,7 +82,7 @@ public Builder implPackage(String implPackage) {

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

String elementPackage = elementUtils.getPackageOf( element ).getQualifiedName().toString();
String packageName = implPackage.replace( PACKAGE_NAME_PLACEHOLDER, elementPackage );
Expand All @@ -164,7 +99,7 @@ public Mapper build() {
element.getKind() == ElementKind.INTERFACE ? element.getSimpleName().toString() : null,
customPackage,
customName,
mappingMethods,
methods,
options,
versionInformation,
Accessibility.fromModifiers( element.getModifiers() ),
Expand All @@ -177,6 +112,38 @@ public Mapper build() {

}

private final boolean customPackage;
private final boolean customImplName;
private Decorator decorator;

@SuppressWarnings( "checkstyle:parameternumber" )
private Mapper(TypeFactory typeFactory, String packageName, String name, String superClassName,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why move the constructor down?

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private Mapper getMapper(TypeElement element, MapperConfiguration mapperConfig,

Mapper mapper = new Mapper.Builder()
.element( element )
.mappingMethods( mappingMethods )
.methods( mappingMethods )
.fields( fields )
.constructorFragments( constructorFragments )
.options( options )
Expand Down