Skip to content

Commit

Permalink
mapstruct#1742 Fix escalation & using Optional for (optional) annotat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
sjaakd committed May 11, 2019
1 parent c1a0c50 commit 02d51d3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
package org.mapstruct.ap.internal.model;

import java.util.Collection;
import java.util.Optional;
import javax.lang.model.element.ExecutableElement;

import org.mapstruct.ap.internal.model.common.BuilderType;
import org.mapstruct.ap.internal.model.source.BeanMapping;
import org.mapstruct.ap.internal.model.source.MappingOptions;
import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.prism.BuilderPrism;
import org.mapstruct.ap.internal.util.MapperConfiguration;
Expand All @@ -36,14 +38,18 @@ public static MethodReference getBuilderFinisherMethod(Method method, BuilderTyp
return null;
}

BuilderPrism builderMapping = builderMappingPrism( method, ctx );
if ( builderMapping == null && buildMethods.size() == 1 ) {
Optional<BuilderPrism> beanMethodBuilder = Optional.ofNullable( method.getMappingOptions() )
.map( MappingOptions::getBeanMapping )
.flatMap( BeanMapping::getBuilder );
Optional<BuilderPrism> builderMapping = method.getMapperConfiguration().getBuilderPrism( beanMethodBuilder );

if ( !builderMapping.isPresent() && buildMethods.size() == 1 ) {
return MethodReference.forMethodCall( first( buildMethods ).getSimpleName().toString() );
}
else {
String buildMethodPattern = DEFAULT_BUILD_METHOD_NAME;
if ( builderMapping != null ) {
buildMethodPattern = builderMapping.buildMethod();
if ( builderMapping.isPresent() ) {
buildMethodPattern = builderMapping.get().buildMethod();
}
for ( ExecutableElement buildMethod : buildMethods ) {
String methodName = buildMethod.getSimpleName().toString();
Expand All @@ -52,7 +58,7 @@ public static MethodReference getBuilderFinisherMethod(Method method, BuilderTyp
}
}

if ( builderMapping == null ) {
if ( !builderMapping.isPresent() ) {
ctx.getMessager().printMessage(
method.getExecutable(),
Message.BUILDER_NO_BUILD_METHOD_FOUND_DEFAULT,
Expand All @@ -65,7 +71,7 @@ public static MethodReference getBuilderFinisherMethod(Method method, BuilderTyp
else {
ctx.getMessager().printMessage(
method.getExecutable(),
builderMapping.mirror,
builderMapping.get().mirror,
Message.BUILDER_NO_BUILD_METHOD_FOUND,
buildMethodPattern,
builderType.getBuilder(),
Expand All @@ -78,11 +84,4 @@ public static MethodReference getBuilderFinisherMethod(Method method, BuilderTyp
return null;
}

private static BuilderPrism builderMappingPrism(Method method, MappingBuilderContext ctx) {
BeanMapping beanMapping = method.getMappingOptions().getBeanMapping();
if ( beanMapping != null && beanMapping.getBuilder() != null ) {
return beanMapping.getBuilder();
}
return MapperConfiguration.getInstanceOn( ctx.getMapperTypeElement() ).getBuilderPrism();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.util.Types;
Expand Down Expand Up @@ -174,7 +175,7 @@ public List<String> getIgnoreUnmappedSourceProperties() {
return ignoreUnmappedSourceProperties;
}

public BuilderPrism getBuilder() {
return builder;
public Optional<BuilderPrism> getBuilder() {
return Optional.ofNullable( builder );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
Expand Down Expand Up @@ -271,15 +272,18 @@ public boolean isDisableSubMappingMethodsGeneration() {
return mapperPrism.disableSubMappingMethodsGeneration(); // fall back to default defined in the annotation
}

public BuilderPrism getBuilderPrism() {
if ( mapperPrism.values.builder() != null ) {
return mapperPrism.builder();
public Optional<BuilderPrism> getBuilderPrism(Optional<BuilderPrism> beanMappingBuilderPrism) {
if ( beanMappingBuilderPrism != null && beanMappingBuilderPrism.isPresent() ) {
return beanMappingBuilderPrism;
}
else if ( mapperPrism.values.builder() != null ) {
return Optional.ofNullable( mapperPrism.builder() );
}
else if ( mapperConfigPrism != null && mapperConfigPrism.values.builder() != null ) {
return mapperConfigPrism.builder();
return Optional.ofNullable( mapperConfigPrism.builder() );
}
else {
return null;
return Optional.empty();
}
}

Expand Down

0 comments on commit 02d51d3

Please sign in to comment.