forked from mapstruct/mapstruct
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContext.java
More file actions
181 lines (178 loc) · 5.9 KB
/
Context.java
File metadata and controls
181 lines (178 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Marks a parameter of a method to be treated as <em>mapping context</em>. Such parameters are passed to other mapping
* methods, {@code @}{@link ObjectFactory} methods or {@code @}{@link BeforeMapping}/{@code @}{@link AfterMapping}
* methods when applicable and can thus be used in custom code.
* <p>
* The type of an {@code @Context} parameter is also inspected for
* {@code @}{@link BeforeMapping}/{@code @}{@link AfterMapping} methods, which are called on the provided context
* parameter value if applicable.
* <p>
* <strong>Note:</strong> no {@code null} checks are performed before calling before/after mapping methods or object
* factory methods on {@code @}{@link Context} annotated parameters. The caller needs to make sure that no {@code null}
* are passed in that case.
* <p>
* For generated code to call a method that is declared with {@code @Context} parameters, the declaration of the mapping
* method being generated needs to contain at least those (or assignable) {@code @Context} parameters as well. MapStruct
* will not create new instances of missing {@code @Context} parameters nor will it pass {@code null} instead.
* <p>
* <strong>Example 1:</strong> Using {@code @Context} parameters for passing data down to hand-written property mapping
* methods and {@code @}{@link BeforeMapping} methods:
*
* <pre>
* <code>
* // multiple @Context parameters can be added
* public abstract CarDto toCar(Car car, @Context VehicleRegistration context, @Context Locale localeToUse);
*
* protected OwnerManualDto translateOwnerManual(OwnerManual ownerManual, @Context Locale locale) {
* // manually implemented logic to translate the OwnerManual with the given Locale
* }
*
* @BeforeMapping
* protected void registerVehicle(Vehicle mappedVehicle, @Context VehicleRegistration context) {
* context.register( mappedVehicle );
* }
*
* @BeforeMapping
* protected void notCalled(Vehicle mappedVehicle, @Context DifferentMappingContextType context) {
* // not called, because no context parameter of type DifferentMappingContextType is available
* // within toCar(Car, VehicleRegistration, Locale)
* }
*
* // generates:
*
* public CarDto toCar(Car car, VehicleRegistration context, Locale localeToUse) {
* registerVehicle( car, context );
*
* if ( car == null ) {
* return null;
* }
*
* CarDto carDto = new CarDto();
*
* carDto.setOwnerManual( translateOwnerManual( car.getOwnerManual(), localeToUse );
* // more generated mapping code
*
* return carDto;
* }
* </code>
* </pre>
* <p>
* <strong>Example 2:</strong> Using an {@code @Context} parameter with a type that provides its own {@code @}
* {@link BeforeMapping} methods to handle cycles in Graph structures:
*
* <pre>
* <code>
* // type of the context parameter
* public class CyclicGraphContext {
* private Map<Object, Object> knownInstances = new IdentityHashMap<>();
*
* @BeforeMapping
* public <T extends NodeDto> T getMappedInstance(Object source, @TargetType Class<T> targetType) {
* return (T) knownInstances.get( source );
* }
*
* @BeforeMapping
* public void storeMappedInstance(Object source, @MappingTarget NodeDto target) {
* knownInstances.put( source, target );
* }
* }
*
* @Mapper
* public interface GraphMapper {
* NodeDto toNodeDto(Node node, @Context CyclicGraphContext cycleContext);
* }
*
*
* // generates:
*
* public NodeDto toNodeDto(Node node, CyclicGraphContext cycleContext) {
* NodeDto target = cycleContext.getMappedInstance( node, NodeDto.class );
* if ( target != null ) {
* return target;
* }
*
* if ( node == null ) {
* return null;
* }
*
* NodeDto nodeDto = new NodeDto();
*
* cycleContext.storeMappedInstance( node, nodeDto );
*
* nodeDto.setParent( toNodeDto( node.getParent(), cycleContext ) );
* List<NodeDto> list = nodeListToNodeDtoList( node.getChildren(), cycleContext );
* if ( list != null ) {
* nodeDto.setChildren( list );
* }
*
* // more mapping code
*
* return nodeDto;
* }
* </code>
* </pre>
* <p>
* <strong>Example 3:</strong> Using {@code @Context} parameters for creating an entity object by calling an
* {@code @}{@link ObjectFactory} methods:
*
* <pre>
* <code>
* // type of the context parameter
* public class ContextObjectFactory {
* @PersistenceContext(unitName = "my-unit")
* private EntityManager em;
*
* @ObjectFactory
* public Valve create( String id ) {
* Query query = em.createNamedQuery("Valve.findById");
* query.setParameter("id", id);
* Valve result = query.getSingleResult();
* if ( result != null ) {
* result = new Valve( id );
* }
* return result;
* }
*
* }
*
* @Mapper
* public interface ContextWithObjectFactoryMapper {
* Valve map(ValveDto dto, @Context ContextObjectFactory factory, String id);
* }
*
*
* // generates:
* public class ContextWithObjectFactoryMapperImpl implements ContextWithObjectFactoryMapper {
*
* @Override
* public Valve map(ValveDto dto, ContextObjectFactory factory, String id) {
* if ( dto == null ) {
* return null;
* }
*
* Valve valve = factory.create( id );
*
* valve.setOneWay( dto.isOneWay() );
*
* return valve;
* }
* }
* </code>
* </pre>
* @author Andreas Gudian
* @since 1.2
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.CLASS)
public @interface Context {
}