Skip to content

Commit 1415e32

Browse files
authored
mapstruct#1797 Use EnumSet.noneOf when creating a new instance of EnumSet
1 parent 871353f commit 1415e32

6 files changed

Lines changed: 133 additions & 0 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.mapstruct.ap.internal.model.common.Parameter;
1313
import org.mapstruct.ap.internal.model.common.Type;
1414

15+
import static org.mapstruct.ap.internal.util.Collections.first;
16+
1517
/**
1618
* Model element that can be used to create a type of {@link Iterable} or {@link java.util.Map}. If an implementation
1719
* type is used and the target type has a constructor with {@code int} as parameter and the source parameter is of
@@ -69,6 +71,20 @@ public Set<Type> getImportTypes() {
6971
if ( factoryMethod == null && resultType.getImplementationType() != null ) {
7072
types.addAll( resultType.getImplementationType().getImportTypes() );
7173
}
74+
75+
if ( isEnumSet() ) {
76+
types.add( getEnumSetElementType() );
77+
// The result type itself is an EnumSet
78+
types.add( resultType );
79+
}
7280
return types;
7381
}
82+
83+
public Type getEnumSetElementType() {
84+
return first( getResultType().determineTypeArguments( Iterable.class ) );
85+
}
86+
87+
public boolean isEnumSet() {
88+
return "java.util.EnumSet".equals( resultType.getFullyQualifiedName() );
89+
}
7490
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<@compress single_line=true>
1010
<#if factoryMethod??>
1111
<@includeModel object=factoryMethod targetType=resultType/>
12+
<#elseif enumSet>
13+
EnumSet.noneOf( <@includeModel object=enumSetElementType raw=true/>.class )
1214
<#else>
1315
new
1416
<#if resultType.implementationType??>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.ap.test.bugs._1797;
7+
8+
import java.util.EnumSet;
9+
10+
/**
11+
* @author Filip Hrisafov
12+
*/
13+
public class Customer {
14+
15+
public enum Type {
16+
ONE, TWO
17+
}
18+
19+
private final EnumSet<Type> types;
20+
21+
public Customer(EnumSet<Type> types) {
22+
this.types = types;
23+
}
24+
25+
public EnumSet<Type> getTypes() {
26+
return types;
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.ap.test.bugs._1797;
7+
8+
import java.util.EnumSet;
9+
10+
/**
11+
* @author Filip Hrisafov
12+
*/
13+
public class CustomerDto {
14+
15+
public enum Type {
16+
ONE, TWO
17+
}
18+
19+
private EnumSet<Type> types;
20+
21+
public EnumSet<Type> getTypes() {
22+
return types;
23+
}
24+
25+
public void setTypes(EnumSet<Type> types) {
26+
this.types = types;
27+
}
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.ap.test.bugs._1797;
7+
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.factory.Mappers;
10+
11+
/**
12+
* @author Filip Hrisafov
13+
*/
14+
@Mapper
15+
public interface Issue1797Mapper {
16+
17+
Issue1797Mapper INSTANCE = Mappers.getMapper( Issue1797Mapper.class );
18+
19+
CustomerDto map(Customer customer);
20+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.ap.test.bugs._1797;
7+
8+
import java.util.EnumSet;
9+
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.mapstruct.ap.testutil.IssueKey;
13+
import org.mapstruct.ap.testutil.WithClasses;
14+
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
18+
/**
19+
* @author Filip Hrisafov
20+
*/
21+
@IssueKey("1797")
22+
@RunWith(AnnotationProcessorTestRunner.class)
23+
@WithClasses({
24+
Customer.class,
25+
CustomerDto.class,
26+
Issue1797Mapper.class
27+
})
28+
public class Issue1797Test {
29+
30+
@Test
31+
public void shouldCorrectlyMapEnumSetToEnumSet() {
32+
33+
Customer customer = new Customer( EnumSet.of( Customer.Type.ONE ) );
34+
35+
CustomerDto customerDto = Issue1797Mapper.INSTANCE.map( customer );
36+
37+
assertThat( customerDto.getTypes() ).containsExactly( CustomerDto.Type.ONE );
38+
}
39+
}

0 commit comments

Comments
 (0)