Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Comment thread
zyberzebra marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public Builder forgedMethod(ForgedMethod forgedMethod) {
method( forgedMethod );
mappingReferences = forgedMethod.getMappingReferences();
Parameter sourceParameter = first( Parameter.getSourceParameters( forgedMethod.getParameters() ) );
for ( MappingReference mappingReference : mappingReferences.getMappingReferences() ) {
for ( MappingReference mappingReference: mappingReferences.getMappingReferences() ) {
SourceReference sourceReference = mappingReference.getSourceReference();
if ( sourceReference != null ) {
mappingReference.setSourceReference( new SourceReference.BuilderFromSourceReference()
Expand Down Expand Up @@ -2110,7 +2110,15 @@ private boolean doesNotNeedPresenceCheckForSourceParameter(PropertyMapping mappi
return false;
}

return mapping.getAssignment().isSourceReferenceParameter();
return mapping.getAssignment().isSourceReferenceParameter()
|| isCheckedByGuardClause( mapping );
}

private boolean isCheckedByGuardClause(PropertyMapping mapping) {
boolean isUpdateMethod = getReturnTypeToConstruct() == null && isMapNullToDefault();
return !isUpdateMethod
&& presenceChecksByParameter.size() == 1
&& presenceChecksByParameter.containsKey( mapping.getSourceBeanName() );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.nullcheck.redundant;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

@Mapper
public interface FooMapper {

FooMapper INSTANCE = Mappers.getMapper( FooMapper.class );

void updateFoo(FooSource input, @MappingTarget FooTarget toUpdate, boolean baz);

void updateFoo(FooSource input, @MappingTarget FooTarget toUpdate, boolean baz, int bay);

FooTarget getUpdatedFooTarget(FooSource input, @MappingTarget FooTarget toUpdate, boolean baz);

@Mapping(source = "input.nested.bar", target = "bar")
FooTarget map(FooSourceNested input, @MappingTarget FooTarget toUpdate, boolean baz);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.nullcheck.redundant;

public class FooSource {
private String bar;

public String getBar() {
return bar;
}

public void setBar(String bar) {
this.bar = bar;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.nullcheck.redundant;

public class FooSourceNested {
private String bar;
private FooSource nested;

public FooSource getNested() {
return nested;
}

public void setNested(FooSource nested) {
this.nested = nested;
}

public String getBar() {
return bar;
}

public void setBar(String bar) {
this.bar = bar;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.nullcheck.redundant;

public class FooTarget {
private String bar;

public String getBar() {
return bar;
}

public void setBar(String bar) {
this.bar = bar;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.nullcheck.redundant;

import org.junit.jupiter.api.extension.RegisterExtension;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.GeneratedSource;

@WithClasses({
FooMapper.class,
FooSource.class,
FooTarget.class,
FooSourceNested.class
})
@IssueKey("3133")
class RedundantNullCheckTest {

@RegisterExtension
final GeneratedSource generatedSource = new GeneratedSource();

@ProcessorTest
void generatedMapperShouldNotContainAnyRedundantNullChecks() {
generatedSource.addComparisonToFixtureFor( FooMapper.class );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.nullcheck.redundant;

import javax.annotation.processing.Generated;

@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2024-05-07T18:50:27+0200",
comments = "version: , compiler: javac, environment: Java 21.0.2 (Azul Systems, Inc.)"
)
public class FooMapperImpl implements FooMapper {

@Override
public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz) {
if ( input == null ) {
return;
}

toUpdate.setBar( input.getBar() );
}

@Override
public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz, int bay) {
if ( input == null ) {
return;
}

toUpdate.setBar( input.getBar() );
}

@Override
public FooTarget getUpdatedFooTarget(FooSource input, FooTarget toUpdate, boolean baz) {
if ( input == null ) {
return toUpdate;
}

toUpdate.setBar( input.getBar() );

return toUpdate;
}

@Override
public FooTarget map(FooSourceNested input, FooTarget toUpdate, boolean baz) {
if ( input == null ) {
return toUpdate;
}

toUpdate.setBar( inputNestedBar( input ) );

return toUpdate;
}

private String inputNestedBar(FooSourceNested fooSourceNested) {
FooSource nested = fooSourceNested.getNested();
if ( nested == null ) {
return null;
}
return nested.getBar();
}
}