Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.util.Strings;

import java.util.ArrayList;
import java.util.List;

/**
* An abstract builder that can be reused for building {@link MappingMethod}(s).
*
Expand Down Expand Up @@ -117,4 +120,15 @@ public ForgedMethodHistory getDescription() {
return description;
}

public List<Annotation> getMethodAnnotations() {
AdditionalAnnotationsBuilder additionalAnnotationsBuilder =
new AdditionalAnnotationsBuilder(
ctx.getElementUtils(),
ctx.getTypeFactory(),
ctx.getMessager() );
List<Annotation> annotations = new ArrayList<>();
annotations.addAll( additionalAnnotationsBuilder.getProcessedAnnotations( method.getExecutable() ) );
return annotations;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
*/
public class BeanMappingMethod extends NormalTypeMappingMethod {

private final List<Annotation> annotations;
private final List<PropertyMapping> propertyMappings;
private final Map<String, List<PropertyMapping>> mappingsByParameter;
private final Map<String, List<PropertyMapping>> constructorMappingsByParameter;
Expand Down Expand Up @@ -113,7 +112,6 @@ public static class Builder extends AbstractMappingMethodBuilder<Builder, BeanMa
private final Set<Parameter> unprocessedSourceParameters = new HashSet<>();
private final Set<String> existingVariableNames = new HashSet<>();
private final Map<String, Set<MappingReference>> unprocessedDefinedTargets = new LinkedHashMap<>();
private final List<Annotation> annotations = new ArrayList<>();

private MappingReferences mappingReferences;
private MethodReference factoryMethod;
Expand Down Expand Up @@ -216,12 +214,6 @@ else if ( !method.isUpdateMethod() ) {
// If the return type cannot be constructed then no need to try to create mappings
return null;
}
AdditionalAnnotationsBuilder additionalAnnotationsBuilder =
new AdditionalAnnotationsBuilder(
ctx.getElementUtils(),
ctx.getTypeFactory(),
ctx.getMessager() );
annotations.addAll( additionalAnnotationsBuilder.getProcessedAnnotations( method.getExecutable() ) );

/* the type that needs to be used in the mapping process as target */
Type resultTypeToMap = returnTypeToConstruct == null ? method.getResultType() : returnTypeToConstruct;
Expand Down Expand Up @@ -370,7 +362,7 @@ else if ( !method.isUpdateMethod() ) {

return new BeanMappingMethod(
method,
annotations,
getMethodAnnotations(),
existingVariableNames,
propertyMappings,
factoryMethod,
Expand Down Expand Up @@ -1724,6 +1716,7 @@ private BeanMappingMethod(Method method,
List<SubclassMapping> subclassMappings) {
super(
method,
annotations,
existingVariableNames,
factoryMethod,
mapNullToDefault,
Expand All @@ -1732,7 +1725,6 @@ private BeanMappingMethod(Method method,
);
//CHECKSTYLE:ON

this.annotations = annotations;
this.propertyMappings = propertyMappings;
this.returnTypeBuilder = returnTypeBuilder;
this.finalizerMethod = finalizerMethod;
Expand Down Expand Up @@ -1771,10 +1763,6 @@ else if ( sourceParameterNames.contains( mapping.getSourceBeanName() ) ) {
this.subclassMappings = subclassMappings;
}

public List<Annotation> getAnnotations() {
return annotations;
}

public List<PropertyMapping> getConstantMappings() {
return constantMappings;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ public abstract class ContainerMappingMethod extends NormalTypeMappingMethod {
private final String index2Name;
private IterableCreation iterableCreation;

ContainerMappingMethod(Method method, Collection<String> existingVariables, Assignment parameterAssignment,
ContainerMappingMethod(Method method, List<Annotation> annotations,
Collection<String> existingVariables, Assignment parameterAssignment,
MethodReference factoryMethod, boolean mapNullToDefault, String loopVariableName,
List<LifecycleCallbackMethodReference> beforeMappingReferences,
List<LifecycleCallbackMethodReference> afterMappingReferences,
SelectionParameters selectionParameters) {
super( method, existingVariables, factoryMethod, mapNullToDefault, beforeMappingReferences,
super( method, annotations, existingVariables, factoryMethod, mapNullToDefault, beforeMappingReferences,
afterMappingReferences );
this.elementAssignment = parameterAssignment;
this.loopVariableName = loopVariableName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ protected IterableMappingMethod instantiateMappingMethod(Method method, Collecti
List<LifecycleCallbackMethodReference> afterMappingMethods, SelectionParameters selectionParameters) {
return new IterableMappingMethod(
method,
getMethodAnnotations(),
existingVariables,
assignment,
factoryMethod,
Expand All @@ -69,13 +70,15 @@ protected IterableMappingMethod instantiateMappingMethod(Method method, Collecti
}
}

private IterableMappingMethod(Method method, Collection<String> existingVariables, Assignment parameterAssignment,
private IterableMappingMethod(Method method, List<Annotation> annotations,
Collection<String> existingVariables, Assignment parameterAssignment,
MethodReference factoryMethod, boolean mapNullToDefault, String loopVariableName,
List<LifecycleCallbackMethodReference> beforeMappingReferences,
List<LifecycleCallbackMethodReference> afterMappingReferences,
SelectionParameters selectionParameters) {
super(
method,
annotations,
existingVariables,
parameterAssignment,
factoryMethod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public MapMappingMethod build() {

return new MapMappingMethod(
method,
getMethodAnnotations(),
existingVariables,
keyAssignment,
valueAssignment,
Expand All @@ -224,11 +225,12 @@ protected boolean shouldUsePropertyNamesInHistory() {

}

private MapMappingMethod(Method method, Collection<String> existingVariableNames, Assignment keyAssignment,
private MapMappingMethod(Method method, List<Annotation> annotations,
Collection<String> existingVariableNames, Assignment keyAssignment,
Assignment valueAssignment, MethodReference factoryMethod, boolean mapNullToDefault,
List<LifecycleCallbackMethodReference> beforeMappingReferences,
List<LifecycleCallbackMethodReference> afterMappingReferences) {
super( method, existingVariableNames, factoryMethod, mapNullToDefault, beforeMappingReferences,
super( method, annotations, existingVariableNames, factoryMethod, mapNullToDefault, beforeMappingReferences,
afterMappingReferences );

this.keyAssignment = keyAssignment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ public abstract class NormalTypeMappingMethod extends MappingMethod {
private final boolean overridden;
private final boolean mapNullToDefault;

NormalTypeMappingMethod(Method method, Collection<String> existingVariableNames, MethodReference factoryMethod,
private final List<Annotation> annotations;

NormalTypeMappingMethod(Method method, List<Annotation> annotations,
Collection<String> existingVariableNames, MethodReference factoryMethod,
boolean mapNullToDefault,
List<LifecycleCallbackMethodReference> beforeMappingReferences,
List<LifecycleCallbackMethodReference> afterMappingReferences) {
super( method, existingVariableNames, beforeMappingReferences, afterMappingReferences );
this.factoryMethod = factoryMethod;
this.overridden = method.overridesMethod();
this.mapNullToDefault = mapNullToDefault;
this.annotations = annotations;
}

@Override
Expand All @@ -45,6 +49,9 @@ public Set<Type> getImportTypes() {
else if ( factoryMethod != null ) {
types.addAll( factoryMethod.getImportTypes() );
}
for ( Annotation annotation : annotations ) {
types.addAll( annotation.getImportTypes() );
}
return types;
}

Expand All @@ -60,6 +67,10 @@ public MethodReference getFactoryMethod() {
return this.factoryMethod;
}

public List<Annotation> getAnnotations() {
return annotations;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
*/
package org.mapstruct.ap.internal.model;

import org.mapstruct.ap.internal.model.assignment.Java8FunctionWrapper;
import org.mapstruct.ap.internal.model.common.Assignment;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.model.source.SelectionParameters;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
Expand All @@ -13,12 +19,6 @@
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.mapstruct.ap.internal.model.assignment.Java8FunctionWrapper;
import org.mapstruct.ap.internal.model.common.Assignment;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.model.source.SelectionParameters;

import static org.mapstruct.ap.internal.util.Collections.first;

/**
Expand Down Expand Up @@ -63,9 +63,9 @@ protected StreamMappingMethod instantiateMappingMethod(Method method, Collection
sourceParameterType.isIterableType() ) {
helperImports.add( ctx.getTypeFactory().getType( StreamSupport.class ) );
}

return new StreamMappingMethod(
method,
getMethodAnnotations(),
existingVariables,
assignment,
factoryMethod,
Expand All @@ -78,14 +78,16 @@ protected StreamMappingMethod instantiateMappingMethod(Method method, Collection
);
}
}

private StreamMappingMethod(Method method, Collection<String> existingVariables, Assignment parameterAssignment,
//CHECKSTYLE:OFF
private StreamMappingMethod(Method method, List<Annotation> annotations,
Collection<String> existingVariables, Assignment parameterAssignment,
MethodReference factoryMethod, boolean mapNullToDefault, String loopVariableName,
List<LifecycleCallbackMethodReference> beforeMappingReferences,
List<LifecycleCallbackMethodReference> afterMappingReferences,
SelectionParameters selectionParameters, Set<Type> helperImports) {
super(
method,
annotations,
existingVariables,
parameterAssignment,
factoryMethod,
Expand All @@ -95,6 +97,7 @@ private StreamMappingMethod(Method method, Collection<String> existingVariables,
afterMappingReferences,
selectionParameters
);
//CHECKSTYLE:ON
this.helperImports = helperImports;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
*/
public class ValueMappingMethod extends MappingMethod {

private final List<Annotation> annotations;
private final List<MappingEntry> valueMappings;
private final MappingEntry defaultTarget;
private final MappingEntry nullTarget;
Expand Down Expand Up @@ -116,9 +117,16 @@ else if ( sourceType.isString() && targetType.isEnumType() ) {
LifecycleMethodResolver.beforeMappingMethods( method, selectionParameters, ctx, existingVariables );
List<LifecycleCallbackMethodReference> afterMappingMethods =
LifecycleMethodResolver.afterMappingMethods( method, selectionParameters, ctx, existingVariables );

AdditionalAnnotationsBuilder additionalAnnotationsBuilder =
new AdditionalAnnotationsBuilder(
ctx.getElementUtils(),
ctx.getTypeFactory(),
ctx.getMessager() );
List<Annotation> annotations = new ArrayList<>();
annotations.addAll( additionalAnnotationsBuilder.getProcessedAnnotations( method.getExecutable() ) );
// finally return a mapping
return new ValueMappingMethod( method,
annotations,
mappingEntries,
valueMappings.nullValueTarget,
valueMappings.defaultTargetValue,
Expand Down Expand Up @@ -532,6 +540,7 @@ String getValue(ValueMappingOptions valueMapping) {
}

private ValueMappingMethod(Method method,
List<Annotation> annotations,
List<MappingEntry> enumMappings,
String nullTarget,
String defaultTarget,
Expand All @@ -544,6 +553,7 @@ private ValueMappingMethod(Method method,
this.defaultTarget = new MappingEntry( null, defaultTarget != null ? defaultTarget : THROW_EXCEPTION);
this.unexpectedValueMappingException = unexpectedValueMappingException;
this.overridden = method.overridesMethod();
this.annotations = annotations;
}

@Override
Expand All @@ -556,7 +566,9 @@ public Set<Type> getImportTypes() {
importTypes.addAll( unexpectedValueMappingException.getImportTypes() );
}
}

for ( Annotation annotation : annotations ) {
importTypes.addAll( annotation.getImportTypes() );
}
return importTypes;
}

Expand Down Expand Up @@ -590,6 +602,10 @@ public boolean isOverridden() {
return overridden;
}

public List<Annotation> getAnnotations() {
return annotations;
}

public static class MappingEntry {
private final String source;
private final String target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

-->
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.IterableMappingMethod" -->
<#list annotations as annotation>
<#nt><@includeModel object=annotation/>
</#list>
<#if overridden>@Override</#if>
<#lt>${accessibility.keyword} <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>)<@throws/> {
<#list beforeMappingReferencesWithoutMappingTarget as callback>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

-->
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.MapMappingMethod" -->
<#list annotations as annotation>
<#nt><@includeModel object=annotation/>
</#list>
<#if overridden>@Override</#if>
<#lt>${accessibility.keyword} <@includeModel object=returnType /> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>)<@throws/> {
<#list beforeMappingReferencesWithoutMappingTarget as callback>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

-->
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.StreamMappingMethod" -->
<#list annotations as annotation>
<#nt><@includeModel object=annotation/>
</#list>
<#if overridden>@Override</#if>
<#lt>${accessibility.keyword} <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>)<@throws/> {
<#--TODO does it even make sense to do a callback if the result is a Stream, as they are immutable-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

-->
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.ValueMappingMethod" -->
<#list annotations as annotation>
<#nt><@includeModel object=annotation/>
</#list>
<#if overridden>@Override</#if>
<#lt>${accessibility.keyword} <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>) {
<#list beforeMappingReferencesWithoutMappingTarget as callback>
Expand Down
Loading