From ea45666d665c51c02a79bb9a670855b9fab06059 Mon Sep 17 00:00:00 2001 From: Zegveld <41897697+Zegveld@users.noreply.github.com> Date: Sat, 11 Dec 2021 14:16:19 +0100 Subject: [PATCH] #2673: Fix optional wrapping pattern throwing exception when primitive types are present MethodMatcher incorrectly reported that a primitive type matches a candidate for a type var --- .../internal/model/source/MethodMatcher.java | 7 +- .../ap/test/bugs/_2673/Issue2673Mapper.java | 66 +++++++++++++++++++ .../ap/test/bugs/_2673/Issue2673Test.java | 24 +++++++ 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_2673/Issue2673Mapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_2673/Issue2673Test.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/MethodMatcher.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/MethodMatcher.java index f08e4a915..f0d953679 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/MethodMatcher.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/MethodMatcher.java @@ -10,7 +10,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; - import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeMirror; @@ -239,6 +238,8 @@ public class MethodMatcher { return true; } + boolean foundAMatch = false; + for ( Type mthdParType : candidateMethod.getTypeParameters() ) { // typeFromCandidateMethod itself is a generic type, e.g. String method( T par ); @@ -256,6 +257,8 @@ public class MethodMatcher { continue; } + foundAMatch = true; // there is a rare case where we do not arrive here at all. + // resolved something at this point, a candidate can be fetched or created TypeVarCandidate typeVarCandidate; if ( candidates.containsKey( mthdParType ) ) { @@ -297,7 +300,7 @@ public class MethodMatcher { return false; } } - return true; + return foundAMatch; } /** diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_2673/Issue2673Mapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2673/Issue2673Mapper.java new file mode 100644 index 000000000..221197d18 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2673/Issue2673Mapper.java @@ -0,0 +1,66 @@ +/* + * 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.bugs._2673; + +import java.util.Optional; + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +/** + * @author Ben Zegveld + */ +@Mapper +public interface Issue2673Mapper { + + Issue2673Mapper INSTANCE = Mappers.getMapper( Issue2673Mapper.class ); + + Target map(Source source); + + default T map(Optional opt) { + return opt.orElse( null ); + } + + default Optional map(T t) { + return Optional.ofNullable( t ); + } + + class Target { + private final int primitive; + private final String nonPrimitive; + + public Target(int primitive, String nonPrimitive) { + this.primitive = primitive; + this.nonPrimitive = nonPrimitive; + } + + public int getPrimitive() { + return primitive; + } + + public String getNonPrimitive() { + return nonPrimitive; + } + } + + class Source { + private final int primitive; + private final Optional nonPrimitive; + + public Source(int primitive, Optional nonPrimitive) { + this.primitive = primitive; + this.nonPrimitive = nonPrimitive; + } + + public int getPrimitive() { + return primitive; + } + + public Optional getNonPrimitive() { + return nonPrimitive; + } + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_2673/Issue2673Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2673/Issue2673Test.java new file mode 100644 index 000000000..40a5e7927 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2673/Issue2673Test.java @@ -0,0 +1,24 @@ +/* + * 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.bugs._2673; + +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.ProcessorTest; +import org.mapstruct.ap.testutil.WithClasses; + +/** + * @author Ben Zegveld + */ +@WithClasses({ + Issue2673Mapper.class +}) +class Issue2673Test { + + @ProcessorTest + @IssueKey( "2673" ) + void shouldCompile() { + } +}