mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#65 Doc/formatting improvements
This commit is contained in:
parent
d0db16072a
commit
160af62acf
@ -36,15 +36,14 @@ import org.mapstruct.ap.model.common.Parameter;
|
|||||||
import org.mapstruct.ap.model.common.Type;
|
import org.mapstruct.ap.model.common.Type;
|
||||||
import org.mapstruct.ap.model.source.ForgedMethod;
|
import org.mapstruct.ap.model.source.ForgedMethod;
|
||||||
import org.mapstruct.ap.model.source.SourceMethod;
|
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.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.DIRECT;
|
||||||
import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED;
|
import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED;
|
||||||
import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED_MAPPED;
|
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
|
* 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 {
|
public class PropertyMapping extends ModelElement {
|
||||||
|
|
||||||
private final String sourceBeanName;
|
private final String sourceBeanName;
|
||||||
|
|
||||||
private final String targetAccessorName;
|
private final String targetAccessorName;
|
||||||
private final Type targetType;
|
private final Type targetType;
|
||||||
|
|
||||||
private final Assignment assignment;
|
private final Assignment assignment;
|
||||||
|
|
||||||
public static class PropertyMappingBuilder {
|
public static class PropertyMappingBuilder {
|
||||||
@ -141,7 +138,8 @@ public class PropertyMapping extends ModelElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private PropertyMapping getPropertyMapping(Type sourceType, Type targetType,
|
private PropertyMapping getPropertyMapping(Type sourceType, Type targetType,
|
||||||
TargetAccessorType targetAccessorType, String sourceRefStr, String sourceElement) {
|
TargetAccessorType targetAccessorType, String sourceRefStr,
|
||||||
|
String sourceElement) {
|
||||||
|
|
||||||
Assignment assignment = ctx.getMappingResolver().getTargetAssignment(
|
Assignment assignment = ctx.getMappingResolver().getTargetAssignment(
|
||||||
method,
|
method,
|
||||||
@ -267,18 +265,20 @@ public class PropertyMapping extends ModelElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String getSourceRef() {
|
private String getSourceRef() {
|
||||||
|
|
||||||
Parameter sourceParam = sourceReference.getParameter();
|
Parameter sourceParam = sourceReference.getParameter();
|
||||||
List<PropertyEntry> propertyEntries = sourceReference.getPropertyEntries();
|
List<PropertyEntry> propertyEntries = sourceReference.getPropertyEntries();
|
||||||
|
|
||||||
|
// parameter reference
|
||||||
if ( propertyEntries.isEmpty() ) {
|
if ( propertyEntries.isEmpty() ) {
|
||||||
return sourceParam.getName();
|
return sourceParam.getName();
|
||||||
}
|
}
|
||||||
|
// simple property
|
||||||
else if ( propertyEntries.size() == 1 ) {
|
else if ( propertyEntries.size() == 1 ) {
|
||||||
PropertyEntry propertyEntry = propertyEntries.get( 0 );
|
PropertyEntry propertyEntry = propertyEntries.get( 0 );
|
||||||
return sourceParam.getName() + "." + propertyEntry.getAccessor().getSimpleName() + "()";
|
return sourceParam.getName() + "." + propertyEntry.getAccessor().getSimpleName() + "()";
|
||||||
}
|
}
|
||||||
|
// nested property given as dot path
|
||||||
else {
|
else {
|
||||||
PropertyEntry lastPropertyEntry = propertyEntries.get( propertyEntries.size() - 1 );
|
PropertyEntry lastPropertyEntry = propertyEntries.get( propertyEntries.size() - 1 );
|
||||||
|
|
||||||
@ -318,7 +318,8 @@ public class PropertyMapping extends ModelElement {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PropertyEntry lastPropertyEntry = propertyEntries.get( propertyEntries.size() - 1 );
|
PropertyEntry lastPropertyEntry = propertyEntries.get( propertyEntries.size() - 1 );
|
||||||
return String.format( "property \"%s %s\"",
|
return String.format(
|
||||||
|
"property \"%s %s\"",
|
||||||
lastPropertyEntry.getType(),
|
lastPropertyEntry.getType(),
|
||||||
Strings.join( sourceReference.getElementNames(), "." )
|
Strings.join( sourceReference.getElementNames(), "." )
|
||||||
);
|
);
|
||||||
|
@ -21,7 +21,6 @@ package org.mapstruct.ap.model.source;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.common.Accessibility;
|
import org.mapstruct.ap.model.common.Accessibility;
|
||||||
@ -42,8 +41,8 @@ public class ForgedMethod implements Method {
|
|||||||
private final ExecutableElement positionHintElement;
|
private final ExecutableElement positionHintElement;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Forged Method.
|
* Creates a new forged method.
|
||||||
*
|
* <p>
|
||||||
* The name will be based on the source type name and target type name.
|
* The name will be based on the source type name and target type name.
|
||||||
*
|
*
|
||||||
* @param sourceType the source type
|
* @param sourceType the source type
|
||||||
@ -62,7 +61,7 @@ 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 name the (unique name) for this method
|
||||||
* @param sourceType the source type
|
* @param sourceType the source type
|
||||||
|
@ -34,8 +34,8 @@ import javax.lang.model.type.TypeKind;
|
|||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
import javax.tools.Diagnostic.Kind;
|
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.MappingPrism;
|
||||||
import org.mapstruct.ap.prism.MappingsPrism;
|
import org.mapstruct.ap.prism.MappingsPrism;
|
||||||
|
|
||||||
@ -289,7 +289,6 @@ public class Mapping {
|
|||||||
return reverse;
|
return reverse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Mapping {" +
|
return "Mapping {" +
|
||||||
|
@ -24,6 +24,7 @@ import java.util.List;
|
|||||||
import javax.annotation.processing.Messager;
|
import javax.annotation.processing.Messager;
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.common.Parameter;
|
import org.mapstruct.ap.model.common.Parameter;
|
||||||
import org.mapstruct.ap.model.common.Type;
|
import org.mapstruct.ap.model.common.Type;
|
||||||
import org.mapstruct.ap.model.common.TypeFactory;
|
import org.mapstruct.ap.model.common.TypeFactory;
|
||||||
@ -32,10 +33,13 @@ import org.mapstruct.ap.util.Strings;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This class describes the source side of a property mapping.
|
* This class describes the source side of a property mapping.
|
||||||
*
|
* <p>
|
||||||
* 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:
|
* mapping method:
|
||||||
|
*
|
||||||
* {@code
|
* {@code
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
* @Mapping( source = "in.propA.propB" target = "propC" )
|
* @Mapping( source = "in.propA.propB" target = "propC" )
|
||||||
* TypeB mappingMethod ( TypeA in );
|
* TypeB mappingMethod ( TypeA in );
|
||||||
* }
|
* }
|
||||||
@ -47,9 +51,7 @@ import org.mapstruct.ap.util.Strings;
|
|||||||
* <li>{@link #propertyEntries[1]} will describe {@code propB}</li>
|
* <li>{@link #propertyEntries[1]} will describe {@code propB}</li>
|
||||||
* </ol>
|
* </ol>
|
||||||
*
|
*
|
||||||
* After building, the {@link #isValid} will be true when when no problems are detected during building.
|
* After building, {@link #isValid()} will return true when when no problems are detected during building.
|
||||||
*
|
|
||||||
* @author Sjaak Derksen
|
|
||||||
*/
|
*/
|
||||||
public class SourceReference {
|
public class SourceReference {
|
||||||
|
|
||||||
|
@ -18,19 +18,21 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.nestedsourceproperties;
|
package org.mapstruct.ap.test.nestedsourceproperties;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import static org.fest.assertions.Assertions.assertThat;
|
import static org.fest.assertions.Assertions.assertThat;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import org.mapstruct.ap.test.nestedsourceproperties.source.Studio;
|
|
||||||
import org.mapstruct.ap.test.nestedsourceproperties.source.Song;
|
import java.util.Arrays;
|
||||||
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 org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
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.AdderUsageObserver;
|
||||||
|
import org.mapstruct.ap.test.nestedsourceproperties.target.ChartEntry;
|
||||||
import org.mapstruct.ap.test.nestedsourceproperties.target.ChartPositions;
|
import org.mapstruct.ap.test.nestedsourceproperties.target.ChartPositions;
|
||||||
import org.mapstruct.ap.testutil.IssueKey;
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
import org.mapstruct.ap.testutil.WithClasses;
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
@ -74,10 +76,8 @@ public class NestedSourcePropertiesTest {
|
|||||||
assertThat( chartEntry.getPosition() ).isEqualTo( 0 );
|
assertThat( chartEntry.getPosition() ).isEqualTo( 0 );
|
||||||
assertThat( chartEntry.getRecordedAt() ).isEqualTo( "Abbey Road" );
|
assertThat( chartEntry.getRecordedAt() ).isEqualTo( "Abbey Road" );
|
||||||
assertThat( chartEntry.getTitle() ).isEqualTo( "A Hard Day's Night" );
|
assertThat( chartEntry.getTitle() ).isEqualTo( "A Hard Day's Night" );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@WithClasses( { ArtistToChartEntry.class } )
|
@WithClasses( { ArtistToChartEntry.class } )
|
||||||
public void shouldGenerateImplementationForMultipleParam() {
|
public void shouldGenerateImplementationForMultipleParam() {
|
||||||
@ -111,12 +111,11 @@ public class NestedSourcePropertiesTest {
|
|||||||
assertThat( chartEntry.getPosition() ).isEqualTo( 1 );
|
assertThat( chartEntry.getPosition() ).isEqualTo( 1 );
|
||||||
assertThat( chartEntry.getRecordedAt() ).isEqualTo( "Abbey Road" );
|
assertThat( chartEntry.getRecordedAt() ).isEqualTo( "Abbey Road" );
|
||||||
assertThat( chartEntry.getTitle() ).isEqualTo( "A Hard Day's Night" );
|
assertThat( chartEntry.getTitle() ).isEqualTo( "A Hard Day's Night" );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@WithClasses( { ArtistToChartEntry.class } )
|
@WithClasses( { ArtistToChartEntry.class } )
|
||||||
public void shouldPickPropertyNameIsoParameterName() {
|
public void shouldPickPropertyNameOverParameterName() {
|
||||||
|
|
||||||
Chart chart = new Chart();
|
Chart chart = new Chart();
|
||||||
chart.setName( "Billboard" );
|
chart.setName( "Billboard" );
|
||||||
@ -131,7 +130,6 @@ public class NestedSourcePropertiesTest {
|
|||||||
assertThat( chartEntry.getPosition() ).isEqualTo( 0 );
|
assertThat( chartEntry.getPosition() ).isEqualTo( 0 );
|
||||||
assertThat( chartEntry.getRecordedAt() ).isNull();
|
assertThat( chartEntry.getRecordedAt() ).isNull();
|
||||||
assertThat( chartEntry.getTitle() ).isNull();
|
assertThat( chartEntry.getTitle() ).isNull();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -150,7 +148,6 @@ public class NestedSourcePropertiesTest {
|
|||||||
assertThat( positions.getPositions() ).containsExactly( 3L, 5L );
|
assertThat( positions.getPositions() ).containsExactly( 3L, 5L );
|
||||||
|
|
||||||
assertTrue( AdderUsageObserver.isUsed() );
|
assertTrue( AdderUsageObserver.isUsed() );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -169,7 +166,5 @@ public class NestedSourcePropertiesTest {
|
|||||||
assertThat( positions.getPositions() ).containsExactly( 3L, 5L );
|
assertThat( positions.getPositions() ).containsExactly( 3L, 5L );
|
||||||
|
|
||||||
assertFalse( AdderUsageObserver.isUsed() );
|
assertFalse( AdderUsageObserver.isUsed() );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,4 @@ public class Chart {
|
|||||||
public void setSong( Song song ) {
|
public void setSong( Song song ) {
|
||||||
this.song = song;
|
this.song = song;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,4 @@ public class Label {
|
|||||||
public void setStudio( Studio studio ) {
|
public void setStudio( Studio studio ) {
|
||||||
this.studio = studio;
|
this.studio = studio;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,4 @@ public class Song {
|
|||||||
public void setPositions( List<Integer> positions ) {
|
public void setPositions( List<Integer> positions ) {
|
||||||
this.positions = positions;
|
this.positions = positions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,4 @@ public class Studio {
|
|||||||
public void setName( String name ) {
|
public void setName( String name ) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ package org.mapstruct.ap.test.nestedsourceproperties.target;
|
|||||||
*/
|
*/
|
||||||
public class ChartEntry {
|
public class ChartEntry {
|
||||||
|
|
||||||
|
|
||||||
private String chartName;
|
private String chartName;
|
||||||
private String title;
|
private String title;
|
||||||
private String artistName;
|
private String artistName;
|
||||||
@ -79,7 +78,4 @@ public class ChartEntry {
|
|||||||
public void setPosition( int position ) {
|
public void setPosition( int position ) {
|
||||||
this.position = position;
|
this.position = position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user