mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#35 Adding processor option for setting default target mapping policy
This commit is contained in:
parent
04beaacbe1
commit
522c9e851a
@ -141,7 +141,7 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
||||
//2.) build up aggregated "target" model
|
||||
List<BeanMapping> mappings = getMappings(
|
||||
methods,
|
||||
ReportingPolicy.valueOf( MapperPrism.getInstanceOn( element ).unmappedTargetPolicy() )
|
||||
getEffectiveUnmappedTargetPolicy( element )
|
||||
);
|
||||
List<Type> usedMapperTypes = getUsedMapperTypes( element );
|
||||
|
||||
@ -157,6 +157,31 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
||||
return mapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the effective policy for reporting unmapped target properties. If
|
||||
* explicitly set via {@code Mapper}, this value will be returned. Otherwise
|
||||
* the value from the corresponding processor option will be returned. If
|
||||
* that is not set either, the default value from
|
||||
* {@code Mapper#unmappedTargetPolicy()} will be returned.
|
||||
*
|
||||
* @param element The type declaring the generated mapper type
|
||||
*
|
||||
* @return The effective policy for reporting unmapped target properties.
|
||||
*/
|
||||
private ReportingPolicy getEffectiveUnmappedTargetPolicy(TypeElement element) {
|
||||
MapperPrism mapperPrism = MapperPrism.getInstanceOn( element );
|
||||
boolean setViaAnnotation = mapperPrism.values.unmappedTargetPolicy() != null;
|
||||
ReportingPolicy annotationValue = ReportingPolicy.valueOf( mapperPrism.unmappedTargetPolicy() );
|
||||
|
||||
if ( setViaAnnotation ||
|
||||
options.getUnmappedTargetPolicy() == null ) {
|
||||
return annotationValue;
|
||||
}
|
||||
else {
|
||||
return options.getUnmappedTargetPolicy();
|
||||
}
|
||||
}
|
||||
|
||||
private List<BeanMapping> getMappings(List<Method> methods,
|
||||
ReportingPolicy unmappedTargetPolicy) {
|
||||
Conversions conversions = new Conversions( elementUtils, typeUtils, typeUtil );
|
||||
|
@ -35,6 +35,7 @@ import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.ap.model.Options;
|
||||
import org.mapstruct.ap.model.ReportingPolicy;
|
||||
|
||||
@SupportedAnnotationTypes("org.mapstruct.Mapper")
|
||||
@GeneratePrisms({
|
||||
@ -42,7 +43,7 @@ import org.mapstruct.ap.model.Options;
|
||||
@GeneratePrism(value = Mapping.class, publicAccess = true),
|
||||
@GeneratePrism(value = Mappings.class, publicAccess = true)
|
||||
})
|
||||
@SupportedOptions(MappingProcessor.SUPPRESS_GENERATOR_TIMESTAMP)
|
||||
@SupportedOptions({ MappingProcessor.SUPPRESS_GENERATOR_TIMESTAMP, MappingProcessor.UNMAPPED_TARGET_POLICY })
|
||||
public class MappingProcessor extends AbstractProcessor {
|
||||
|
||||
/**
|
||||
@ -51,6 +52,7 @@ public class MappingProcessor extends AbstractProcessor {
|
||||
private static final boolean ANNOTATIONS_CLAIMED_EXCLUSIVELY = false;
|
||||
|
||||
protected static final String SUPPRESS_GENERATOR_TIMESTAMP = "suppressGeneratorTimestamp";
|
||||
protected static final String UNMAPPED_TARGET_POLICY = "unmappedTargetPolicy";
|
||||
|
||||
private Options options;
|
||||
|
||||
@ -88,6 +90,11 @@ public class MappingProcessor extends AbstractProcessor {
|
||||
}
|
||||
|
||||
private Options createOptions() {
|
||||
return new Options( Boolean.valueOf( processingEnv.getOptions().get( SUPPRESS_GENERATOR_TIMESTAMP ) ) );
|
||||
String unmappedTargetPolicy = processingEnv.getOptions().get( UNMAPPED_TARGET_POLICY );
|
||||
|
||||
return new Options(
|
||||
Boolean.valueOf( processingEnv.getOptions().get( SUPPRESS_GENERATOR_TIMESTAMP ) ),
|
||||
unmappedTargetPolicy != null ? ReportingPolicy.valueOf( unmappedTargetPolicy ) : null
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -22,15 +22,22 @@ package org.mapstruct.ap.model;
|
||||
* The options passed to the code generator.
|
||||
*
|
||||
* @author Andreas Gudian
|
||||
* @autor Gunnar Morling
|
||||
*/
|
||||
public class Options {
|
||||
private final boolean suppressGeneratorTimestamp;
|
||||
private final ReportingPolicy unmappedTargetPolicy;
|
||||
|
||||
public Options(boolean suppressGeneratorTimestamp) {
|
||||
public Options(boolean suppressGeneratorTimestamp, ReportingPolicy unmappedTargetPolicy) {
|
||||
this.suppressGeneratorTimestamp = suppressGeneratorTimestamp;
|
||||
this.unmappedTargetPolicy = unmappedTargetPolicy;
|
||||
}
|
||||
|
||||
public boolean isSuppressGeneratorTimestamp() {
|
||||
return suppressGeneratorTimestamp;
|
||||
}
|
||||
|
||||
public ReportingPolicy getUnmappedTargetPolicy() {
|
||||
return unmappedTargetPolicy;
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
|
||||
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
|
||||
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
|
||||
import org.mapstruct.ap.testutil.compilation.annotation.ProcessorOption;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
@ -81,4 +82,23 @@ public class UnmappedTargetTest extends MapperTestBase {
|
||||
)
|
||||
public void shouldRaiseErrorDueToUnsetTargetProperty() {
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses({ Source.class, Target.class, SourceTargetMapper.class })
|
||||
@ProcessorOption(name = "unmappedTargetPolicy", value = "ERROR")
|
||||
@ExpectedCompilationOutcome(
|
||||
value = CompilationResult.FAILED,
|
||||
diagnostics = {
|
||||
@Diagnostic(type = SourceTargetMapper.class,
|
||||
kind = Kind.ERROR,
|
||||
line = 29,
|
||||
messageRegExp = "Unmapped target property: \"bar\""),
|
||||
@Diagnostic(type = SourceTargetMapper.class,
|
||||
kind = Kind.ERROR,
|
||||
line = 31,
|
||||
messageRegExp = "Unmapped target property: \"qux\"")
|
||||
}
|
||||
)
|
||||
public void shouldRaiseErrorDueToUnsetTargetPropertyWithPolicySetViaProcessorOption() {
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user