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.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<TypeMirror> qualifiers ) {
|
||||
public PropertyMappingBuilder qualifiers(List<TypeMirror> 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,
|
||||
@ -267,18 +265,20 @@ public class PropertyMapping extends ModelElement {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String getSourceRef() {
|
||||
|
||||
Parameter sourceParam = sourceReference.getParameter();
|
||||
List<PropertyEntry> 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 );
|
||||
|
||||
@ -318,7 +318,8 @@ public class PropertyMapping extends ModelElement {
|
||||
}
|
||||
else {
|
||||
PropertyEntry lastPropertyEntry = propertyEntries.get( propertyEntries.size() - 1 );
|
||||
return String.format( "property \"%s %s\"",
|
||||
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:
|
||||
|
@ -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.
|
||||
* <p>
|
||||
* 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<Type> sourceTypes, Type targetType ) {
|
||||
public boolean matches(List<Type> sourceTypes, Type targetType) {
|
||||
|
||||
if ( !targetType.equals( returnType ) ) {
|
||||
return false;
|
||||
|
@ -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;
|
||||
|
||||
@ -195,7 +195,7 @@ 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()
|
||||
@ -289,7 +289,6 @@ public class Mapping {
|
||||
return reverse;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Mapping {" +
|
||||
|
@ -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,10 +33,13 @@ 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
|
||||
* <p>
|
||||
* It contains the source parameter, and all individual (nested) property entries. So consider the following
|
||||
* mapping method:
|
||||
*
|
||||
* {@code
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
* @Mapping( source = "in.propA.propB" target = "propC" )
|
||||
* TypeB mappingMethod ( TypeA in );
|
||||
* }
|
||||
@ -47,9 +51,7 @@ import org.mapstruct.ap.util.Strings;
|
||||
* <li>{@link #propertyEntries[1]} will describe {@code propB}</li>
|
||||
* </ol>
|
||||
*
|
||||
* 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;
|
||||
|
||||
@ -169,7 +171,7 @@ public class SourceReference {
|
||||
return new SourceReference( parameter, entries, isValid );
|
||||
}
|
||||
|
||||
private List<PropertyEntry> getSourceEntries( Type type, String[] entryNames ) {
|
||||
private List<PropertyEntry> getSourceEntries(Type type, String[] entryNames) {
|
||||
List<PropertyEntry> sourceEntries = new ArrayList<PropertyEntry>();
|
||||
Type newType = type;
|
||||
for ( String entryName : entryNames ) {
|
||||
@ -190,7 +192,7 @@ 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 ),
|
||||
@ -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<PropertyEntry> sourcePropertyEntries = Arrays.asList( new PropertyEntry(name, accessor, type) );
|
||||
return new SourceReference(sourceParameter, sourcePropertyEntries, true );
|
||||
List<PropertyEntry> sourcePropertyEntries = Arrays.asList( new PropertyEntry( name, accessor, type ) );
|
||||
return new SourceReference( sourceParameter, sourcePropertyEntries, true );
|
||||
}
|
||||
}
|
||||
|
||||
private SourceReference( Parameter sourceParameter, List<PropertyEntry> sourcePropertyEntries, boolean isValid ) {
|
||||
private SourceReference(Parameter sourceParameter, List<PropertyEntry> sourcePropertyEntries, boolean isValid) {
|
||||
this.parameter = sourceParameter;
|
||||
this.propertyEntries = sourcePropertyEntries;
|
||||
this.isValid = isValid;
|
||||
@ -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;
|
||||
|
@ -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() );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -51,7 +51,4 @@ public class Chart {
|
||||
public void setSong( Song song ) {
|
||||
this.song = song;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -42,6 +42,4 @@ public class Label {
|
||||
public void setStudio( Studio studio ) {
|
||||
this.studio = studio;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -54,7 +54,4 @@ public class Song {
|
||||
public void setPositions( List<Integer> positions ) {
|
||||
this.positions = positions;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -42,6 +42,4 @@ public class Studio {
|
||||
public void setName( String name ) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user