#17 Mapping attributes inherited from super types (from Andreas)

This commit is contained in:
Gunnar Morling 2013-06-16 20:21:45 +02:00
parent 5046eda92e
commit 9318adf7b7
8 changed files with 258 additions and 24 deletions

View File

@ -435,12 +435,23 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
}
private List<MappedProperty> getMappedProperties(ExecutableElement method, Map<String, Mapping> mappings) {
Element returnTypeElement = typeUtils.asElement( method.getReturnType() );
Element parameterElement = typeUtils.asElement( method.getParameters().get( 0 ).asType() );
TypeElement returnTypeElement = (TypeElement) typeUtils.asElement( method.getReturnType() );
TypeElement parameterElement = (TypeElement) typeUtils.asElement( method.getParameters().get( 0 ).asType() );
List<MappedProperty> properties = new ArrayList<MappedProperty>();
List<ExecutableElement> sourceGetters = Filters.getterMethodsIn( parameterElement.getEnclosedElements() );
List<ExecutableElement> targetSetters = Filters.setterMethodsIn( returnTypeElement.getEnclosedElements() );
List<ExecutableElement> sourceGetters = Filters.getterMethodsIn(
elementUtils.getAllMembers( parameterElement )
);
List<ExecutableElement> targetSetters = Filters.setterMethodsIn(
elementUtils.getAllMembers( returnTypeElement )
);
List<ExecutableElement> sourceSetters = Filters.setterMethodsIn(
elementUtils.getAllMembers( parameterElement )
);
List<ExecutableElement> targetGetters = Filters.getterMethodsIn(
elementUtils.getAllMembers( returnTypeElement )
);
reportErrorIfMappedPropertiesDontExist( method, mappings, sourceGetters, targetSetters );
@ -456,12 +467,12 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
new MappedProperty(
sourcePropertyName,
getterMethod.getSimpleName().toString(),
Executables.getCorrespondingSetterMethod( parameterElement, getterMethod )
Executables.getCorrespondingPropertyAccessor( getterMethod, sourceSetters )
.getSimpleName()
.toString(),
retrieveReturnType( getterMethod ),
mapping != null ? mapping.getTargetName() : targetPropertyName,
Executables.getCorrespondingGetterMethod( returnTypeElement, setterMethod )
Executables.getCorrespondingPropertyAccessor( setterMethod, targetGetters )
.getSimpleName()
.toString(),
setterMethod.getSimpleName().toString(),

View File

@ -19,7 +19,7 @@
package org.mapstruct.ap.util;
import java.beans.Introspector;
import javax.lang.model.element.Element;
import java.util.List;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeKind;
@ -86,24 +86,24 @@ public class Executables {
throw new IllegalArgumentException( "Executable " + getterOrSetterMethod + " is not getter or setter method." );
}
public static ExecutableElement getCorrespondingSetterMethod(Element element, ExecutableElement getterMethod) {
String propertyName = getPropertyName( getterMethod );
/**
* Returns that setter or getter from the given list of executables which
* corresponds to the given getter or setter method.
*
* @param getterOrSetter The getter or setter method of interest.
* @param elements A list of executables to retrieve the corresponding accessor
* from.
*
* @return The setter corresponding to the given getter or the getter
* corresponding to the given getter
*/
public static ExecutableElement getCorrespondingPropertyAccessor(ExecutableElement getterOrSetter,
List<ExecutableElement> elements) {
String propertyName = getPropertyName( getterOrSetter );
for ( ExecutableElement setterMethod : Filters.setterMethodsIn( element.getEnclosedElements() ) ) {
if ( getPropertyName( setterMethod ).equals( propertyName ) ) {
return setterMethod;
}
}
return null;
}
public static ExecutableElement getCorrespondingGetterMethod(Element element, ExecutableElement setterMethod) {
String propertyName = getPropertyName( setterMethod );
for ( ExecutableElement getterMethod : Filters.getterMethodsIn( element.getEnclosedElements() ) ) {
if ( getPropertyName( getterMethod ).equals( propertyName ) ) {
return getterMethod;
for ( ExecutableElement method : elements ) {
if ( getPropertyName( method ).equals( propertyName ) ) {
return method;
}
}

View File

@ -0,0 +1,63 @@
/**
* 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.inheritance;
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;
/**
* Test for propagation of attributes inherited from super types.
*
* @author Gunnar Morling
*/
@WithClasses({ SourceBase.class, SourceExt.class, TargetBase.class, TargetExt.class, SourceTargetMapper.class })
public class InheritanceTest extends MapperTestBase {
@Test
@IssueKey("17")
public void shouldMapAttributeFromSuperType() {
SourceExt source = new SourceExt();
source.setFoo( 42 );
source.setBar( 23L );
TargetExt target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target ).isNotNull();
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 42 ) );
assertThat( target.getBar() ).isEqualTo( 23 );
}
@Test
@IssueKey("17")
public void shouldReverseMapAttributeFromSuperType() {
TargetExt target = new TargetExt();
target.setFoo( 42L );
target.setBar( 23 );
SourceExt source = SourceTargetMapper.INSTANCE.targetToSource( target );
assertThat( source ).isNotNull();
assertThat( source.getFoo() ).isEqualTo( 42 );
assertThat( source.getBar() ).isEqualTo( Long.valueOf( 23 ) );
}
}

View File

@ -0,0 +1,32 @@
/**
* 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.inheritance;
public class SourceBase {
private int foo;
public int getFoo() {
return foo;
}
public void setFoo(int foo) {
this.foo = foo;
}
}

View File

@ -0,0 +1,32 @@
/**
* 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.inheritance;
public class SourceExt extends SourceBase {
private Long bar;
public Long getBar() {
return bar;
}
public void setBar(Long bar) {
this.bar = bar;
}
}

View File

@ -0,0 +1,32 @@
/**
* 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.inheritance;
import org.mapstruct.Mapper;
import org.mapstruct.Mappers;
@Mapper
public interface SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
TargetExt sourceToTarget(SourceExt source);
SourceExt targetToSource(TargetExt target);
}

View File

@ -0,0 +1,32 @@
/**
* 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.inheritance;
public class TargetBase {
private Long foo;
public Long getFoo() {
return foo;
}
public void setFoo(Long foo) {
this.foo = foo;
}
}

View File

@ -0,0 +1,32 @@
/**
* 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.inheritance;
public class TargetExt extends TargetBase {
private int bar;
public int getBar() {
return bar;
}
public void setBar(int bar) {
this.bar = bar;
}
}