use of org.mapstruct.ap.internal.model.source.MappingMethodOptions in project mapstruct by mapstruct.
the class MapperCreationProcessor method mergeInheritedOptions.
private void mergeInheritedOptions(SourceMethod method, MapperOptions mapperConfig, List<SourceMethod> availableMethods, List<SourceMethod> initializingMethods, AnnotationMirror annotationMirror) {
if (initializingMethods.contains(method)) {
// cycle detected
initializingMethods.add(method);
messager.printMessage(method.getExecutable(), Message.INHERITCONFIGURATION_CYCLE, Strings.join(initializingMethods, " -> "));
return;
}
initializingMethods.add(method);
MappingMethodOptions mappingOptions = method.getOptions();
List<SourceMethod> applicableReversePrototypeMethods = method.getApplicableReversePrototypeMethods();
SourceMethod inverseTemplateMethod = getInverseTemplateMethod(join(availableMethods, applicableReversePrototypeMethods), method, initializingMethods, mapperConfig);
List<SourceMethod> applicablePrototypeMethods = method.getApplicablePrototypeMethods();
SourceMethod forwardTemplateMethod = getForwardTemplateMethod(join(availableMethods, applicablePrototypeMethods), method, initializingMethods, mapperConfig);
// apply defined (@InheritConfiguration, @InheritInverseConfiguration) mappings
if (forwardTemplateMethod != null) {
mappingOptions.applyInheritedOptions(method, forwardTemplateMethod, false, annotationMirror);
}
if (inverseTemplateMethod != null) {
mappingOptions.applyInheritedOptions(method, inverseTemplateMethod, true, annotationMirror);
}
// apply auto inherited options
MappingInheritanceStrategyGem inheritanceStrategy = mapperConfig.getMappingInheritanceStrategy();
if (inheritanceStrategy.isAutoInherit()) {
// but.. there should not be an @InheritedConfiguration
if (forwardTemplateMethod == null && inheritanceStrategy.isApplyForward()) {
if (applicablePrototypeMethods.size() == 1) {
mappingOptions.applyInheritedOptions(method, first(applicablePrototypeMethods), false, annotationMirror);
} else if (applicablePrototypeMethods.size() > 1) {
messager.printMessage(method.getExecutable(), Message.INHERITCONFIGURATION_MULTIPLE_PROTOTYPE_METHODS_MATCH, Strings.join(applicablePrototypeMethods, ", "));
}
}
// or no @InheritInverseConfiguration
if (inverseTemplateMethod == null && inheritanceStrategy.isApplyReverse()) {
if (applicableReversePrototypeMethods.size() == 1) {
mappingOptions.applyInheritedOptions(method, first(applicableReversePrototypeMethods), true, annotationMirror);
} else if (applicableReversePrototypeMethods.size() > 1) {
messager.printMessage(method.getExecutable(), Message.INHERITINVERSECONFIGURATION_MULTIPLE_PROTOTYPE_METHODS_MATCH, Strings.join(applicableReversePrototypeMethods, ", "));
}
}
}
// @BeanMapping( ignoreByDefault = true )
if (mappingOptions.getBeanMapping() != null && mappingOptions.getBeanMapping().isignoreByDefault()) {
mappingOptions.applyIgnoreAll(method, typeFactory, mappingContext.getMessager());
}
mappingOptions.markAsFullyInitialized();
}
use of org.mapstruct.ap.internal.model.source.MappingMethodOptions in project mapstruct by mapstruct.
the class MapperCreationProcessor method getMappingMethods.
private List<MappingMethod> getMappingMethods(MapperOptions mapperAnnotation, List<SourceMethod> methods) {
List<MappingMethod> mappingMethods = new ArrayList<>();
for (SourceMethod method : methods) {
if (!method.overridesMethod()) {
continue;
}
mergeInheritedOptions(method, mapperAnnotation, methods, new ArrayList<>(), null);
MappingMethodOptions mappingOptions = method.getOptions();
boolean hasFactoryMethod = false;
if (method.isIterableMapping()) {
this.messager.note(1, Message.ITERABLEMAPPING_CREATE_NOTE, method);
IterableMappingMethod iterableMappingMethod = createWithElementMappingMethod(method, mappingOptions, new IterableMappingMethod.Builder());
hasFactoryMethod = iterableMappingMethod.getFactoryMethod() != null;
mappingMethods.add(iterableMappingMethod);
} else if (method.isMapMapping()) {
MapMappingMethod.Builder builder = new MapMappingMethod.Builder();
SelectionParameters keySelectionParameters = null;
FormattingParameters keyFormattingParameters = null;
SelectionParameters valueSelectionParameters = null;
FormattingParameters valueFormattingParameters = null;
NullValueMappingStrategyGem nullValueMappingStrategy = null;
if (mappingOptions.getMapMapping() != null) {
keySelectionParameters = mappingOptions.getMapMapping().getKeySelectionParameters();
keyFormattingParameters = mappingOptions.getMapMapping().getKeyFormattingParameters();
valueSelectionParameters = mappingOptions.getMapMapping().getValueSelectionParameters();
valueFormattingParameters = mappingOptions.getMapMapping().getValueFormattingParameters();
nullValueMappingStrategy = mappingOptions.getMapMapping().getNullValueMappingStrategy();
}
this.messager.note(1, Message.MAPMAPPING_CREATE_NOTE, method);
MapMappingMethod mapMappingMethod = builder.mappingContext(mappingContext).method(method).keyFormattingParameters(keyFormattingParameters).keySelectionParameters(keySelectionParameters).valueFormattingParameters(valueFormattingParameters).valueSelectionParameters(valueSelectionParameters).build();
hasFactoryMethod = mapMappingMethod.getFactoryMethod() != null;
mappingMethods.add(mapMappingMethod);
} else if (method.isValueMapping()) {
// prefer value mappings over enum mapping
this.messager.note(1, Message.VALUEMAPPING_CREATE_NOTE, method);
ValueMappingMethod valueMappingMethod = new ValueMappingMethod.Builder().mappingContext(mappingContext).method(method).valueMappings(mappingOptions.getValueMappings()).enumMapping(mappingOptions.getEnumMappingOptions()).build();
if (valueMappingMethod != null) {
mappingMethods.add(valueMappingMethod);
}
} else if (method.isRemovedEnumMapping()) {
messager.printMessage(method.getExecutable(), Message.ENUMMAPPING_REMOVED);
} else if (method.isStreamMapping()) {
this.messager.note(1, Message.STREAMMAPPING_CREATE_NOTE, method);
StreamMappingMethod streamMappingMethod = createWithElementMappingMethod(method, mappingOptions, new StreamMappingMethod.Builder());
// If we do StreamMapping that means that internally there is a way to generate the result type
hasFactoryMethod = streamMappingMethod.getFactoryMethod() != null || method.getResultType().isStreamType();
mappingMethods.add(streamMappingMethod);
} else {
this.messager.note(1, Message.BEANMAPPING_CREATE_NOTE, method);
BuilderGem builder = method.getOptions().getBeanMapping().getBuilder();
Type userDefinedReturnType = getUserDesiredReturnType(method);
Type builderBaseType = userDefinedReturnType != null ? userDefinedReturnType : method.getReturnType();
BeanMappingMethod.Builder beanMappingBuilder = new BeanMappingMethod.Builder();
BeanMappingMethod beanMappingMethod = beanMappingBuilder.mappingContext(mappingContext).sourceMethod(method).userDefinedReturnType(userDefinedReturnType).returnTypeBuilder(typeFactory.builderTypeFor(builderBaseType, builder)).build();
// We can consider that the bean mapping method can always be constructed. If there is a problem
// it would have been reported in its build
hasFactoryMethod = true;
if (beanMappingMethod != null) {
mappingMethods.add(beanMappingMethod);
}
}
if (!hasFactoryMethod) {
// A factory method is allowed to return an interface type and hence, the generated
// implementation as well. The check below must only be executed if there's no factory
// method that could be responsible.
reportErrorIfNoImplementationTypeIsRegisteredForInterfaceReturnType(method);
}
}
return mappingMethods;
}
Aggregations