#2225 Add support for suppressing the generation of the timestamp through Mapper and MapperConfig

This commit is contained in:
Filip Hrisafov 2021-11-06 09:21:16 +01:00 committed by GitHub
parent 166eb699c7
commit 735a5bef6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 130 additions and 8 deletions

View File

@ -357,4 +357,17 @@ public @interface Mapper {
* @since 1.4
*/
Class<? extends Exception> unexpectedValueMappingException() default IllegalArgumentException.class;
/**
* Flag indicating whether the addition of a time stamp in the {@code @Generated} annotation should be suppressed.
* i.e. not be added.
*
* The method overrides the flag set in a central configuration set by {@link #config()}
* or through an annotation processor option.
*
* @return whether the addition of a timestamp should be suppressed
*
* @since 1.5
*/
boolean suppressTimestampInGenerated() default false;
}

View File

@ -329,5 +329,16 @@ public @interface MapperConfig {
*/
Class<? extends Exception> unexpectedValueMappingException() default IllegalArgumentException.class;
/**
* Flag indicating whether the addition of a time stamp in the {@code @Generated} annotation should be suppressed.
* i.e. not be added.
*
* The method overrides the flag set through an annotation processor option.
*
* @return whether the addition of a timestamp should be suppressed
*
* @since 1.5
*/
boolean suppressTimestampInGenerated() default false;
}

View File

