diff --git a/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java index df0e473da..4d2eb1e07 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java @@ -36,15 +36,14 @@ import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.source.ForgedMethod; import org.mapstruct.ap.model.source.SourceMethod; +import org.mapstruct.ap.model.source.SourceReference; +import org.mapstruct.ap.model.source.SourceReference.PropertyEntry; import org.mapstruct.ap.util.Executables; +import org.mapstruct.ap.util.Strings; import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.DIRECT; import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED; import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED_MAPPED; -import org.mapstruct.ap.model.source.SourceReference; -import org.mapstruct.ap.model.source.SourceReference.PropertyEntry; -import org.mapstruct.ap.util.Strings; - /** * Represents the mapping between a source and target property, e.g. from @@ -57,10 +56,8 @@ import org.mapstruct.ap.util.Strings; public class PropertyMapping extends ModelElement { private final String sourceBeanName; - private final String targetAccessorName; private final Type targetType; - private final Assignment assignment; public static class PropertyMappingBuilder { @@ -99,12 +96,12 @@ public class PropertyMapping extends ModelElement { return this; } - public PropertyMappingBuilder qualifiers( List qualifiers ) { + public PropertyMappingBuilder qualifiers(List qualifiers) { this.qualifiers = qualifiers; return this; } - public PropertyMappingBuilder dateFormat( String dateFormat ) { + public PropertyMappingBuilder dateFormat(String dateFormat) { this.dateFormat = dateFormat; return this; } @@ -141,7 +138,8 @@ public class PropertyMapping extends ModelElement { } private PropertyMapping getPropertyMapping(Type sourceType, Type targetType, - TargetAccessorType targetAccessorType, String sourceRefStr, String sourceElement) { + TargetAccessorType targetAccessorType, String sourceRefStr, + String sourceElement) { Assignment assignment = ctx.getMappingResolver().getTargetAssignment( method, @@ -234,10 +232,10 @@ public class PropertyMapping extends ModelElement { ctx.getMessager().printMessage( Diagnostic.Kind.ERROR, String.format( - "Can't map %s to \"%s %s\".", - sourceElement, - targetType, - targetPropertyName + "Can't map %s to \"%s %s\".", + sourceElement, + targetType, + targetPropertyName ), method.getExecutable() ); @@ -267,34 +265,36 @@ public class PropertyMapping extends ModelElement { } } - private String getSourceRef() { - Parameter sourceParam = sourceReference.getParameter(); List propertyEntries = sourceReference.getPropertyEntries(); + + // parameter reference if ( propertyEntries.isEmpty() ) { return sourceParam.getName(); } + // simple property else if ( propertyEntries.size() == 1 ) { PropertyEntry propertyEntry = propertyEntries.get( 0 ); return sourceParam.getName() + "." + propertyEntry.getAccessor().getSimpleName() + "()"; } + // nested property given as dot path else { PropertyEntry lastPropertyEntry = propertyEntries.get( propertyEntries.size() - 1 ); // forge a method from the parameter type to the last entry type. String forgedMethodName = Strings.joinAndCamelize( sourceReference.getElementNames() ); ForgedMethod methodToGenerate = new ForgedMethod( - forgedMethodName, - sourceReference.getParameter().getType(), - lastPropertyEntry.getType(), - method.getExecutable() + forgedMethodName, + sourceReference.getParameter().getType(), + lastPropertyEntry.getType(), + method.getExecutable() ); NestedPropertyMappingMethod.Builder builder = new NestedPropertyMappingMethod.Builder(); NestedPropertyMappingMethod nestedPropertyMapping = builder - .method( methodToGenerate ) - .propertyEntries( sourceReference.getPropertyEntries() ) - .build(); + .method( methodToGenerate ) + .propertyEntries( sourceReference.getPropertyEntries() ) + .build(); // add if not yet existing if ( !ctx.getMappingsToGenerate().contains( nestedPropertyMapping ) ) { @@ -318,9 +318,10 @@ public class PropertyMapping extends ModelElement { } else { PropertyEntry lastPropertyEntry = propertyEntries.get( propertyEntries.size() - 1 ); - return String.format( "property \"%s %s\"", - lastPropertyEntry.getType(), - Strings.join( sourceReference.getElementNames(), "." ) + return String.format( + "property \"%s %s\"", + lastPropertyEntry.getType(), + Strings.join( sourceReference.getElementNames(), "." ) ); } } @@ -337,7 +338,7 @@ public class PropertyMapping extends ModelElement { } } - private Type getTargetType( TargetAccessorType targetAccessorType) { + private Type getTargetType(TargetAccessorType targetAccessorType) { switch ( targetAccessorType ) { case ADDER: case SETTER: @@ -349,7 +350,7 @@ public class PropertyMapping extends ModelElement { } private Assignment forgeMapOrIterableMapping(Type sourceType, Type targetType, String sourceReference, - ExecutableElement element) { + ExecutableElement element) { Assignment assignment = null; if ( sourceType.isCollectionType() && targetType.isCollectionType() ) { diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/ForgedMethod.java b/processor/src/main/java/org/mapstruct/ap/model/source/ForgedMethod.java index 24f067908..12b1ccdb2 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/ForgedMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/ForgedMethod.java @@ -21,7 +21,6 @@ package org.mapstruct.ap.model.source; import java.util.Arrays; import java.util.Collections; import java.util.List; - import javax.lang.model.element.ExecutableElement; import org.mapstruct.ap.model.common.Accessibility; @@ -42,15 +41,15 @@ public class ForgedMethod implements Method { private final ExecutableElement positionHintElement; /** - * Creates a new Forged Method. - * + * Creates a new forged method. + *

* The name will be based on the source type name and target type name. * * @param sourceType the source type * @param targetType the target type. * @param positionHintElement element used to for reference to the position in the source file. */ - public ForgedMethod( Type sourceType, Type targetType, ExecutableElement positionHintElement ) { + public ForgedMethod(Type sourceType, Type targetType, ExecutableElement positionHintElement) { this.parameters = Arrays.asList( new Parameter( Strings.decapitalize( sourceType.getName() ), sourceType ) ); this.returnType = targetType; @@ -62,21 +61,21 @@ public class ForgedMethod implements Method { } /** - * Creates a new Forged Method. with the given name. + * Creates a new forged method with the given name. * * @param name the (unique name) for this method * @param sourceType the source type * @param targetType the target type. * @param positionHintElement element used to for reference to the position in the source file. */ - public ForgedMethod( String name, Type sourceType, Type targetType, ExecutableElement positionHintElement ) { + public ForgedMethod(String name, Type sourceType, Type targetType, ExecutableElement positionHintElement) { this.parameters = Arrays.asList( new Parameter( Strings.decapitalize( sourceType.getName() ), sourceType ) ); this.returnType = targetType; this.name = name; this.positionHintElement = positionHintElement; } - private String getName( Type type ) { + private String getName(Type type) { StringBuilder builder = new StringBuilder(); for ( Type typeParam : type.getTypeParameters() ) { builder.append( typeParam.getName() ); @@ -86,7 +85,7 @@ public class ForgedMethod implements Method { } @Override - public boolean matches( List sourceTypes, Type targetType ) { + public boolean matches(List sourceTypes, Type targetType) { if ( !targetType.equals( returnType ) ) { return false; diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java b/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java index 96325e175..66737975b 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java @@ -34,8 +34,8 @@ import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.tools.Diagnostic; import javax.tools.Diagnostic.Kind; -import org.mapstruct.ap.model.common.TypeFactory; +import org.mapstruct.ap.model.common.TypeFactory; import org.mapstruct.ap.prism.MappingPrism; import org.mapstruct.ap.prism.MappingsPrism; @@ -177,10 +177,10 @@ public class Mapping { } //CHECKSTYLE:OFF - private Mapping(String sourceName, String constant, String javaExpression, String targetName, + private Mapping(String sourceName, String constant, String javaExpression, String targetName, String dateFormat, List qualifiers, boolean isIgnored, boolean isInheritedFromInverseMethod, AnnotationMirror mirror, - AnnotationValue sourceAnnotationValue, AnnotationValue targetAnnotationValue) { + AnnotationValue sourceAnnotationValue, AnnotationValue targetAnnotationValue) { this.sourceName = sourceName; this.constant = constant; this.javaExpression = javaExpression; @@ -195,15 +195,15 @@ public class Mapping { } //CHECKSTYLE:ON - public void init( SourceMethod method, Messager messager, TypeFactory typeFactory ) { + public void init(SourceMethod method, Messager messager, TypeFactory typeFactory) { if ( !method.isEnumMapping() ) { sourceReference = new SourceReference.BuilderFromMapping() - .mapping( this ) - .method( method ) - .messager( messager ) - .typeFactory( typeFactory ) - .build(); + .mapping( this ) + .method( method ) + .messager( messager ) + .typeFactory( typeFactory ) + .build(); } } @@ -272,24 +272,23 @@ public class Mapping { } Mapping reverse = new Mapping( - sourceName != null ? targetName : null, - null, // constant - null, // expression - sourceName != null ? sourceName : targetName, - dateFormat, - qualifiers, - isIgnored, - true, - mirror, - sourceAnnotationValue, - targetAnnotationValue - ); + sourceName != null ? targetName : null, + null, // constant + null, // expression + sourceName != null ? sourceName : targetName, + dateFormat, + qualifiers, + isIgnored, + true, + mirror, + sourceAnnotationValue, + targetAnnotationValue + ); reverse.init( method, messager, typeFactory ); return reverse; } - @Override public String toString() { return "Mapping {" + diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/SourceReference.java b/processor/src/main/java/org/mapstruct/ap/model/source/SourceReference.java index 965d483e4..0a183ac7b 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/SourceReference.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/SourceReference.java @@ -24,6 +24,7 @@ import java.util.List; import javax.annotation.processing.Messager; import javax.lang.model.element.ExecutableElement; import javax.tools.Diagnostic; + import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.TypeFactory; @@ -32,12 +33,15 @@ import org.mapstruct.ap.util.Strings; /** * This class describes the source side of a property mapping. - * - * It contains the source parameter, and all individual (nested) PropertyEntries. So consider the following + *

+ * It contains the source parameter, and all individual (nested) property entries. So consider the following * mapping method: + * * {@code - * @Mapping( source = "in.propA.propB" target = "propC" ) - * TypeB mappingMethod ( TypeA in ); + * + * @author Sjaak Derksen + * @Mapping( source = "in.propA.propB" target = "propC" ) + * TypeB mappingMethod ( TypeA in ); * } * * Then: @@ -47,9 +51,7 @@ import org.mapstruct.ap.util.Strings; *

  • {@link #propertyEntries[1]} will describe {@code propB}
  • * * - * After building, the {@link #isValid} will be true when when no problems are detected during building. - * - * @author Sjaak Derksen + * After building, {@link #isValid()} will return true when when no problems are detected during building. */ public class SourceReference { @@ -67,22 +69,22 @@ public class SourceReference { private Messager messager; private TypeFactory typeFactory; - public BuilderFromMapping messager( Messager messager ) { + public BuilderFromMapping messager(Messager messager) { this.messager = messager; return this; } - public BuilderFromMapping mapping( Mapping mapping ) { + public BuilderFromMapping mapping(Mapping mapping) { this.mapping = mapping; return this; } - public BuilderFromMapping method( SourceMethod method ) { + public BuilderFromMapping method(SourceMethod method) { this.method = method; return this; } - public BuilderFromMapping typeFactory( TypeFactory typeFactory ) { + public BuilderFromMapping typeFactory(TypeFactory typeFactory) { this.typeFactory = typeFactory; return this; } @@ -98,7 +100,7 @@ public class SourceReference { boolean isValid = true; boolean foundEntryMatch; - String[] sourcePropertyNames = new String[ 0 ]; + String[] sourcePropertyNames = new String[0]; String[] segments = sourceName.split( "\\." ); Parameter parameter = null; @@ -152,15 +154,15 @@ public class SourceReference { if ( parameter != null ) { reportMappingError( - "The type of parameter \"%s\" has no property named \"%s\".", - parameter.getName(), - Strings.join( Arrays.asList( sourcePropertyNames ), "." ) + "The type of parameter \"%s\" has no property named \"%s\".", + parameter.getName(), + Strings.join( Arrays.asList( sourcePropertyNames ), "." ) ); } else { reportMappingError( - "No property named \"%s\" exists in source parameter(s).", - mapping.getSourceName() + "No property named \"%s\" exists in source parameter(s).", + mapping.getSourceName() ); } isValid = false; @@ -169,7 +171,7 @@ public class SourceReference { return new SourceReference( parameter, entries, isValid ); } - private List getSourceEntries( Type type, String[] entryNames ) { + private List getSourceEntries(Type type, String[] entryNames) { List sourceEntries = new ArrayList(); Type newType = type; for ( String entryName : entryNames ) { @@ -190,12 +192,12 @@ public class SourceReference { return sourceEntries; } - private void reportMappingError( String message, Object... objects ) { + private void reportMappingError(String message, Object... objects) { messager.printMessage( - Diagnostic.Kind.ERROR, - String.format( message, objects ), - method.getExecutable(), mapping.getMirror(), - mapping.getSourceAnnotationValue() + Diagnostic.Kind.ERROR, + String.format( message, objects ), + method.getExecutable(), mapping.getMirror(), + mapping.getSourceAnnotationValue() ); } } @@ -210,33 +212,33 @@ public class SourceReference { private Type type; private Parameter sourceParameter; - public BuilderFromProperty name( String name ) { + public BuilderFromProperty name(String name) { this.name = name; return this; } - public BuilderFromProperty accessor( ExecutableElement accessor ) { + public BuilderFromProperty accessor(ExecutableElement accessor) { this.accessor = accessor; return this; } - public BuilderFromProperty type( Type type ) { + public BuilderFromProperty type(Type type) { this.type = type; return this; } - public BuilderFromProperty sourceParameter( Parameter sourceParameter ) { + public BuilderFromProperty sourceParameter(Parameter sourceParameter) { this.sourceParameter = sourceParameter; return this; } public SourceReference build() { - List sourcePropertyEntries = Arrays.asList( new PropertyEntry(name, accessor, type) ); - return new SourceReference(sourceParameter, sourcePropertyEntries, true ); + List sourcePropertyEntries = Arrays.asList( new PropertyEntry( name, accessor, type ) ); + return new SourceReference( sourceParameter, sourcePropertyEntries, true ); } } - private SourceReference( Parameter sourceParameter, List sourcePropertyEntries, boolean isValid ) { + private SourceReference(Parameter sourceParameter, List sourcePropertyEntries, boolean isValid) { this.parameter = sourceParameter; this.propertyEntries = sourcePropertyEntries; this.isValid = isValid; @@ -264,7 +266,7 @@ public class SourceReference { } /** - * A PropertyEntry contains information on the name, accessor and return type of a property. + * A PropertyEntry contains information on the name, accessor and return type of a property. */ public static class PropertyEntry { @@ -272,7 +274,7 @@ public class SourceReference { private final ExecutableElement accessor; private final Type type; - public PropertyEntry( String name, ExecutableElement accessor, Type type ) { + public PropertyEntry(String name, ExecutableElement accessor, Type type) { this.name = name; this.accessor = accessor; this.type = type; diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/NestedSourcePropertiesTest.java b/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/NestedSourcePropertiesTest.java index 3fe814f94..510b30b05 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/NestedSourcePropertiesTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/NestedSourcePropertiesTest.java @@ -18,19 +18,21 @@ */ package org.mapstruct.ap.test.nestedsourceproperties; -import java.util.Arrays; import static org.fest.assertions.Assertions.assertThat; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import org.mapstruct.ap.test.nestedsourceproperties.source.Studio; -import org.mapstruct.ap.test.nestedsourceproperties.source.Song; -import org.mapstruct.ap.test.nestedsourceproperties.source.Label; -import org.mapstruct.ap.test.nestedsourceproperties.source.Chart; -import org.mapstruct.ap.test.nestedsourceproperties.source.Artist; -import org.mapstruct.ap.test.nestedsourceproperties.target.ChartEntry; + +import java.util.Arrays; + import org.junit.Test; import org.junit.runner.RunWith; +import org.mapstruct.ap.test.nestedsourceproperties.source.Artist; +import org.mapstruct.ap.test.nestedsourceproperties.source.Chart; +import org.mapstruct.ap.test.nestedsourceproperties.source.Label; +import org.mapstruct.ap.test.nestedsourceproperties.source.Song; +import org.mapstruct.ap.test.nestedsourceproperties.source.Studio; import org.mapstruct.ap.test.nestedsourceproperties.target.AdderUsageObserver; +import org.mapstruct.ap.test.nestedsourceproperties.target.ChartEntry; import org.mapstruct.ap.test.nestedsourceproperties.target.ChartPositions; import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.testutil.WithClasses; @@ -74,10 +76,8 @@ public class NestedSourcePropertiesTest { assertThat( chartEntry.getPosition() ).isEqualTo( 0 ); assertThat( chartEntry.getRecordedAt() ).isEqualTo( "Abbey Road" ); assertThat( chartEntry.getTitle() ).isEqualTo( "A Hard Day's Night" ); - } - @Test @WithClasses( { ArtistToChartEntry.class } ) public void shouldGenerateImplementationForMultipleParam() { @@ -111,12 +111,11 @@ public class NestedSourcePropertiesTest { assertThat( chartEntry.getPosition() ).isEqualTo( 1 ); assertThat( chartEntry.getRecordedAt() ).isEqualTo( "Abbey Road" ); assertThat( chartEntry.getTitle() ).isEqualTo( "A Hard Day's Night" ); - } @Test @WithClasses( { ArtistToChartEntry.class } ) - public void shouldPickPropertyNameIsoParameterName() { + public void shouldPickPropertyNameOverParameterName() { Chart chart = new Chart(); chart.setName( "Billboard" ); @@ -131,7 +130,6 @@ public class NestedSourcePropertiesTest { assertThat( chartEntry.getPosition() ).isEqualTo( 0 ); assertThat( chartEntry.getRecordedAt() ).isNull(); assertThat( chartEntry.getTitle() ).isNull(); - } @Test @@ -150,7 +148,6 @@ public class NestedSourcePropertiesTest { assertThat( positions.getPositions() ).containsExactly( 3L, 5L ); assertTrue( AdderUsageObserver.isUsed() ); - } @Test @@ -169,7 +166,5 @@ public class NestedSourcePropertiesTest { assertThat( positions.getPositions() ).containsExactly( 3L, 5L ); assertFalse( AdderUsageObserver.isUsed() ); - } - } diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Chart.java b/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Chart.java index 06fa35c39..794b55eed 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Chart.java +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Chart.java @@ -51,7 +51,4 @@ public class Chart { public void setSong( Song song ) { this.song = song; } - - - } diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Label.java b/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Label.java index dc13d8dea..a8254433e 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Label.java +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Label.java @@ -42,6 +42,4 @@ public class Label { public void setStudio( Studio studio ) { this.studio = studio; } - - } diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Song.java b/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Song.java index eca2fdce9..92fe259d0 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Song.java +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Song.java @@ -54,7 +54,4 @@ public class Song { public void setPositions( List positions ) { this.positions = positions; } - - - } diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Studio.java b/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Studio.java index 390694b31..44d8e8e46 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Studio.java +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/source/Studio.java @@ -42,6 +42,4 @@ public class Studio { public void setName( String name ) { this.name = name; } - - } diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/target/ChartEntry.java b/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/target/ChartEntry.java index 45e80b733..95e3f560d 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/target/ChartEntry.java +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/target/ChartEntry.java @@ -24,7 +24,6 @@ package org.mapstruct.ap.test.nestedsourceproperties.target; */ public class ChartEntry { - private String chartName; private String title; private String artistName; @@ -79,7 +78,4 @@ public class ChartEntry { public void setPosition( int position ) { this.position = position; } - - - }