mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#429 Using Builder Pattern for SourceMethod
This commit is contained in:
parent
08ac33e3b1
commit
d3de4991c9
@ -19,7 +19,6 @@
|
||||
package org.mapstruct.ap.model.source;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -66,21 +65,87 @@ public class SourceMethod implements Method {
|
||||
private IterableMapping iterableMapping;
|
||||
private MapMapping mapMapping;
|
||||
|
||||
//CHECKSTYLE:OFF
|
||||
// TODO use builder
|
||||
public static SourceMethod forMethodRequiringImplementation(ExecutableElement executable,
|
||||
List<Parameter> parameters,
|
||||
Type returnType,
|
||||
List<Type> exceptionTypes,
|
||||
Map<String, List<Mapping>> mappings,
|
||||
IterableMapping iterableMapping, MapMapping mapMapping,
|
||||
Types typeUtils,
|
||||
Messager messager,
|
||||
TypeFactory typeFactory,
|
||||
MapperConfig config) {
|
||||
public static class Builder {
|
||||
|
||||
private Type declaringMapper = null;
|
||||
private ExecutableElement executable;
|
||||
private List<Parameter> parameters;
|
||||
private Type returnType = null;
|
||||
private List<Type> exceptionTypes;
|
||||
private Map<String, List<Mapping>> mappings;
|
||||
private IterableMapping iterableMapping = null;
|
||||
private MapMapping mapMapping = null;
|
||||
private Types typeUtils;
|
||||
private TypeFactory typeFactory = null;
|
||||
private Messager messager = null;
|
||||
private MapperConfig mapperConfig = null;
|
||||
|
||||
public Builder() {
|
||||
}
|
||||
|
||||
public Builder setDeclaringMapper(Type declaringMapper) {
|
||||
this.declaringMapper = declaringMapper;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setExecutable(ExecutableElement executable) {
|
||||
this.executable = executable;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setParameters(List<Parameter> parameters) {
|
||||
this.parameters = parameters;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setReturnType(Type returnType) {
|
||||
this.returnType = returnType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setExceptionTypes(List<Type> exceptionTypes) {
|
||||
this.exceptionTypes = exceptionTypes;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setMappings(Map<String, List<Mapping>> mappings) {
|
||||
this.mappings = mappings;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setIterableMapping(IterableMapping iterableMapping) {
|
||||
this.iterableMapping = iterableMapping;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setMapMapping(MapMapping mapMapping) {
|
||||
this.mapMapping = mapMapping;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setTypeUtils(Types typeUtils) {
|
||||
this.typeUtils = typeUtils;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setTypeFactory(TypeFactory typeFactory) {
|
||||
this.typeFactory = typeFactory;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setMessager(Messager messager) {
|
||||
this.messager = messager;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setMapperConfig(MapperConfig mapperConfig) {
|
||||
this.mapperConfig = mapperConfig;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SourceMethod createSourceMethod() {
|
||||
SourceMethod sourceMethod = new SourceMethod(
|
||||
null,
|
||||
declaringMapper,
|
||||
executable,
|
||||
parameters,
|
||||
returnType,
|
||||
@ -91,55 +156,18 @@ public class SourceMethod implements Method {
|
||||
typeUtils,
|
||||
typeFactory,
|
||||
messager,
|
||||
config
|
||||
mapperConfig
|
||||
);
|
||||
|
||||
if ( mappings != null ) {
|
||||
for ( Map.Entry<String, List<Mapping>> entry : sourceMethod.getMappings().entrySet() ) {
|
||||
for ( Mapping mapping : entry.getValue() ) {
|
||||
mapping.init( sourceMethod, messager, typeFactory );
|
||||
}
|
||||
}
|
||||
}
|
||||
return sourceMethod;
|
||||
}
|
||||
//CHECKSTYLE:ON
|
||||
|
||||
public static SourceMethod forReferencedMethod(Type declaringMapper, ExecutableElement executable,
|
||||
List<Parameter> parameters, Type returnType,
|
||||
List<Type> exceptionTypes, Types typeUtils) {
|
||||
|
||||
return new SourceMethod(
|
||||
declaringMapper,
|
||||
executable,
|
||||
parameters,
|
||||
returnType,
|
||||
exceptionTypes,
|
||||
Collections.<String, List<Mapping>>emptyMap(),
|
||||
null,
|
||||
null,
|
||||
typeUtils,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
public static SourceMethod forFactoryMethod(Type declaringMapper, ExecutableElement executable, Type returnType,
|
||||
List<Type> exceptionTypes, Types typeUtils) {
|
||||
|
||||
return new SourceMethod(
|
||||
declaringMapper,
|
||||
executable,
|
||||
Collections.<Parameter>emptyList(),
|
||||
returnType,
|
||||
exceptionTypes,
|
||||
Collections.<String, List<Mapping>>emptyMap(),
|
||||
null,
|
||||
null,
|
||||
typeUtils,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
//CHECKSTYLE:OFF
|
||||
|
@ -168,19 +168,21 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
|
||||
return null;
|
||||
}
|
||||
|
||||
return SourceMethod.forMethodRequiringImplementation(
|
||||
method,
|
||||
parameters,
|
||||
returnType,
|
||||
exceptionTypes,
|
||||
getMappings( method ),
|
||||
IterableMapping.fromPrism( IterableMappingPrism.getInstanceOn( method ), method, messager ),
|
||||
MapMapping.fromPrism( MapMappingPrism.getInstanceOn( method ), method, messager ),
|
||||
typeUtils,
|
||||
messager,
|
||||
typeFactory,
|
||||
MapperConfig.getInstanceOn( mapperToImplement )
|
||||
);
|
||||
return new SourceMethod.Builder()
|
||||
.setExecutable( method )
|
||||
.setParameters( parameters )
|
||||
.setReturnType( returnType )
|
||||
.setExceptionTypes( exceptionTypes )
|
||||
.setMappings( getMappings( method) )
|
||||
.setIterableMapping( IterableMapping.fromPrism(
|
||||
IterableMappingPrism.getInstanceOn( method ), method, messager ) )
|
||||
.setMapMapping(
|
||||
MapMapping.fromPrism( MapMappingPrism.getInstanceOn( method ), method, messager ) )
|
||||
.setTypeUtils( typeUtils )
|
||||
.setMessager( messager )
|
||||
.setTypeFactory( typeFactory )
|
||||
.setMapperConfig( MapperConfig.getInstanceOn( mapperToImplement ) )
|
||||
.createSourceMethod();
|
||||
}
|
||||
|
||||
private SourceMethod getReferencedMethod(TypeElement usedMapper, ExecutableType methodType,
|
||||
@ -195,17 +197,16 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
|
||||
return null;
|
||||
}
|
||||
|
||||
return SourceMethod.forReferencedMethod(
|
||||
usedMapper.equals( mapperToImplement ) ? null : usedMapperAsType,
|
||||
method,
|
||||
parameters,
|
||||
returnType,
|
||||
exceptionTypes,
|
||||
typeUtils
|
||||
);
|
||||
return new SourceMethod.Builder()
|
||||
.setDeclaringMapper( usedMapper.equals( mapperToImplement ) ? null : usedMapperAsType )
|
||||
.setExecutable( method )
|
||||
.setParameters( parameters )
|
||||
.setReturnType( returnType )
|
||||
.setExceptionTypes( exceptionTypes )
|
||||
.setTypeUtils( typeUtils )
|
||||
.createSourceMethod();
|
||||
}
|
||||
|
||||
|
||||
private boolean isValidReferencedMethod(List<Parameter> parameters) {
|
||||
return isValidReferencedOrFactoryMethod( 1, parameters );
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user