Skip to content

Commit daaa99d

Browse files
committed
mapstruct#272 Allowing to inject decorated mapper in Spring without qualifier
1 parent 2ad77b5 commit daaa99d

9 files changed

Lines changed: 65 additions & 25 deletions

File tree

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.mapstruct.itest.cdi.other.DateMapper;
2424

2525
@Mapper( componentModel = "cdi", uses = DateMapper.class )
26-
@DecoratedWith( SourceTargetMapperDecorator.class )
2726
public interface DecoratedSourceTargetMapper {
2827

2928
Target sourceToTarget(Source source);

integrationtest/src/test/resources/springTest/src/main/java/org/mapstruct/itest/spring/SourceTargetMapperDecorator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020

2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.beans.factory.annotation.Qualifier;
23+
import org.springframework.context.annotation.Primary;
2324
import org.springframework.stereotype.Component;
2425

2526
@Component
27+
@Primary
2628
public class SourceTargetMapperDecorator implements DecoratedSourceTargetMapper {
2729

2830
@Autowired
29-
@Qualifier( "decoratedSourceTargetMapperImpl_" )
31+
@Qualifier( "delegate" )
3032
private DecoratedSourceTargetMapper delegate;
3133

3234
public SourceTargetMapperDecorator() {

integrationtest/src/test/resources/springTest/src/test/java/org/mapstruct/itest/spring/SpringBasedMapperTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
@ContextConfiguration(classes = SpringTestConfig.class )
3939
@RunWith( SpringJUnit4ClassRunner.class )
4040
public class SpringBasedMapperTest {
41+
4142
@Configuration
4243
@ComponentScan(basePackageClasses = SpringBasedMapperTest.class)
4344
public static class SpringTestConfig {
@@ -47,7 +48,6 @@ public static class SpringTestConfig {
4748
private SourceTargetMapper mapper;
4849

4950
@Autowired
50-
@Qualifier( "sourceTargetMapperDecorator" )
5151
private DecoratedSourceTargetMapper decoratedMapper;
5252

5353
@Test
@@ -70,5 +70,4 @@ public void shouldInjectDecorator() {
7070
assertThat( target ).isNotNull();
7171
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 43 ) );
7272
}
73-
74-
}
73+
}

processor/src/main/java/org/mapstruct/ap/internal/model/Annotation.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.mapstruct.ap.internal.model;
2020

2121
import java.util.Collections;
22+
import java.util.List;
2223
import java.util.Set;
2324

2425
import org.mapstruct.ap.internal.model.common.ModelElement;
@@ -33,8 +34,18 @@ public class Annotation extends ModelElement {
3334

3435
private final Type type;
3536

37+
/**
38+
* List of annotation attributes. Quite simplistic, but it's sufficient for now.
39+
*/
40+
private List<String> properties;
41+
3642
public Annotation(Type type) {
43+
this( type, Collections.<String>emptyList() );
44+
}
45+
46+
public Annotation(Type type, List<String> properties) {
3747
this.type = type;
48+
this.properties = properties;
3849
}
3950

4051
public Type getType() {
@@ -45,4 +56,8 @@ public Type getType() {
4556
public Set<Type> getImportTypes() {
4657
return Collections.singleton( type );
4758
}
59+
60+
public List<String> getProperties() {
61+
return properties;
62+
}
4863
}

processor/src/main/java/org/mapstruct/ap/internal/processor/AnnotationBasedComponentModelProcessor.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.mapstruct.ap.internal.processor;
2020

21+
import java.util.List;
2122
import java.util.ListIterator;
2223

2324
import javax.lang.model.element.TypeElement;
@@ -56,11 +57,13 @@ public Mapper process(ProcessorContext context, TypeElement mapperTypeElement, M
5657
return mapper;
5758
}
5859

59-
if ( shouldDecoratorBeRemoved() ) {
60-
mapper.removeDecorator();
60+
for ( Annotation typeAnnotation : getTypeAnnotations( mapper ) ) {
61+
mapper.addAnnotation( typeAnnotation );
6162
}
6263

63-
mapper.addAnnotation( getTypeAnnotation() );
64+
if ( !requiresGenerationOfDecoratorClass() ) {
65+
mapper.removeDecorator();
66+
}
6467

6568
ListIterator<MapperReference> iterator = mapper.getReferencedMappers().listIterator();
6669
while ( iterator.hasNext() ) {
@@ -92,19 +95,19 @@ protected MapperReference replacementMapperReference(MapperReference originalRef
9295
protected abstract String getComponentModelIdentifier();
9396

9497
/**
95-
* @return the annotation of the mapper implementation
98+
* @return the annotation(s) to be added at the mapper type implementation
9699
*/
97-
protected abstract Annotation getTypeAnnotation();
100+
protected abstract List<Annotation> getTypeAnnotations(Mapper mapper);
98101

99102
/**
100103
* @return the annotation of the field for the mapper reference
101104
*/
102105
protected abstract Annotation getMapperReferenceAnnotation();
103106

104107
/**
105-
* @return if generated decorator class should be removed
108+
* @return if a decorator (sub-)class needs to be generated or not
106109
*/
107-
protected abstract boolean shouldDecoratorBeRemoved();
110+
protected abstract boolean requiresGenerationOfDecoratorClass();
108111

109112
@Override
110113
public int getPriority() {

processor/src/main/java/org/mapstruct/ap/internal/processor/CdiComponentProcessor.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
package org.mapstruct.ap.internal.processor;
2020

21+
import java.util.Collections;
22+
import java.util.List;
23+
2124
import org.mapstruct.ap.internal.model.Annotation;
2225
import org.mapstruct.ap.internal.model.Mapper;
2326

@@ -36,8 +39,10 @@ protected String getComponentModelIdentifier() {
3639
}
3740

3841
@Override
39-
protected Annotation getTypeAnnotation() {
40-
return new Annotation( getTypeFactory().getType( "javax.enterprise.context.ApplicationScoped" ) );
42+
protected List<Annotation> getTypeAnnotations(Mapper mapper) {
43+
return Collections.singletonList(
44+
new Annotation( getTypeFactory().getType( "javax.enterprise.context.ApplicationScoped" ) )
45+
);
4146
}
4247

4348
@Override
@@ -46,7 +51,7 @@ protected Annotation getMapperReferenceAnnotation() {
4651
}
4752

4853
@Override
49-
protected boolean shouldDecoratorBeRemoved() {
50-
return true;
54+
protected boolean requiresGenerationOfDecoratorClass() {
55+
return false;
5156
}
5257
}

processor/src/main/java/org/mapstruct/ap/internal/processor/Jsr330ComponentProcessor.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import org.mapstruct.ap.internal.model.Annotation;
2222
import org.mapstruct.ap.internal.model.Mapper;
23+
import java.util.Collections;
24+
import java.util.List;
2325

2426
/**
2527
* A {@link ModelElementProcessor} which converts the given {@link Mapper}
@@ -36,8 +38,8 @@ protected String getComponentModelIdentifier() {
3638
}
3739

3840
@Override
39-
protected Annotation getTypeAnnotation() {
40-
return new Annotation( getTypeFactory().getType( "javax.inject.Named" ) );
41+
protected List<Annotation> getTypeAnnotations(Mapper mapper) {
42+
return Collections.singletonList( new Annotation( getTypeFactory().getType( "javax.inject.Named" ) ) );
4143
}
4244

4345
@Override
@@ -46,7 +48,7 @@ protected Annotation getMapperReferenceAnnotation() {
4648
}
4749

4850
@Override
49-
protected boolean shouldDecoratorBeRemoved() {
50-
return false;
51+
protected boolean requiresGenerationOfDecoratorClass() {
52+
return true;
5153
}
5254
}

processor/src/main/java/org/mapstruct/ap/internal/processor/SpringComponentProcessor.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
*/
1919
package org.mapstruct.ap.internal.processor;
2020

21+
import java.util.ArrayList;
22+
import java.util.Arrays;
23+
import java.util.List;
24+
2125
import org.mapstruct.ap.internal.model.Annotation;
2226
import org.mapstruct.ap.internal.model.Mapper;
2327

@@ -36,8 +40,19 @@ protected String getComponentModelIdentifier() {
3640
}
3741

3842
@Override
39-
protected Annotation getTypeAnnotation() {
40-
return new Annotation( getTypeFactory().getType( "org.springframework.stereotype.Component" ) );
43+
protected List<Annotation> getTypeAnnotations(Mapper mapper) {
44+
List<Annotation> typeAnnotations = new ArrayList<Annotation>();
45+
typeAnnotations.add( new Annotation( getTypeFactory().getType( "org.springframework.stereotype.Component" ) ) );
46+
47+
if ( mapper.getDecorator() != null ) {
48+
Annotation qualifier = new Annotation(
49+
getTypeFactory().getType( "org.springframework.beans.factory.annotation.Qualifier" ),
50+
Arrays.asList( "\"delegate\"" )
51+
);
52+
typeAnnotations.add( qualifier );
53+
}
54+
55+
return typeAnnotations;
4156
}
4257

4358
@Override
@@ -46,7 +61,7 @@ protected Annotation getMapperReferenceAnnotation() {
4661
}
4762

4863
@Override
49-
protected boolean shouldDecoratorBeRemoved() {
50-
return true;
64+
protected boolean requiresGenerationOfDecoratorClass() {
65+
return false;
5166
}
5267
}

processor/src/main/resources/org/mapstruct/ap/internal/model/Annotation.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
limitations under the License.
1919
2020
-->
21-
@<@includeModel object=type/>
21+
@<@includeModel object=type/><#if (properties?size > 0) >(<#list properties as property>${property}<#if property_has_next>, </#if></#list>)</#if>

0 commit comments

Comments
 (0)