Skip to content

Commit

Permalink
mapstruct#1661 Add support for globally disabling builders
Browse files Browse the repository at this point in the history
  • Loading branch information
filiphr committed Jan 23, 2022
1 parent 0a8e9b7 commit 5801bf2
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 5 deletions.
5 changes: 5 additions & 0 deletions documentation/src/main/asciidoc/chapter-2-set-up.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ Supported values are:
If a policy is given for a specific mapper via `@Mapper#unmappedSourcePolicy()`, the value from the annotation takes precedence.
If a policy is given for a specific bean mapping via `@BeanMapping#ignoreUnmappedSourceProperties()`, it takes precedence over both `@Mapper#unmappedSourcePolicy()` and the option.
|`WARN`

|`mapstruct.
disableBuilders`
|If set to `true`, then MapStruct will not use builder patterns when doing the mapping. This is equivalent to doing `@Mapper( builder = @Builder( disableBuilder = true ) )` for all of your mappers.
|`false`
|===

=== Using MapStruct with the Java Module System
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ Otherwise, you would need to write a custom `BuilderProvider`

[TIP]
====
In case you want to disable using builders then you can use the `NoOpBuilderProvider` by creating a `org.mapstruct.ap.spi.BuilderProvider` file in the `META-INF/services` directory with `org.mapstruct.ap.spi.NoOpBuilderProvider` as it's content.
In case you want to disable using builders then you can set pass the MapStruct processor option `mapstruct.disableBuilders` to the compiler. e.g. `-Amapstruct.disableBuilders=true`.
====

