Skip to content

Commit 1b960a4

Browse files
authored
#110 Fix IndexOutOfBoundsException for target methods without parameter
1 parent c27f853 commit 1b960a4

File tree

7 files changed

+108
-2
lines changed

7 files changed

+108
-2
lines changed

src/main/java/org/mapstruct/intellij/codeinsight/references/MapstructTargetReference.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ else if ( psiMember instanceof PsiVariable ) {
199199
* @return the type of the first parameter of the method
200200
*/
201201
private static PsiType firstParameterPsiType(PsiMethod psiMethod) {
202-
return psiMethod.getParameterList().getParameters()[0].getType();
202+
PsiParameter[] psiParameters = psiMethod.getParameterList().getParameters();
203+
if ( psiParameters.length == 0) {
204+
return null;
205+
}
206+
return psiParameters[0].getType();
203207
}
204208
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
177177
if ( references.length > 0 ) {
178178
PsiElement resolved = references[0].resolve();
179179
if ( resolved instanceof PsiMethod ) {
180-
targetType = ( (PsiMethod) resolved ).getParameterList().getParameters()[0].getType();
180+
PsiParameter[] psiParameters =
181+
((PsiMethod) resolved).getParameterList().getParameters();
182+
if ( psiParameters.length > 0) {
183+
targetType = psiParameters[0].getType();
184+
}
181185
}
182186
else if ( resolved instanceof PsiParameter ) {
183187
targetType = ( (PsiParameter) resolved ).getType();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.intellij.bugs._110;
7+
8+
import org.mapstruct.intellij.MapstructBaseCompletionTestCase;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
/**
13+
* @author hdulme
14+
*/
15+
public class MethodeWithNoParametersTest extends MapstructBaseCompletionTestCase {
16+
17+
@Override
18+
protected String getTestDataPath() {
19+
return "testData/bugs/_110";
20+
}
21+
22+
@Override
23+
protected void setUp() throws Exception {
24+
super.setUp();
25+
myFixture.copyFileToProject( "TargetNoPropertyMethods.java",
26+
"org/example/TargetNoPropertyMethode.java" );
27+
}
28+
29+
public void testExpressionCompletion() {
30+
myFixture.configureByFile( "MapExpression.java" );
31+
assertThat( myFixture.completeBasic() ).isEmpty();
32+
}
33+
34+
public void testDefaultExpressionCompletion() {
35+
myFixture.configureByFile( "MapDefaultExpression.java" );
36+
assertThat( myFixture.completeBasic() ).isEmpty();
37+
}
38+
39+
public void testTargetCompletion() {
40+
myFixture.configureByFile( "TargetCompletion.java" );
41+
assertThat( myFixture.completeBasic() ).isEmpty();
42+
}
43+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
import org.mapstruct.*;
7+
import org.example.TargetNoPropertyMethode;
8+
9+
@Mapper
10+
public interface MapExpression {
11+
12+
@Mapping(target = "active", defaultExpression = "java(<caret>)")
13+
TargetNoPropertyMethode mapDefaultExpression(String string)
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
import org.mapstruct.*;
7+
import org.example.TargetNoPropertyMethode;
8+
9+
@Mapper
10+
public interface MapExpression {
11+
12+
@Mapping(target = "active", expression = "java(<caret>)")
13+
TargetNoPropertyMethode mapExpression(String string)
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
import org.mapstruct.*;
7+
import org.example.TargetNoPropertyMethode;
8+
9+
@Mapper
10+
public interface MapExpression {
11+
12+
@Mapping(target = "active.<caret>")
13+
TargetNoPropertyMethode mapExpression(String string)
14+
}
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 https://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.example;
7+
8+
public class TargetNoPropertyMethods {
9+
10+
public void setActive() {
11+
12+
}
13+
}

0 commit comments

Comments
 (0)