initial version

This commit is contained in:
2021-05-01 15:12:14 +02:00
parent 725a438479
commit fa0de02716
6 changed files with 199 additions and 69 deletions

View File

@ -0,0 +1,26 @@
package de.muehlencord.mapstruct.support;
import javax.lang.model.element.ExecutableElement;
import org.mapstruct.ap.spi.DefaultAccessorNamingStrategy;
/**
* MapStruct naming strategy to to support fluent API withXYZ setters. When this strategy is applied, the wither are
* ignored so just the normal setter are used.
*
* @author Joern Muehlencord, 2021-05-01
* @since 1.4.2
*/
public class WitherAccessorNamingStrategy extends DefaultAccessorNamingStrategy {
@Override
protected boolean isFluentSetter(ExecutableElement method) {
return !isWitherMethod(method) && super.isFluentSetter(method);
}
protected boolean isWitherMethod(ExecutableElement method) {
String methodName = method.getSimpleName().toString();
return methodName.length() > 4 && methodName.startsWith("with") && Character.isUpperCase(methodName.charAt(4));
}
}

View File

@ -0,0 +1 @@
de.muehlencord.mapstruct.support.WitherAccessorNamingStrategy

View File

@ -0,0 +1,45 @@
package de.muehlencord.mapstruct.support;
/**
* Example class used in README.md
*
* @author Joern Muehlencord, 2021-05-01
* @since 1.4.2
*/
public class Person {
private String firstName;
private String lastName;
// standard getter / setter - these are covered by mapstruct by default */
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
/* fluent setter - these are detected as extra setters and would
cause an mapping exception, if the mapper is annotated with
unmappedTargetPolicy = ReportingPolicy.ERROR.
*/
public Person withFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public Person withLastName(String lastName) {
this.lastName = lastName;
return this;
}
}