-
-
Notifications
You must be signed in to change notification settings - Fork 41
Add generics support #273
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
base: main
Are you sure you want to change the base?
Add generics support #273
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| package org.mapstruct.intellij.codeinsight.references; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import com.intellij.codeInsight.lookup.LookupElement; | ||
|
|
@@ -23,6 +24,7 @@ | |
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.mapstruct.intellij.util.MapstructUtil; | ||
| import org.mapstruct.intellij.util.SourceUtils; | ||
|
|
||
| import static org.mapstruct.intellij.util.MapstructUtil.asLookup; | ||
| import static org.mapstruct.intellij.util.MapstructUtil.findRecordComponent; | ||
|
|
@@ -133,21 +135,31 @@ Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod) { | |
| @Override | ||
| PsiType resolvedType() { | ||
| PsiElement element = resolve(); | ||
|
|
||
| if ( element instanceof PsiMethod psiMethod ) { | ||
| return psiMethod.getReturnType(); | ||
| } | ||
| else if ( element instanceof PsiParameter psiParameter ) { | ||
| return psiParameter.getType(); | ||
| } | ||
| else if ( element instanceof PsiRecordComponent psiRecordComponent ) { | ||
| return psiRecordComponent.getType(); | ||
| } | ||
| else if ( element instanceof PsiField psiField ) { | ||
| return psiField.getType(); | ||
| PsiType elementType = switch ( element ) { | ||
| case PsiMethod psiMethod -> psiMethod.getReturnType(); | ||
| case PsiParameter psiParameter -> psiParameter.getType(); | ||
| case PsiRecordComponent psiRecordComponent -> psiRecordComponent.getType(); | ||
| case PsiField psiField -> psiField.getType(); | ||
| case null, default -> null; | ||
| }; | ||
|
|
||
| if ( elementType == null ) { | ||
| return null; | ||
| } | ||
|
|
||
| return null; | ||
| PsiType contextType = Optional.ofNullable( getPrevious() ) | ||
| .map( MapstructBaseReference::resolvedType ) | ||
| .or( () -> Optional.ofNullable( this.getMappingMethod() ) | ||
| .map( MapstructUtil::getSourceParameters ) | ||
| .filter( params -> params.length == 1 ) | ||
| .map( psiParameters -> psiParameters[0] ) | ||
| .map( SourceUtils::getParameterType ) | ||
| ) | ||
| .orElse( null ); | ||
|
|
||
|
Comment on lines
+150
to
+159
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this only supported for one source parameter?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, it's been so long, but I finally remembered why it was done this way🥹
Accordingly, if we have only one argument, then the user can immediately choose the fields of that object. By the way, I personally prefer to always explicitly type the argument name. In this case, I will get "No suggestions", but this is the plugin's standard behavior, and it might be an open question for discussion – whether we should change it or not?(But not in this PR) |
||
| return PsiUtil.resolveGenericsClassInType( contextType ) | ||
| .getSubstitutor() | ||
| .substitute( elementType ); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Copyright MapStruct Authors. | ||
| * | ||
| * Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package org.mapstruct.ap.test.complex; | ||
|
|
||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
|
|
||
| @Mapper | ||
| public interface GenericCarWrapperMapper { | ||
|
|
||
| @Mapping(target = "id", source = "wrapper.car.<caret>winCode") | ||
| CarEntity toCarDto(CarWrapper<Car> wrapper); | ||
| } | ||
|
|
||
| class CarEntity { | ||
|
|
||
| private String id; | ||
|
|
||
| public String getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(String id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class Car { | ||
|
|
||
| private String winCode; | ||
|
|
||
| public String getWinCode() { | ||
| return winCode; | ||
| } | ||
|
|
||
| public void setWinCode(String winCode) { | ||
| this.winCode = winCode; | ||
| } | ||
| } | ||
|
|
||
| class CarWrapper<T> { | ||
|
|
||
| private T car; | ||
|
|
||
| public T getCar() { | ||
| return car; | ||
| } | ||
|
|
||
| public void setCar(T car) { | ||
| this.car = car; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Copyright MapStruct Authors. | ||
| * | ||
| * Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package org.mapstruct.ap.test.complex; | ||
|
|
||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
|
|
||
| @Mapper | ||
| public interface GenericCarWrapperSourceAutoCompleteAfterCarWithoutParameterPrefix { | ||
| @Mapping(target = "id", source = "car.<caret>winCode") | ||
| CarEntity toCarDto(CarWrapper<Car> wrapper); | ||
| } | ||
|
|
||
| class CarEntity { | ||
| private String id; | ||
| public String getId() { | ||
| return id; | ||
| } | ||
| public void setId(String id) { | ||
| this.id = id; | ||
| } | ||
| } | ||
|
|
||
| class Car { | ||
| private String winCode; | ||
| public String getWinCode() { | ||
| return winCode; | ||
| } | ||
| public void setWinCode(String winCode) { | ||
| this.winCode = winCode; | ||
| } | ||
| } | ||
|
|
||
| class CarWrapper<T> { | ||
| private T car; | ||
| public T getCar() { | ||
| return car; | ||
| } | ||
| public void setCar(T car) { | ||
| this.car = car; | ||
| } | ||
| } |



Uh oh!
There was an error while loading. Please reload this page.