Skip to content

Commit 44f5d97

Browse files
Cerber-Ursifiliphr
authored andcommitted
Fixed language injection when target is field
Fixes #95
1 parent a5adaa5 commit 44f5d97

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

change-notes.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ <h2>1.4.0</h2>
33
<ul>
44
<li>Suppress redundant default parameter value assignment warning for <code>Mapping#constant</code> and <code>Mapping#defaultValue</code></li>
55
<li>Support for Java records</li>
6+
<li>Bug fix: language injections inside expressions when target is field</li>
67
</ul>
78
<h2>1.3.1</h2>
89
<ul>

src/main/java/org/mapstruct/intellij/expression/JavaExpressionInjector.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.intellij.psi.PsiClassObjectAccessExpression;
2828
import com.intellij.psi.PsiClassType;
2929
import com.intellij.psi.PsiElement;
30+
import com.intellij.psi.PsiField;
3031
import com.intellij.psi.PsiJavaCodeReferenceElement;
3132
import com.intellij.psi.PsiLanguageInjectionHost;
3233
import com.intellij.psi.PsiLiteralExpression;
@@ -181,6 +182,9 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
181182
else if ( resolved instanceof PsiParameter ) {
182183
targetType = ( (PsiParameter) resolved ).getType();
183184
}
185+
else if ( resolved instanceof PsiField) {
186+
targetType = ( (PsiField) resolved ).getType();
187+
}
184188
}
185189
}
186190
break;

src/test/java/org/mapstruct/intellij/expression/JavaExpressionInjectionTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,23 @@ public class JavaExpressionInjectionTest extends MapstructBaseCompletionTestCase
160160
" CarDto carToCarDto(Car car, String make);\n" +
161161
"}";
162162

163+
@Language("java")
164+
private static final String MAPPER_TO_DTO_WITHOUT_ACCESSORS = "" +
165+
"import java.util.List;\n" +
166+
"\n" +
167+
"import org.mapstruct.Mapper;\n" +
168+
"import org.mapstruct.Mapping;\n" +
169+
"import org.mapstruct.Mappings;\n" +
170+
"import org.example.dto.CarPlainDto;\n" +
171+
"import org.example.dto.Car;\n" +
172+
"\n" +
173+
"@Mapper(" + MAPPER + ")\n" +
174+
"public interface CarMapper {\n" +
175+
"\n" +
176+
" " + MAPPING + "\n" +
177+
" CarPlainDto carToCarPlainDto(Car car);\n" +
178+
"}";
179+
163180
@Override
164181
protected String getTestDataPath() {
165182
return "testData/expression";
@@ -602,6 +619,41 @@ protected void withTargetUsingStaticString(String attribute) {
602619
"}" );
603620
}
604621

622+
public void testExpressionWithMapperToDtoWithoutAccessors() {
623+
withMapperToDtoWithoutAccessors( "expression" );
624+
withMapperToDtoWithoutAccessors( "defaultExpression" );
625+
}
626+
627+
protected void withMapperToDtoWithoutAccessors(String attribute) {
628+
String mapping = "@Mapping(target = \"manufacturingYear\", " + attribute + " = \"java(car.<caret>)\")\n";
629+
@Language("java")
630+
String mapper = formatMapper( MAPPER_TO_DTO_WITHOUT_ACCESSORS, mapping );
631+
PsiFile file = configureMapperByText( mapper );
632+
633+
assertThat( myFixture.completeBasic() )
634+
.extracting( LookupElementPresentation::renderElement )
635+
.extracting( LookupElementPresentation::getItemText )
636+
.contains(
637+
"getMake",
638+
"setMake",
639+
"getManufacturingDate",
640+
"setManufacturingDate",
641+
"getNumberOfSeats",
642+
"setNumberOfSeats"
643+
);
644+
645+
assertThat( myFixture.complete( CompletionType.SMART ) )
646+
.extracting( LookupElementPresentation::renderElement )
647+
.extracting( LookupElementPresentation::getItemText )
648+
.containsExactlyInAnyOrder( "getMake", "toString" );
649+
650+
PsiElement elementAt = file.findElementAt( myFixture.getCaretOffset() );
651+
assertThat( elementAt )
652+
.isNotNull()
653+
.isInstanceOf( PsiJavaToken.class );
654+
assertThat( elementAt.getText() ).isEqualTo( ";" );
655+
}
656+
605657
private PsiFile configureMapperByText(@Language("java") String text) {
606658
return myFixture.configureByText( JavaFileType.INSTANCE, text );
607659
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.example.dto;
7+
8+
public class CarPlainDto {
9+
10+
public String make;
11+
public int seatCount;
12+
public String manufacturingYear;
13+
}

0 commit comments

Comments
 (0)