From 1b0447d40457022575466e6d4166ff783c966fd7 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Mon, 15 Jul 2013 23:43:11 +0200 Subject: [PATCH] #53 Avoiding conlicting variable names --- .../ap/model/AnnotationMapperReference.java | 10 ++- .../ap/model/DefaultMapperReference.java | 13 ++-- .../ap/model/IterableMappingMethod.java | 13 ++++ .../mapstruct/ap/model/MapMappingMethod.java | 25 ++++++- .../org/mapstruct/ap/model/MappingMethod.java | 10 +++ .../ap/model/MappingMethodReference.java | 7 ++ .../ap/processor/MapperCreationProcessor.java | 10 ++- .../java/org/mapstruct/ap/util/Strings.java | 70 +++++++++++++++++++ ...uct.ap.model.AnnotationMapperReference.ftl | 2 +- ...g.mapstruct.ap.model.BeanMappingMethod.ftl | 4 +- ...struct.ap.model.DefaultMapperReference.ftl | 2 +- ...pstruct.ap.model.IterableMappingMethod.ftl | 12 ++-- ...rg.mapstruct.ap.model.MapMappingMethod.ftl | 24 +++---- ...struct.ap.model.MappingMethodReference.ftl | 2 +- .../org/mapstruct/ap/test/naming/Break.java | 54 ++++++++++++++ .../org/mapstruct/ap/test/naming/Source.java | 54 ++++++++++++++ .../ap/test/naming/SourceTargetMapper.java | 40 +++++++++++ .../ap/test/naming/VariableNamingTest.java | 68 ++++++++++++++++++ .../org/mapstruct/ap/test/naming/While.java | 26 +++++++ 19 files changed, 412 insertions(+), 34 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/naming/Break.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/naming/Source.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/naming/SourceTargetMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/naming/VariableNamingTest.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/naming/While.java diff --git a/processor/src/main/java/org/mapstruct/ap/model/AnnotationMapperReference.java b/processor/src/main/java/org/mapstruct/ap/model/AnnotationMapperReference.java index 20f15b972..14c2e98b4 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/AnnotationMapperReference.java +++ b/processor/src/main/java/org/mapstruct/ap/model/AnnotationMapperReference.java @@ -18,9 +18,11 @@ */ package org.mapstruct.ap.model; +import java.beans.Introspector; import java.util.Set; import org.mapstruct.ap.util.Collections; +import org.mapstruct.ap.util.Strings; /** * Mapper reference which is retrieved via Annotation-based dependency injection. @@ -30,8 +32,8 @@ import org.mapstruct.ap.util.Collections; */ public class AnnotationMapperReference extends AbstractModelElement implements MapperReference { - private Annotation annotation; - private Type type; + private final Annotation annotation; + private final Type type; public AnnotationMapperReference(Annotation annotation, Type type) { this.annotation = annotation; @@ -51,4 +53,8 @@ public class AnnotationMapperReference extends AbstractModelElement implements M public Set getImportTypes() { return Collections.asSet( annotation.getImportTypes(), type ); } + + public String getVariableName() { + return Strings.getSaveVariableName( Introspector.decapitalize( type.getName() ) ); + } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/DefaultMapperReference.java b/processor/src/main/java/org/mapstruct/ap/model/DefaultMapperReference.java index 42c053d21..ae2da9a58 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/DefaultMapperReference.java +++ b/processor/src/main/java/org/mapstruct/ap/model/DefaultMapperReference.java @@ -18,20 +18,21 @@ */ package org.mapstruct.ap.model; +import java.beans.Introspector; import java.util.Set; import org.mapstruct.ap.util.Collections; +import org.mapstruct.ap.util.Strings; /** - * Mapper reference which is retrieved via the {@code Mappers#getMapper()} - * method. Used by default if no other component model is specified via - * {@code Mapper#uses()}. + * Mapper reference which is retrieved via the {@code Mappers#getMapper()} method. Used by default if no other component + * model is specified via {@code Mapper#uses()}. * * @author Gunnar Morling */ public class DefaultMapperReference extends AbstractModelElement implements MapperReference { - private Type type; + private final Type type; public DefaultMapperReference(Type type) { this.type = type; @@ -46,4 +47,8 @@ public class DefaultMapperReference extends AbstractModelElement implements Mapp public Set getImportTypes() { return Collections.asSet( type ); } + + public String getVariableName() { + return Strings.getSaveVariableName( Introspector.decapitalize( type.getName() ) ); + } } 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 1d43483d9..55ccf444f 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java @@ -18,8 +18,11 @@ */ package org.mapstruct.ap.model; +import java.beans.Introspector; import java.util.Set; +import org.mapstruct.ap.util.Strings; + /** * A {@link MappingMethod} implemented by a {@link Mapper} class which maps one iterable type to another. The collection * elements are mapped either by a {@link TypeConversion} or another mapping method. @@ -56,4 +59,14 @@ public class IterableMappingMethod extends MappingMethod { return types; } + + public String getLoopVariableName() { + return Strings.getSaveVariableName( + Introspector.decapitalize( + getSourceType().getTypeParameters() + .get( 0 ) + .getName() + ), getParameterName() + ); + } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java index ea06a614f..d602c91ca 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java @@ -20,6 +20,8 @@ package org.mapstruct.ap.model; import java.util.Set; +import org.mapstruct.ap.util.Strings; + /** * A {@link MappingMethod} implemented by a {@link Mapper} class which maps one {@code Map} type to another. Keys and * values are mapped either by a {@link TypeConversion} or another mapping method if required. @@ -29,8 +31,8 @@ import java.util.Set; public class MapMappingMethod extends MappingMethod { private final MappingMethodReference keyMappingMethod; - private final TypeConversion keyConversion; private final MappingMethodReference valueMappingMethod; + private final TypeConversion keyConversion; private final TypeConversion valueConversion; public MapMappingMethod(String name, String parameterName, Type sourceType, Type targetType, @@ -69,7 +71,24 @@ public class MapMappingMethod extends MappingMethod { return types; } - public String getReturnValueName() { - return "map"; + public String getKeyVariableName() { + return Strings.getSaveVariableName( + "key", + getParameterName() + ); + } + + public String getValueVariableName() { + return Strings.getSaveVariableName( + "value", + getParameterName() + ); + } + + public String getEntryVariableName() { + return Strings.getSaveVariableName( + "entry", + getParameterName() + ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java index 5a57d02d3..ca2d763dc 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java @@ -18,9 +18,12 @@ */ package org.mapstruct.ap.model; +import java.beans.Introspector; import java.util.HashSet; import java.util.Set; +import org.mapstruct.ap.util.Strings; + /** * A method implemented or referenced by a {@link Mapper} class. * @@ -65,6 +68,13 @@ public abstract class MappingMethod extends AbstractModelElement { return types; } + public String getReturnValueName() { + return Strings.getSaveVariableName( + Introspector.decapitalize( getTargetType().getName() ), + getParameterName() + ); + } + @Override public String toString() { return "MappingMethod {" + diff --git a/processor/src/main/java/org/mapstruct/ap/model/MappingMethodReference.java b/processor/src/main/java/org/mapstruct/ap/model/MappingMethodReference.java index 311546cf2..4d423d340 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MappingMethodReference.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MappingMethodReference.java @@ -18,9 +18,12 @@ */ package org.mapstruct.ap.model; +import java.beans.Introspector; import java.util.HashSet; import java.util.Set; +import org.mapstruct.ap.util.Strings; + /** * Represents a reference to {@link MappingMethod}. * @@ -40,6 +43,10 @@ public class MappingMethodReference extends MappingMethod { return declaringMapper; } + public String getMapperVariableName() { + return Strings.getSaveVariableName( Introspector.decapitalize( declaringMapper.getName() ) ); + } + public Set getReferencedTypes() { Set types = new HashSet(); types.add( getSourceType() ); diff --git a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java index 093df31a8..45ec19905 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java @@ -340,7 +340,10 @@ public class MapperCreationProcessor implements ModelElementProcessor KEYWORDS = asSet( + "abstract", + "continue", + "for", + "new", + "switch", + "assert", + "default", + "goto", + "package", + "synchronized", + "boolean", + "do", + "if", + "private", + "this", + "break", + "double", + "implements", + "protected", + "throw", + "byte", + "else", + "import", + "public", + "throws", + "case", + "enum", + "instanceof", + "return", + "transient", + "catch", + "extends", + "int", + "short", + "try", + "char", + "final", + "interface", + "static", + "void", + "class", + "finally", + "long", + "strictfp", + "volatile", + "const", + "float", + "native", + "super", + "while" + ); + private Strings() { } @@ -53,4 +112,15 @@ public class Strings { public static boolean isEmpty(String string) { return string == null || string.isEmpty(); } + + public static String getSaveVariableName(String name, String... existingVariableNames) { + Set conflictingNames = new HashSet( KEYWORDS ); + conflictingNames.addAll( Arrays.asList( existingVariableNames ) ); + + while ( conflictingNames.contains( name ) ) { + name = name + "_"; + } + + return name; + } } diff --git a/processor/src/main/resources/org.mapstruct.ap.model.AnnotationMapperReference.ftl b/processor/src/main/resources/org.mapstruct.ap.model.AnnotationMapperReference.ftl index aac5bb775..9f1de8f0e 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.AnnotationMapperReference.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.AnnotationMapperReference.ftl @@ -19,4 +19,4 @@ --> <#nt><@includeModel object=annotation/> - private ${mapperType.name} ${mapperType.name?uncap_first}; \ No newline at end of file + private ${mapperType.name} ${variableName}; \ No newline at end of file diff --git a/processor/src/main/resources/org.mapstruct.ap.model.BeanMappingMethod.ftl b/processor/src/main/resources/org.mapstruct.ap.model.BeanMappingMethod.ftl index 11acaeb51..1d914ee39 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.BeanMappingMethod.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.BeanMappingMethod.ftl @@ -24,11 +24,11 @@ return null; } - ${targetType.name} ${targetType.name?uncap_first} = new ${targetType.name}(); + ${targetType.name} ${returnValueName} = new ${targetType.name}(); <#list propertyMappings as propertyMapping> <@includeModel object=propertyMapping/> - return ${targetType.name?uncap_first}; + return ${returnValueName}; } diff --git a/processor/src/main/resources/org.mapstruct.ap.model.DefaultMapperReference.ftl b/processor/src/main/resources/org.mapstruct.ap.model.DefaultMapperReference.ftl index 5c8a8bde7..bc2b59426 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.DefaultMapperReference.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.DefaultMapperReference.ftl @@ -18,4 +18,4 @@ limitations under the License. --> - private final ${mapperType.name} ${mapperType.name?uncap_first} = new ${mapperType.name}(); \ No newline at end of file + private final ${mapperType.name} ${variableName} = new ${mapperType.name}(); \ No newline at end of file diff --git a/processor/src/main/resources/org.mapstruct.ap.model.IterableMappingMethod.ftl b/processor/src/main/resources/org.mapstruct.ap.model.IterableMappingMethod.ftl index 0916185e1..ce6202ff7 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.IterableMappingMethod.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.IterableMappingMethod.ftl @@ -25,17 +25,17 @@ } <#-- Use the interface type on the left side, except it is java.lang.Iterable; use the implementation type - if present - on the right side --> - <#if targetType.name == "Iterable" && targetType.packageName == "java.lang">${targetType.iterableImplementationType.name}<#else>${targetType.name}<<@includeModel object=targetType.typeParameters[0]/>> ${targetType.name?uncap_first} = new <#if targetType.iterableImplementationType??>${targetType.iterableImplementationType.name}<#else>${targetType.name}<<@includeModel object=targetType.typeParameters[0]/>>(); + <#if targetType.name == "Iterable" && targetType.packageName == "java.lang">${targetType.iterableImplementationType.name}<#else>${targetType.name}<<@includeModel object=targetType.typeParameters[0]/>> ${returnValueName} = new <#if targetType.iterableImplementationType??>${targetType.iterableImplementationType.name}<#else>${targetType.name}<<@includeModel object=targetType.typeParameters[0]/>>(); - for ( <@includeModel object=sourceType.typeParameters[0]/> ${sourceType.typeParameters[0].name?uncap_first} : ${parameterName} ) { + for ( <@includeModel object=sourceType.typeParameters[0]/> ${loopVariableName} : ${parameterName} ) { <#if elementMappingMethod??> - ${targetType.name?uncap_first}.add( <@includeModel object=elementMappingMethod input="${sourceType.typeParameters[0].name?uncap_first}"/> ); + ${returnValueName}.add( <@includeModel object=elementMappingMethod input="${loopVariableName}"/> ); <#else> <#if (conversion.exceptionTypes?size == 0) > - ${targetType.name?uncap_first}.add( <@includeModel object=conversion/> ); + ${returnValueName}.add( <@includeModel object=conversion/> ); <#else> try { - ${targetType.name?uncap_first}.add( <@includeModel object=conversion/> ); + ${returnValueName}.add( <@includeModel object=conversion/> ); } <#list conversion.exceptionTypes as exceptionType> catch( ${exceptionType.name} e ) { @@ -46,5 +46,5 @@ } - return ${targetType.name?uncap_first}; + return ${returnValueName}; } 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 07ce9c77e..2d6a784ac 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl @@ -26,18 +26,18 @@ <@includeModel object=targetType /> ${returnValueName} = new <#if targetType.mapImplementationType??><@includeModel object=targetType.mapImplementationType /><#else><@includeModel object=targetType />(); - for ( Map.Entry<<#list sourceType.typeParameters as typeParameter><@includeModel object=typeParameter /><#if typeParameter_has_next>, > entry : ${parameterName}.entrySet() ) { + for ( Map.Entry<<#list sourceType.typeParameters as typeParameter><@includeModel object=typeParameter /><#if typeParameter_has_next>, > ${entryVariableName} : ${parameterName}.entrySet() ) { <#-- key --> <#if keyMappingMethod??> - <@includeModel object=targetType.typeParameters[0]/> key = <@includeModel object=keyMappingMethod input="entry.getKey()"/>; + <@includeModel object=targetType.typeParameters[0]/> ${keyVariableName} = <@includeModel object=keyMappingMethod input="entry.getKey()"/>; <#elseif keyConversion??> <#if (keyConversion.exceptionTypes?size == 0) > - <@includeModel object=targetType.typeParameters[0]/> key = <@includeModel object=keyConversion/>; + <@includeModel object=targetType.typeParameters[0]/> ${keyVariableName} = <@includeModel object=keyConversion/>; <#else> - <@includeModel object=targetType.typeParameters[0]/> key; + <@includeModel object=targetType.typeParameters[0]/> ${keyVariableName}; try { - key = <@includeModel object=keyConversion/>; + ${keyVariableName} = <@includeModel object=keyConversion/>; } <#list keyConversion.exceptionTypes as exceptionType> catch( ${exceptionType.name} e ) { @@ -46,18 +46,18 @@ <#else> - <@includeModel object=targetType.typeParameters[0]/> key = entry.getKey(); + <@includeModel object=targetType.typeParameters[0]/> ${keyVariableName} = entry.getKey(); <#-- value --> <#if valueMappingMethod??> - <@includeModel object=targetType.typeParameters[1]/> value = <@includeModel object=valueMappingMethod input="entry.getValue()"/>; + <@includeModel object=targetType.typeParameters[1]/> ${valueVariableName} = <@includeModel object=valueMappingMethod input="entry.getValue()"/>; <#elseif valueConversion??> <#if (valueConversion.exceptionTypes?size == 0) > - <@includeModel object=targetType.typeParameters[1]/> value = <@includeModel object=valueConversion/>; + <@includeModel object=targetType.typeParameters[1]/> ${valueVariableName} = <@includeModel object=valueConversion/>; <#else> - <@includeModel object=targetType.typeParameters[1]/> value; + <@includeModel object=targetType.typeParameters[1]/> ${valueVariableName}; try { - value = <@includeModel object=valueConversion/>; + ${valueVariableName} = <@includeModel object=valueConversion/>; } <#list valueConversion.exceptionTypes as exceptionType> catch( ${exceptionType.name} e ) { @@ -66,10 +66,10 @@ <#else> - <@includeModel object=targetType.typeParameters[1]/> value = entry.getValue(); + <@includeModel object=targetType.typeParameters[1]/> ${valueVariableName} = entry.getValue(); - ${returnValueName}.put( key, value ); + ${returnValueName}.put( ${keyVariableName}, ${valueVariableName} ); } return ${returnValueName}; diff --git a/processor/src/main/resources/org.mapstruct.ap.model.MappingMethodReference.ftl b/processor/src/main/resources/org.mapstruct.ap.model.MappingMethodReference.ftl index 574bab8ae..a763ed9f2 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.MappingMethodReference.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.MappingMethodReference.ftl @@ -18,4 +18,4 @@ limitations under the License. --> -<#if declaringMapper??>${declaringMapper.name?uncap_first}.${name}( ${ext.input} ) \ No newline at end of file +<#if declaringMapper??>${mapperVariableName}.${name}( ${ext.input} ) \ No newline at end of file diff --git a/processor/src/test/java/org/mapstruct/ap/test/naming/Break.java b/processor/src/test/java/org/mapstruct/ap/test/naming/Break.java new file mode 100644 index 000000000..a8b247bec --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/naming/Break.java @@ -0,0 +1,54 @@ +/** + * Copyright 2012-2013 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.naming; + +import java.util.List; +import java.util.Map; + +public class Break { + + private List values; + private String someNumber; + private Map map; + + public List getValues() { + return values; + } + + public void setValues(List values) { + this.values = values; + } + + public String getSomeNumber() { + return someNumber; + } + + public void setSomeNumber(String someNumber) { + this.someNumber = someNumber; + } + + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/naming/Source.java b/processor/src/test/java/org/mapstruct/ap/test/naming/Source.java new file mode 100644 index 000000000..b33fae36e --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/naming/Source.java @@ -0,0 +1,54 @@ +/** + * Copyright 2012-2013 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.naming; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +public class Source { + + private List values; + private int someNumber; + private Map map; + + public List getValues() { + return values; + } + + public void setValues(List values) { + this.values = values; + } + + public int getSomeNumber() { + return someNumber; + } + + public void setSomeNumber(int someNumber) { + this.someNumber = someNumber; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/naming/SourceTargetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/naming/SourceTargetMapper.java new file mode 100644 index 000000000..b06850c2d --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/naming/SourceTargetMapper.java @@ -0,0 +1,40 @@ +/** + * Copyright 2012-2013 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.naming; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +import org.mapstruct.MapMapping; +import org.mapstruct.Mapper; +import org.mapstruct.Mappers; + +@Mapper(uses = While.class) +public interface SourceTargetMapper { + + SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class ); + + Break sourceToBreak(Source source); + + List longIterableToStringList(List long1); + + @MapMapping(valueDateFormat = "dd.MM.yyyy") + Map longDateMapToStringStringMap(Map value); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/naming/VariableNamingTest.java b/processor/src/test/java/org/mapstruct/ap/test/naming/VariableNamingTest.java new file mode 100644 index 000000000..03aaddf9a --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/naming/VariableNamingTest.java @@ -0,0 +1,68 @@ +/** + * Copyright 2012-2013 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.naming; + +import java.util.Arrays; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.Map; + +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.MapperTestBase; +import org.mapstruct.ap.testutil.WithClasses; +import org.testng.annotations.Test; + +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.MapAssert.entry; + +/** + * Test for aming of variables/members which conflict with keywords or parameter names. + * + * @author Gunnar Morling + */ +@WithClasses({ SourceTargetMapper.class, While.class, Break.class, Source.class }) +@IssueKey("53") +public class VariableNamingTest extends MapperTestBase { + + @Test + public void shouldGenerateImplementationsOfMethodsWithProblematicVariableNmes() { + Source source = new Source(); + + source.setSomeNumber( 42 ); + source.setValues( Arrays.asList( 42L, 121L ) ); + + Map map = new HashMap(); + map.put( 42L, new GregorianCalendar( 1980, 0, 1 ).getTime() ); + map.put( 121L, new GregorianCalendar( 2013, 6, 20 ).getTime() ); + source.setMap( map ); + + Break target = SourceTargetMapper.INSTANCE.sourceToBreak( source ); + + assertThat( target ).isNotNull(); + assertThat( target.getValues() ).isNotNull(); + assertThat( target.getValues() ).containsOnly( "42", "121" ); + assertThat( target.getSomeNumber() ).isEqualTo( "42" ); + assertThat( target.getMap() ).hasSize( 2 ); + assertThat( target.getMap() ).includes( + entry( "42", "01.01.1980" ), + entry( "121", "20.07.2013" ) + ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/naming/While.java b/processor/src/test/java/org/mapstruct/ap/test/naming/While.java new file mode 100644 index 000000000..00cb67033 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/naming/While.java @@ -0,0 +1,26 @@ +/** + * Copyright 2012-2013 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.naming; + +public class While { + + public String asString(int i) { + return String.valueOf( i ); + } +}