Skip to content

Commit 75f963a

Browse files
committed
mapstruct#2263 Fix IndexOutOfBoundsException when resolving TypeVar to a Type
1 parent 8f9df5b commit 75f963a

5 files changed

Lines changed: 108 additions & 1 deletion

File tree

processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,12 @@ public Type visitTypeVariable(TypeVariable t, Type parameterized) {
11591159
@Override
11601160
public Type visitDeclared(DeclaredType t, Type parameterized) {
11611161
if ( types.isAssignable( types.erasure( t ), types.erasure( parameterized.getTypeMirror() ) ) ) {
1162-
// if same type, we can cast en assume number of type args are also the same
1162+
// We can't assume that the type args are the same
1163+
// e.g. List<T> is assignable to Object
1164+
if ( t.getTypeArguments().size() != parameterized.getTypeParameters().size() ) {
1165+
return super.visitDeclared( t, parameterized );
1166+
}
1167+
11631168
for ( int i = 0; i < t.getTypeArguments().size(); i++ ) {
11641169
Type result = visit( t.getTypeArguments().get( i ), parameterized.getTypeParameters().get( i ) );
11651170
if ( result != null ) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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.mapstruct.ap.test.bugs._2263;
7+
8+
import org.mapstruct.Mapper;
9+
10+
/**
11+
* @author Filip Hrisafov
12+
*/
13+
@Mapper
14+
public interface Erroneous2263Mapper {
15+
16+
Target map(Source source);
17+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.mapstruct.ap.test.bugs._2263;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.mapstruct.ap.testutil.IssueKey;
11+
import org.mapstruct.ap.testutil.WithClasses;
12+
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
13+
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
14+
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
15+
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
16+
17+
/**
18+
* @author Filip Hrisafov
19+
*/
20+
@IssueKey("2263")
21+
@RunWith(AnnotationProcessorTestRunner.class)
22+
@WithClasses({
23+
Erroneous2263Mapper.class,
24+
Source.class,
25+
Target.class,
26+
})
27+
public class Issue2263Test {
28+
29+
@Test
30+
@ExpectedCompilationOutcome(value = CompilationResult.FAILED,
31+
diagnostics = {
32+
@Diagnostic(type = Erroneous2263Mapper.class,
33+
kind = javax.tools.Diagnostic.Kind.ERROR,
34+
line = 16,
35+
message = "Can't map property \"Object value\" to \"String value\". " +
36+
"Consider to declare/implement a mapping method: \"String map(Object value)\".")
37+
})
38+
public void shouldCorrectlyReportUnmappableTargetObject() {
39+
40+
}
41+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.mapstruct.ap.test.bugs._2263;
7+
8+
/**
9+
* @author Filip Hrisafov
10+
*/
11+
public class Source {
12+
13+
private final Object value;
14+
15+
public Source(Object value) {
16+
this.value = value;
17+
}
18+
19+
public Object getValue() {
20+
return value;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.mapstruct.ap.test.bugs._2263;
7+
8+
/**
9+
* @author Filip Hrisafov
10+
*/
11+
public class Target {
12+
13+
private final String value;
14+
15+
public Target(String value) {
16+
this.value = value;
17+
}
18+
19+
public String getValue() {
20+
return value;
21+
}
22+
}

0 commit comments

Comments
 (0)