mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#237 NPE check for type converted sources followed by method mapping
This commit is contained in:
parent
c550af2529
commit
02c81ae651
@ -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.
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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 );
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user