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;
|
package org.mapstruct.ap.model.source;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -66,84 +65,113 @@ public class SourceMethod implements Method {
|
|||||||
private IterableMapping iterableMapping;
|
private IterableMapping iterableMapping;
|
||||||
private MapMapping mapMapping;
|
private MapMapping mapMapping;
|
||||||
|
|
||||||
//CHECKSTYLE:OFF
|
public static class Builder {
|
||||||
// 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) {
|
|
||||||
|
|
||||||
SourceMethod sourceMethod = new SourceMethod(
|
private Type declaringMapper = null;
|
||||||
null,
|
private ExecutableElement executable;
|
||||||
executable,
|
private List<Parameter> parameters;
|
||||||
parameters,
|
private Type returnType = null;
|
||||||
returnType,
|
private List<Type> exceptionTypes;
|
||||||
exceptionTypes,
|
private Map<String, List<Mapping>> mappings;
|
||||||
mappings,
|
private IterableMapping iterableMapping = null;
|
||||||
iterableMapping,
|
private MapMapping mapMapping = null;
|
||||||
mapMapping,
|
private Types typeUtils;
|
||||||
typeUtils,
|
private TypeFactory typeFactory = null;
|
||||||
typeFactory,
|
private Messager messager = null;
|
||||||
messager,
|
private MapperConfig mapperConfig = null;
|
||||||
config
|
|
||||||
);
|
|
||||||
|
|
||||||
for ( Map.Entry<String, List<Mapping>> entry : sourceMethod.getMappings().entrySet() ) {
|
public Builder() {
|
||||||
for ( Mapping mapping : entry.getValue() ) {
|
|
||||||
mapping.init( sourceMethod, messager, typeFactory );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return sourceMethod;
|
|
||||||
}
|
|
||||||
//CHECKSTYLE:ON
|
|
||||||
|
|
||||||
public static SourceMethod forReferencedMethod(Type declaringMapper, ExecutableElement executable,
|
public Builder setDeclaringMapper(Type declaringMapper) {
|
||||||
List<Parameter> parameters, Type returnType,
|
this.declaringMapper = declaringMapper;
|
||||||
List<Type> exceptionTypes, Types typeUtils) {
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
return new SourceMethod(
|
public Builder setExecutable(ExecutableElement executable) {
|
||||||
declaringMapper,
|
this.executable = executable;
|
||||||
executable,
|
return this;
|
||||||
parameters,
|
}
|
||||||
returnType,
|
|
||||||
exceptionTypes,
|
|
||||||
Collections.<String, List<Mapping>>emptyMap(),
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
typeUtils,
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SourceMethod forFactoryMethod(Type declaringMapper, ExecutableElement executable, Type returnType,
|
public Builder setParameters(List<Parameter> parameters) {
|
||||||
List<Type> exceptionTypes, Types typeUtils) {
|
this.parameters = parameters;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
return new SourceMethod(
|
public Builder setReturnType(Type returnType) {
|
||||||
declaringMapper,
|
this.returnType = returnType;
|
||||||
executable,
|
return this;
|
||||||
Collections.<Parameter>emptyList(),
|
}
|
||||||
returnType,
|
|
||||||
exceptionTypes,
|
public Builder setExceptionTypes(List<Type> exceptionTypes) {
|
||||||
Collections.<String, List<Mapping>>emptyMap(),
|
this.exceptionTypes = exceptionTypes;
|
||||||
null,
|
return this;
|
||||||
null,
|
}
|
||||||
typeUtils,
|
|
||||||
null,
|
public Builder setMappings(Map<String, List<Mapping>> mappings) {
|
||||||
null,
|
this.mappings = mappings;
|
||||||
null
|
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(
|
||||||
|
declaringMapper,
|
||||||
|
executable,
|
||||||
|
parameters,
|
||||||
|
returnType,
|
||||||
|
exceptionTypes,
|
||||||
|
mappings,
|
||||||
|
iterableMapping,
|
||||||
|
mapMapping,
|
||||||
|
typeUtils,
|
||||||
|
typeFactory,
|
||||||
|
messager,
|
||||||
|
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:OFF
|
//CHECKSTYLE:OFF
|
||||||
private SourceMethod(Type declaringMapper, ExecutableElement executable, List<Parameter> parameters,
|
private SourceMethod( Type declaringMapper, ExecutableElement executable, List<Parameter> parameters,
|
||||||
Type returnType, List<Type> exceptionTypes, Map<String, List<Mapping>> mappings,
|
Type returnType, List<Type> exceptionTypes, Map<String, List<Mapping>> mappings,
|
||||||
IterableMapping iterableMapping, MapMapping mapMapping, Types typeUtils,
|
IterableMapping iterableMapping, MapMapping mapMapping, Types typeUtils,
|
||||||
TypeFactory typeFactory, Messager messager, MapperConfig config) {
|
TypeFactory typeFactory, Messager messager, MapperConfig config) {
|
||||||
|
@ -168,19 +168,21 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return SourceMethod.forMethodRequiringImplementation(
|
return new SourceMethod.Builder()
|
||||||
method,
|
.setExecutable( method )
|
||||||
parameters,
|
.setParameters( parameters )
|
||||||
returnType,
|
.setReturnType( returnType )
|
||||||
exceptionTypes,
|
.setExceptionTypes( exceptionTypes )
|
||||||
getMappings( method ),
|
.setMappings( getMappings( method) )
|
||||||
IterableMapping.fromPrism( IterableMappingPrism.getInstanceOn( method ), method, messager ),
|
.setIterableMapping( IterableMapping.fromPrism(
|
||||||
MapMapping.fromPrism( MapMappingPrism.getInstanceOn( method ), method, messager ),
|
IterableMappingPrism.getInstanceOn( method ), method, messager ) )
|
||||||
typeUtils,
|
.setMapMapping(
|
||||||
messager,
|
MapMapping.fromPrism( MapMappingPrism.getInstanceOn( method ), method, messager ) )
|
||||||
typeFactory,
|
.setTypeUtils( typeUtils )
|
||||||
MapperConfig.getInstanceOn( mapperToImplement )
|
.setMessager( messager )
|
||||||
);
|
.setTypeFactory( typeFactory )
|
||||||
|
.setMapperConfig( MapperConfig.getInstanceOn( mapperToImplement ) )
|
||||||
|
.createSourceMethod();
|
||||||
}
|
}
|
||||||
|
|
||||||
private SourceMethod getReferencedMethod(TypeElement usedMapper, ExecutableType methodType,
|
private SourceMethod getReferencedMethod(TypeElement usedMapper, ExecutableType methodType,
|
||||||
@ -195,17 +197,16 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return SourceMethod.forReferencedMethod(
|
return new SourceMethod.Builder()
|
||||||
usedMapper.equals( mapperToImplement ) ? null : usedMapperAsType,
|
.setDeclaringMapper( usedMapper.equals( mapperToImplement ) ? null : usedMapperAsType )
|
||||||
method,
|
.setExecutable( method )
|
||||||
parameters,
|
.setParameters( parameters )
|
||||||
returnType,
|
.setReturnType( returnType )
|
||||||
exceptionTypes,
|
.setExceptionTypes( exceptionTypes )
|
||||||
typeUtils
|
.setTypeUtils( typeUtils )
|
||||||
);
|
.createSourceMethod();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private boolean isValidReferencedMethod(List<Parameter> parameters) {
|
private boolean isValidReferencedMethod(List<Parameter> parameters) {
|
||||||
return isValidReferencedOrFactoryMethod( 1, parameters );
|
return isValidReferencedOrFactoryMethod( 1, parameters );
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user