[[mapping-with-constructors]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
MappingProcessor.UNMAPPED_SOURCE_POLICY,
MappingProcessor.DEFAULT_COMPONENT_MODEL,
MappingProcessor.DEFAULT_INJECTION_STRATEGY,
MappingProcessor.DISABLE_BUILDERS,
MappingProcessor.VERBOSE
})
public class MappingProcessor extends AbstractProcessor {
Expand All @@ -104,6 +105,7 @@ public class MappingProcessor extends AbstractProcessor {
protected static final String DEFAULT_COMPONENT_MODEL = "mapstruct.defaultComponentModel";
protected static final String DEFAULT_INJECTION_STRATEGY = "mapstruct.defaultInjectionStrategy";
protected static final String ALWAYS_GENERATE_SERVICE_FILE = "mapstruct.alwaysGenerateServicesFile";
protected static final String DISABLE_BUILDERS = "mapstruct.disableBuilders";
protected static final String VERBOSE = "mapstruct.verbose";

private Options options;
Expand All @@ -130,6 +132,7 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
processingEnv.getElementUtils(),
processingEnv.getTypeUtils(),
processingEnv.getMessager(),
options.isDisableBuilders(),
options.isVerbose()
);
}
Expand All @@ -146,6 +149,7 @@ private Options createOptions() {
processingEnv.getOptions().get( DEFAULT_COMPONENT_MODEL ),
processingEnv.getOptions().get( DEFAULT_INJECTION_STRATEGY ),
Boolean.valueOf( processingEnv.getOptions().get( ALWAYS_GENERATE_SERVICE_FILE ) ),
Boolean.valueOf( processingEnv.getOptions().get( DISABLE_BUILDERS ) ),
Boolean.valueOf( processingEnv.getOptions().get( VERBOSE ) )
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@ public class Options {
private final boolean alwaysGenerateSpi;
private final String defaultComponentModel;
private final String defaultInjectionStrategy;
private final boolean disableBuilders;
private final boolean verbose;

public Options(boolean suppressGeneratorTimestamp, boolean suppressGeneratorVersionComment,
ReportingPolicyGem unmappedTargetPolicy,
ReportingPolicyGem unmappedSourcePolicy,
String defaultComponentModel, String defaultInjectionStrategy,
boolean alwaysGenerateSpi, boolean verbose) {
boolean alwaysGenerateSpi,
boolean disableBuilders,
boolean verbose) {
this.suppressGeneratorTimestamp = suppressGeneratorTimestamp;
this.suppressGeneratorVersionComment = suppressGeneratorVersionComment;
this.unmappedTargetPolicy = unmappedTargetPolicy;
this.unmappedSourcePolicy = unmappedSourcePolicy;
this.defaultComponentModel = defaultComponentModel;
this.defaultInjectionStrategy = defaultInjectionStrategy;
this.alwaysGenerateSpi = alwaysGenerateSpi;
this.disableBuilders = disableBuilders;
this.verbose = verbose;
}

Expand Down Expand Up @@ -66,6 +70,10 @@ public boolean isAlwaysGenerateSpi() {
return alwaysGenerateSpi;
}

public boolean isDisableBuilders() {
return disableBuilders;
}

public boolean isVerbose() {
return verbose;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.mapstruct.ap.spi.ImmutablesAccessorNamingStrategy;
import org.mapstruct.ap.spi.ImmutablesBuilderProvider;
import org.mapstruct.ap.spi.MapStructProcessingEnvironment;
import org.mapstruct.ap.spi.NoOpBuilderProvider;

/**
* Keeps contextual data in the scope of the entire annotation processor ("application scope").
Expand All @@ -51,14 +52,17 @@ public class AnnotationProcessorContext implements MapStructProcessingEnvironmen
private Elements elementUtils;
private Types typeUtils;
private Messager messager;
private boolean disableBuilder;
private boolean verbose;

public AnnotationProcessorContext(Elements elementUtils, Types typeUtils, Messager messager, boolean verbose) {
public AnnotationProcessorContext(Elements elementUtils, Types typeUtils, Messager messager, boolean disableBuilder,
boolean verbose) {
astModifyingAnnotationProcessors = java.util.Collections.unmodifiableList(
findAstModifyingAnnotationProcessors( messager ) );
this.elementUtils = elementUtils;
this.typeUtils = typeUtils;
this.messager = messager;
this.disableBuilder = disableBuilder;
this.verbose = verbose;
}

Expand Down Expand Up @@ -103,7 +107,9 @@ else if ( elementUtils.getTypeElement( FreeBuilderConstants.FREE_BUILDER_FQN ) !
+ this.accessorNamingStrategy.getClass().getCanonicalName()
);
}
this.builderProvider = Services.get( BuilderProvider.class, defaultBuilderProvider );
this.builderProvider = this.disableBuilder ?
new NoOpBuilderProvider() :
Services.get( BuilderProvider.class, defaultBuilderProvider );
this.builderProvider.init( this );
if ( verbose ) {
messager.printMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.compilation.annotation.ProcessorOption;
import org.mapstruct.ap.testutil.runner.GeneratedSource;
import org.mapstruct.factory.Mappers;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@IssueKey("1743")
@IssueKey("1661")
@WithClasses({
SimpleMutablePerson.class,
SimpleNotRealyImmutablePerson.class
Expand All @@ -36,4 +38,18 @@ public void testSimpleImmutableBuilderHappyPath() {
assertThat( targetObject.getName() ).isEqualTo( "Bob" );

}

@ProcessorTest
@WithClasses({ SimpleWithBuilderMapper.class })
@ProcessorOption( name = "mapstruct.disableBuilders", value = "true")
public void builderGloballyDisabled() {
SimpleWithBuilderMapper mapper = Mappers.getMapper( SimpleWithBuilderMapper.class );
SimpleMutablePerson source = new SimpleMutablePerson();
source.setFullName( "Bob" );

SimpleNotRealyImmutablePerson targetObject = mapper.toNotRealyImmutable( source );

assertThat( targetObject.getName() ).isEqualTo( "Bob" );

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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.builder.off;

import org.mapstruct.CollectionMappingStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
public interface SimpleWithBuilderMapper {

@Mapping(target = "name", source = "fullName")
SimpleNotRealyImmutablePerson toNotRealyImmutable(SimpleMutablePerson source);
}

0 comments on commit 5801bf2

Please sign in to comment.