mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#124 Making sure variable names always start with a lower-case letter
This commit is contained in:
parent
f122daec6d
commit
cd14bd368f
@ -18,7 +18,6 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model;
|
||||
|
||||
import java.beans.Introspector;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@ -52,7 +51,7 @@ public class DefaultMapperReference extends MapperReference {
|
||||
}
|
||||
|
||||
String variableName = Strings.getSaveVariableName(
|
||||
Introspector.decapitalize( type.getName() ),
|
||||
type.getName(),
|
||||
otherMapperReferences
|
||||
);
|
||||
|
||||
|
@ -18,7 +18,6 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model;
|
||||
|
||||
import java.beans.Introspector;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.common.Parameter;
|
||||
@ -77,12 +76,8 @@ public class IterableMappingMethod extends MappingMethod {
|
||||
|
||||
public String getLoopVariableName() {
|
||||
return Strings.getSaveVariableName(
|
||||
Introspector.decapitalize(
|
||||
getSourceParameter().getType()
|
||||
.getTypeParameters()
|
||||
.get( 0 )
|
||||
.getName()
|
||||
), getParameterNames()
|
||||
getSourceParameter().getType().getTypeParameters().get( 0 ).getName(),
|
||||
getParameterNames()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,6 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model;
|
||||
|
||||
import java.beans.Introspector;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -75,7 +74,7 @@ public abstract class MappingMethod extends ModelElement {
|
||||
|
||||
public String getResultName() {
|
||||
return targetParameter != null ? targetParameter.getName() :
|
||||
Strings.getSaveVariableName( Introspector.decapitalize( getResultType().getName() ), getParameterNames() );
|
||||
Strings.getSaveVariableName( getResultType().getName(), getParameterNames() );
|
||||
}
|
||||
|
||||
public Type getReturnType() {
|
||||
|
@ -18,7 +18,6 @@
|
||||
*/
|
||||
package org.mapstruct.ap.processor;
|
||||
|
||||
import java.beans.Introspector;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@ -27,7 +26,6 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.processing.Messager;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
@ -601,7 +599,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
targetElementType,
|
||||
method.getIterableMapping() != null ? method.getIterableMapping().getDateFormat() : null,
|
||||
Strings.getSaveVariableName(
|
||||
Introspector.decapitalize( sourceElementType.getName() ),
|
||||
sourceElementType.getName(),
|
||||
method.getParameterNames()
|
||||
)
|
||||
);
|
||||
|
@ -88,8 +88,12 @@ public class Strings {
|
||||
private Strings() {
|
||||
}
|
||||
|
||||
public static String capitalize(String name) {
|
||||
return name == null ? null : name.substring( 0, 1 ).toUpperCase() + name.substring( 1 );
|
||||
public static String capitalize(String string) {
|
||||
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) {
|
||||
@ -118,7 +122,19 @@ public class Strings {
|
||||
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) {
|
||||
name = decapitalize( name );
|
||||
|
||||
Set<String> conflictingNames = new HashSet<String>( KEYWORDS );
|
||||
conflictingNames.addAll( existingVariableNames );
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user