mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#134 Some clean-up:
* Removing obsolete method * Using more descriptive method and test name * Some comments
This commit is contained in:
parent
27c85d8f97
commit
53ff1b7e13
@ -18,7 +18,6 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.common.ConversionContext;
|
||||
@ -34,6 +33,12 @@ import org.mapstruct.ap.model.source.builtin.BuiltInMethod;
|
||||
public class MethodReference extends MappingMethod {
|
||||
|
||||
private final MapperReference declaringMapper;
|
||||
|
||||
/**
|
||||
* A reference to another mapping method in case this is a two-step mapping, e.g. from {@code JAXBElement<Bar>} to
|
||||
* {@code Foo} to for which a nested method call will be generated:
|
||||
* {@code setFoo(barToFoo( jaxbElemToValue( bar) ) )}
|
||||
*/
|
||||
private MethodReference methodRefChild;
|
||||
|
||||
/**
|
||||
@ -63,10 +68,6 @@ public class MethodReference extends MappingMethod {
|
||||
return declaringMapper.getVariableName();
|
||||
}
|
||||
|
||||
public Set<Type> getReferencedTypes() {
|
||||
return new HashSet<Type>();
|
||||
}
|
||||
|
||||
public String getContextParam() {
|
||||
return contextParam;
|
||||
}
|
||||
|
@ -1029,7 +1029,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
/**
|
||||
* Suppose mapping required from A to C and:
|
||||
* <ul>
|
||||
* <li>no direct referenced mapping method either BuiltIn or Referenced is avaliable from A to C</li>
|
||||
* <li>no direct referenced mapping method either built-in or referenced is available from A to C</li>
|
||||
* <li>no conversion is available</li>
|
||||
* <li>there is a method from A to B, methodX</li>
|
||||
* <li>there is a method from B to C, methodY</li>
|
||||
@ -1154,8 +1154,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
return new MethodReference( method, ctx );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns false if source the property can't be mapped from source to target. A mapping if possible if one of
|
||||
/**
|
||||
* Whether the specified property can be mapped from source to target or not. A mapping if possible if one of
|
||||
* the following conditions is true:
|
||||
* <ul>
|
||||
* <li>the source type is assignable to the target type</li>
|
||||
@ -1165,9 +1165,9 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
* implementation type) accepts the source type.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param method The mapping method owning the property mapping.
|
||||
* @param property The property mapping to check.
|
||||
* @return false if property cannot be mapped
|
||||
*
|
||||
* @return {@code true} if the specified property can be mapped, {@code false} otherwise.
|
||||
*/
|
||||
private boolean isPropertyMappable(PropertyMapping property) {
|
||||
boolean collectionOrMapTargetTypeHasCompatibleConstructor = false;
|
||||
|
@ -18,7 +18,6 @@
|
||||
*/
|
||||
package org.mapstruct.ap.test.nestedmethodcall;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
@ -26,73 +25,84 @@ import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import javax.xml.namespace.QName;
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.MapperTestBase;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
* Test for the nested invocation of mapping methods.
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@IssueKey( "134" )
|
||||
@WithClasses( {
|
||||
@IssueKey("134")
|
||||
@WithClasses({
|
||||
SourceTargetMapper.class,
|
||||
OrderDto.class,
|
||||
OrderDetailsDto.class,
|
||||
OrderDetailsType.class,
|
||||
OrderType.class
|
||||
} )
|
||||
public class MapperTest extends MapperTestBase {
|
||||
})
|
||||
public class NestedMappingMethodInvocationTest extends MapperTestBase {
|
||||
|
||||
private static final QName QNAME = new QName("dont-care");
|
||||
private static final QName QNAME = new QName( "dont-care" );
|
||||
|
||||
@Test
|
||||
public void referencedMappersAreInstatiatedCorrectly() throws DatatypeConfigurationException {
|
||||
public void shouldGeneratedNestedMappingMethodCalls() throws DatatypeConfigurationException {
|
||||
SourceTargetMapper instance = SourceTargetMapper.INSTANCE;
|
||||
OrderDto target = instance.sourceToTarget( createOrderType() );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getOrderNumber() ).isEqualTo( 5L );
|
||||
|
||||
assertThat( target.getDates().size() ).isEqualTo( 2 );
|
||||
assertThat( target.getDates().get( 0 ) ).isEqualTo( "02.03.1999" );
|
||||
assertThat( target.getDates().get( 1 ) ).isEqualTo( "28.07.2004" );
|
||||
assertThat( target.getDates() ).containsExactly( "02.03.1999", "28.07.2004" );
|
||||
|
||||
assertThat( target.getOrderDetails() ).isNotNull();
|
||||
assertThat( target.getOrderDetails().getName() ).isEqualTo( "test" );
|
||||
assertThat( target.getOrderDetails().getDescription() ).isNotNull();
|
||||
assertThat( target.getOrderDetails().getDescription().size() ).isEqualTo( 2 );
|
||||
assertThat( target.getOrderDetails().getDescription().get( 0 ) ).isEqualTo( "elem1" );
|
||||
assertThat( target.getOrderDetails().getDescription().get( 1 ) ).isEqualTo( "elem2" );
|
||||
|
||||
|
||||
assertThat( target.getOrderDetails().getDescription() ).containsExactly( "elem1", "elem2" );
|
||||
}
|
||||
|
||||
|
||||
private OrderType createOrderType() throws DatatypeConfigurationException {
|
||||
|
||||
List<JAXBElement<XMLGregorianCalendar>> dates = new ArrayList<JAXBElement<XMLGregorianCalendar>>();
|
||||
dates.add( new JAXBElement(QNAME, XMLGregorianCalendar.class, createXmlCal( 1999, 3, 2, 1 ) ) );
|
||||
dates.add( new JAXBElement(QNAME, XMLGregorianCalendar.class, createXmlCal( 2004, 7, 29, 3 ) ) );
|
||||
dates.add(
|
||||
new JAXBElement<XMLGregorianCalendar>(
|
||||
QNAME,
|
||||
XMLGregorianCalendar.class,
|
||||
createXmlCal( 1999, 3, 2, 1 )
|
||||
)
|
||||
);
|
||||
dates.add(
|
||||
new JAXBElement<XMLGregorianCalendar>(
|
||||
QNAME,
|
||||
XMLGregorianCalendar.class,
|
||||
createXmlCal( 2004, 7, 29, 3 )
|
||||
)
|
||||
);
|
||||
|
||||
List<JAXBElement<String>> description = new ArrayList<JAXBElement<String>>();
|
||||
description.add( new JAXBElement(QNAME, String.class, "elem1" ) );
|
||||
description.add( new JAXBElement(QNAME, String.class, "elem2" ) );
|
||||
description.add( new JAXBElement<String>( QNAME, String.class, "elem1" ) );
|
||||
description.add( new JAXBElement<String>( QNAME, String.class, "elem2" ) );
|
||||
|
||||
OrderType orderType = new OrderType();
|
||||
orderType.setOrderNumber( new JAXBElement(QNAME, Long.class, 5L ) );
|
||||
orderType.setOrderDetails( new JAXBElement(QNAME, OrderDetailsType.class, new OrderDetailsType() ) );
|
||||
orderType.getOrderDetails().getValue().setName( new JAXBElement(QNAME, String.class, "test" ) );
|
||||
orderType.setOrderNumber( new JAXBElement<Long>( QNAME, Long.class, 5L ) );
|
||||
orderType.setOrderDetails(
|
||||
new JAXBElement<OrderDetailsType>(
|
||||
QNAME,
|
||||
OrderDetailsType.class,
|
||||
new OrderDetailsType()
|
||||
)
|
||||
);
|
||||
orderType.getOrderDetails().getValue().setName( new JAXBElement<String>( QNAME, String.class, "test" ) );
|
||||
orderType.getOrderDetails().getValue().setDescription( description );
|
||||
orderType.setDates( dates );
|
||||
|
||||
return orderType;
|
||||
}
|
||||
|
||||
private XMLGregorianCalendar createXmlCal( int year, int month, int day, int tz )
|
||||
throws DatatypeConfigurationException {
|
||||
private XMLGregorianCalendar createXmlCal(int year, int month, int day, int tz)
|
||||
throws DatatypeConfigurationException {
|
||||
return DatatypeFactory.newInstance().newXMLGregorianCalendarDate( year, month, day, tz );
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user