From bc22e6ce1567e27fa338fa0c329b61065507c291 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Sun, 16 Jun 2013 20:28:57 +0200 Subject: [PATCH] #17 Fixing NPE in case of attribute without setter in source type --- .../mapstruct/ap/MapperGenerationVisitor.java | 16 ++++--- .../mapstruct/ap/test/oneway/OnewayTest.java | 47 +++++++++++++++++++ .../org/mapstruct/ap/test/oneway/Source.java | 28 +++++++++++ .../ap/test/oneway/SourceTargetMapper.java | 30 ++++++++++++ .../org/mapstruct/ap/test/oneway/Target.java | 32 +++++++++++++ 5 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/oneway/OnewayTest.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/oneway/Source.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/oneway/SourceTargetMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/oneway/Target.java diff --git a/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java b/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java index b95874990..2f38a5e6d 100644 --- a/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java +++ b/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java @@ -463,18 +463,22 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { String targetPropertyName = Executables.getPropertyName( setterMethod ); if ( targetPropertyName.equals( mapping != null ? mapping.getTargetName() : sourcePropertyName ) ) { + ExecutableElement correspondingSetter = Executables.getCorrespondingPropertyAccessor( + getterMethod, + sourceSetters + ); + ExecutableElement correspondingGetter = Executables.getCorrespondingPropertyAccessor( + setterMethod, + targetGetters + ); properties.add( new MappedProperty( sourcePropertyName, getterMethod.getSimpleName().toString(), - Executables.getCorrespondingPropertyAccessor( getterMethod, sourceSetters ) - .getSimpleName() - .toString(), + correspondingSetter != null ? correspondingSetter.getSimpleName().toString() : null, retrieveReturnType( getterMethod ), mapping != null ? mapping.getTargetName() : targetPropertyName, - Executables.getCorrespondingPropertyAccessor( setterMethod, targetGetters ) - .getSimpleName() - .toString(), + correspondingGetter != null ? correspondingGetter.getSimpleName().toString() : null, setterMethod.getSimpleName().toString(), retrieveParameter( setterMethod ).getType() ) diff --git a/processor/src/test/java/org/mapstruct/ap/test/oneway/OnewayTest.java b/processor/src/test/java/org/mapstruct/ap/test/oneway/OnewayTest.java new file mode 100644 index 000000000..1c1490e71 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/oneway/OnewayTest.java @@ -0,0 +1,47 @@ +/** + * 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.oneway; + +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 attribute without setter in source and getter in + * target. + * + * @author Gunnar Morling + */ +@WithClasses({ Source.class, Target.class, SourceTargetMapper.class }) +public class OnewayTest extends MapperTestBase { + + @Test + @IssueKey("17") + public void shouldMapAttributeWithoutSetterInSourceType() { + Source source = new Source(); + + Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source ); + + assertThat( target ).isNotNull(); + assertThat( target.retrieveFoo() ).isEqualTo( Long.valueOf( 42 ) ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/oneway/Source.java b/processor/src/test/java/org/mapstruct/ap/test/oneway/Source.java new file mode 100644 index 000000000..357da1ecf --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/oneway/Source.java @@ -0,0 +1,28 @@ +/** + * 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.oneway; + +public class Source { + + private int foo = 42; + + public int getFoo() { + return foo; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/oneway/SourceTargetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/oneway/SourceTargetMapper.java new file mode 100644 index 000000000..0a0120899 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/oneway/SourceTargetMapper.java @@ -0,0 +1,30 @@ +/** + * 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.oneway; + +import org.mapstruct.Mapper; +import org.mapstruct.Mappers; + +@Mapper +public interface SourceTargetMapper { + + SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class ); + + Target sourceToTarget(Source source); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/oneway/Target.java b/processor/src/test/java/org/mapstruct/ap/test/oneway/Target.java new file mode 100644 index 000000000..f01c0d6c1 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/oneway/Target.java @@ -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.oneway; + +public class Target { + + private Long foo; + + public void setFoo(Long foo) { + this.foo = foo; + } + + public Long retrieveFoo() { + return foo; + } +}