|
| 1 | +/* |
| 2 | + * Copyright MapStruct Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +package org.mapstruct.control; |
| 7 | + |
| 8 | +import java.lang.annotation.ElementType; |
| 9 | +import java.lang.annotation.Repeatable; |
| 10 | +import java.lang.annotation.Retention; |
| 11 | +import java.lang.annotation.RetentionPolicy; |
| 12 | +import java.lang.annotation.Target; |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * Controls which means of mapping are considered between the source and the target in mappings. |
| 17 | + * |
| 18 | + * <p> |
| 19 | + * There are several applications of <code>MappingControl</code> conceivable. One application, "deep cloning" is |
| 20 | + * explained below in the example. |
| 21 | + * </p> |
| 22 | + * |
| 23 | + * <p> |
| 24 | + * Another application is controlling so called "complex mappings", which are not always desirable and sometimes lead to |
| 25 | + * unexpected behaviour and prolonged compilation time. |
| 26 | + * </p> |
| 27 | + * |
| 28 | + * <p><strong>Example:</strong>Cloning of an object</p> |
| 29 | + * <p> |
| 30 | + * When all methods are allowed, MapStruct would make a shallow copy. It would take the <code>ShelveDTO</code> in |
| 31 | + * the <code>FridgeDTO</code> and directly enter that as target on the target <code>FridgeDTO</code>. By disabling all |
| 32 | + * other kinds of mappings apart from {@link MappingControl.Use#MAPPING_METHOD}, see {@link DeepClone} MapStruct is |
| 33 | + * forced to generate mapping methods all through the object graph `FridgeDTO` and hence create a deep clone. |
| 34 | + * </p> |
| 35 | + * <pre><code class='java'> |
| 36 | + * public class FridgeDTO { |
| 37 | + * |
| 38 | + * private ShelveDTO shelve; |
| 39 | + * |
| 40 | + * public ShelveDTO getShelve() { |
| 41 | + * return shelve; |
| 42 | + * } |
| 43 | + * |
| 44 | + * public void setShelve(ShelveDTO shelve) { |
| 45 | + * this.shelve = shelve; |
| 46 | + * } |
| 47 | + * } |
| 48 | + * </code></pre> |
| 49 | + * <pre><code class='java'> |
| 50 | + * public class ShelveDTO { |
| 51 | + * |
| 52 | + * private CoolBeerDTO coolBeer; |
| 53 | + * |
| 54 | + * public CoolBeerDTO getCoolBeer() { |
| 55 | + * return coolBeer; |
| 56 | + * } |
| 57 | + * |
| 58 | + * public void setCoolBeer(CoolBeerDTO coolBeer) { |
| 59 | + * this.coolBeer = coolBeer; |
| 60 | + * } |
| 61 | + * } |
| 62 | + * </code></pre> |
| 63 | + * <pre><code class='java'> |
| 64 | + * public class CoolBeerDTO { |
| 65 | + * |
| 66 | + * private String beerCount; |
| 67 | + * |
| 68 | + * public String getBeerCount() { |
| 69 | + * return beerCount; |
| 70 | + * } |
| 71 | + * |
| 72 | + * public void setBeerCount(String beerCount) { |
| 73 | + * this.beerCount = beerCount; |
| 74 | + * } |
| 75 | + * } |
| 76 | + * </code></pre> |
| 77 | + * |
| 78 | + * <pre><code class='java'> |
| 79 | + * @Mapper(mappingControl = DeepClone.class) |
| 80 | + * public interface CloningMapper { |
| 81 | + * |
| 82 | + * CloningMapper INSTANCE = Mappers.getMapper( CloningMapper.class ); |
| 83 | + * |
| 84 | + * FridgeDTO clone(FridgeDTO in); |
| 85 | + * |
| 86 | + * } |
| 87 | + * </code></pre> |
| 88 | + * |
| 89 | + * @author Sjaak Derksen |
| 90 | + */ |
| 91 | +@Retention(RetentionPolicy.CLASS) |
| 92 | +@Repeatable(MappingControls.class) |
| 93 | +@Target( ElementType.ANNOTATION_TYPE ) |
| 94 | +@MappingControl( MappingControl.Use.DIRECT ) |
| 95 | +@MappingControl( MappingControl.Use.BUILT_IN_CONVERSION ) |
| 96 | +@MappingControl( MappingControl.Use.MAPPING_METHOD ) |
| 97 | +@MappingControl( MappingControl.Use.COMPLEX_MAPPING ) |
| 98 | +public @interface MappingControl { |
| 99 | + |
| 100 | + Use value(); |
| 101 | + |
| 102 | + enum Use { |
| 103 | + |
| 104 | + /** |
| 105 | + * Controls the mapping, allows for type conversion from source type to target type |
| 106 | + * <p> |
| 107 | + * Type conversions are typically supported directly in Java. The "toString()" is such an example, |
| 108 | + * which allows for mapping for instance a {@link java.lang.Number} type to a {@link java.lang.String}. |
| 109 | + * <p> |
| 110 | + * Please refer to the MapStruct guide for more info. |
| 111 | + * |
| 112 | + * @since 1.4 |
| 113 | + */ |
| 114 | + BUILT_IN_CONVERSION, |
| 115 | + |
| 116 | + /** |
| 117 | + * Controls the mapping from source to target type, allows mapping by calling: |
| 118 | + * <ol> |
| 119 | + * <li>A type conversion, passed into a mapping method</li> |
| 120 | + * <li>A mapping method, passed into a type conversion</li> |
| 121 | + * <li>A mapping method passed into another mapping method</li> |
| 122 | + * </ol> |
| 123 | + * |
| 124 | + * @since 1.4 |
| 125 | + */ |
| 126 | + COMPLEX_MAPPING, |
| 127 | + /** |
| 128 | + * Controls the mapping, allows for a direct mapping from source type to target type. |
| 129 | + * <p> |
| 130 | + * This means if source type and target type are of the same type, MapStruct will not perform |
| 131 | + * any mappings anymore and assign the target to the source direct. |
| 132 | + * <p> |
| 133 | + * An exception are types from the package {@link java}, which will be mapped always directly. |
| 134 | + * |
| 135 | + * @since 1.4 |
| 136 | + */ |
| 137 | + DIRECT, |
| 138 | + |
| 139 | + /** |
| 140 | + * Controls the mapping, allows for Direct Mapping from source type to target type. |
| 141 | + * <p> |
| 142 | + * The mapping method can be either a custom referred mapping method, or a MapStruct built in |
| 143 | + * mapping method. |
| 144 | + * |
| 145 | + * @since 1.4 |
| 146 | + */ |
| 147 | + MAPPING_METHOD |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments