Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;

import static org.mapstruct.ap.internal.util.Collections.first;

/**
* Model element that can be used to create a type of {@link Iterable} or {@link java.util.Map}. If an implementation
* type is used and the target type has a constructor with {@code int} as parameter and the source parameter is of
Expand Down Expand Up @@ -69,6 +71,20 @@ public Set<Type> getImportTypes() {
if ( factoryMethod == null && resultType.getImplementationType() != null ) {
types.addAll( resultType.getImplementationType().getImportTypes() );
}

if ( isEnumSet() ) {
types.add( getEnumSetElementType() );
// The result type itself is an EnumSet
types.add( resultType );
}
return types;
}

public Type getEnumSetElementType() {
return first( getResultType().determineTypeArguments( Iterable.class ) );
}

public boolean isEnumSet() {
return "java.util.EnumSet".equals( resultType.getFullyQualifiedName() );
Comment thread
chris922 marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<@compress single_line=true>
<#if factoryMethod??>
<@includeModel object=factoryMethod targetType=resultType/>
<#elseif enumSet>
EnumSet.noneOf( <@includeModel object=enumSetElementType raw=true/>.class )
Comment thread
chris922 marked this conversation as resolved.
<#else>
new
<#if resultType.implementationType??>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._1797;

import java.util.EnumSet;

/**
* @author Filip Hrisafov
*/
public class Customer {

public enum Type {
ONE, TWO
}

private final EnumSet<Type> types;

public Customer(EnumSet<Type> types) {
this.types = types;
}

public EnumSet<Type> getTypes() {
return types;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._1797;

import java.util.EnumSet;

/**
* @author Filip Hrisafov
*/
public class CustomerDto {

public enum Type {
ONE, TWO
}

private EnumSet<Type> types;

public EnumSet<Type> getTypes() {
return types;
}

public void setTypes(EnumSet<Type> types) {
this.types = types;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._1797;

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

/**
* @author Filip Hrisafov
*/
@Mapper
public interface Issue1797Mapper {

Issue1797Mapper INSTANCE = Mappers.getMapper( Issue1797Mapper.class );

CustomerDto map(Customer customer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._1797;

import java.util.EnumSet;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Filip Hrisafov
*/
@IssueKey("1797")
@RunWith(AnnotationProcessorTestRunner.class)
@WithClasses({
Customer.class,
CustomerDto.class,
Issue1797Mapper.class
})
public class Issue1797Test {

@Test
public void shouldCorrectlyMapEnumSetToEnumSet() {

Customer customer = new Customer( EnumSet.of( Customer.Type.ONE ) );

CustomerDto customerDto = Issue1797Mapper.INSTANCE.map( customer );

assertThat( customerDto.getTypes() ).containsExactly( CustomerDto.Type.ONE );
}
}