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

70
.gitignore vendored
View File

@ -36,75 +36,9 @@ nbdist/
# ---> JetBrains # ---> JetBrains
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
.idea
*.iml
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
# ---> Eclipse # ---> Eclipse
.metadata .metadata

View File

@ -1,3 +1,71 @@
# mapstruct-support # mapstruct-support
Additional DefaultAccessorNamingStrategy to support fluent API withXYZ setters. Additional DefaultAccessorNamingStrategy to support fluent API withXYZ setters.
Mapstruct default naming strategy detects the methods `withFirstName` and `withLastName` (see example below) as extra
target fields are therefore cause an error, when the mapper is annotated with
`unmappedTargetPolicy = ReportingPolicy.ERROR`.
## Installation
To use the extended naming strategy, just include it as project dependency alongside with mapstruct.
```xml
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.2.Final</version>
</dependency>
<dependency>
<groupId>de.muehlencord.mapstruct</groupId>
<artifactId>mapstruct-support</artifactId>
<version>1.4.2</version>
</dependency>
</dependencies>
```
## Example
```java
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;
}
}
```

56
pom.xml Normal file
View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.muehlencord.mapstruct</groupId>
<artifactId>mapstruct-support</artifactId>
<version>1.4.2-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<inceptionYear>2021</inceptionYear>
<description>mapstruct-support</description>
<url>https://jomu.timelord.de/git/jomu/mapstruct-support</url>
<issueManagement>
<system>Gitea</system>
<url>https://jomu.timelord.de/git/jomu/mapstruct-support/issues</url>
</issueManagement>
<scm>
<connection>scm:git:https://jomu.timelord.de/git/jomu/mapstruct-support</connection>
<developerConnection>scm:git:git@jomu.timelord.de:jomu/mapstruct-support.git</developerConnection>
<tag>HEAD</tag>
</scm>
<developers>
<developer>
<name>Joern Muehlencord</name>
<email>joern@muehlencord.de</email>
<roles>
<role>developer</role>
</roles>
</developer>
</developers>
<organization>
<name>Joern Muehlencord</name>
<url>https://www.muehlencord.de</url>
</organization>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version>
</dependency>
</dependencies>
</project>

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;
}
}