#429 Using Builder Pattern for SourceMethod

This commit is contained in:
sjaakd 2014-12-25 20:20:35 +01:00
parent 08ac33e3b1
commit d3de4991c9
2 changed files with 120 additions and 91 deletions

View File

@ -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,21 +65,87 @@ 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) {
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( SourceMethod sourceMethod = new SourceMethod(
null, declaringMapper,
executable, executable,
parameters, parameters,
returnType, returnType,
@ -91,55 +156,18 @@ public class SourceMethod implements Method {
typeUtils, typeUtils,
typeFactory, typeFactory,
messager, messager,
config mapperConfig
); );
if ( mappings != null ) {
for ( Map.Entry<String, List<Mapping>> entry : sourceMethod.getMappings().entrySet() ) { for ( Map.Entry<String, List<Mapping>> entry : sourceMethod.getMappings().entrySet() ) {
for ( Mapping mapping : entry.getValue() ) { for ( Mapping mapping : entry.getValue() ) {
mapping.init( sourceMethod, messager, typeFactory ); mapping.init( sourceMethod, messager, typeFactory );
} }
} }
}
return sourceMethod; 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 //CHECKSTYLE:OFF

View File

@ -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 );
} }