mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#732 Properly copying enum sets
This commit is contained in:
parent
f93487f288
commit
4a74ee8f6c
@ -69,4 +69,8 @@ public class Direct extends ModelElement implements Assignment {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return sourceReference;
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ import javax.lang.model.type.TypeMirror;
|
||||
import org.mapstruct.ap.internal.model.assignment.AdderWrapper;
|
||||
import org.mapstruct.ap.internal.model.assignment.ArrayCopyWrapper;
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.assignment.EnumSetCopyWrapper;
|
||||
import org.mapstruct.ap.internal.model.assignment.GetterWrapperForCollectionsAndMaps;
|
||||
import org.mapstruct.ap.internal.model.assignment.NewCollectionOrMapWrapper;
|
||||
import org.mapstruct.ap.internal.model.assignment.NullCheckWrapper;
|
||||
@ -347,7 +348,14 @@ public class PropertyMapping extends ModelElement {
|
||||
else {
|
||||
implementationTypes = targetType.getImportTypes();
|
||||
}
|
||||
newCollectionOrMap = new NewCollectionOrMapWrapper( result, implementationTypes );
|
||||
|
||||
if ( "java.util.EnumSet".equals( targetType.getFullyQualifiedName() ) ) {
|
||||
newCollectionOrMap = new EnumSetCopyWrapper( ctx.getTypeFactory(), result );
|
||||
}
|
||||
else {
|
||||
newCollectionOrMap = new NewCollectionOrMapWrapper( result, implementationTypes );
|
||||
}
|
||||
|
||||
newCollectionOrMap = new SetterWrapper( newCollectionOrMap, method.getThrownTypes() );
|
||||
}
|
||||
if ( result.isUpdateMethod() ) {
|
||||
|
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.internal.model.assignment;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.common.TypeFactory;
|
||||
|
||||
/**
|
||||
* Invokes the copy method for enum sets.
|
||||
*
|
||||
* @author Gunnar Morling
|
||||
*/
|
||||
public class EnumSetCopyWrapper extends AssignmentWrapper {
|
||||
|
||||
private Type enumSetType;
|
||||
|
||||
public EnumSetCopyWrapper(TypeFactory typeFactory, Assignment decoratedAssignment) {
|
||||
super( decoratedAssignment );
|
||||
enumSetType = typeFactory.getType( EnumSet.class );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
Set<Type> imported = new HashSet<Type>( getAssignment().getImportTypes().size() + 1 );
|
||||
imported.addAll( getAssignment().getImportTypes() );
|
||||
imported.add( enumSetType );
|
||||
|
||||
return imported;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EnumSet.copyOf( " + getAssignment() + " )";
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
<#--
|
||||
|
||||
Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
and/or other contributors as indicated by the @authors tag. See the
|
||||
copyright.txt file in the distribution for a full listing of all
|
||||
contributors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
-->
|
||||
EnumSet.copyOf( <@includeModel object=assignment targetBeanName=ext.targetBeanName targetReadAccessorName=ext.targetReadAccessorName targetWriteAccessorName=ext.targetWriteAccessorName targetType=ext.targetType/> )
|
@ -376,4 +376,17 @@ public class CollectionMappingTest {
|
||||
assertThat( numbers ).isNotNull();
|
||||
assertThat( numbers ).containsOnly( 123, 456 );
|
||||
}
|
||||
|
||||
@Test
|
||||
@IssueKey("732")
|
||||
public void shouldEnumSetAsCopy() {
|
||||
Source source = new Source();
|
||||
source.setEnumSet( EnumSet.of( Colour.BLUE, Colour.GREEN ) );
|
||||
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||
source.getEnumSet().add( Colour.RED );
|
||||
|
||||
assertThat( source.getEnumSet() ).containsOnly( Colour.BLUE, Colour.GREEN, Colour.RED );
|
||||
assertThat( target.getEnumSet() ).containsOnly( Colour.BLUE, Colour.GREEN );
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ package org.mapstruct.ap.test.collection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -52,6 +53,8 @@ public class Source {
|
||||
|
||||
private Set<String> stringSet2;
|
||||
|
||||
private EnumSet<Colour> enumSet;
|
||||
|
||||
public List<String> getStringList() {
|
||||
return stringList;
|
||||
}
|
||||
@ -164,4 +167,11 @@ public class Source {
|
||||
this.stringSet2 = stringSet2;
|
||||
}
|
||||
|
||||
public EnumSet<Colour> getEnumSet() {
|
||||
return enumSet;
|
||||
}
|
||||
|
||||
public void setEnumSet(EnumSet<Colour> enumSet) {
|
||||
this.enumSet = enumSet;
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ package org.mapstruct.ap.test.collection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -55,6 +56,8 @@ public class Target {
|
||||
@SuppressWarnings("rawtypes")
|
||||
private Set set;
|
||||
|
||||
private EnumSet<Colour> enumSet;
|
||||
|
||||
public Target() {
|
||||
otherStringLongMap = Maps.newHashMap();
|
||||
otherStringLongMap.put( "not-present-after-mapping", 42L );
|
||||
@ -174,4 +177,11 @@ public class Target {
|
||||
this.otherStringList = otherStringList;
|
||||
}
|
||||
|
||||
public EnumSet<Colour> getEnumSet() {
|
||||
return enumSet;
|
||||
}
|
||||
|
||||
public void setEnumSet(EnumSet<Colour> enumSet) {
|
||||
this.enumSet = enumSet;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user