@ -32,6 +32,7 @@ public class Decorator extends GeneratedType {
private boolean hasDelegateConstructor;
private String implName;
private String implPackage;
private boolean suppressGeneratorTimestamp;
public Builder() {
super( Builder.class );
@ -62,6 +63,11 @@ public class Decorator extends GeneratedType {
return this;
}
public Builder suppressGeneratorTimestamp(boolean suppressGeneratorTimestamp) {
this.suppressGeneratorTimestamp = suppressGeneratorTimestamp;
return this;
}
public Decorator build() {
String implementationName = implName.replace( Mapper.CLASS_NAME_PLACEHOLDER,
Mapper.getFlatName( mapperElement ) );
@ -86,6 +92,7 @@ public class Decorator extends GeneratedType {
methods,
options,
versionInformation,
suppressGeneratorTimestamp,
Accessibility.fromModifiers( mapperElement.getModifiers() ),
extraImportedTypes,
decoratorConstructor
@ -101,6 +108,7 @@ public class Decorator extends GeneratedType {
Type mapperType,
List<MappingMethod> methods,
Options options, VersionInformation versionInformation,
boolean suppressGeneratorTimestamp,
Accessibility accessibility, SortedSet<Type> extraImports,
DecoratorConstructor decoratorConstructor) {
super(
@ -112,6 +120,7 @@ public class Decorator extends GeneratedType {
Arrays.asList( new Field( mapperType, "delegate", true ) ),
options,
versionInformation,
suppressGeneratorTimestamp,
accessibility,
extraImports,
decoratorConstructor

View File

@ -103,6 +103,7 @@ public abstract class GeneratedType extends ModelElement {
protected GeneratedType(TypeFactory typeFactory, String packageName, String name,
Type mapperDefinitionType, List<MappingMethod> methods,
List<Field> fields, Options options, VersionInformation versionInformation,
boolean suppressGeneratorTimestamp,
Accessibility accessibility, SortedSet<Type> extraImportedTypes, Constructor constructor) {
this.packageName = packageName;
this.name = name;
@ -113,7 +114,7 @@ public abstract class GeneratedType extends ModelElement {
this.methods = methods;
this.fields = fields;
this.suppressGeneratorTimestamp = options.isSuppressGeneratorTimestamp();
this.suppressGeneratorTimestamp = suppressGeneratorTimestamp;
this.suppressGeneratorVersionComment = options.isSuppressGeneratorVersionComment();
this.versionInformation = versionInformation;
this.accessibility = accessibility;

View File

@ -42,6 +42,7 @@ public class Mapper extends GeneratedType {
private boolean customName;
private String implPackage;
private boolean customPackage;
private boolean suppressGeneratorTimestamp;
public Builder() {
super( Builder.class );
@ -79,6 +80,11 @@ public class Mapper extends GeneratedType {
return this;
}
public Builder suppressGeneratorTimestamp(boolean suppressGeneratorTimestamp) {
this.suppressGeneratorTimestamp = suppressGeneratorTimestamp;
return this;
}
public Mapper build() {
String implementationName = implName.replace( CLASS_NAME_PLACEHOLDER, getFlatName( element ) ) +
( decorator == null ? "" : "_" );
@ -102,6 +108,7 @@ public class Mapper extends GeneratedType {
methods,
options,
versionInformation,
suppressGeneratorTimestamp,
Accessibility.fromModifiers( element.getModifiers() ),
fields,
constructor,
@ -121,6 +128,7 @@ public class Mapper extends GeneratedType {
Type mapperDefinitionType,
boolean customPackage, boolean customImplName,
List<MappingMethod> methods, Options options, VersionInformation versionInformation,
boolean suppressGeneratorTimestamp,
Accessibility accessibility, List<Field> fields, Constructor constructor,
Decorator decorator, SortedSet<Type> extraImportedTypes ) {
@ -133,6 +141,7 @@ public class Mapper extends GeneratedType {
fields,
options,
versionInformation,
suppressGeneratorTimestamp,
accessibility,
extraImportedTypes,
constructor

View File

@ -83,6 +83,13 @@ public class DefaultOptions extends DelegatingOptions {
return mapper.componentModel().getDefaultValue();
}
public boolean suppressTimestampInGenerated() {
if ( mapper.suppressTimestampInGenerated().hasValue() ) {
return mapper.suppressTimestampInGenerated().getValue();
}
return options.isSuppressGeneratorTimestamp();
}
@Override
public MappingInheritanceStrategyGem getMappingInheritanceStrategy() {
return MappingInheritanceStrategyGem.valueOf( mapper.mappingInheritanceStrategy().getDefaultValue() );

View File

@ -68,6 +68,10 @@ public abstract class DelegatingOptions {
return next.componentModel();
}
public boolean suppressTimestampInGenerated() {
return next.suppressTimestampInGenerated();
}
public MappingInheritanceStrategyGem getMappingInheritanceStrategy() {
return next.getMappingInheritanceStrategy();
}

View File

@ -76,6 +76,13 @@ public class MapperConfigOptions extends DelegatingOptions {
return mapperConfig.componentModel().hasValue() ? mapperConfig.componentModel().get() : next().componentModel();
}
@Override
public boolean suppressTimestampInGenerated() {
return mapperConfig.suppressTimestampInGenerated().hasValue() ?
mapperConfig.suppressTimestampInGenerated().get() :
next().suppressTimestampInGenerated();
}
@Override
public MappingInheritanceStrategyGem getMappingInheritanceStrategy() {
return mapperConfig.mappingInheritanceStrategy().hasValue() ?

View File

@ -105,6 +105,13 @@ public class MapperOptions extends DelegatingOptions {
return mapper.componentModel().hasValue() ? mapper.componentModel().get() : next().componentModel();
}
@Override
public boolean suppressTimestampInGenerated() {
return mapper.suppressTimestampInGenerated().hasValue() ?
mapper.suppressTimestampInGenerated().get() :
next().suppressTimestampInGenerated();
}
@Override
public MappingInheritanceStrategyGem getMappingInheritanceStrategy() {
return mapper.mappingInheritanceStrategy().hasValue() ?

View File

@ -198,13 +198,13 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
.constructorFragments( constructorFragments )
.options( options )
.versionInformation( versionInformation )
.decorator( getDecorator( element, methods, mapperOptions.implementationName(),
mapperOptions.implementationPackage(), getExtraImports( element, mapperOptions ) ) )
.decorator( getDecorator( element, methods, mapperOptions ) )
.typeFactory( typeFactory )
.elementUtils( elementUtils )
.extraImports( getExtraImports( element, mapperOptions ) )
.implName( mapperOptions.implementationName() )
.implPackage( mapperOptions.implementationPackage() )
.suppressGeneratorTimestamp( mapperOptions.suppressTimestampInGenerated() )
.build();
if ( !mappingContext.getForgedMethodsUnderCreation().isEmpty() ) {
@ -226,8 +226,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
return mapper;
}
private Decorator getDecorator(TypeElement element, List<SourceMethod> methods, String implName,
String implPackage, SortedSet<Type> extraImports) {
private Decorator getDecorator(TypeElement element, List<SourceMethod> methods, MapperOptions mapperOptions) {
DecoratedWithGem decoratedWith = DecoratedWithGem.instanceOn( element );
if ( decoratedWith == null ) {
@ -287,9 +286,10 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
.hasDelegateConstructor( hasDelegateConstructor )
.options( options )
.versionInformation( versionInformation )
.implName( implName )
.implPackage( implPackage )
.extraImports( extraImports )
.implName( mapperOptions.implementationName() )
.implPackage( mapperOptions.implementationPackage() )
.extraImports( getExtraImports( element, mapperOptions ) )
.suppressGeneratorTimestamp( mapperOptions.suppressTimestampInGenerated() )
.build();
return decorator;

View File

@ -0,0 +1,16 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.versioninfo;
import org.mapstruct.Mapper;
/**
* @author Filip Hrisafov
*/
@Mapper(suppressTimestampInGenerated = true)
public interface SuppressTimestampViaMapper {
Object toObject(Object object);
}

View File

@ -0,0 +1,22 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.versioninfo;
import org.mapstruct.Mapper;
import org.mapstruct.MapperConfig;
/**
* @author Filip Hrisafov
*/
@Mapper(config = SuppressTimestampViaMapperConfig.Config.class)
public interface SuppressTimestampViaMapperConfig {
Object toObject(Object object);
@MapperConfig(suppressTimestampInGenerated = true)
interface Config {
}
}

View File

@ -49,4 +49,20 @@ public class VersionInfoTest {
.contains( "comments = \"version: " );
}
@ProcessorTest
@WithClasses(SuppressTimestampViaMapper.class)
@IssueKey("2225")
void includesNoTimestampViaMapper() {
generatedSource.forMapper( SuppressTimestampViaMapper.class ).content()
.doesNotContain( "date = \"" );
}
@ProcessorTest
@WithClasses(SuppressTimestampViaMapperConfig.class)
@IssueKey("2225")
void includesNoTimestampViaMapperConfig() {
generatedSource.forMapper( SuppressTimestampViaMapperConfig.class ).content()
.doesNotContain( "date = \"" );
}
}