You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/src/main/asciidoc/mapstruct-reference-guide.asciidoc
+129Lines changed: 129 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1974,3 +1974,132 @@ The order in which the selected methods are applied is roughly determined by the
1974
1974
====
1975
1975
`@BeforeMapping` and `@AfterMapping` are considered experimental as of the 1.0.0.CR1 release. Details in the selection of before/after mapping methods that are applicable for a mapping method or the order in which they are called might still be changed.
1976
1976
====
1977
+
[[using-spi]]
1978
+
== Using the MapStruct SPI
1979
+
=== Custom Accessor Naming Strategy
1980
+
1981
+
MapStruct offers the possibility to override the `AccessorNamingStrategy` via the Service Provide Interface (SPI). A nice example is the use of the fluent API on the source object `GolfPlayer` and `GolfPlayerDto` below.
1982
+
1983
+
.Source object GolfPlayer with fluent API.
1984
+
====
1985
+
[source, java, linenums]
1986
+
[subs="verbatim,attributes"]
1987
+
----
1988
+
public class GolfPlayer {
1989
+
1990
+
private double handicap;
1991
+
private String name;
1992
+
1993
+
public double handicap() {
1994
+
return handicap;
1995
+
}
1996
+
1997
+
public GolfPlayer withHandicap(double handicap) {
1998
+
this.handicap = handicap;
1999
+
return this;
2000
+
}
2001
+
2002
+
public String name() {
2003
+
return name;
2004
+
}
2005
+
2006
+
public GolfPlayer withName(String name) {
2007
+
this.name = name;
2008
+
return this;
2009
+
}
2010
+
}
2011
+
----
2012
+
====
2013
+
2014
+
.Source object GolfPlayerDto with fluent API.
2015
+
====
2016
+
[source, java, linenums]
2017
+
[subs="verbatim,attributes"]
2018
+
----
2019
+
public class GolfPlayerDto {
2020
+
2021
+
private double handicap;
2022
+
private String name;
2023
+
2024
+
public double handicap() {
2025
+
return handicap;
2026
+
}
2027
+
2028
+
public GolfPlayerDto withHandicap(double handicap) {
2029
+
this.handicap = handicap;
2030
+
return this;
2031
+
}
2032
+
2033
+
public String name() {
2034
+
return name;
2035
+
}
2036
+
2037
+
public GolfPlayerDto withName(String name) {
2038
+
this.name = name;
2039
+
return this
2040
+
}
2041
+
}
2042
+
----
2043
+
====
2044
+
2045
+
We want `GolfPlayer` to be mapped to a target object `GolfPlayerDto` similar like we 'always' do this:
This can be achieved with implementing the SPI `org.mapstruct.ap.spi.AccessorNamingStrategy` as in the following example. Here's an implemented `org.mapstruct.ap.spi.AccessorNamingStrategy`:
2066
+
2067
+
.CustomAccessorNamingStrategy
2068
+
====
2069
+
[source, java, linenums]
2070
+
[subs="verbatim,attributes"]
2071
+
----
2072
+
/**
2073
+
* A custom {@link AccessorNamingStrategy} recognizing getters in the form of {@code property()} and setters in the
2074
+
* form of {@code withProperty(value)}.
2075
+
*/
2076
+
public class CustomAccessorNamingStrategy extends DefaultAccessorNamingStrategy {
2077
+
2078
+
@Override
2079
+
public boolean isGetterMethod(ExecutableElement method) {
The `CustomAccessorNamingStrategy` makes use of the `DefaultAccessorNamingStrategy` (also available in mapstruct-processor) and relies on that class to leave most of the default behaviour unchanged.
2099
+
2100
+
To use a custom SPI implementation, it must be located in a seperate .jar file together with the file `META-INF/services/org.mapstruct.ap.spi.AccessorNamingStrategy` with the fully qualified name of your custom implementation as content (e.g. `org.mapstruct.example.CustomAccessorNamingStrategy`). This .jar file needs to be added to the annotation processor classpath (i.e. add it next to the place where you added the mapstruct-processor jar).
2101
+
2102
+
[TIP]
2103
+
Fore more details: There's the above example is present in our our examples repository (https://github.com/mapstruct/mapstruct-examples).
0 commit comments