Permalink
Browse files

Add a FreeBuilder specific AccessNamingStrategy

The builder created by FreeBuilder have different fluent methods created.
When using FreeBuilder one needs to use the JavaBean convention for the getters (as otherwise MapStruct won't recognize them),
this leads to FreeBuilder creating fluent setters that start with set (so we can safely ignore all other fluent setters)
  • Loading branch information...
filiphr committed Oct 28, 2018
1 parent 6f19d56 commit 104ebf88da8c6145b790905f0c1db66a3cd35a6b
@@ -10,7 +10,6 @@
import java.util.List;
import java.util.ServiceLoader;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
@@ -19,6 +18,7 @@
import org.mapstruct.ap.spi.BuilderProvider;
import org.mapstruct.ap.spi.DefaultAccessorNamingStrategy;
import org.mapstruct.ap.spi.DefaultBuilderProvider;
import org.mapstruct.ap.spi.FreeBuilderAccessorNamingStrategy;
import org.mapstruct.ap.spi.ImmutablesAccessorNamingStrategy;
import org.mapstruct.ap.spi.ImmutablesBuilderProvider;
import org.mapstruct.ap.spi.MapStructProcessingEnvironment;
@@ -61,14 +61,17 @@ private void initialize() {
AccessorNamingStrategy defaultAccessorNamingStrategy;
BuilderProvider defaultBuilderProvider;
TypeElement immutableElement = elementUtils.getTypeElement( ImmutablesConstants.IMMUTABLE_FQN );
if ( immutableElement == null ) {
defaultAccessorNamingStrategy = new DefaultAccessorNamingStrategy();
if ( elementUtils.getTypeElement( ImmutablesConstants.IMMUTABLE_FQN ) != null ) {
defaultAccessorNamingStrategy = new ImmutablesAccessorNamingStrategy();
defaultBuilderProvider = new ImmutablesBuilderProvider();
}
else if ( elementUtils.getTypeElement( FreeBuilderConstants.FREE_BUILDER_FQN ) != null ) {
defaultAccessorNamingStrategy = new FreeBuilderAccessorNamingStrategy();
defaultBuilderProvider = new DefaultBuilderProvider();
}
else {
defaultAccessorNamingStrategy = new ImmutablesAccessorNamingStrategy();
defaultBuilderProvider = new ImmutablesBuilderProvider();
defaultAccessorNamingStrategy = new DefaultAccessorNamingStrategy();
defaultBuilderProvider = new DefaultBuilderProvider();
}
this.accessorNamingStrategy = Services.get( AccessorNamingStrategy.class, defaultAccessorNamingStrategy );
this.accessorNamingStrategy.init( this );
@@ -0,0 +1,20 @@
/*
* 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.internal.util;
/**
* Helper for holding FreeBuilder FQN.
*
* @author Filip Hrisafov
*/
public class FreeBuilderConstants {
public static final String FREE_BUILDER_FQN = "org.inferred.freebuilder.FreeBuilder";
private FreeBuilderConstants() {
}
}
@@ -0,0 +1,35 @@
/*
* 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.spi;
import javax.lang.model.element.ExecutableElement;
/**
* Accessor naming strategy for FreeBuilder.
* FreeBuilder adds a lot of other methods that can be considered as fluent setters. Such as:
* <ul>
* <li>{@code from(Target)}</li>
* <li>{@code mapXXX(UnaryOperator)}</li>
* <li>{@code mutateXXX(Consumer)}</li>
* <li>{@code mergeFrom(Target)}</li>
* <li>{@code mergeFrom(Target.Builder)}</li>
* </ul>
* <p>
* When the JavaBean convention is not used with FreeBuilder then the getters are non standard and MapStruct
* won't recognize them. Therefore one needs to use the JavaBean convention in which the fluent setters
* start with {@code set}.
*
* @author Filip Hrisafov
*/
public class FreeBuilderAccessorNamingStrategy extends DefaultAccessorNamingStrategy {
@Override
protected boolean isFluentSetter(ExecutableElement method) {
// When using FreeBuilder one needs to use the JavaBean convention, which means that all setters will start
// with set
return false;
}
}

0 comments on commit 104ebf8

Please sign in to comment.