#2263 Fix IndexOutOfBoundsException when resolving TypeVar to a Type

This commit is contained in:
Filip Hrisafov 2020-11-02 22:26:17 +01:00
parent 8f9df5b69b
commit 75f963adf6
5 changed files with 108 additions and 1 deletions

View File

@ -1159,7 +1159,12 @@ public class Type extends ModelElement implements Comparable<Type> {
@Override
public Type visitDeclared(DeclaredType t, Type parameterized) {
if ( types.isAssignable( types.erasure( t ), types.erasure( parameterized.getTypeMirror() ) ) ) {
// if same type, we can cast en assume number of type args are also the same
// We can't assume that the type args are the same
// e.g. List<T> is assignable to Object
if ( t.getTypeArguments().size() != parameterized.getTypeParameters().size() ) {
return super.visitDeclared( t, parameterized );
}
for ( int i = 0; i < t.getTypeArguments().size(); i++ ) {
Type result = visit( t.getTypeArguments().get( i ), parameterized.getTypeParameters().get( i ) );
if ( result != null ) {

View File

@ -0,0 +1,17 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._2263;
import org.mapstruct.Mapper;
/**
* @author Filip Hrisafov
*/
@Mapper
public interface Erroneous2263Mapper {
Target map(Source source);
}

View File

@ -0,0 +1,41 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._2263;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
/**
* @author Filip Hrisafov
*/
@IssueKey("2263")
@RunWith(AnnotationProcessorTestRunner.class)
@WithClasses({
Erroneous2263Mapper.class,
Source.class,
Target.class,
})
public class Issue2263Test {
@Test
@ExpectedCompilationOutcome(value = CompilationResult.FAILED,
diagnostics = {
@Diagnostic(type = Erroneous2263Mapper.class,
kind = javax.tools.Diagnostic.Kind.ERROR,
line = 16,
message = "Can't map property \"Object value\" to \"String value\". " +
"Consider to declare/implement a mapping method: \"String map(Object value)\".")
})
public void shouldCorrectlyReportUnmappableTargetObject() {
}
}

View File

@ -0,0 +1,22 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._2263;
/**
* @author Filip Hrisafov
*/
public class Source {
private final Object value;
public Source(Object value) {
this.value = value;
}
public Object getValue() {
return value;
}
}

View File

@ -0,0 +1,22 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._2263;
/**
* @author Filip Hrisafov
*/
public class Target {
private final String value;
public Target(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}