diff --git a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java index 0688178ce..aa81ff20d 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java @@ -27,7 +27,6 @@ import org.mapstruct.ap.model.assignment.Assignment; import org.mapstruct.ap.model.assignment.SetterWrapper; import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; -import org.mapstruct.ap.model.common.TypeFactory; import org.mapstruct.ap.model.source.Method; import org.mapstruct.ap.prism.NullValueMappingPrism; import org.mapstruct.ap.util.MapperConfig; @@ -45,7 +44,7 @@ public class IterableMappingMethod extends MappingMethod { private final MethodReference factoryMethod; private final boolean overridden; private final boolean mapNullToDefault; - private final TypeFactory typeFactory; + private final String loopVariableName; public static class Builder { @@ -79,7 +78,7 @@ public class IterableMappingMethod extends MappingMethod { method.getSourceParameters().iterator().next().getType().getTypeParameters().get( 0 ); Type targetElementType = method.getResultType().getTypeParameters().get( 0 ); - String conversionStr = + String loopVariableName = Strings.getSaveVariableName( sourceElementType.getName(), method.getParameterNames() ); @@ -91,7 +90,7 @@ public class IterableMappingMethod extends MappingMethod { null, // there is no targetPropertyName dateFormat, qualifiers, - conversionStr + loopVariableName ); if ( assignment == null ) { @@ -117,20 +116,20 @@ public class IterableMappingMethod extends MappingMethod { assignment, factoryMethod, mapNullToDefault, - ctx.getTypeFactory() + loopVariableName ); } } private IterableMappingMethod(Method method, Assignment parameterAssignment, MethodReference factoryMethod, - boolean mapNullToDefault, TypeFactory typeFactory) { + boolean mapNullToDefault, String loopVariableName ) { super( method ); this.elementAssignment = parameterAssignment; this.factoryMethod = factoryMethod; this.overridden = method.overridesMethod(); this.mapNullToDefault = mapNullToDefault; - this.typeFactory = typeFactory; + this.loopVariableName = loopVariableName; } public Parameter getSourceParameter() { @@ -167,10 +166,7 @@ public class IterableMappingMethod extends MappingMethod { } public String getLoopVariableName() { - return Strings.getSaveVariableName( - getSourceParameter().getType().getTypeParameters().get( 0 ).getName(), - getParameterNames() - ); + return loopVariableName; } public MethodReference getFactoryMethod() { diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/ForgedMethod.java b/processor/src/main/java/org/mapstruct/ap/model/source/ForgedMethod.java index 12b1ccdb2..3729a4ae2 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/ForgedMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/ForgedMethod.java @@ -18,6 +18,7 @@ */ package org.mapstruct.ap.model.source; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -50,7 +51,9 @@ public class ForgedMethod implements Method { * @param positionHintElement element used to for reference to the position in the source file. */ public ForgedMethod(Type sourceType, Type targetType, ExecutableElement positionHintElement) { - this.parameters = Arrays.asList( new Parameter( Strings.decapitalize( sourceType.getName() ), sourceType ) ); + String sourceParamName = Strings.decapitalize( sourceType.getName() ); + String sourceParamSafeName = Strings.getSaveVariableName( sourceParamName ); + this.parameters = Arrays.asList( new Parameter( sourceParamSafeName, sourceType ) ); this.returnType = targetType; String fromName = getName( parameters.iterator().next().getType() ); @@ -69,7 +72,9 @@ public class ForgedMethod implements Method { * @param positionHintElement element used to for reference to the position in the source file. */ public ForgedMethod(String name, Type sourceType, Type targetType, ExecutableElement positionHintElement) { - this.parameters = Arrays.asList( new Parameter( Strings.decapitalize( sourceType.getName() ), sourceType ) ); + String sourceParamName = Strings.decapitalize( sourceType.getName() ); + String sourceParamSafeName = Strings.getSaveVariableName( sourceParamName ); + this.parameters = Arrays.asList( new Parameter( sourceParamSafeName, sourceType ) ); this.returnType = targetType; this.name = name; this.positionHintElement = positionHintElement; @@ -150,7 +155,11 @@ public class ForgedMethod implements Method { @Override public List getParameterNames() { - return Arrays.asList( "source" ); + List parameterNames = new ArrayList(); + for ( Parameter parameter : getParameters() ) { + parameterNames.add( parameter.getName() ); + } + return parameterNames; } @Override diff --git a/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl b/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl index 4aa59ff5a..478b5509c 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl @@ -39,13 +39,11 @@ <#-- key --> <@includeModel object=keyAssignment targetAccessorName=keyVariableName - targetType=resultType.typeParameters[0] - isLocalVar=true/> + targetType=resultType.typeParameters[0]/> <#-- value --> <@includeModel object=valueAssignment targetAccessorName=valueVariableName - targetType=resultType.typeParameters[1] - isLocalVar=true/> + targetType=resultType.typeParameters[1]/> ${resultName}.put( ${keyVariableName}, ${valueVariableName} ); } <#if returnType.name != "void"> diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Case.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Case.java new file mode 100644 index 000000000..c4408429c --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Case.java @@ -0,0 +1,27 @@ +/** + * Copyright 2012-2014 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._375; + +import java.util.Map; + +/** + * + * @author Sjaak Derksen + */ +public interface Case extends Map { } diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Int.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Int.java new file mode 100644 index 000000000..bf2f0c1ee --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Int.java @@ -0,0 +1,29 @@ +/** + * Copyright 2012-2014 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._375; + +import java.util.List; + +/** + * + * @author Sjaak Derksen + */ +public interface Int extends List { + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Issue375Mapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Issue375Mapper.java new file mode 100644 index 000000000..6f48a4b16 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Issue375Mapper.java @@ -0,0 +1,31 @@ +/** + * Copyright 2012-2014 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._375; + + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface Issue375Mapper { + + Issue375Mapper INSTANCE = Mappers.getMapper( Issue375Mapper.class ); + + Target mapTo(Source string); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Issue375Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Issue375Test.java new file mode 100644 index 000000000..bbcf16387 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Issue375Test.java @@ -0,0 +1,40 @@ +/** + * Copyright 2012-2014 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._375; + +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; + +/** + * Reproducer for https://github.com/mapstruct/mapstruct/issues/375. + * + * @author Sjaak Derksen + */ +@IssueKey( "375" ) +@RunWith(AnnotationProcessorTestRunner.class) +public class Issue375Test { + + @Test + @WithClasses( { Issue375Mapper.class, Source.class, Target.class, Int.class, Case.class } ) + public void shouldForgeNewMappings() { + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Source.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Source.java new file mode 100644 index 000000000..678e9a99d --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Source.java @@ -0,0 +1,48 @@ +/** + * Copyright 2012-2014 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._375; + + +/** + * + * @author Sjaak Derksen + */ +public class Source { + + private Int testIterable; + + private Case testMap; + + public Int getTestIterable() { + return testIterable; + } + + public void setTestIterable(Int testIterable) { + this.testIterable = testIterable; + } + + public Case getTestMap() { + return testMap; + } + + public void setTestMap(Case testMap) { + this.testMap = testMap; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Target.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Target.java new file mode 100644 index 000000000..72866cb49 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_375/Target.java @@ -0,0 +1,50 @@ +/** + * Copyright 2012-2014 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._375; + +import java.util.List; +import java.util.Map; + +/** + * + * @author Sjaak Derksen + */ +public class Target { + + private List testIterable; + + private Map testMap; + + public List getTestIterable() { + return testIterable; + } + + public void setTestIterable(List testIterable) { + this.testIterable = testIterable; + } + + public Map getTestMap() { + return testMap; + } + + public void setTestMap(Map testMap) { + this.testMap = testMap; + } + +}