#124 Making sure variable names always start with a lower-case letter

This commit is contained in:
Gunnar Morling 2014-02-09 17:50:51 +01:00
parent f122daec6d
commit cd14bd368f
5 changed files with 23 additions and 16 deletions

View File

@ -18,7 +18,6 @@
*/ */
package org.mapstruct.ap.model; package org.mapstruct.ap.model;
import java.beans.Introspector;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -52,7 +51,7 @@ public class DefaultMapperReference extends MapperReference {
} }
String variableName = Strings.getSaveVariableName( String variableName = Strings.getSaveVariableName(
Introspector.decapitalize( type.getName() ), type.getName(),
otherMapperReferences otherMapperReferences
); );

View File

@ -18,7 +18,6 @@
*/ */
package org.mapstruct.ap.model; package org.mapstruct.ap.model;
import java.beans.Introspector;
import java.util.Set; import java.util.Set;
import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Parameter;
@ -77,12 +76,8 @@ public class IterableMappingMethod extends MappingMethod {
public String getLoopVariableName() { public String getLoopVariableName() {
return Strings.getSaveVariableName( return Strings.getSaveVariableName(
Introspector.decapitalize( getSourceParameter().getType().getTypeParameters().get( 0 ).getName(),
getSourceParameter().getType() getParameterNames()
.getTypeParameters()
.get( 0 )
.getName()
), getParameterNames()
); );
} }

View File

@ -18,7 +18,6 @@
*/ */
package org.mapstruct.ap.model; package org.mapstruct.ap.model;
import java.beans.Introspector;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -75,7 +74,7 @@ public abstract class MappingMethod extends ModelElement {
public String getResultName() { public String getResultName() {
return targetParameter != null ? targetParameter.getName() : return targetParameter != null ? targetParameter.getName() :
Strings.getSaveVariableName( Introspector.decapitalize( getResultType().getName() ), getParameterNames() ); Strings.getSaveVariableName( getResultType().getName(), getParameterNames() );
} }
public Type getReturnType() { public Type getReturnType() {

View File

@ -18,7 +18,6 @@
*/ */
package org.mapstruct.ap.processor; package org.mapstruct.ap.processor;
import java.beans.Introspector;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -27,7 +26,6 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.annotation.processing.Messager; import javax.annotation.processing.Messager;
import javax.lang.model.element.Element; import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
@ -601,7 +599,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
targetElementType, targetElementType,
method.getIterableMapping() != null ? method.getIterableMapping().getDateFormat() : null, method.getIterableMapping() != null ? method.getIterableMapping().getDateFormat() : null,
Strings.getSaveVariableName( Strings.getSaveVariableName(
Introspector.decapitalize( sourceElementType.getName() ), sourceElementType.getName(),
method.getParameterNames() method.getParameterNames()
) )
); );

View File

@ -88,8 +88,12 @@ public class Strings {
private Strings() { private Strings() {
} }
public static String capitalize(String name) { public static String capitalize(String string) {
return name == null ? null : name.substring( 0, 1 ).toUpperCase() + name.substring( 1 ); return string == null ? null : string.substring( 0, 1 ).toUpperCase() + string.substring( 1 );
}
public static String decapitalize(String string) {
return string == null ? null : string.substring( 0, 1 ).toLowerCase() + string.substring( 1 );
} }
public static String join(Iterable<?> iterable, String separator) { public static String join(Iterable<?> iterable, String separator) {
@ -118,7 +122,19 @@ public class Strings {
return getSaveVariableName( name, Arrays.asList( existingVariableNames ) ); return getSaveVariableName( name, Arrays.asList( existingVariableNames ) );
} }
/**
* Returns a variable name which doesn't conflict with the given variable names existing in the same scope and the
* Java keywords.
*
* @param name the name to get a safe version for
* @param existingVariableNames the names of other variables existing in the same scope
*
* @return a variable name based on the given original name, not conflicting with any of the given other names or
* any Java keyword; starting with a lower-case letter
*/
public static String getSaveVariableName(String name, Collection<String> existingVariableNames) { public static String getSaveVariableName(String name, Collection<String> existingVariableNames) {
name = decapitalize( name );
Set<String> conflictingNames = new HashSet<String>( KEYWORDS ); Set<String> conflictingNames = new HashSet<String>( KEYWORDS );
conflictingNames.addAll( existingVariableNames ); conflictingNames.addAll( existingVariableNames );