Skip to content

Commit c1d1581

Browse files
committed
Avoid ClassCastException in JavaExpressionInjector
Fixes #52
1 parent 18f53cb commit c1d1581

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

change-notes.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<html>
22
<h2>1.2.1</h2>
3+
<h2>1.2.2</h2>
4+
<ul>
5+
<li>Bug fix: ClassCastException in language injection in expressions</li>
6+
</ul>
37
<ul>
48
<li>Support code completion in Mapping#expression and Mapping#defaultExpression</li>
59
<li>Support meta annotations for when looking for unmapped target properties</li>

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.intellij.psi.PsiReference;
3232
import com.intellij.psi.PsiType;
3333
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;
34+
import com.intellij.psi.util.PsiTreeUtil;
3435
import com.intellij.psi.util.PsiUtil;
3536
import org.jetbrains.annotations.NotNull;
3637
import org.mapstruct.intellij.util.MapstructElementUtils;
@@ -64,8 +65,13 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
6465
// PsiModifierList
6566
// PsiMethod
6667

67-
PsiAnnotationParameterList annotationParameterList = (PsiAnnotationParameterList) context.getParent()
68-
.getParent();
68+
PsiAnnotationParameterList annotationParameterList = PsiTreeUtil.getParentOfType(
69+
context,
70+
PsiAnnotationParameterList.class
71+
);
72+
if ( annotationParameterList == null ) {
73+
return;
74+
}
6975
PsiType targetType = null;
7076
for ( PsiNameValuePair attribute : annotationParameterList.getAttributes() ) {
7177
if ( "target" .equals( attribute.getAttributeName() ) ) {
@@ -91,8 +97,15 @@ else if ( resolved instanceof PsiParameter ) {
9197
return;
9298
}
9399

94-
PsiMethod method = (PsiMethod) annotationParameterList.getParent().getParent().getParent();
95-
PsiClass mapperClass = (PsiClass) method.getParent();
100+
PsiMethod method = PsiTreeUtil.getParentOfType( annotationParameterList, PsiMethod.class );;
101+
if ( method == null ) {
102+
return;
103+
}
104+
105+
PsiClass mapperClass = PsiTreeUtil.getParentOfType( method, PsiClass.class );
106+
if ( mapperClass == null ) {
107+
return;
108+
}
96109
StringBuilder importsBuilder = new StringBuilder();
97110
StringBuilder prefixBuilder = new StringBuilder();
98111

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class JavaExpressionInjectionTest extends MapstructBaseCompletionTestCase
2828
"\n" +
2929
"import org.mapstruct.Mapper;\n" +
3030
"import org.mapstruct.Mapping;\n" +
31+
"import org.mapstruct.Mappings;\n" +
3132
"import org.example.dto.CarDto;\n" +
3233
"import org.example.dto.Car;\n" +
3334
"\n" +
@@ -126,6 +127,43 @@ protected void withTargetDefinedMapper(String attribute) {
126127
assertThat( elementAt.getText() ).isEqualTo( ";" );
127128
}
128129

130+
public void testExpressionWithTargetDefinedMapperInMappings() {
131+
withTargetDefinedMapperInMappings( "expression" );
132+
withTargetDefinedMapperInMappings( "defaultExpression" );
133+
}
134+
135+
protected void withTargetDefinedMapperInMappings(String attribute) {
136+
String mapping = "@Mappings(\n" +
137+
"@Mapping(target = \"manufacturingYear\", " + attribute + " = \"java(car.<caret>)\")\n" +
138+
")\n";
139+
@Language("java")
140+
String mapper = String.format( CAR_MAPPER, "", mapping );
141+
PsiFile file = configureMapperByText( mapper );
142+
143+
assertThat( myFixture.completeBasic() )
144+
.extracting( LookupElementPresentation::renderElement )
145+
.extracting( LookupElementPresentation::getItemText )
146+
.contains(
147+
"getMake",
148+
"setMake",
149+
"getManufacturingDate",
150+
"setManufacturingDate",
151+
"getNumberOfSeats",
152+
"setNumberOfSeats"
153+
);
154+
155+
assertThat( myFixture.complete( CompletionType.SMART ) )
156+
.extracting( LookupElementPresentation::renderElement )
157+
.extracting( LookupElementPresentation::getItemText )
158+
.containsExactlyInAnyOrder( "getMake", "toString" );
159+
160+
PsiElement elementAt = file.findElementAt( myFixture.getCaretOffset() );
161+
assertThat( elementAt )
162+
.isNotNull()
163+
.isInstanceOf( PsiJavaToken.class );
164+
assertThat( elementAt.getText() ).isEqualTo( ";" );
165+
}
166+
129167
public void testExpressionWithMapperWithImports() {
130168
withMapperWithImports( "expression" );
131169
withMapperWithImports( "defaultExpression" );

0 commit comments

Comments
 (0)