Skip to content

Commit 4223e3a

Browse files
Saljackfiliphr
authored andcommitted
mapstruct#2255 Add constants for componentModel
1 parent 700293f commit 4223e3a

46 files changed

Lines changed: 166 additions & 48 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

core/src/main/java/org/mapstruct/Mapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@
3535
* </p>
3636
* <pre><code class='java'>
3737
* // we have MarkMapper (map field "mark" to field "name" to upper case)
38-
* &#64;Mapper(componentModel = "spring")
38+
* &#64;Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
3939
* public class MarkMapper {
4040
* public String mapMark(String mark) {
4141
* return mark.toUpperCase();
4242
* }
4343
* }
4444
* // we have CarMapper
4545
* &#64;Mapper(
46-
* componentModel = "spring",
46+
* componentModel = MappingConstants.ComponentModel.SPRING,
4747
* uses = MarkMapper.class,
4848
* injectionStrategy = InjectionStrategy.CONSTRUCTOR)
4949
* public interface CarMapper {
@@ -148,7 +148,7 @@
148148
*
149149
* @return The component model for the generated mapper.
150150
*/
151-
String componentModel() default "default";
151+
String componentModel() default MappingConstants.ComponentModel.DEFAULT;
152152

153153
/**
154154
* Specifies the name of the implementation class. The {@code <CLASS_NAME>} will be replaced by the

core/src/main/java/org/mapstruct/MapperConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
*
136136
* @return The component model for the generated mapper.
137137
*/
138-
String componentModel() default "default";
138+
String componentModel() default MappingConstants.ComponentModel.DEFAULT;
139139

140140
/**
141141
* Specifies the name of the implementation class. The {@code <CLASS_NAME>} will be replaced by the

core/src/main/java/org/mapstruct/MappingConstants.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,49 @@ private MappingConstants() {
6666
*/
6767
public static final String STRIP_PREFIX_TRANSFORMATION = "stripPrefix";
6868

69+
/**
70+
* Specifies the component model constants to which the generated mapper should adhere.
71+
* It can be used with the annotation {@link Mapper#componentModel()} or {@link MapperConfig#componentModel()}
72+
*
73+
* <p>
74+
* <strong>Example:</strong>
75+
* </p>
76+
* <pre><code class='java'>
77+
* // Spring component model
78+
* &#64;Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
79+
* </code></pre>
80+
*
81+
* @since 1.5.0
82+
*/
83+
public static final class ComponentModel {
84+
85+
private ComponentModel() {
86+
}
87+
88+
/**
89+
* The mapper uses no component model, instances are typically retrieved
90+
* via {@link org.mapstruct.factory.Mappers#getMapper(java.lang.Class)}
91+
*
92+
*/
93+
public static final String DEFAULT = "default";
94+
95+
/**
96+
* The generated mapper is an application-scoped CDI bean and can be retrieved via @Inject
97+
*/
98+
public static final String CDI = "cdi";
99+
100+
/**
101+
* The generated mapper is a Spring bean and can be retrieved via @Autowired
102+
*
103+
*/
104+
public static final String SPRING = "spring";
105+
106+
/**
107+
* The generated mapper is annotated with @javax.inject.Named and @Singleton, and can be retrieved via @Inject
108+
*
109+
*/
110+
public static final String JSR330 = "jsr330";
111+
112+
}
113+
69114
}

documentation/src/main/asciidoc/chapter-4-retrieving-a-mapper.asciidoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ Note that mappers generated by MapStruct are stateless and thread-safe and thus
6969

7070
If you're working with a dependency injection framework such as http://jcp.org/en/jsr/detail?id=346[CDI] (Contexts and Dependency Injection for Java^TM^ EE) or the http://www.springsource.org/spring-framework[Spring Framework], it is recommended to obtain mapper objects via dependency injection and *not* via the `Mappers` class as described above. For that purpose you can specify the component model which generated mapper classes should be based on either via `@Mapper#componentModel` or using a processor option as described in <<configuration-options>>.
7171

72-
Currently there is support for CDI and Spring (the latter either via its custom annotations or using the JSR 330 annotations). See <<configuration-options>> for the allowed values of the `componentModel` attribute which are the same as for the `mapstruct.defaultComponentModel` processor option. In both cases the required annotations will be added to the generated mapper implementations classes in order to make the same subject to dependency injection. The following shows an example using CDI:
72+
Currently there is support for CDI and Spring (the latter either via its custom annotations or using the JSR 330 annotations). See <<configuration-options>> for the allowed values of the `componentModel` attribute which are the same as for the `mapstruct.defaultComponentModel` processor option and constants are defined in a class `MappingConstants.ComponentModel`. In both cases the required annotations will be added to the generated mapper implementations classes in order to make the same subject to dependency injection. The following shows an example using CDI:
7373

