From f31d234ba0d8a124b3cc5befe7c4a873f32f5b43 Mon Sep 17 00:00:00 2001 From: Jeff Smyth Date: Fri, 29 Dec 2017 16:18:20 -0500 Subject: [PATCH] #1353 Add a trim for source and target mappings, but log a warning message if trimmed. --- .../model/source/SourceReference.java | 13 +++- .../model/source/TargetReference.java | 14 +++- .../mapstruct/ap/internal/util/Message.java | 1 + .../ap/test/bugs/_1353/Issue1353Mapper.java | 38 ++++++++++ .../ap/test/bugs/_1353/Issue1353Test.java | 71 +++++++++++++++++++ .../mapstruct/ap/test/bugs/_1353/Source.java | 35 +++++++++ .../mapstruct/ap/test/bugs/_1353/Target.java | 35 +++++++++ 7 files changed, 204 insertions(+), 3 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Issue1353Mapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Issue1353Test.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Source.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Target.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/SourceReference.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/SourceReference.java index c8374db8a..60353dc94 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/SourceReference.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/SourceReference.java @@ -108,8 +108,19 @@ public class SourceReference { boolean isValid = true; boolean foundEntryMatch; + String sourceNameTrimmed = sourceName.trim(); + if ( !sourceName.equals( sourceNameTrimmed ) ) { + messager.printMessage( + method.getExecutable(), + mapping.getMirror(), + mapping.getSourceAnnotationValue(), + Message.PROPERTYMAPPING_WHITESPACE_TRIMMED, + sourceName, + sourceNameTrimmed + ); + } String[] sourcePropertyNames = new String[0]; - String[] segments = sourceName.split( "\\." ); + String[] segments = sourceNameTrimmed.split( "\\." ); Parameter parameter = null; List entries = new ArrayList(); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/TargetReference.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/TargetReference.java index bd51a0948..b298194f3 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/TargetReference.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/TargetReference.java @@ -138,8 +138,18 @@ public class TargetReference { return null; } - - String[] segments = targetName.split( "\\." ); + String targetNameTrimmed = targetName.trim(); + if ( !targetName.equals( targetNameTrimmed ) ) { + messager.printMessage( + method.getExecutable(), + mapping.getMirror(), + mapping.getTargetAnnotationValue(), + Message.PROPERTYMAPPING_WHITESPACE_TRIMMED, + targetName, + targetNameTrimmed + ); + } + String[] segments = targetNameTrimmed.split( "\\." ); Parameter parameter = method.getMappingTargetParameter(); boolean foundEntryMatch; diff --git a/processor/src/main/java/org/mapstruct/ap/internal/util/Message.java b/processor/src/main/java/org/mapstruct/ap/internal/util/Message.java index 7a664cf20..ce86aa06c 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/util/Message.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/util/Message.java @@ -61,6 +61,7 @@ public enum Message { PROPERTYMAPPING_NO_PRESENCE_CHECKER_FOR_SOURCE_TYPE( "Using custom source value presence checking strategy, but no presence checker found for %s in source type." ), PROPERTYMAPPING_NO_READ_ACCESSOR_FOR_TARGET_TYPE( "No read accessor found for property \"%s\" in target type." ), PROPERTYMAPPING_NO_WRITE_ACCESSOR_FOR_TARGET_TYPE( "No write accessor found for property \"%s\" in target type." ), + PROPERTYMAPPING_WHITESPACE_TRIMMED( "The property named \"%s\" has whitespaces, using trimmed property \"%s\" instead.", Diagnostic.Kind.WARNING ), CONSTANTMAPPING_MAPPING_NOT_FOUND( "Can't map \"%s %s\" to \"%s %s\"." ), CONSTANTMAPPING_NO_READ_ACCESSOR_FOR_TARGET_TYPE( "No read accessor found for property \"%s\" in target type." ), diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Issue1353Mapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Issue1353Mapper.java new file mode 100644 index 000000000..f94551460 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Issue1353Mapper.java @@ -0,0 +1,38 @@ +/** + * Copyright 2012-2017 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._1353; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; +import org.mapstruct.factory.Mappers; + +/** + * @author Jeffrey Smyth + */ +@Mapper +public interface Issue1353Mapper { + + Issue1353Mapper INSTANCE = Mappers.getMapper( Issue1353Mapper.class ); + + @Mappings ({ + @Mapping (target = "string2 ", source = " source.string1") + }) + Target sourceToTarget(Source source); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Issue1353Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Issue1353Test.java new file mode 100644 index 000000000..ad074548e --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Issue1353Test.java @@ -0,0 +1,71 @@ +/** + * Copyright 2012-2017 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._1353; + +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.compilation.annotation.CompilationResult; +import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic; +import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome; +import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Jeffrey Smyth + */ +@IssueKey ("1353") +@RunWith (AnnotationProcessorTestRunner.class) +@WithClasses ({ + Issue1353Mapper.class, + Source.class, + Target.class +}) +public class Issue1353Test { + + @Test + @ExpectedCompilationOutcome ( + value = CompilationResult.SUCCEEDED, + diagnostics = { + @Diagnostic (type = Issue1353Mapper.class, + kind = javax.tools.Diagnostic.Kind.WARNING, + line = 35, + messageRegExp = "The property named \" source.string1\" has whitespaces," + + " using trimmed property \"source.string1\" instead." + ), + @Diagnostic (type = Issue1353Mapper.class, + kind = javax.tools.Diagnostic.Kind.WARNING, + line = 35, + messageRegExp = "The property named \"string2 \" has whitespaces," + + " using trimmed property \"string2\" instead." + ) + } + ) + public void shouldTrimArguments() { + Source source = new Source(); + source.setString1( "TestString" ); + + Target target = Issue1353Mapper.INSTANCE.sourceToTarget( source ); + + assertThat( target.getString2() ).isNotNull(); + assertThat( target.getString2() ).isEqualTo( source.getString1() ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Source.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Source.java new file mode 100644 index 000000000..d216236bf --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Source.java @@ -0,0 +1,35 @@ +/** + * Copyright 2012-2017 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._1353; + +/** + * @author Jeffrey Smyth + */ +public class Source { + + private String string1; + + public String getString1() { + return string1; + } + + public void setString1(String string1) { + this.string1 = string1; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Target.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Target.java new file mode 100644 index 000000000..3197f092d --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1353/Target.java @@ -0,0 +1,35 @@ +/** + * Copyright 2012-2017 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._1353; + +/** + * @author Jeffrey Smyth + */ +public class Target { + + private String string2; + + public String getString2() { + return string2; + } + + public void setString2(String string2) { + this.string2 = string2; + } +}