mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#375 safe variable for parameter in forged methods
This commit is contained in:
parent
e4de641911
commit
1d9372a45b
@ -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() {
|
||||
|
@ -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<String> getParameterNames() {
|
||||
return Arrays.asList( "source" );
|
||||
List<String> parameterNames = new ArrayList<String>();
|
||||
for ( Parameter parameter : getParameters() ) {
|
||||
parameterNames.add( parameter.getName() );
|
||||
}
|
||||
return parameterNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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">
|
||||
|
@ -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<K, V> extends Map<K, V> { }
|
@ -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<E> extends List<E> {
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
@ -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() {
|
||||
}
|
||||
}
|
@ -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<String> testIterable;
|
||||
|
||||
private Case<String, Integer> testMap;
|
||||
|
||||
public Int<String> getTestIterable() {
|
||||
return testIterable;
|
||||
}
|
||||
|
||||
public void setTestIterable(Int<String> testIterable) {
|
||||
this.testIterable = testIterable;
|
||||
}
|
||||
|
||||
public Case<String, Integer> getTestMap() {
|
||||
return testMap;
|
||||
}
|
||||
|
||||
public void setTestMap(Case<String, Integer> testMap) {
|
||||
this.testMap = testMap;
|
||||
}
|
||||
|
||||
}
|
@ -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<Integer> testIterable;
|
||||
|
||||
private Map<String, String> testMap;
|
||||
|
||||
public List<Integer> getTestIterable() {
|
||||
return testIterable;
|
||||
}
|
||||
|
||||
public void setTestIterable(List<Integer> testIterable) {
|
||||
this.testIterable = testIterable;
|
||||
}
|
||||
|
||||
public Map<String, String> getTestMap() {
|
||||
return testMap;
|
||||
}
|
||||
|
||||
public void setTestMap(Map<String, String> testMap) {
|
||||
this.testMap = testMap;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user