Skip to content

Commit 458718e

Browse files
committed
mapstruct#12 Allowing delegation to other mapper classes
1 parent 8f4b45c commit 458718e

File tree

18 files changed

+186
-154
lines changed

18 files changed

+186
-154
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@
2929
@Target(ElementType.TYPE)
3030
@Retention(RetentionPolicy.SOURCE)
3131
public @interface Mapper {
32+
Class<?>[] uses() default { };
3233
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,9 @@
1515
*/
1616
package org.mapstruct;
1717

18-
import org.mapstruct.converter.Converter;
19-
import org.mapstruct.converter.NoOpConverter;
20-
2118
public @interface Mapping {
2219

2320
String source();
2421

2522
String target();
26-
27-
Class<? extends Converter<?, ?>> converter() default NoOpConverter.class;
28-
2923
}

core/src/main/java/org/mapstruct/converter/Converter.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

core/src/main/java/org/mapstruct/converter/NoOpConverter.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

core/src/test/java/org/mapstruct/MappersTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.mapstruct;
1717

1818

19-
import org.mapstruct.Mappers;
2019
import org.mapstruct.test.model.Foo;
2120
import org.testng.annotations.Test;
2221

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@
3737
<module>core</module>
3838
<module>processor</module>
3939
<module>integrationtest</module>
40-
</modules>
40+
</modules>
4141
</project>

processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import javax.lang.model.element.ExecutableElement;
3030
import javax.lang.model.element.TypeElement;
3131
import javax.lang.model.element.VariableElement;
32+
import javax.lang.model.type.DeclaredType;
3233
import javax.lang.model.type.TypeKind;
34+
import javax.lang.model.type.TypeMirror;
3335
import javax.lang.model.util.ElementKindVisitor6;
3436
import javax.lang.model.util.Elements;
3537
import javax.lang.model.util.Types;
@@ -91,18 +93,34 @@ private void writeModelToSourceFile(String fileName, Mapper model) {
9193
}
9294

9395
private Mapper retrieveModel(TypeElement element) {
94-
List<Method> methods = retrieveMethods( element );
95-
Set<Method> processedMethods = new HashSet<Method>();
96-
List<BeanMapping> mappings = new ArrayList<BeanMapping>();
96+
List<Method> methods = retrieveMethods( null, element );
97+
List<BeanMapping> mappings = getMappings( methods );
98+
List<Type> usedMapperTypes = getUsedMapperTypes( element );
99+
100+
Mapper mapper = new Mapper(
101+
elementUtils.getPackageOf( element ).getQualifiedName().toString(),
102+
element.getSimpleName().toString(),
103+
element.getSimpleName() + IMPLEMENTATION_SUFFIX,
104+
mappings,
105+
usedMapperTypes
106+
);
97107

108+
return mapper;
109+
}
110+
111+
private List<BeanMapping> getMappings(List<Method> methods) {
98112
Conversions conversions = new Conversions( elementUtils, typeUtils, typeUtil );
99113

114+
List<BeanMapping> mappings = new ArrayList<BeanMapping>();
115+
Set<Method> processedMethods = new HashSet<Method>();
116+
100117
for ( Method method : methods ) {
101118
if ( processedMethods.contains( method ) ) {
102119
continue;
103120
}
104121

105122
MappingMethod mappingMethod = new MappingMethod(
123+
method.getDeclaringMapper(),
106124
method.getName(),
107125
method.getParameterName(),
108126
getElementMappingMethod( methods, method )
@@ -114,6 +132,7 @@ private Mapper retrieveModel(TypeElement element) {
114132
processedMethods.add( rawReverseMappingMethod );
115133

116134
reverseMappingMethod = new MappingMethod(
135+
rawReverseMappingMethod.getDeclaringMapper(),
117136
rawReverseMappingMethod.getName(),
118137
rawReverseMappingMethod.getParameterName(),
119138
getElementMappingMethod( methods, rawReverseMappingMethod )
@@ -134,12 +153,13 @@ private Mapper retrieveModel(TypeElement element) {
134153
property.getSourceType(),
135154
property.getTargetName(),
136155
property.getTargetType(),
137-
property.getConverterType(),
138156
propertyMappingMethod != null ? new MappingMethod(
157+
propertyMappingMethod.getDeclaringMapper(),
139158
propertyMappingMethod.getName(),
140159
propertyMappingMethod.getParameterName()
141160
) : null,
142161
reversePropertyMappingMethod != null ? new MappingMethod(
162+
reversePropertyMappingMethod.getDeclaringMapper(),
143163
reversePropertyMappingMethod.getName(),
144164
reversePropertyMappingMethod.getParameterName()
145165
) : null,
@@ -168,15 +188,16 @@ private Mapper retrieveModel(TypeElement element) {
168188

169189
mappings.add( mapping );
170190
}
191+
return mappings;
192+
}
171193

172-
Mapper mapper = new Mapper(
173-
elementUtils.getPackageOf( element ).getQualifiedName().toString(),
174-
element.getSimpleName().toString(),
175-
element.getSimpleName() + IMPLEMENTATION_SUFFIX,
176-
mappings
177-
);
178-
179-
return mapper;
194+
private List<Type> getUsedMapperTypes(TypeElement element) {
195+
List<Type> usedMapperTypes = new LinkedList<Type>();
196+
MapperPrism mapperPrism = MapperPrism.getInstanceOn( element );
197+
for ( TypeMirror usedMapper : mapperPrism.uses() ) {
198+
usedMapperTypes.add( typeUtil.retrieveType( usedMapper ) );
199+
}
200+
return usedMapperTypes;
180201
}
181202

182203
private String getAccessor(String name) {
@@ -196,6 +217,7 @@ private MappingMethod getElementMappingMethod(Iterable<Method> methods, Method m
196217
}
197218
}
198219
return elementMappingMethod == null ? null : new MappingMethod(
220+
elementMappingMethod.getDeclaringMapper(),
199221
elementMappingMethod.getName(),
200222
elementMappingMethod.getParameterName()
201223
);
@@ -231,7 +253,7 @@ private Method getReverseMappingMethod(List<Method> rawMethods,
231253
return null;
232254
}
233255

234-
private List<Method> retrieveMethods(TypeElement element) {
256+
private List<Method> retrieveMethods(Type declaringMapper, Element element) {
235257
List<Method> methods = new ArrayList<Method>();
236258

237259
for ( ExecutableElement method : methodsIn( element.getEnclosedElements() ) ) {
@@ -241,6 +263,7 @@ private List<Method> retrieveMethods(TypeElement element) {
241263

242264
methods.add(
243265
new Method(
266+
declaringMapper,
244267
method.getSimpleName().toString(),
245268
parameter.getName(),
246269
parameter.getType(),
@@ -250,6 +273,21 @@ private List<Method> retrieveMethods(TypeElement element) {
250273
);
251274
}
252275

276+
List<Type> usedMapperTypes = new LinkedList<Type>();
277+
MapperPrism mapperPrism = MapperPrism.getInstanceOn( element );
278+
279+
if ( mapperPrism != null ) {
280+
for ( TypeMirror usedMapper : mapperPrism.uses() ) {
281+
methods.addAll(
282+
retrieveMethods(
283+
typeUtil.retrieveType( usedMapper ),
284+
( (DeclaredType) usedMapper ).asElement()
285+
)
286+
);
287+
}
288+
}
289+
290+
253291
return methods;
254292
}
255293

@@ -291,8 +329,7 @@ private List<MappedProperty> getMappedProperties(ExecutableElement method, Map<S
291329
sourcePropertyName,
292330
retrieveReturnType( getterMethod ),
293331
mapping != null ? mapping.getTargetName() : targetPropertyName,
294-
retrieveParameter( setterMethod ).getType(),
295-
mapping != null ? mapping.getConverterType() : null
332+
retrieveParameter( setterMethod ).getType()
296333
)
297334
);
298335
}
@@ -320,12 +357,7 @@ private Map<String, Mapping> getMappings(MappingsPrism mappingsAnnotation) {
320357
}
321358

322359
private Mapping getMapping(MappingPrism mapping) {
323-
Type converterType = typeUtil.retrieveType( mapping.converter() );
324-
return new Mapping(
325-
mapping.source(),
326-
mapping.target(),
327-
converterType.getName().equals( "NoOpConverter" ) ? null : converterType
328-
);
360+
return new Mapping( mapping.source(), mapping.target() );
329361
}
330362

331363
private Parameter retrieveParameter(ExecutableElement method) {

processor/src/main/java/org/mapstruct/ap/model/Mapper.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@
2020
public class Mapper {
2121

2222
private final String packageName;
23-
2423
private final String interfaceName;
25-
2624
private final String implementationName;
27-
2825
private final List<BeanMapping> beanMappings;
26+
private final List<Type> usedMapperTypes;
2927

3028
public Mapper(String packageName, String interfaceName,
31-
String implementationName, List<BeanMapping> beanMappings) {
29+
String implementationName, List<BeanMapping> beanMappings, List<Type> usedMapperTypes) {
3230
this.packageName = packageName;
3331
this.interfaceName = interfaceName;
3432
this.implementationName = implementationName;
3533
this.beanMappings = beanMappings;
34+
this.usedMapperTypes = usedMapperTypes;
3635
}
3736

3837
public String getPackageName() {
@@ -50,4 +49,8 @@ public String getImplementationName() {
5049
public List<BeanMapping> getBeanMappings() {
5150
return beanMappings;
5251
}
52+
53+
public List<Type> getUsedMapperTypes() {
54+
return usedMapperTypes;
55+
}
5356
}

processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,29 @@
1717

1818
public class MappingMethod {
1919

20+
private final Type declaringMapper;
2021
private final String name;
2122
private final String parameterName;
2223
private final MappingMethod elementMappingMethod;
2324

24-
public MappingMethod(String name, String parameterName) {
25+
public MappingMethod(Type declaringMapper, String name, String parameterName) {
26+
this.declaringMapper = declaringMapper;
2527
this.name = name;
2628
this.parameterName = parameterName;
2729
this.elementMappingMethod = null;
2830
}
2931

30-
public MappingMethod(String name, String parameterName, MappingMethod elementMappingMethod) {
32+
public MappingMethod(Type declaringMapper, String name, String parameterName, MappingMethod elementMappingMethod) {
33+
this.declaringMapper = declaringMapper;
3134
this.name = name;
3235
this.parameterName = parameterName;
3336
this.elementMappingMethod = elementMappingMethod;
3437
}
3538

39+
public Type getDeclaringMapper() {
40+
return declaringMapper;
41+
}
42+
3643
public String getName() {
3744
return name;
3845
}
@@ -44,4 +51,8 @@ public String getParameterName() {
4451
public MappingMethod getElementMappingMethod() {
4552
return elementMappingMethod;
4653
}
54+
55+
public boolean isGenerationRequired() {
56+
return declaringMapper == null;
57+
}
4758
}

processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,16 @@ public class PropertyMapping {
2121
private final Type sourceType;
2222
private final String targetName;
2323
private final Type targetType;
24-
private final Type converterType;
2524
private final MappingMethod mappingMethod;
2625
private final MappingMethod reverseMappingMethod;
2726
private final String toConversion;
2827
private final String fromConversion;
2928

30-
public PropertyMapping(String sourceName, Type sourceType, String targetName, Type targetType, Type converterType, MappingMethod mappingMethod, MappingMethod reverseMappingMethod, String toConversion, String fromConversion) {
29+
public PropertyMapping(String sourceName, Type sourceType, String targetName, Type targetType, MappingMethod mappingMethod, MappingMethod reverseMappingMethod, String toConversion, String fromConversion) {
3130
this.sourceName = sourceName;
3231
this.sourceType = sourceType;
3332
this.targetName = targetName;
3433
this.targetType = targetType;
35-
this.converterType = converterType;
3634
this.mappingMethod = mappingMethod;
3735
this.reverseMappingMethod = reverseMappingMethod;
3836
this.toConversion = toConversion;
@@ -55,10 +53,6 @@ public Type getTargetType() {
5553
return targetType;
5654
}
5755

58-
public Type getConverterType() {
59-
return converterType;
60-
}
61-
6256
public MappingMethod getMappingMethod() {
6357
return mappingMethod;
6458
}

0 commit comments

Comments
 (0)