mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
parent
b565c6fe60
commit
8bdec6405d
@ -18,12 +18,10 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model;
|
||||
|
||||
import java.beans.Introspector;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.source.Method;
|
||||
import org.mapstruct.ap.util.Strings;
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
import org.mapstruct.ap.model.common.ModelElement;
|
||||
|
||||
@ -36,13 +34,13 @@ public class FactoryMethod extends ModelElement {
|
||||
|
||||
private final String name;
|
||||
private final boolean hasDeclaringMapper;
|
||||
private final Type declaringMapper;
|
||||
private final MapperReference declaringMapper;
|
||||
private final Type returnType;
|
||||
|
||||
public FactoryMethod(Method method) {
|
||||
public FactoryMethod( Method method, MapperReference declaringMapper ) {
|
||||
this.name = method.getName();
|
||||
this.hasDeclaringMapper = method.getDeclaringMapper() != null;
|
||||
this.declaringMapper = method.getDeclaringMapper();
|
||||
this.declaringMapper = declaringMapper;
|
||||
this.returnType = method.getReturnType();
|
||||
}
|
||||
|
||||
@ -51,11 +49,11 @@ public class FactoryMethod extends ModelElement {
|
||||
}
|
||||
|
||||
public Type getDeclaringMapper() {
|
||||
return declaringMapper;
|
||||
return declaringMapper.getMapperType();
|
||||
}
|
||||
|
||||
public String getMapperVariableName() {
|
||||
return Strings.getSaveVariableName( Introspector.decapitalize( declaringMapper.getName() ) );
|
||||
return declaringMapper.getVariableName();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -209,14 +209,23 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
return mappingMethods;
|
||||
}
|
||||
|
||||
private FactoryMethod getFactoryMethod(List<Method> methods, Type returnType) {
|
||||
private FactoryMethod getFactoryMethod(List<MapperReference> mapperReferences, List<Method> methods,
|
||||
Type returnType) {
|
||||
FactoryMethod result = null;
|
||||
for ( Method method : methods ) {
|
||||
if ( !method.requiresImplementation() && !method.isIterableMapping() && !method.isMapMapping()
|
||||
&& method.getMappings().isEmpty() && method.getParameters().isEmpty() ) {
|
||||
if ( method.getReturnType().equals( returnType ) ) {
|
||||
if ( result == null ) {
|
||||
result = new FactoryMethod(method);
|
||||
MapperReference mapperReference = null;
|
||||
for ( MapperReference ref : mapperReferences ) {
|
||||
if ( ref.getMapperType().equals( method.getDeclaringMapper() ) ) {
|
||||
mapperReference = ref;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
result = new FactoryMethod(method, mapperReference);
|
||||
}
|
||||
else {
|
||||
messager.printMessage(
|
||||
@ -379,7 +388,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
mappedTargetProperties
|
||||
);
|
||||
|
||||
FactoryMethod factoryMethod = getFactoryMethod( methods, method.getReturnType() );
|
||||
FactoryMethod factoryMethod = getFactoryMethod( mapperReferences, methods, method.getReturnType() );
|
||||
return new BeanMappingMethod( method, propertyMappings, factoryMethod );
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* 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.factories;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
*
|
||||
*/
|
||||
public class Bar3 {
|
||||
private String prop;
|
||||
|
||||
private final String someTypeProp;
|
||||
|
||||
public Bar3( String someTypeProp ) {
|
||||
this.someTypeProp = someTypeProp;
|
||||
}
|
||||
|
||||
public String getProp() {
|
||||
return prop;
|
||||
}
|
||||
|
||||
public void setProp(String prop) {
|
||||
this.prop = prop;
|
||||
}
|
||||
|
||||
public String getSomeTypeProp() {
|
||||
return someTypeProp;
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
*/
|
||||
package org.mapstruct.ap.test.factories;
|
||||
|
||||
import org.mapstruct.ap.test.factories.a.BarFactory;
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
@ -30,8 +31,9 @@ import org.testng.annotations.Test;
|
||||
*
|
||||
*/
|
||||
@IssueKey( "81" )
|
||||
@WithClasses( { Bar1.class, Foo1.class, Bar2.class, Foo2.class, Bar1Factory.class, Source.class,
|
||||
SourceTargetMapperAndBar2Factory.class, Target.class } )
|
||||
@WithClasses( { Bar1.class, Foo1.class, Bar2.class, Foo2.class, Bar3.class, Foo3.class, BarFactory.class,
|
||||
org.mapstruct.ap.test.factories.b.BarFactory.class, Source.class, SourceTargetMapperAndBar2Factory.class,
|
||||
Target.class } )
|
||||
public class FactoryTest extends MapperTestBase {
|
||||
@Test
|
||||
public void shouldUseTwoFactoryMethods() {
|
||||
@ -44,6 +46,9 @@ public class FactoryTest extends MapperTestBase {
|
||||
assertThat( target.getProp2() ).isNotNull();
|
||||
assertThat( target.getProp2().getProp() ).isEqualTo( "foo2" );
|
||||
assertThat( target.getProp2().getSomeTypeProp()).isEqualTo( "BAR2" );
|
||||
assertThat( target.getProp3() ).isNotNull();
|
||||
assertThat( target.getProp3().getProp() ).isEqualTo( "foo3" );
|
||||
assertThat( target.getProp3().getSomeTypeProp()).isEqualTo( "BAR3" );
|
||||
}
|
||||
|
||||
private Source createSource() {
|
||||
@ -57,6 +62,10 @@ public class FactoryTest extends MapperTestBase {
|
||||
foo2.setProp( "foo2" );
|
||||
source.setProp2( foo2 );
|
||||
|
||||
Foo3 foo3 = new Foo3();
|
||||
foo3.setProp( "foo3" );
|
||||
source.setProp3( foo3 );
|
||||
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
@ -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.factories;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
*
|
||||
*/
|
||||
public class Foo3 {
|
||||
private String prop;
|
||||
|
||||
public String getProp() {
|
||||
return prop;
|
||||
}
|
||||
|
||||
public void setProp(String prop) {
|
||||
this.prop = prop;
|
||||
}
|
||||
}
|
@ -26,6 +26,7 @@ public class Source {
|
||||
|
||||
private Foo1 prop1;
|
||||
private Foo2 prop2;
|
||||
private Foo3 prop3;
|
||||
|
||||
public Foo1 getProp1() {
|
||||
return prop1;
|
||||
@ -43,4 +44,11 @@ public class Source {
|
||||
this.prop2 = prop2;
|
||||
}
|
||||
|
||||
public Foo3 getProp3() {
|
||||
return prop3;
|
||||
}
|
||||
|
||||
public void setProp3( Foo3 prop3 ) {
|
||||
this.prop3 = prop3;
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@
|
||||
*/
|
||||
package org.mapstruct.ap.test.factories;
|
||||
|
||||
import org.mapstruct.ap.test.factories.a.BarFactory;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@ -25,7 +26,7 @@ import org.mapstruct.factory.Mappers;
|
||||
* @author Sjaak Derksen
|
||||
*
|
||||
*/
|
||||
@Mapper( uses = { Bar1Factory.class } )
|
||||
@Mapper( uses = { BarFactory.class, org.mapstruct.ap.test.factories.b.BarFactory.class } )
|
||||
public abstract class SourceTargetMapperAndBar2Factory {
|
||||
public static final SourceTargetMapperAndBar2Factory INSTANCE =
|
||||
Mappers.getMapper( SourceTargetMapperAndBar2Factory.class );
|
||||
@ -34,7 +35,9 @@ public abstract class SourceTargetMapperAndBar2Factory {
|
||||
|
||||
public abstract Bar1 foo1ToBar1(Foo1 foo1);
|
||||
|
||||
public abstract Bar2 foo1ToBar1(Foo2 foo2);
|
||||
public abstract Bar2 foo2ToBar2(Foo2 foo2);
|
||||
|
||||
public abstract Bar3 foo3ToBar3(Foo3 foo3);
|
||||
|
||||
public Bar2 createBar2() {
|
||||
return new Bar2("BAR2");
|
||||
|
@ -27,6 +27,7 @@ public class Target {
|
||||
|
||||
private Bar1 prop1;
|
||||
private Bar2 prop2;
|
||||
private Bar3 prop3;
|
||||
|
||||
|
||||
public Bar1 getProp1() {
|
||||
@ -45,5 +46,11 @@ public class Target {
|
||||
this.prop2 = prop2;
|
||||
}
|
||||
|
||||
public Bar3 getProp3() {
|
||||
return prop3;
|
||||
}
|
||||
|
||||
public void setProp3( Bar3 prop3 ) {
|
||||
this.prop3 = prop3;
|
||||
}
|
||||
}
|
||||
|
@ -16,13 +16,15 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.factories;
|
||||
package org.mapstruct.ap.test.factories.a;
|
||||
|
||||
import org.mapstruct.ap.test.factories.Bar1;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public class Bar1Factory {
|
||||
public class BarFactory {
|
||||
|
||||
public Bar1 createBar1() {
|
||||
return new Bar1("BAR1");
|
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* 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.factories.b;
|
||||
|
||||
import org.mapstruct.ap.test.factories.Bar3;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public class BarFactory {
|
||||
|
||||
public Bar3 createBar3() {
|
||||
return new Bar3("BAR3");
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user