-
-
Notifications
You must be signed in to change notification settings - Fork 1k
#1018 Use diamond operator for new expressions in generated code
#4045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
filiphr
merged 2 commits into
mapstruct:main
from
filiphr:use-diamond-operator-everywhere
May 4, 2026
+470
−203
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
processor/src/main/java/org/mapstruct/ap/internal/model/common/NewInstanceCreation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * 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.model.common; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Model element representing the head of a {@code new T<>(...)} expression. It always renders the diamond | ||
| * operator for generic types, leaving any type-argument inference to the surrounding Java context. | ||
| * <p> | ||
| * The caller is responsible for emitting the parenthesised argument list after the model. | ||
| * <p> | ||
| * Imports contributed by this element only include the raw constructor type, not its type parameters: the | ||
| * generated source never references the parameter classes through this expression, so they are not needed | ||
| * here. If the surrounding code references a type parameter elsewhere (e.g. in a variable declaration or a | ||
| * method signature), that reference contributes its own imports through its own model element. | ||
| * | ||
| * @author Filip Hrisafov | ||
| */ | ||
| public class NewInstanceCreation extends ModelElement { | ||
|
|
||
| private final Type type; | ||
| private final Type rawType; | ||
|
|
||
| private NewInstanceCreation(Type type) { | ||
| this.type = type; | ||
| this.rawType = type.asRawType(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a {@link NewInstanceCreation} for the given target type. If the target has an implementation | ||
| * type (e.g. {@code Collection} -> {@code ArrayList}), the implementation type is used. | ||
| * | ||
| * @param targetType the target type to be instantiated; must not be {@code null} | ||
| * @return a new model | ||
| */ | ||
| public static NewInstanceCreation forType(Type targetType) { | ||
| Type effective = targetType.getImplementationType() != null | ||
| ? targetType.getImplementationType() | ||
| : targetType; | ||
| return new NewInstanceCreation( effective ); | ||
| } | ||
|
|
||
| public Type getType() { | ||
| return type; | ||
| } | ||
|
|
||
| public Type getRawType() { | ||
| return rawType; | ||
| } | ||
|
|
||
| public boolean isGeneric() { | ||
| return !type.getTypeParameters().isEmpty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<Type> getImportTypes() { | ||
| return rawType.getImportTypes(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
processor/src/main/resources/org/mapstruct/ap/internal/model/common/NewInstanceCreation.ftl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <#-- | ||
|
|
||
| Copyright MapStruct Authors. | ||
|
|
||
| Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| --> | ||
| <#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.common.NewInstanceCreation" --> | ||
| <@compress single_line=true> | ||
| new <@includeModel object=rawType/><#if generic><></#if> | ||
| </@compress> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
processor/src/test/java/org/mapstruct/ap/test/generics/wildcard/BoundCopyMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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.test.generics.wildcard; | ||
|
|
||
| import org.mapstruct.Mapper; | ||
|
|
||
| @Mapper | ||
| public interface BoundCopyMapper { | ||
|
|
||
| CollectionSuperTypes copySuperCollection(CollectionSuperTypes collectionSuperTypes); | ||
|
|
||
| CollectionExtendTypes copyExtendsCollection(CollectionExtendTypes collectionExtendTypes); | ||
|
|
||
| MapSuperType copySuperMap(MapSuperType mapSuperType); | ||
|
|
||
| MapExtendType copyExtendMap(MapExtendType mapExtendType); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A check could be added to verify whether an effective target can be constructed.
This isn’t a new issue—it existed before this change—but this might be a good place to catch or handle it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a nice catch. However, I do not believe that this is the right place for this, we need to do this way earlier in order to provide a proper error message, like the line of the
@Mapping