#237 NPE check for type converted sources followed by method mapping

This commit is contained in:
sjaakd 2014-06-24 19:51:46 +02:00 committed by Gunnar Morling
parent c550af2529
commit 02c81ae651
7 changed files with 112 additions and 3 deletions

View File

@ -76,6 +76,7 @@ import org.mapstruct.ap.util.Strings;
import static org.mapstruct.ap.model.Assignment.AssignmentType.DIRECT;
import static org.mapstruct.ap.model.Assignment.AssignmentType.TYPE_CONVERTED;
import static org.mapstruct.ap.model.Assignment.AssignmentType.TYPE_CONVERTED_MAPPED;
/**
* A {@link ModelElementProcessor} which creates a {@link Mapper} from the given
@ -800,6 +801,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
if ( !sourceType.isPrimitive() &&
( assignment.getType() == TYPE_CONVERTED ||
assignment.getType() == TYPE_CONVERTED_MAPPED ||
assignment.getType() == DIRECT && targetType.isPrimitive() ) ) {
// for primitive types null check is not possible at all, but a conversion needs
// a null check.

View File

@ -0,0 +1,34 @@
/**
* 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.npe;
import java.math.BigInteger;
/**
*
* @author Sjaak Derksen
*/
public class MyBigIntMapper {
public MyBigIntWrapper toMyBigIntWrapper(BigInteger bigInteger) {
MyBigIntWrapper wrapper = new MyBigIntWrapper();
wrapper.setMyBigInt( bigInteger );
return wrapper;
}
}

View File

@ -0,0 +1,38 @@
/**
* 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.npe;
import java.math.BigInteger;
/**
*
* @author Sjaak Derksen
*/
public class MyBigIntWrapper {
private BigInteger myBigInt;
public BigInteger getMyBigInt() {
return myBigInt;
}
public void setMyBigInt( BigInteger myBigInt ) {
this.myBigInt = myBigInt;
}
}

View File

@ -30,44 +30,63 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
*
* @author Sjaak Derksen
*/
@IssueKey( "134" )
@WithClasses( {
SourceTargetMapper.class,
NullObjectMapper.class,
NullObject.class,
MyBigIntMapper.class,
MyBigIntWrapper.class,
Source.class,
Target.class
} )
@RunWith( AnnotationProcessorTestRunner.class )
public class NullPtrCheckTest {
@IssueKey( "214" )
@Test( expected = NullPointerException.class )
public void shouldThrowNullptrWhenCustomMapperIsInvoked() {
Source source = new Source();
source.setNumber( "5" );
source.setSomeInteger( 7 );
SourceTargetMapper.INSTANCE.sourceToTarget( source );
}
@IssueKey( "214" )
@Test
public void shouldSurroundTypeConversionWithNPECheck() {
Source source = new Source();
source.setSomeObject( new NullObject() );
source.setSomeInteger( 7 );
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target.getNumber() ).isNull();
}
@Test
@IssueKey( "214" )
@Test
public void shouldSurroundArrayListConstructionWithNPECheck() {
Source source = new Source();
source.setSomeObject( new NullObject() );
source.setSomeInteger( 7 );
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target.getSomeList() ).isNull();
}
@IssueKey( "237" )
@Test
public void shouldMapMappedTypeConversion() {
Source source = new Source();
source.setSomeObject( new NullObject() );
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target.getSomeList() ).isNull();
assertThat( target.getSomeInteger() ).isNull();
}
}

View File

@ -29,6 +29,7 @@ public class Source {
private NullObject someObject;
private String number;
private List<String> someList;
private Integer someInteger;
public NullObject getSomeObject() {
return someObject;
@ -54,6 +55,12 @@ public class Source {
this.someList = someList;
}
public Integer getSomeInteger() {
return someInteger;
}
public void setSomeInteger( Integer someInteger ) {
this.someInteger = someInteger;
}
}

View File

@ -25,7 +25,7 @@ import org.mapstruct.factory.Mappers;
*
* @author Sjaak Derksen
*/
@Mapper (uses = NullObjectMapper.class)
@Mapper (uses = { NullObjectMapper.class, MyBigIntMapper.class } )
public interface SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );

View File

@ -29,6 +29,7 @@ public class Target {
private String someObject;
private Integer number;
private List<String> someList;
private MyBigIntWrapper someInteger;
public String getSomeObject() {
return someObject;
@ -54,6 +55,14 @@ public class Target {
this.someList = someList;
}
public MyBigIntWrapper getSomeInteger() {
return someInteger;
}
public void setSomeInteger( MyBigIntWrapper someInteger ) {
this.someInteger = someInteger;
}
}