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.DIRECT;
|
||||||
import static org.mapstruct.ap.model.Assignment.AssignmentType.TYPE_CONVERTED;
|
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
|
* 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() );
|
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
||||||
if ( !sourceType.isPrimitive() &&
|
if ( !sourceType.isPrimitive() &&
|
||||||
( assignment.getType() == TYPE_CONVERTED ||
|
( assignment.getType() == TYPE_CONVERTED ||
|
||||||
|
assignment.getType() == TYPE_CONVERTED_MAPPED ||
|
||||||
assignment.getType() == DIRECT && targetType.isPrimitive() ) ) {
|
assignment.getType() == DIRECT && targetType.isPrimitive() ) ) {
|
||||||
// for primitive types null check is not possible at all, but a conversion needs
|
// for primitive types null check is not possible at all, but a conversion needs
|
||||||
// a null check.
|
// 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
|
* @author Sjaak Derksen
|
||||||
*/
|
*/
|
||||||
@IssueKey( "134" )
|
|
||||||
@WithClasses( {
|
@WithClasses( {
|
||||||
SourceTargetMapper.class,
|
SourceTargetMapper.class,
|
||||||
NullObjectMapper.class,
|
NullObjectMapper.class,
|
||||||
NullObject.class,
|
NullObject.class,
|
||||||
|
MyBigIntMapper.class,
|
||||||
|
MyBigIntWrapper.class,
|
||||||
Source.class,
|
Source.class,
|
||||||
Target.class
|
Target.class
|
||||||
} )
|
} )
|
||||||
@RunWith( AnnotationProcessorTestRunner.class )
|
@RunWith( AnnotationProcessorTestRunner.class )
|
||||||
public class NullPtrCheckTest {
|
public class NullPtrCheckTest {
|
||||||
|
|
||||||
|
@IssueKey( "214" )
|
||||||
@Test( expected = NullPointerException.class )
|
@Test( expected = NullPointerException.class )
|
||||||
public void shouldThrowNullptrWhenCustomMapperIsInvoked() {
|
public void shouldThrowNullptrWhenCustomMapperIsInvoked() {
|
||||||
|
|
||||||
Source source = new Source();
|
Source source = new Source();
|
||||||
source.setNumber( "5" );
|
source.setNumber( "5" );
|
||||||
|
source.setSomeInteger( 7 );
|
||||||
SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@IssueKey( "214" )
|
||||||
@Test
|
@Test
|
||||||
public void shouldSurroundTypeConversionWithNPECheck() {
|
public void shouldSurroundTypeConversionWithNPECheck() {
|
||||||
|
|
||||||
Source source = new Source();
|
Source source = new Source();
|
||||||
source.setSomeObject( new NullObject() );
|
source.setSomeObject( new NullObject() );
|
||||||
|
source.setSomeInteger( 7 );
|
||||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||||
|
|
||||||
assertThat( target.getNumber() ).isNull();
|
assertThat( target.getNumber() ).isNull();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@IssueKey( "214" )
|
||||||
@Test
|
@Test
|
||||||
public void shouldSurroundArrayListConstructionWithNPECheck() {
|
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 source = new Source();
|
||||||
source.setSomeObject( new NullObject() );
|
source.setSomeObject( new NullObject() );
|
||||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||||
|
|
||||||
assertThat( target.getSomeList() ).isNull();
|
assertThat( target.getSomeList() ).isNull();
|
||||||
|
assertThat( target.getSomeInteger() ).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@ public class Source {
|
|||||||
private NullObject someObject;
|
private NullObject someObject;
|
||||||
private String number;
|
private String number;
|
||||||
private List<String> someList;
|
private List<String> someList;
|
||||||
|
private Integer someInteger;
|
||||||
|
|
||||||
public NullObject getSomeObject() {
|
public NullObject getSomeObject() {
|
||||||
return someObject;
|
return someObject;
|
||||||
@ -54,6 +55,12 @@ public class Source {
|
|||||||
this.someList = someList;
|
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
|
* @author Sjaak Derksen
|
||||||
*/
|
*/
|
||||||
@Mapper (uses = NullObjectMapper.class)
|
@Mapper (uses = { NullObjectMapper.class, MyBigIntMapper.class } )
|
||||||
public interface SourceTargetMapper {
|
public interface SourceTargetMapper {
|
||||||
|
|
||||||
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
||||||
|
@ -29,6 +29,7 @@ public class Target {
|
|||||||
private String someObject;
|
private String someObject;
|
||||||
private Integer number;
|
private Integer number;
|
||||||
private List<String> someList;
|
private List<String> someList;
|
||||||
|
private MyBigIntWrapper someInteger;
|
||||||
|
|
||||||
public String getSomeObject() {
|
public String getSomeObject() {
|
||||||
return someObject;
|
return someObject;
|
||||||
@ -54,6 +55,14 @@ public class Target {
|
|||||||
this.someList = someList;
|
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