#843 Inefficient code generated when using NullValueCheckStrategy.ALWAYS icw nested source properties.

This commit is contained in:
sjaakd 2016-08-27 22:51:21 +02:00
parent a5a82cf5f1
commit e793853fc8
6 changed files with 230 additions and 0 deletions

View File

@ -348,6 +348,12 @@ public class PropertyMapping extends ModelElement {
return result;
}
// a nested source reference will take the bean mapping parameter itself, no null check is required
// since this is handled by the BeanMapping
if ( sourceReference.getPropertyEntries().size() > 1 ) {
return result;
}
// add a null / presence checked when required
if ( sourceType.isPrimitive() ) {
if ( getSourcePresenceCheckerRef() != null ) {

View File

@ -0,0 +1,46 @@
/**
* Copyright 2012-2016 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.bugs._843;
import java.util.Date;
/**
*
* @author Sjaak Derksen
*/
public class Commit {
private static int callCounter;
private Date authoredDate;
public Date getAuthoredDate() {
callCounter++;
return authoredDate;
}
public void setAuthoredDate(Date authoredDate) {
this.authoredDate = authoredDate;
}
public static int getCallCounter() {
return callCounter;
}
}

View File

@ -0,0 +1,44 @@
/**
* Copyright 2012-2016 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.bugs._843;
/**
*
* @author Sjaak Derksen
*/
public class GitlabTag {
private static int callCounter = 0;
private Commit commit;
public Commit getCommit() {
callCounter++;
return commit;
}
public void setCommit(Commit commit) {
this.commit = commit;
}
public static int getCallCounter() {
return callCounter;
}
}

View File

@ -0,0 +1,58 @@
/**
* Copyright 2012-2016 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.bugs._843;
import java.util.Date;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
/**
*
* @author Sjaak Derksen
*/
@RunWith(AnnotationProcessorTestRunner.class)
@WithClasses({
Commit.class,
TagInfo.class,
GitlabTag.class,
TagMapper.class
})
@IssueKey("843")
public class Issue843Test {
@Test
public void testMapperCreation() {
Commit commit = new Commit();
commit.setAuthoredDate( new Date() );
GitlabTag gitlabTag = new GitlabTag();
gitlabTag.setCommit( commit );
TagMapper.INSTANCE.gitlabTagToTagInfo( gitlabTag );
assertThat( Commit.getCallCounter() ).isEqualTo( 1 );
assertThat( GitlabTag.getCallCounter() ).isEqualTo( 1 );
}
}

View File

@ -0,0 +1,39 @@
/**
* Copyright 2012-2016 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.bugs._843;
import java.util.Date;
/**
*
* @author Sjaak Derksen
*/
public class TagInfo {
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}

View File

@ -0,0 +1,37 @@
/**
* Copyright 2012-2016 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.bugs._843;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueCheckStrategy;
import org.mapstruct.factory.Mappers;
/**
*
* @author Sjaak Derksen
*/
@Mapper( nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS )
public interface TagMapper {
TagMapper INSTANCE = Mappers.getMapper( TagMapper.class );
@Mapping(target = "date", source = "gitlabTag.commit.authoredDate")
TagInfo gitlabTagToTagInfo(GitlabTag gitlabTag);
}