mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#136 Add additional test to verify compatibility of @TargetType with nested mapping methods
This commit is contained in:
parent
1db853137c
commit
16112a6156
@ -1075,7 +1075,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
mappedElement,
|
mappedElement,
|
||||||
mapperReferences,
|
mapperReferences,
|
||||||
methods,
|
methods,
|
||||||
methodYCandidate.getParameters().get( 0 ).getType(),
|
methodYCandidate.getSourceParameters().get( 0 ).getType(),
|
||||||
returnType,
|
returnType,
|
||||||
targetPropertyName,
|
targetPropertyName,
|
||||||
dateFormat
|
dateFormat
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2014 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.references;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Andreas Gudian
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class GenericWrapper<T> {
|
||||||
|
private final T wrapped;
|
||||||
|
|
||||||
|
public GenericWrapper(T someType) {
|
||||||
|
this.wrapped = someType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getWrapped() {
|
||||||
|
return wrapped;
|
||||||
|
}
|
||||||
|
}
|
@ -86,17 +86,20 @@ public class ReferencedMapperTest extends MapperTestBase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@IssueKey( "136" )
|
@IssueKey( "136" )
|
||||||
@WithClasses( { SourceTargetMapperWithPrimitives.class, SourceWithWrappers.class, TargetWithPrimitives.class } )
|
@WithClasses( { SourceTargetMapperWithPrimitives.class, SourceWithWrappers.class, TargetWithPrimitives.class,
|
||||||
|
GenericWrapper.class } )
|
||||||
public void shouldMapPrimitivesWithCustomMapper() {
|
public void shouldMapPrimitivesWithCustomMapper() {
|
||||||
SourceWithWrappers source = new SourceWithWrappers();
|
SourceWithWrappers source = new SourceWithWrappers();
|
||||||
source.setProp1( new SomeType( "42" ) );
|
source.setProp1( new SomeType( "42" ) );
|
||||||
source.setProp2( new SomeType( "1701" ) );
|
source.setProp2( new SomeType( "1701" ) );
|
||||||
source.setProp3( new SomeType( "true" ) );
|
source.setProp3( new SomeType( "true" ) );
|
||||||
|
source.setProp4( new GenericWrapper<SomeType>( new SomeType( "x" ) ) );
|
||||||
|
|
||||||
TargetWithPrimitives result = SourceTargetMapperWithPrimitives.INSTANCE.sourceToTarget( source );
|
TargetWithPrimitives result = SourceTargetMapperWithPrimitives.INSTANCE.sourceToTarget( source );
|
||||||
|
|
||||||
assertThat( result.getProp1() ).isEqualTo( 42 );
|
assertThat( result.getProp1() ).isEqualTo( 42 );
|
||||||
assertThat( result.getProp2() ).isEqualTo( 1701 );
|
assertThat( result.getProp2() ).isEqualTo( 1701 );
|
||||||
assertThat( result.isProp3() ).isEqualTo( true );
|
assertThat( result.isProp3() ).isEqualTo( true );
|
||||||
|
assertThat( result.getProp4() ).isEqualTo( 'x' );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,14 @@ public abstract class SourceTargetMapperWithPrimitives {
|
|||||||
else if ( clazz == boolean.class ) {
|
else if ( clazz == boolean.class ) {
|
||||||
return (T) Boolean.valueOf( wrapper.getValue() );
|
return (T) Boolean.valueOf( wrapper.getValue() );
|
||||||
}
|
}
|
||||||
|
else if ( clazz == char.class ) {
|
||||||
|
return (T) Character.valueOf( wrapper.getValue().charAt( 0 ) );
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T extends BaseType> T unwrapGenericWrapper(GenericWrapper<T> source, @TargetType Class<T> targetType) {
|
||||||
|
return source.getWrapped();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ public class SourceWithWrappers {
|
|||||||
private SomeType prop1;
|
private SomeType prop1;
|
||||||
private SomeType prop2;
|
private SomeType prop2;
|
||||||
private SomeType prop3;
|
private SomeType prop3;
|
||||||
|
private GenericWrapper<SomeType> prop4;
|
||||||
|
|
||||||
public SomeType getProp1() {
|
public SomeType getProp1() {
|
||||||
return prop1;
|
return prop1;
|
||||||
@ -51,4 +52,11 @@ public class SourceWithWrappers {
|
|||||||
this.prop3 = prop3;
|
this.prop3 = prop3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GenericWrapper<SomeType> getProp4() {
|
||||||
|
return prop4;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProp4(GenericWrapper<SomeType> prop4) {
|
||||||
|
this.prop4 = prop4;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ public class TargetWithPrimitives {
|
|||||||
private int prop1;
|
private int prop1;
|
||||||
private long prop2;
|
private long prop2;
|
||||||
private boolean prop3;
|
private boolean prop3;
|
||||||
|
private char prop4;
|
||||||
|
|
||||||
public int getProp1() {
|
public int getProp1() {
|
||||||
return prop1;
|
return prop1;
|
||||||
@ -50,4 +51,12 @@ public class TargetWithPrimitives {
|
|||||||
public void setProp3(boolean prop3) {
|
public void setProp3(boolean prop3) {
|
||||||
this.prop3 = prop3;
|
this.prop3 = prop3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public char getProp4() {
|
||||||
|
return prop4;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProp4(char prop4) {
|
||||||
|
this.prop4 = prop4;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user