#1111 local variabele should not clash with loop variable

This commit is contained in:
sjaakd 2017-03-01 22:23:00 +01:00
parent 880ae56652
commit 8fab3dd4e5
9 changed files with 140 additions and 19 deletions

View File

@ -22,6 +22,7 @@ import static org.mapstruct.ap.internal.util.Collections.first;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
@ -211,6 +212,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
return new BeanMappingMethod(
method,
existingVariableNames,
propertyMappings,
factoryMethod,
mapNullToDefault,
@ -724,6 +726,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
}
private BeanMappingMethod(Method method,
Collection<String> existingVariableNames,
List<PropertyMapping> propertyMappings,
MethodReference factoryMethod,
boolean mapNullToDefault,
@ -732,6 +735,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
List<LifecycleCallbackMethodReference> afterMappingReferences) {
super(
method,
existingVariableNames,
factoryMethod,
mapNullToDefault,
beforeMappingReferences,

View File

@ -18,6 +18,7 @@
*/
package org.mapstruct.ap.internal.model;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@ -39,16 +40,21 @@ public abstract class ContainerMappingMethod extends NormalTypeMappingMethod {
private final Assignment elementAssignment;
private final String loopVariableName;
private final SelectionParameters selectionParameters;
private final String index1Name;
private final String index2Name;
ContainerMappingMethod(Method method, Assignment parameterAssignment, MethodReference factoryMethod,
boolean mapNullToDefault, String loopVariableName,
ContainerMappingMethod(Method method, Collection<String> existingVariables, Assignment parameterAssignment,
MethodReference factoryMethod, boolean mapNullToDefault, String loopVariableName,
List<LifecycleCallbackMethodReference> beforeMappingReferences,
List<LifecycleCallbackMethodReference> afterMappingReferences,
SelectionParameters selectionParameters) {
super( method, factoryMethod, mapNullToDefault, beforeMappingReferences, afterMappingReferences );
super( method, existingVariables, factoryMethod, mapNullToDefault, beforeMappingReferences,
afterMappingReferences );
this.elementAssignment = parameterAssignment;
this.loopVariableName = loopVariableName;
this.selectionParameters = selectionParameters;
this.index1Name = Strings.getSaveVariableName( "i", existingVariables );
this.index2Name = Strings.getSaveVariableName( "j", existingVariables );
}
public Parameter getSourceParameter() {
@ -81,11 +87,11 @@ public abstract class ContainerMappingMethod extends NormalTypeMappingMethod {
public abstract Type getResultElementType();
public String getIndex1Name() {
return Strings.getSaveVariableName( "i", loopVariableName, getSourceParameter().getName(), getResultName() );
return index1Name;
}
public String getIndex2Name() {
return Strings.getSaveVariableName( "j", loopVariableName, getSourceParameter().getName(), getResultName() );
return index2Name;
}
@Override

View File

@ -20,6 +20,7 @@ package org.mapstruct.ap.internal.model;
import static org.mapstruct.ap.internal.util.Collections.first;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -143,6 +144,7 @@ public abstract class ContainerMappingMethodBuilder<B extends ContainerMappingMe
return instantiateMappingMethod(
method,
existingVariables,
assignment,
factoryMethod,
mapNullToDefault,
@ -153,7 +155,8 @@ public abstract class ContainerMappingMethodBuilder<B extends ContainerMappingMe
);
}
protected abstract M instantiateMappingMethod(Method method, Assignment assignment, MethodReference factoryMethod,
protected abstract M instantiateMappingMethod(Method method, Collection<String> existingVariables,
Assignment assignment, MethodReference factoryMethod,
boolean mapNullToDefault, String loopVariableName,
List<LifecycleCallbackMethodReference> beforeMappingMethods,
List<LifecycleCallbackMethodReference> afterMappingMethods,

View File

@ -20,6 +20,7 @@ package org.mapstruct.ap.internal.model;
import static org.mapstruct.ap.internal.util.Collections.first;
import java.util.Collection;
import java.util.List;
import org.mapstruct.ap.internal.model.assignment.Assignment;
@ -62,12 +63,13 @@ public class IterableMappingMethod extends ContainerMappingMethod {
}
@Override
protected IterableMappingMethod instantiateMappingMethod(Method method, Assignment assignment,
MethodReference factoryMethod, boolean mapNullToDefault, String loopVariableName,
protected IterableMappingMethod instantiateMappingMethod(Method method, Collection<String> existingVariables,
Assignment assignment, MethodReference factoryMethod, boolean mapNullToDefault, String loopVariableName,
List<LifecycleCallbackMethodReference> beforeMappingMethods,
List<LifecycleCallbackMethodReference> afterMappingMethods, SelectionParameters selectionParameters) {
return new IterableMappingMethod(
method,
existingVariables,
assignment,
factoryMethod,
mapNullToDefault,
@ -79,13 +81,14 @@ public class IterableMappingMethod extends ContainerMappingMethod {
}
}
private IterableMappingMethod(Method method, Assignment parameterAssignment, MethodReference factoryMethod,
boolean mapNullToDefault, String loopVariableName,
private IterableMappingMethod(Method method, Collection<String> existingVariables, Assignment parameterAssignment,
MethodReference factoryMethod, boolean mapNullToDefault, String loopVariableName,
List<LifecycleCallbackMethodReference> beforeMappingReferences,
List<LifecycleCallbackMethodReference> afterMappingReferences,
SelectionParameters selectionParameters) {
super(
method,
existingVariables,
parameterAssignment,
factoryMethod,
mapNullToDefault,

View File

@ -18,6 +18,7 @@
*/
package org.mapstruct.ap.internal.model;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@ -161,6 +162,7 @@ public class MapMappingMethod extends NormalTypeMappingMethod {
return new MapMappingMethod(
method,
existingVariables,
keyAssignment,
valueAssignment,
factoryMethod,
@ -176,11 +178,12 @@ public class MapMappingMethod extends NormalTypeMappingMethod {
}
}
private MapMappingMethod(Method method, Assignment keyAssignment, Assignment valueAssignment,
MethodReference factoryMethod, boolean mapNullToDefault,
private MapMappingMethod(Method method, Collection<String> existingVariableNames, Assignment keyAssignment,
Assignment valueAssignment, MethodReference factoryMethod, boolean mapNullToDefault,
List<LifecycleCallbackMethodReference> beforeMappingReferences,
List<LifecycleCallbackMethodReference> afterMappingReferences) {
super( method, factoryMethod, mapNullToDefault, beforeMappingReferences, afterMappingReferences );
super( method, existingVariableNames, factoryMethod, mapNullToDefault, beforeMappingReferences,
afterMappingReferences );
this.keyAssignment = keyAssignment;
this.valueAssignment = valueAssignment;

View File

@ -18,6 +18,7 @@
*/
package org.mapstruct.ap.internal.model;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@ -36,10 +37,11 @@ public abstract class NormalTypeMappingMethod extends MappingMethod {
private final boolean overridden;
private final boolean mapNullToDefault;
NormalTypeMappingMethod(Method method, MethodReference factoryMethod, boolean mapNullToDefault,
NormalTypeMappingMethod(Method method, Collection<String> existingVariableNames, MethodReference factoryMethod,
boolean mapNullToDefault,
List<LifecycleCallbackMethodReference> beforeMappingReferences,
List<LifecycleCallbackMethodReference> afterMappingReferences) {
super( method, beforeMappingReferences, afterMappingReferences );
super( method, existingVariableNames, beforeMappingReferences, afterMappingReferences );
this.factoryMethod = factoryMethod;
this.overridden = method.overridesMethod();
this.mapNullToDefault = mapNullToDefault;

View File

@ -18,6 +18,7 @@
*/
package org.mapstruct.ap.internal.model;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -60,8 +61,8 @@ public class StreamMappingMethod extends ContainerMappingMethod {
}
@Override
protected StreamMappingMethod instantiateMappingMethod(Method method, Assignment assignment,
MethodReference factoryMethod, boolean mapNullToDefault, String loopVariableName,
protected StreamMappingMethod instantiateMappingMethod(Method method, Collection<String> existingVariables,
Assignment assignment, MethodReference factoryMethod, boolean mapNullToDefault, String loopVariableName,
List<LifecycleCallbackMethodReference> beforeMappingMethods,
List<LifecycleCallbackMethodReference> afterMappingMethods, SelectionParameters selectionParameters) {
@ -78,6 +79,7 @@ public class StreamMappingMethod extends ContainerMappingMethod {
return new StreamMappingMethod(
method,
existingVariables,
assignment,
factoryMethod,
mapNullToDefault,
@ -90,13 +92,14 @@ public class StreamMappingMethod extends ContainerMappingMethod {
}
}
private StreamMappingMethod(Method method, Assignment parameterAssignment, MethodReference factoryMethod,
boolean mapNullToDefault, String loopVariableName,
private StreamMappingMethod(Method method, Collection<String> existingVariables, Assignment parameterAssignment,
MethodReference factoryMethod, boolean mapNullToDefault, String loopVariableName,
List<LifecycleCallbackMethodReference> beforeMappingReferences,
List<LifecycleCallbackMethodReference> afterMappingReferences,
SelectionParameters selectionParameters, Set<Type> helperImports) {
super(
method,
existingVariables,
parameterAssignment,
factoryMethod,
mapNullToDefault,

View File

@ -0,0 +1,44 @@
/**
* 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._1111;
import java.util.List;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
*
* @author Sjaak Derksen
*/
@Mapper
public interface Issue1111Mapper {
Issue1111Mapper INSTANCE = Mappers.getMapper( Issue1111Mapper.class );
Target toTarget(Source in);
List<Target> list(List<Source> in);
List<List<Target>> listList(List<List<Source>> in);
class Source { }
class Target { }
}

View File

@ -0,0 +1,53 @@
/**
* 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._1111;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.test.bugs._1111.Issue1111Mapper.Source;
import org.mapstruct.ap.test.bugs._1111.Issue1111Mapper.Target;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
/**
*
* @author Sjaak Derksen
*/
@IssueKey( "1111")
@WithClasses({Issue1111Mapper.class})
@RunWith(AnnotationProcessorTestRunner.class)
public class Issue1111Test {
@Test
public void shouldCompile() {
List<List<Source>> source = Arrays.asList( Arrays.asList( new Source() ) );
List<List<Issue1111Mapper.Target>> target = Issue1111Mapper.INSTANCE.listList( source );
assertThat( target ).hasSize( 1 );
assertThat( target.get( 0 ) ).hasSize( 1 );
assertThat( target.get( 0 ).get( 0 ) ).isInstanceOf( Target.class );
}
}