Skip to content

Add generics support - #273

Open
maybetm wants to merge 1 commit into
mapstruct:mainfrom
maybetm:add_generics_support
Open

Add generics support#273
maybetm wants to merge 1 commit into
mapstruct:mainfrom
maybetm:add_generics_support

Conversation

@maybetm

@maybetm maybetm commented Jul 19, 2026

Copy link
Copy Markdown

Hi! ☺️ I noticed that the MapStruct IDEA plugin didn't support generics properly in some cases.
This PR adds support for generic types. I'd appreciate it if you could review and merge these changes!

Before

image

After

image

@maybetm maybetm changed the title add generics support Add generics support Jul 19, 2026

@hduelme hduelme left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maybetm, good catch! I left a few comments for you. Could you also add tests covering the other branches?

"testData",
"build/test-libs"
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you changes this code? I think this could break building on other OS (Windows)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this specifically to ensure the tests run correctly on Windows. Without these configurations, the testing framework is unable to access packages

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, before fix

14:52:40,037 FINE   .intellij.openapi.command.impl - finishCommand: name = Renaming method setTestName(String) of class org.mapstruct.intellij.test.examples.SimpleMapper.Target to setNewName, groupId = null 
14:52:40,039 SEVERE                 #TestFramework - Test failed 
com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess$VfsRootAccessNotAllowedError: File accessed outside allowed roots: file://D:/projects/github/mapstruct-idea/testData/usages/RenameTargetReferenceAfter.java;

Comment on lines 210 to 212
else if ( element instanceof PsiParameter psiParameter ) {
return psiParameter.getType();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is it with PsiParameter (constructor target parameter)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point – I fixed it

Added tests for:

  • GenericConstructorCarMapper.java
  • GenericConstructorMappingTargetUpdateMapper.java
  • GenericTwoTypeParamsConstructorTargetMapper.java
  • GenericRecordConstructorTargetMapper.java

Comment on lines 140 to 142
else if ( element instanceof PsiParameter psiParameter ) {
return psiParameter.getType();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the source is named e.g. entity.id?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm not sure I understand. Do you mean a nested source path like this?

@Mapping(source = "entity.id", target = "entityId")
Dto map(Entity entity, Other other);

@maybetm
maybetm force-pushed the add_generics_support branch from 19196bd to 30b3520 Compare August 4, 2026 10:23
Comment thread build.gradle

checkstyle {
toolVersion = '8.36.1'
toolVersion = '10.12.0'

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkstyle 8.36.1 cannot parse Java pattern-matching switch expressions (used in resolvedType()), e.g.:

Output before up version

MapstructSourceReference.java:138:17: unexpected token: psiType

@maybetm
maybetm requested a review from hduelme August 4, 2026 10:42
@maybetm
maybetm force-pushed the add_generics_support branch from 30b3520 to d781aa0 Compare August 4, 2026 10:51
@maybetm
maybetm force-pushed the add_generics_support branch from d781aa0 to f9e342b Compare August 4, 2026 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants