From e10cf7c2449fadeadeaba50741feb30c1a5b8b08 Mon Sep 17 00:00:00 2001 From: nabiljarrai Date: Mon, 9 Mar 2026 05:17:42 +0400 Subject: [PATCH] Reuse cached erased TypeMirrors for Collection/Map/Stream checks in Type The private isCollection(), isMap(), and isStream() methods in Type were redundantly resolving TypeElements and erasing them on every call inside getAlternativeTargetAccessors(). TypeFactory already caches these exact erased TypeMirrors at construction time, so I exposed them via package-private getters and rewired Type to use them directly. Removed the now-unused isSubType helper method. --- .../mapstruct/ap/internal/model/common/Type.java | 14 +++----------- .../ap/internal/model/common/TypeFactory.java | 12 ++++++++++++ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java b/processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java index f742e1ba..d29e545f 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java @@ -44,7 +44,6 @@ import org.mapstruct.ap.internal.util.ElementUtils; import org.mapstruct.ap.internal.util.Executables; import org.mapstruct.ap.internal.util.Filters; -import org.mapstruct.ap.internal.util.JavaStreamConstants; import org.mapstruct.ap.internal.util.NativeTypes; import org.mapstruct.ap.internal.util.Nouns; import org.mapstruct.ap.internal.util.TypeUtils; @@ -1084,23 +1083,16 @@ private boolean isCollectionOrMapOrStream(Accessor getterMethod) { } private boolean isCollection(TypeMirror candidate) { - return isSubType( candidate, Collection.class ); + return typeUtils.isSubtypeErased( candidate, typeFactory.getCollectionType() ); } private boolean isStream(TypeMirror candidate) { - TypeElement streamTypeElement = elementUtils.getTypeElement( JavaStreamConstants.STREAM_FQN ); - TypeMirror streamType = streamTypeElement == null ? null : typeUtils.erasure( streamTypeElement.asType() ); + TypeMirror streamType = typeFactory.getStreamType(); return streamType != null && typeUtils.isSubtypeErased( candidate, streamType ); } private boolean isMap(TypeMirror candidate) { - return isSubType( candidate, Map.class ); - } - - private boolean isSubType(TypeMirror candidate, Class clazz) { - String className = clazz.getCanonicalName(); - TypeMirror classType = typeUtils.erasure( elementUtils.getTypeElement( className ).asType() ); - return typeUtils.isSubtypeErased( candidate, classType ); + return typeUtils.isSubtypeErased( candidate, typeFactory.getMapType() ); } /** diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java b/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java index f28e0c68..6eeddaaa 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java @@ -693,6 +693,18 @@ public BuilderType builderTypeFor( Type type, BuilderGem builder ) { return null; } + TypeMirror getCollectionType() { + return collectionType; + } + + TypeMirror getMapType() { + return mapType; + } + + TypeMirror getStreamType() { + return streamType; + } + public Type effectiveResultTypeFor( Type type, BuilderGem builder ) { if ( type != null ) { BuilderInfo builderInfo = findBuilder( type.getTypeMirror(), builder, false );