mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#65 Removing FactoryMethod interface
This commit is contained in:
parent
cda3ffc2f5
commit
7041361b76
@ -18,10 +18,12 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.model;
|
package org.mapstruct.ap.model;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.assignment.Assignment;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.model.assignment.Assignment;
|
||||||
import org.mapstruct.ap.model.common.ConversionContext;
|
import org.mapstruct.ap.model.common.ConversionContext;
|
||||||
import org.mapstruct.ap.model.common.Type;
|
import org.mapstruct.ap.model.common.Type;
|
||||||
import org.mapstruct.ap.model.source.Method;
|
import org.mapstruct.ap.model.source.Method;
|
||||||
@ -56,8 +58,8 @@ public class AssignmentFactory {
|
|||||||
return new Direct( sourceRef );
|
return new Direct( sourceRef );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static FactoryMethod createFactoryMethod( Type returnType, MappingBuilderContext ctx ) {
|
public static MethodReference createFactoryMethod( Type returnType, MappingBuilderContext ctx ) {
|
||||||
FactoryMethod result = null;
|
MethodReference result = null;
|
||||||
for ( SourceMethod method : ctx.getSourceModel() ) {
|
for ( SourceMethod method : ctx.getSourceModel() ) {
|
||||||
if ( !method.overridesMethod() && !method.isIterableMapping() && !method.isMapMapping()
|
if ( !method.overridesMethod() && !method.isIterableMapping() && !method.isMapMapping()
|
||||||
&& method.getSourceParameters().isEmpty() ) {
|
&& method.getSourceParameters().isEmpty() ) {
|
||||||
|
@ -26,28 +26,28 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
|
|
||||||
import org.mapstruct.CollectionMappingStrategy;
|
import org.mapstruct.CollectionMappingStrategy;
|
||||||
|
import org.mapstruct.ap.model.PropertyMapping.ConstantMappingBuilder;
|
||||||
|
import org.mapstruct.ap.model.PropertyMapping.JavaExpressionMappingBuilder;
|
||||||
|
import org.mapstruct.ap.model.PropertyMapping.PropertyMappingBuilder;
|
||||||
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.source.Mapping;
|
import org.mapstruct.ap.model.source.Mapping;
|
||||||
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.option.ReportingPolicy;
|
import org.mapstruct.ap.option.ReportingPolicy;
|
||||||
import org.mapstruct.ap.util.Executables;
|
import org.mapstruct.ap.util.Executables;
|
||||||
import org.mapstruct.ap.util.MapperConfig;
|
import org.mapstruct.ap.util.MapperConfig;
|
||||||
import org.mapstruct.ap.util.Strings;
|
import org.mapstruct.ap.util.Strings;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.PropertyMapping.PropertyMappingBuilder;
|
|
||||||
import org.mapstruct.ap.model.PropertyMapping.ConstantMappingBuilder;
|
|
||||||
import org.mapstruct.ap.model.PropertyMapping.JavaExpressionMappingBuilder;
|
|
||||||
import org.mapstruct.ap.model.source.SourceReference;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link MappingMethod} implemented by a {@link Mapper} class which maps one
|
* A {@link MappingMethod} implemented by a {@link Mapper} class which maps one
|
||||||
bean sourceParameter to another, optionally configured by one or more
|
* bean sourceParameter to another, optionally configured by one or more
|
||||||
{@link PropertyMapping}s.
|
* {@link PropertyMapping}s.
|
||||||
*
|
*
|
||||||
* @author Gunnar Morling
|
* @author Gunnar Morling
|
||||||
*/
|
*/
|
||||||
@ -56,10 +56,7 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
private final List<PropertyMapping> propertyMappings;
|
private final List<PropertyMapping> propertyMappings;
|
||||||
private final Map<String, List<PropertyMapping>> mappingsByParameter;
|
private final Map<String, List<PropertyMapping>> mappingsByParameter;
|
||||||
private final List<PropertyMapping> constantMappings;
|
private final List<PropertyMapping> constantMappings;
|
||||||
|
private final MethodReference factoryMethod;
|
||||||
|
|
||||||
private final FactoryMethod factoryMethod;
|
|
||||||
|
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
|
|
||||||
@ -98,11 +95,10 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
reportErrorForUnmappedTargetPropertiesIfRequired( );
|
reportErrorForUnmappedTargetPropertiesIfRequired( );
|
||||||
|
|
||||||
|
|
||||||
FactoryMethod factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx );
|
MethodReference factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx );
|
||||||
return new BeanMappingMethod( method, propertyMappings, factoryMethod );
|
return new BeanMappingMethod( method, propertyMappings, factoryMethod );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method builds the list of target accessors.
|
* This method builds the list of target accessors.
|
||||||
*/
|
*/
|
||||||
@ -384,12 +380,11 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private BeanMappingMethod(SourceMethod method,
|
private BeanMappingMethod(SourceMethod method,
|
||||||
List<PropertyMapping> propertyMappings,
|
List<PropertyMapping> propertyMappings,
|
||||||
FactoryMethod factoryMethod) {
|
MethodReference factoryMethod) {
|
||||||
super( method );
|
super( method );
|
||||||
this.propertyMappings = propertyMappings;
|
this.propertyMappings = propertyMappings;
|
||||||
|
|
||||||
@ -434,7 +429,7 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
return types;
|
return types;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FactoryMethod getFactoryMethod() {
|
public MethodReference getFactoryMethod() {
|
||||||
return this.factoryMethod;
|
return this.factoryMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,33 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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.model;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import org.mapstruct.ap.model.common.Type;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* FactoryMethod, implemented in referenced mapper.
|
|
||||||
*
|
|
||||||
* @author Sjaak Derksen
|
|
||||||
*/
|
|
||||||
public interface FactoryMethod {
|
|
||||||
|
|
||||||
List<Type> getExceptionTypes();
|
|
||||||
|
|
||||||
}
|
|
@ -20,6 +20,7 @@ package org.mapstruct.ap.model;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
|
|
||||||
@ -39,7 +40,7 @@ import org.mapstruct.ap.util.Strings;
|
|||||||
public class IterableMappingMethod extends MappingMethod {
|
public class IterableMappingMethod extends MappingMethod {
|
||||||
|
|
||||||
private final Assignment elementAssignment;
|
private final Assignment elementAssignment;
|
||||||
private final FactoryMethod factoryMethod;
|
private final MethodReference factoryMethod;
|
||||||
private final boolean overridden;
|
private final boolean overridden;
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
@ -101,13 +102,13 @@ public class IterableMappingMethod extends MappingMethod {
|
|||||||
// target accessor is setter, so decorate assignment as setter
|
// target accessor is setter, so decorate assignment as setter
|
||||||
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
||||||
|
|
||||||
FactoryMethod factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx );
|
MethodReference factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx );
|
||||||
return new IterableMappingMethod( method, assignment, factoryMethod );
|
return new IterableMappingMethod( method, assignment, factoryMethod );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private IterableMappingMethod(Method method, Assignment parameterAssignment, FactoryMethod factoryMethod) {
|
private IterableMappingMethod(Method method, Assignment parameterAssignment, MethodReference factoryMethod) {
|
||||||
super( method );
|
super( method );
|
||||||
this.elementAssignment = parameterAssignment;
|
this.elementAssignment = parameterAssignment;
|
||||||
this.factoryMethod = factoryMethod;
|
this.factoryMethod = factoryMethod;
|
||||||
@ -146,7 +147,7 @@ public class IterableMappingMethod extends MappingMethod {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FactoryMethod getFactoryMethod() {
|
public MethodReference getFactoryMethod() {
|
||||||
return this.factoryMethod;
|
return this.factoryMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ package org.mapstruct.ap.model;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
|
|
||||||
@ -40,7 +41,7 @@ public class MapMappingMethod extends MappingMethod {
|
|||||||
|
|
||||||
private final Assignment keyAssignment;
|
private final Assignment keyAssignment;
|
||||||
private final Assignment valueAssignment;
|
private final Assignment valueAssignment;
|
||||||
private final FactoryMethod factoryMethod;
|
private final MethodReference factoryMethod;
|
||||||
private final boolean overridden;
|
private final boolean overridden;
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
@ -133,18 +134,17 @@ public class MapMappingMethod extends MappingMethod {
|
|||||||
ctx.getMessager().printMessage( Diagnostic.Kind.ERROR, message, method.getExecutable() );
|
ctx.getMessager().printMessage( Diagnostic.Kind.ERROR, message, method.getExecutable() );
|
||||||
}
|
}
|
||||||
|
|
||||||
FactoryMethod factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx );
|
MethodReference factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx );
|
||||||
|
|
||||||
keyAssignment = new LocalVarWrapper( keyAssignment, method.getThrownTypes() );
|
keyAssignment = new LocalVarWrapper( keyAssignment, method.getThrownTypes() );
|
||||||
valueAssignment = new LocalVarWrapper( valueAssignment, method.getThrownTypes() );
|
valueAssignment = new LocalVarWrapper( valueAssignment, method.getThrownTypes() );
|
||||||
|
|
||||||
return new MapMappingMethod( method, keyAssignment, valueAssignment, factoryMethod );
|
return new MapMappingMethod( method, keyAssignment, valueAssignment, factoryMethod );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private MapMappingMethod(Method method, Assignment keyAssignment, Assignment valueAssignment,
|
private MapMappingMethod(Method method, Assignment keyAssignment, Assignment valueAssignment,
|
||||||
FactoryMethod factoryMethod) {
|
MethodReference factoryMethod) {
|
||||||
super( method );
|
super( method );
|
||||||
|
|
||||||
this.keyAssignment = keyAssignment;
|
this.keyAssignment = keyAssignment;
|
||||||
@ -206,7 +206,7 @@ public class MapMappingMethod extends MappingMethod {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FactoryMethod getFactoryMethod() {
|
public MethodReference getFactoryMethod() {
|
||||||
return this.factoryMethod;
|
return this.factoryMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,12 +18,13 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.model;
|
package org.mapstruct.ap.model;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.assignment.Assignment;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.model.assignment.Assignment;
|
||||||
import org.mapstruct.ap.model.common.ConversionContext;
|
import org.mapstruct.ap.model.common.ConversionContext;
|
||||||
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;
|
||||||
@ -31,11 +32,12 @@ import org.mapstruct.ap.model.source.Method;
|
|||||||
import org.mapstruct.ap.model.source.builtin.BuiltInMethod;
|
import org.mapstruct.ap.model.source.builtin.BuiltInMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a reference to {@link MappingMethod}.
|
* Represents a reference to another method, e.g. used to map a bean property from source to target type or to
|
||||||
|
* instantiate the return value of a mapping method (rather than calling the {@code new} operator).
|
||||||
*
|
*
|
||||||
* @author Gunnar Morling
|
* @author Gunnar Morling
|
||||||
*/
|
*/
|
||||||
public class MethodReference extends MappingMethod implements Assignment, FactoryMethod {
|
public class MethodReference extends MappingMethod implements Assignment {
|
||||||
|
|
||||||
private final MapperReference declaringMapper;
|
private final MapperReference declaringMapper;
|
||||||
private final Set<Type> importTypes;
|
private final Set<Type> importTypes;
|
||||||
@ -54,7 +56,7 @@ public class MethodReference extends MappingMethod implements Assignment, Factor
|
|||||||
* {@code JAXBElement<Bar>} to {@code Foo} to for which a nested method call will be generated:
|
* {@code JAXBElement<Bar>} to {@code Foo} to for which a nested method call will be generated:
|
||||||
* {@code setFoo(barToFoo( jaxbElemToValue( bar) ) )}
|
* {@code setFoo(barToFoo( jaxbElemToValue( bar) ) )}
|
||||||
*
|
*
|
||||||
* If there's no nested typeConversion or other mapping method, this will be a {@link Simple} assignment.
|
* If there's no nested typeConversion or other mapping method, this will be a {@link Direct} assignment.
|
||||||
*/
|
*/
|
||||||
private Assignment assignment;
|
private Assignment assignment;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user