use of org.mapstruct.ap.internal.model.source.MappingOptions in project mapstruct by mapstruct.
the class MapperCreationProcessor method getMappingMethods.
private List<MappingMethod> getMappingMethods(MapperConfiguration mapperConfig, List<SourceMethod> methods) {
List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>();
for (SourceMethod method : methods) {
if (!method.overridesMethod()) {
continue;
}
mergeInheritedOptions(method, mapperConfig, methods, new ArrayList<SourceMethod>());
MappingOptions mappingOptions = method.getMappingOptions();
boolean hasFactoryMethod = false;
if (method.isIterableMapping()) {
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;
NullValueMappingStrategyPrism 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();
}
MapMappingMethod mapMappingMethod = builder.mappingContext(mappingContext).method(method).keyFormattingParameters(keyFormattingParameters).keySelectionParameters(keySelectionParameters).valueFormattingParameters(valueFormattingParameters).valueSelectionParameters(valueSelectionParameters).nullValueMappingStrategy(nullValueMappingStrategy).build();
hasFactoryMethod = mapMappingMethod.getFactoryMethod() != null;
mappingMethods.add(mapMappingMethod);
} else if (method.isValueMapping()) {
// prefer value mappings over enum mapping
ValueMappingMethod valueMappingMethod = new ValueMappingMethod.Builder().mappingContext(mappingContext).method(method).valueMappings(mappingOptions.getValueMappings()).build();
mappingMethods.add(valueMappingMethod);
} else if (method.isEnumMapping()) {
messager.printMessage(method.getExecutable(), Message.ENUMMAPPING_DEPRECATED);
EnumMappingMethod.Builder builder = new EnumMappingMethod.Builder();
MappingMethod enumMappingMethod = builder.mappingContext(mappingContext).souceMethod(method).build();
if (enumMappingMethod != null) {
mappingMethods.add(enumMappingMethod);
}
} else if (method.isStreamMapping()) {
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 {
NullValueMappingStrategyPrism nullValueMappingStrategy = null;
SelectionParameters selectionParameters = null;
if (mappingOptions.getBeanMapping() != null) {
nullValueMappingStrategy = mappingOptions.getBeanMapping().getNullValueMappingStrategy();
selectionParameters = mappingOptions.getBeanMapping().getSelectionParameters();
}
BeanMappingMethod.Builder builder = new BeanMappingMethod.Builder();
BeanMappingMethod beanMappingMethod = builder.mappingContext(mappingContext).souceMethod(method).nullValueMappingStrategy(nullValueMappingStrategy).selectionParameters(selectionParameters).build();
if (beanMappingMethod != null) {
// 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;
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;
}
use of org.mapstruct.ap.internal.model.source.MappingOptions in project mapstruct by mapstruct.
the class MapperCreationProcessor method mergeInheritedOptions.
private void mergeInheritedOptions(SourceMethod method, MapperConfiguration mapperConfig, List<SourceMethod> availableMethods, List<SourceMethod> initializingMethods) {
if (initializingMethods.contains(method)) {
// cycle detected
initializingMethods.add(method);
messager.printMessage(method.getExecutable(), Message.INHERITCONFIGURATION_CYCLE, Strings.join(initializingMethods, " -> "));
return;
}
initializingMethods.add(method);
MappingOptions mappingOptions = method.getMappingOptions();
List<SourceMethod> applicableReversePrototypeMethods = method.getApplicableReversePrototypeMethods();
MappingOptions inverseMappingOptions = getInverseMappingOptions(join(availableMethods, applicableReversePrototypeMethods), method, initializingMethods, mapperConfig);
List<SourceMethod> applicablePrototypeMethods = method.getApplicablePrototypeMethods();
MappingOptions forwardMappingOptions = getTemplateMappingOptions(join(availableMethods, applicablePrototypeMethods), method, initializingMethods, mapperConfig);
// apply defined (@InheritConfiguration, @InheritInverseConfiguration) mappings
if (forwardMappingOptions != null) {
mappingOptions.applyInheritedOptions(forwardMappingOptions, false, method, messager, typeFactory);
}
if (inverseMappingOptions != null) {
mappingOptions.applyInheritedOptions(inverseMappingOptions, true, method, messager, typeFactory);
}
// apply auto inherited options
MappingInheritanceStrategyPrism inheritanceStrategy = mapperConfig.getMappingInheritanceStrategy();
if (inheritanceStrategy.isAutoInherit()) {
// but.. there should not be an @InheritedConfiguration
if (forwardMappingOptions == null && inheritanceStrategy.isApplyForward()) {
if (applicablePrototypeMethods.size() == 1) {
mappingOptions.applyInheritedOptions(first(applicablePrototypeMethods).getMappingOptions(), false, method, messager, typeFactory);
} else if (applicablePrototypeMethods.size() > 1) {
messager.printMessage(method.getExecutable(), Message.INHERITCONFIGURATION_MULTIPLE_PROTOTYPE_METHODS_MATCH, Strings.join(applicablePrototypeMethods, ", "));
}
}
// or no @InheritInverseConfiguration
if (inverseMappingOptions == null && inheritanceStrategy.isApplyReverse()) {
if (applicableReversePrototypeMethods.size() == 1) {
mappingOptions.applyInheritedOptions(first(applicableReversePrototypeMethods).getMappingOptions(), true, method, messager, typeFactory);
} else if (applicableReversePrototypeMethods.size() > 1) {
messager.printMessage(method.getExecutable(), Message.INHERITINVERSECONFIGURATION_MULTIPLE_PROTOTYPE_METHODS_MATCH, Strings.join(applicableReversePrototypeMethods, ", "));
}
}
}
mappingOptions.markAsFullyInitialized();
}
Aggregations