#1170 Fix wildcards in collection adder mappings

This commit is contained in:
Cornelius 2017-06-29 00:01:49 +02:00 committed by Filip Hrisafov
parent 89d7463c93
commit b5b0c04313
9 changed files with 463 additions and 6 deletions

View File

@ -69,6 +69,7 @@ public class FullFeatureCompilationTest {
switch ( processorType ) {
case ORACLE_JAVA_6:
additionalExcludes.add( "org/mapstruct/ap/test/abstractclass/generics/*.java" );
additionalExcludes.add( "org/mapstruct/ap/test/bugs/_1170/*.java" );
case ECLIPSE_JDT_JAVA_6:
case ORACLE_JAVA_7:
case ECLIPSE_JDT_JAVA_7:

View File

@ -63,7 +63,7 @@ public class AdderWrapper extends AssignmentWrapper {
public Set<Type> getImportTypes() {
Set<Type> imported = new HashSet<Type>();
imported.addAll( super.getImportTypes() );
imported.add( getSourceType().getTypeParameters().get( 0 ) );
imported.add( getSourceType().getTypeParameters().get( 0 ).getTypeBound() );
return imported;
}

View File

@ -375,14 +375,16 @@ public class Type extends ModelElement implements Comparable<Type> {
*
* @return {@code true} if and only if this type is assignable to the given other type.
*/
// TODO This doesn't yet take wild card types into account; e.g. ? extends Integer wouldn't be assignable to Number
// atm.
// TODO This doesn't yet take super wild card types into account;
// e.g. Number wouldn't be assignable to ? super Number atm. (is there any practical use case)
public boolean isAssignableTo(Type other) {
if ( equals( other ) ) {
return true;
}
return typeUtils.isAssignable( typeMirror, other.typeMirror );
TypeMirror typeMirrorToMatch = isWildCardExtendsBound() ? getTypeBound().typeMirror : typeMirror;
return typeUtils.isAssignable( typeMirrorToMatch, other.typeMirror );
}
/**
@ -560,7 +562,7 @@ public class Type extends ModelElement implements Comparable<Type> {
// this is a collection, so this can be done always
if ( !collectionProperty.getTypeParameters().isEmpty() ) {
// there's only one type arg to a collection
TypeMirror typeArg = collectionProperty.getTypeParameters().get( 0 ).getTypeMirror();
TypeMirror typeArg = collectionProperty.getTypeParameters().get( 0 ).getTypeBound().getTypeMirror();
// now, look for a method that
// 1) starts with add,
// 2) and has typeArg as one and only arg

View File

@ -22,7 +22,7 @@
<#import "../macro/CommonMacros.ftl" as lib>
<@lib.handleExceptions>
if ( ${sourceReference} != null ) {
for ( <@includeModel object=sourceType.typeParameters[0]/> ${sourceLocalVarName} : ${sourceReference} ) {
for ( <@includeModel object=sourceType.typeParameters[0].typeBound/> ${sourceLocalVarName} : ${sourceReference} ) {
${ext.targetBeanName}.${ext.targetWriteAccessorName}<@lib.handleWrite><@lib.handleAssignment/></@lib.handleWrite>;
}
}

View File

@ -0,0 +1,40 @@
/**
* Copyright 2012-2017 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.test.bugs._1170;
import org.mapstruct.CollectionMappingStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.ap.test.bugs._1170._target.Target;
import org.mapstruct.ap.test.bugs._1170.source.Source;
import org.mapstruct.factory.Mappers;
/**
* @author Cornelius Dirmeier
*/
@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
uses = { PetMapper.class })
public interface AdderSourceTargetMapper {
AdderSourceTargetMapper INSTANCE = Mappers.getMapper( AdderSourceTargetMapper.class );
Target toTarget(Source source);
Source toSource(Target source);
}

View File

@ -0,0 +1,85 @@
/**
* Copyright 2012-2017 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.test.bugs._1170;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.test.bugs._1170._target.Target;
import org.mapstruct.ap.test.bugs._1170.source.Source;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
/**
* @author Cornelius Dirmeier
*/
@WithClasses({
Source.class,
Target.class,
AdderSourceTargetMapper.class,
PetMapper.class
})
@RunWith(AnnotationProcessorTestRunner.class)
public class AdderTest {
@IssueKey("1170")
@Test
public void testWildcardAdder() {
Source source = new Source();
source.addWithoutWildcard( "mouse" );
source.addWildcardInTarget( "mouse" );
source.addWildcardInSource( "mouse" );
source.addWildcardInBoth( "mouse" );
source.addWildcardAdderToSetter( "mouse" );
Target target = AdderSourceTargetMapper.INSTANCE.toTarget( source );
assertThat( target ).isNotNull();
assertThat( target.getWithoutWildcards() ).containsExactly( 2L );
assertThat( target.getWildcardInSources() ).containsExactly( 2L );
assertThat( target.getWildcardInTargets() ).containsExactly( 2L );
assertThat( target.getWildcardInBoths() ).containsExactly( 2L );
assertThat( target.getWildcardAdderToSetters() ).containsExactly( 2L );
}
@IssueKey("1170")
@Test
public void testWildcardAdderTargetToSource() {
Target target = new Target();
target.addWithoutWildcard( 2L );
target.addWildcardInTarget( 2L );
target.getWildcardInSources().add( 2L );
target.addWildcardInBoth( 2L );
target.setWildcardAdderToSetters( Arrays.asList( 2L ) );
Source source = AdderSourceTargetMapper.INSTANCE.toSource( target );
assertThat( source ).isNotNull();
assertThat( source.getWithoutWildcards() ).containsExactly( "mouse" );
assertThat( source.getWildcardInSources() ).containsExactly( "mouse" );
assertThat( source.getWildcardInTargets() ).containsExactly( "mouse" );
assertThat( source.getWildcardInBoths() ).containsExactly( "mouse" );
assertThat( source.getWildcardAdderToSetters() ).containsExactly( "mouse" );
}
}

View File

@ -0,0 +1,89 @@
/**
* Copyright 2012-2017 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.test.bugs._1170;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.mapstruct.Mapper;
import com.google.common.collect.ImmutableMap;
/**
* @author Cornelius Dirmeier
*/
@Mapper
public class PetMapper {
private static final Map<String, Long> PETS_TO_TARGET = ImmutableMap.<String, Long>builder()
.put( "rabbit", 1L )
.put( "mouse", 2L ).build();
private static final Map<Long, String> PETS_TO_SOURCE = ImmutableMap.<Long, String>builder()
.put( 1L, "rabbit" )
.put( 2L, "mouse" )
.put( 3L, "cat" )
.put( 4L, "dog" ).build();
/**
* method to be used when using an adder
*
* @param pet
*
* @return
*
* @throws DogException
*/
public Long toPet(String pet) {
return PETS_TO_TARGET.get( pet );
}
public String toSourcePets(Long pet) {
return PETS_TO_SOURCE.get( pet );
}
/**
* Method to be used when not using an adder
*
* @param pets
*
* @return
*
* @throws CatException
* @throws DogException
*/
public List<Long> toPets(List<? extends String> pets) {
List<Long> result = new ArrayList<Long>();
for ( String pet : pets ) {
result.add( toPet( pet ) );
}
return result;
}
public List<String> toSourcePets(List<Long> pets) {
List<String> result = new ArrayList<String>();
for ( Long pet : pets ) {
result.add( PETS_TO_SOURCE.get( pet ) );
}
return result;
}
}

View File

@ -0,0 +1,120 @@
/**
* Copyright 2012-2017 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.test.bugs._1170._target;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* @author Cornelius Dirmeier
*/
public class Target {
private List<Long> withoutWildcards = new ArrayList<Long>();
private List<Long> wildcardInSources = new ArrayList<Long>();
private List<Long> wildcardInTargets = new ArrayList<Long>();
private List<Long> wildcardInBoths = new ArrayList<Long>();
private List<Long> wildcardAdderToSetters = new ArrayList<Long>();
private List<Long> wildcardInSourcesAddAll = new ArrayList<Long>();
private List<BigDecimal> sameTypeWildcardInSources = new ArrayList<BigDecimal>();
private List<BigDecimal> sameTypeWildcardInTargets = new ArrayList<BigDecimal>();
private List<BigDecimal> sameTypeWildcardInBoths = new ArrayList<BigDecimal>();
public List<Long> getWithoutWildcards() {
return withoutWildcards;
}
public Long addWithoutWildcard(Long pet) {
withoutWildcards.add( pet );
return pet;
}
public List<Long> getWildcardInSources() {
return wildcardInSources;
}
public Long addWildcardInSource(Long pet) {
wildcardInSources.add( pet );
return pet;
}
public List<? extends Long> getWildcardInTargets() {
return wildcardInTargets;
}
public Long addWildcardInTarget(Long pet) {
wildcardInTargets.add( pet );
return pet;
}
public List<? extends Long> getWildcardInBoths() {
return wildcardInTargets;
}
public Long addWildcardInBoth(Long pet) {
wildcardInBoths.add( pet );
return pet;
}
public List<Long> getWildcardAdderToSetters() {
return wildcardAdderToSetters;
}
public void setWildcardAdderToSetters(List<Long> pets) {
wildcardAdderToSetters = pets;
}
public List<Long> getWildcardInSourcesAddAll() {
return wildcardInSourcesAddAll;
}
public List<? extends BigDecimal> getSameTypeWildcardInSources() {
return sameTypeWildcardInSources;
}
public void addSameTypeWildcardInSource(BigDecimal pet) {
sameTypeWildcardInSources.add( pet );
}
public List<BigDecimal> getSameTypeWildcardInTargets() {
return sameTypeWildcardInTargets;
}
public void addSameTypeWildcardInTarget(BigDecimal pet) {
sameTypeWildcardInTargets.add( pet );
}
public List<? extends BigDecimal> getSameTypeWildcardInBoths() {
return sameTypeWildcardInBoths;
}
public void addSameTypeWildcardInBoth(BigDecimal pet) {
sameTypeWildcardInBoths.add( pet );
}
}

View File

@ -0,0 +1,120 @@
/**
* Copyright 2012-2017 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.test.bugs._1170.source;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* @author Cornelius Dirmeier
*/
public class Source {
private List<String> withoutWildcards = new ArrayList<String>();
private List<String> wildcardInSources = new ArrayList<String>();
private List<String> wildcardInTargets = new ArrayList<String>();
private List<String> wildcardInBoths = new ArrayList<String>();
private List<String> wildcardInSourcesAddAll = new ArrayList<String>();
private List<String> wildcardAdderToSetters = new ArrayList<String>();
private List<BigDecimal> sameTypeWildcardInSources = new ArrayList<BigDecimal>();
private List<BigDecimal> sameTypeWildcardInTargets = new ArrayList<BigDecimal>();
private List<BigDecimal> sameTypeWildcardInBoths = new ArrayList<BigDecimal>();
public List<String> getWithoutWildcards() {
return withoutWildcards;
}
public void addWithoutWildcard(String pet) {
this.withoutWildcards.add( pet );
}
public List<? extends String> getWildcardInSources() {
return wildcardInSources;
}
public void addWildcardInSource(String pet) {
wildcardInSources.add( pet );
}
public List<String> getWildcardInTargets() {
return wildcardInTargets;
}
public void addWildcardInTarget(String pet) {
wildcardInTargets.add( pet );
}
public List<? extends String> getWildcardInBoths() {
return wildcardInBoths;
}
public void addWildcardInBoth(String pet) {
wildcardInBoths.add( pet );
}
public List<? extends String> getWildcardInSourcesAddAll() {
return wildcardInSourcesAddAll;
}
public void addWildcardInSourcesAddAll(String pet) {
wildcardInSourcesAddAll.add( pet );
}
public List<? extends String> getWildcardAdderToSetters() {
return wildcardAdderToSetters;
}
public void addWildcardAdderToSetter(String pet) {
wildcardAdderToSetters.add( pet );
}
public List<? extends BigDecimal> getSameTypeWildcardInSources() {
return sameTypeWildcardInSources;
}
public void addSameTypeWildcardInSource(BigDecimal pet) {
sameTypeWildcardInSources.add( pet );
}
public List<BigDecimal> getSameTypeWildcardInTargets() {
return sameTypeWildcardInTargets;
}
public void addSameTypeWildcardInTarget(BigDecimal pet) {
sameTypeWildcardInTargets.add( pet );
}
public List<? extends BigDecimal> getSameTypeWildcardInBoths() {
return sameTypeWildcardInBoths;
}
public void addSameTypeWildcardInBoth(BigDecimal pet) {
sameTypeWildcardInBoths.add( pet );
}
}