diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java b/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java index f77a13a75..ffbf2f285 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java @@ -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,84 +65,113 @@ public class SourceMethod implements Method { private IterableMapping iterableMapping; private MapMapping mapMapping; - //CHECKSTYLE:OFF - // TODO use builder - public static SourceMethod forMethodRequiringImplementation(ExecutableElement executable, - List parameters, - Type returnType, - List exceptionTypes, - Map> mappings, - IterableMapping iterableMapping, MapMapping mapMapping, - Types typeUtils, - Messager messager, - TypeFactory typeFactory, - MapperConfig config) { + public static class Builder { - SourceMethod sourceMethod = new SourceMethod( - null, - executable, - parameters, - returnType, - exceptionTypes, - mappings, - iterableMapping, - mapMapping, - typeUtils, - typeFactory, - messager, - config - ); + private Type declaringMapper = null; + private ExecutableElement executable; + private List parameters; + private Type returnType = null; + private List exceptionTypes; + private Map> mappings; + private IterableMapping iterableMapping = null; + private MapMapping mapMapping = null; + private Types typeUtils; + private TypeFactory typeFactory = null; + private Messager messager = null; + private MapperConfig mapperConfig = null; - for ( Map.Entry> entry : sourceMethod.getMappings().entrySet() ) { - for ( Mapping mapping : entry.getValue() ) { - mapping.init( sourceMethod, messager, typeFactory ); - } + public Builder() { } - return sourceMethod; - } - //CHECKSTYLE:ON - public static SourceMethod forReferencedMethod(Type declaringMapper, ExecutableElement executable, - List parameters, Type returnType, - List exceptionTypes, Types typeUtils) { + public Builder setDeclaringMapper(Type declaringMapper) { + this.declaringMapper = declaringMapper; + return this; + } - return new SourceMethod( - declaringMapper, - executable, - parameters, - returnType, - exceptionTypes, - Collections.>emptyMap(), - null, - null, - typeUtils, - null, - null, - null - ); - } + public Builder setExecutable(ExecutableElement executable) { + this.executable = executable; + return this; + } - public static SourceMethod forFactoryMethod(Type declaringMapper, ExecutableElement executable, Type returnType, - List exceptionTypes, Types typeUtils) { + public Builder setParameters(List parameters) { + this.parameters = parameters; + return this; + } - return new SourceMethod( - declaringMapper, - executable, - Collections.emptyList(), - returnType, - exceptionTypes, - Collections.>emptyMap(), - null, - null, - typeUtils, - null, - null, - null - ); + public Builder setReturnType(Type returnType) { + this.returnType = returnType; + return this; + } + + public Builder setExceptionTypes(List exceptionTypes) { + this.exceptionTypes = exceptionTypes; + return this; + } + + public Builder setMappings(Map> 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( + declaringMapper, + executable, + parameters, + returnType, + exceptionTypes, + mappings, + iterableMapping, + mapMapping, + typeUtils, + typeFactory, + messager, + mapperConfig + ); + + if ( mappings != null ) { + for ( Map.Entry> entry : sourceMethod.getMappings().entrySet() ) { + for ( Mapping mapping : entry.getValue() ) { + mapping.init( sourceMethod, messager, typeFactory ); + } + } + } + return sourceMethod; + } } //CHECKSTYLE:OFF - private SourceMethod(Type declaringMapper, ExecutableElement executable, List parameters, + private SourceMethod( Type declaringMapper, ExecutableElement executable, List parameters, Type returnType, List exceptionTypes, Map> mappings, IterableMapping iterableMapping, MapMapping mapMapping, Types typeUtils, TypeFactory typeFactory, Messager messager, MapperConfig config) { diff --git a/processor/src/main/java/org/mapstruct/ap/processor/MethodRetrievalProcessor.java b/processor/src/main/java/org/mapstruct/ap/processor/MethodRetrievalProcessor.java index ac7824c51..e52931edc 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MethodRetrievalProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MethodRetrievalProcessor.java @@ -168,19 +168,21 @@ public class MethodRetrievalProcessor implements ModelElementProcessor parameters) { return isValidReferencedOrFactoryMethod( 1, parameters ); }