diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/Mapping.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/Mapping.java index 7c4cf47fa..c9d0d6ce5 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/Mapping.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/Mapping.java @@ -313,17 +313,6 @@ public class Mapping { } } - // should generate error when parameter - if ( sourceReference != null && sourceReference.getPropertyEntries().isEmpty() ) { - // parameter mapping only, apparently the @InheritReverseConfiguration is intentional - // but erroneous. Lets raise an error to warn. - messager.printMessage( - method.getExecutable(), Message.PROPERTYMAPPING_REVERSAL_PROBLEM, - sourceReference.getParameter() - ); - return null; - } - Mapping reverse = new Mapping( sourceName != null ? targetName : null, null, // constant 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 8752ece2c..7809dcf00 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 @@ -48,7 +48,6 @@ public enum Message { PROPERTYMAPPING_EXPRESSION_AND_DEFAULT_VALUE_BOTH_DEFINED( "Expression and default value are both defined in @Mapping, either define a defaultValue or an expression." ), PROPERTYMAPPING_CONSTANT_AND_DEFAULT_VALUE_BOTH_DEFINED( "Constant and default value are both defined in @Mapping, either define a defaultValue or a constant." ), PROPERTYMAPPING_INVALID_EXPRESSION( "Value must be given in the form \"java()\"." ), - PROPERTYMAPPING_REVERSAL_PROBLEM( "Parameter %s cannot be reversed.", Diagnostic.Kind.NOTE ), PROPERTYMAPPING_INVALID_PARAMETER_NAME( "Method has no parameter named \"%s\"." ), PROPERTYMAPPING_NO_PROPERTY_IN_PARAMETER( "The type of parameter \"%s\" has no property named \"%s\"." ), PROPERTYMAPPING_INVALID_PROPERTY_NAME( "No property named \"%s\" exists in source parameter(s)." ), diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/FontDto.java b/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/FontDto.java new file mode 100644 index 000000000..4116240dd --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/FontDto.java @@ -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.nestedsource.parameter; + +/** + * + * @author Sjaak Derksen + */ +public class FontDto { + + private String type; + private int size; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/LetterDto.java b/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/LetterDto.java new file mode 100644 index 000000000..7bd32d2bc --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/LetterDto.java @@ -0,0 +1,63 @@ +/** + * 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.nestedsource.parameter; + +/** + * + * @author Sjaak Derksen + */ +public class LetterDto { + + private FontDto font; + private String heading; + private String body; + private String signature; + + public FontDto getFont() { + return font; + } + + public void setFont(FontDto font) { + this.font = font; + } + + public String getHeading() { + return heading; + } + + public void setHeading(String heading) { + this.heading = heading; + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + public String getSignature() { + return signature; + } + + public void setSignature(String signature) { + this.signature = signature; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/LetterEntity.java b/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/LetterEntity.java new file mode 100644 index 000000000..8c68a1729 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/LetterEntity.java @@ -0,0 +1,74 @@ +/** + * 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.nestedsource.parameter; + +/** + * + * @author Sjaak Derksen + */ +public class LetterEntity { + + private String fontType; + private int fontSize; + private String letterHeading; + private String letterBody; + private String letterSignature; + + public String getFontType() { + return fontType; + } + + public void setFontType(String fontType) { + this.fontType = fontType; + } + + public int getFontSize() { + return fontSize; + } + + public void setFontSize(int fontSize) { + this.fontSize = fontSize; + } + + public String getLetterHeading() { + return letterHeading; + } + + public void setLetterHeading(String letterHeading) { + this.letterHeading = letterHeading; + } + + public String getLetterBody() { + return letterBody; + } + + public void setLetterBody(String letterBody) { + this.letterBody = letterBody; + } + + public String getLetterSignature() { + return letterSignature; + } + + public void setLetterSignature(String letterSignature) { + this.letterSignature = letterSignature; + } + + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/LetterMapper.java b/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/LetterMapper.java new file mode 100644 index 000000000..aaaf1d43c --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/LetterMapper.java @@ -0,0 +1,55 @@ +/** + * 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.nestedsource.parameter; + +import org.mapstruct.InheritInverseConfiguration; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; +import org.mapstruct.factory.Mappers; + +/** + * + * @author Sjaak Derksen + */ +@Mapper +public interface LetterMapper { + + LetterMapper INSTANCE = Mappers.getMapper( LetterMapper.class ); + + @Mappings( { + @Mapping( target = "fontType", source = "font.type"), + @Mapping( target = "fontSize", source = "font.size"), + @Mapping( target = "letterHeading", source = "heading"), + @Mapping( target = "letterBody", source = "body"), + @Mapping( target = "letterSignature", source = "dto.signature") + } ) + LetterEntity normalize(LetterDto dto); + + @InheritInverseConfiguration + @Mapping(target = "font", source = "entity") + LetterDto deNormalizeLetter(LetterEntity entity); + + @Mappings( { + @Mapping( target = "type", source = "fontType"), + @Mapping( target = "size", source = "fontSize") + } ) + FontDto deNormalizeFont(LetterEntity entity); + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/NormalizingTest.java b/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/NormalizingTest.java new file mode 100644 index 000000000..b2fa2586a --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedsource/parameter/NormalizingTest.java @@ -0,0 +1,62 @@ +/** + * 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.nestedsource.parameter; + +import static org.fest.assertions.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 + */ +@WithClasses({ FontDto.class, LetterDto.class, LetterEntity.class, LetterMapper.class }) +@IssueKey("836") +@RunWith(AnnotationProcessorTestRunner.class) +public class NormalizingTest { + + @Test + public void shouldGenerateImplementationForPropertyNamesOnly() { + + FontDto fontIn = new FontDto(); + fontIn.setSize( 10 ); + fontIn.setType( "Sans Serif" ); + LetterDto letterIn = new LetterDto(); + letterIn.setFont( fontIn ); + letterIn.setHeading( "dear sir," ); + letterIn.setBody( "Concerning your writing... " ); + letterIn.setSignature( "B. Obama" ); + + LetterEntity letterEntity = LetterMapper.INSTANCE.normalize( letterIn ); + + LetterDto letterOut = LetterMapper.INSTANCE.deNormalizeLetter( letterEntity ); + + assertThat( letterOut ).isNotNull(); + assertThat( letterOut.getHeading() ).isEqualTo( "dear sir," ); + assertThat( letterOut.getBody() ).isEqualTo( "Concerning your writing... " ); + assertThat( letterOut.getSignature() ).isEqualTo( "B. Obama" ); + assertThat( letterOut.getFont() ).isNotNull(); + assertThat( letterOut.getFont().getSize() ).isEqualTo( 10 ); + assertThat( letterOut.getFont().getType() ).isEqualTo( "Sans Serif" ); + } + +}