#17 Fixing NPE in case of attribute without setter in source type

This commit is contained in:
Gunnar Morling 2013-06-16 20:28:57 +02:00
parent 9318adf7b7
commit bc22e6ce15
5 changed files with 147 additions and 6 deletions

View File

@ -463,18 +463,22 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
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()
)

View File

@ -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 ) );
}
}

View File

@ -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;
}
}

View File

@ -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);
}

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.oneway;
public class Target {
private Long foo;
public void setFoo(Long foo) {
this.foo = foo;
}
public Long retrieveFoo() {
return foo;
}
}