7474
.A mapper using the CDI component model
7575
====
7676
[source, java, linenums]
7777
[subs="verbatim,attributes"]
7878
----
79-
@Mapper(componentModel = "cdi")
79+
@Mapper(componentModel = MappingConstants.ComponentModel.CDI)
8080
public interface CarMapper {
8181
8282
CarDto carToCarDto(Car car);
@@ -110,7 +110,7 @@ This can be done by either providing the injection strategy via `@Mapper` or `@M
110110
[source, java, linenums]
111111
[subs="verbatim,attributes"]
112112
----
113-
@Mapper(componentModel = "cdi", uses = EngineMapper.class, injectionStrategy = InjectionStrategy.CONSTRUCTOR)
113+
@Mapper(componentModel = MappingConstants.ComponentModel.CDI, uses = EngineMapper.class, injectionStrategy = InjectionStrategy.CONSTRUCTOR)
114114
public interface CarMapper {
115115
CarDto carToCarDto(Car car);
116116
}

documentation/src/main/asciidoc/chapter-5-data-type-conversions.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ public class ReferenceMapper {
405405
}
406406
}
407407
408-
@Mapper(componentModel = "cdi", uses = ReferenceMapper.class )
408+
@Mapper(componentModel = MappingConstants.ComponentModel.CDI, uses = ReferenceMapper.class )
409409
public interface CarMapper {
410410
411411
Car carDtoToCar(CarDto carDto);

integrationtest/src/test/resources/cdiTest/src/main/java/org/mapstruct/itest/cdi/DecoratedSourceTargetMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
package org.mapstruct.itest.cdi;
77

88
import org.mapstruct.Mapper;
9+
import org.mapstruct.MappingConstants;
910
import org.mapstruct.DecoratedWith;
1011
import org.mapstruct.itest.cdi.other.DateMapper;
1112

12-
@Mapper( componentModel = "cdi", uses = DateMapper.class )
13+
@Mapper( componentModel = MappingConstants.ComponentModel.CDI, uses = DateMapper.class )
1314
public interface DecoratedSourceTargetMapper {
1415

1516
Target sourceToTarget(Source source);

integrationtest/src/test/resources/cdiTest/src/main/java/org/mapstruct/itest/cdi/SourceTargetMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
package org.mapstruct.itest.cdi;
77

88
import org.mapstruct.Mapper;
9+
import org.mapstruct.MappingConstants;
910
import org.mapstruct.itest.cdi.other.DateMapper;
1011

11-
@Mapper(componentModel = "cdi", uses = DateMapper.class)
12+
@Mapper(componentModel = MappingConstants.ComponentModel.CDI, uses = DateMapper.class)
1213
public interface SourceTargetMapper {
1314

1415
Target sourceToTarget(Source source);

integrationtest/src/test/resources/jsr330Test/src/main/java/org/mapstruct/itest/jsr330/DecoratedSourceTargetMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
package org.mapstruct.itest.jsr330;
77

88
import org.mapstruct.Mapper;
9+
import org.mapstruct.MappingConstants;
910
import org.mapstruct.DecoratedWith;
1011
import org.mapstruct.itest.jsr330.other.DateMapper;
1112

12-
@Mapper(componentModel = "jsr330", uses = DateMapper.class)
13+
@Mapper(componentModel = MappingConstants.ComponentModel.JSR330, uses = DateMapper.class)
1314
@DecoratedWith(SourceTargetMapperDecorator.class)
1415
public interface DecoratedSourceTargetMapper {
1516

integrationtest/src/test/resources/jsr330Test/src/main/java/org/mapstruct/itest/jsr330/SecondDecoratedSourceTargetMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
package org.mapstruct.itest.jsr330;
77

88
import org.mapstruct.Mapper;
9+
import org.mapstruct.MappingConstants;
910
import org.mapstruct.DecoratedWith;
1011
import org.mapstruct.itest.jsr330.other.DateMapper;
1112

12-
@Mapper(componentModel = "jsr330", uses = DateMapper.class)
13+
@Mapper(componentModel = MappingConstants.ComponentModel.JSR330, uses = DateMapper.class)
1314
@DecoratedWith(SecondSourceTargetMapperDecorator.class)
1415
public interface SecondDecoratedSourceTargetMapper {
1516

integrationtest/src/test/resources/jsr330Test/src/main/java/org/mapstruct/itest/jsr330/SourceTargetMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
package org.mapstruct.itest.jsr330;
77

88
import org.mapstruct.Mapper;
9+
import org.mapstruct.MappingConstants;
910
import org.mapstruct.itest.jsr330.other.DateMapper;
1011

11-
@Mapper(componentModel = "jsr330", uses = DateMapper.class)
12+
@Mapper(componentModel = MappingConstants.ComponentModel.JSR330, uses = DateMapper.class)
1213
public interface SourceTargetMapper {
1314

1415
Target sourceToTarget(Source source);

0 commit comments

Comments
 (0)