#3163: Strip wild card when checking for type assignability

This commit is contained in:
Ben Zegveld 2023-08-01 15:04:15 +02:00 committed by Filip Hrisafov
parent 721288140a
commit 8cc2bdd092
5 changed files with 122 additions and 4 deletions

View File

@ -629,11 +629,14 @@ public class Type extends ModelElement implements Comparable<Type> {
* @return {@code true} if and only if this type is assignable to the given other type. * @return {@code true} if and only if this type is assignable to the given other type.
*/ */
public boolean isAssignableTo(Type other) { public boolean isAssignableTo(Type other) {
if ( TypeKind.WILDCARD == typeMirror.getKind() ) { TypeMirror otherMirror = other.typeMirror;
return typeUtils.contains( typeMirror, other.typeMirror ); if ( otherMirror.getKind() == TypeKind.WILDCARD ) {
otherMirror = typeUtils.erasure( other.typeMirror );
} }
if ( TypeKind.WILDCARD == typeMirror.getKind() ) {
return typeUtils.isAssignable( typeMirror, other.typeMirror ); return typeUtils.contains( typeMirror, otherMirror );
}
return typeUtils.isAssignable( typeMirror, otherMirror );
} }
/** /**

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._3163;
import java.util.Optional;
import org.mapstruct.Mapper;
@Mapper
public interface Issue3163Mapper {
Target map(Source value);
Target.Nested map(Source.Nested value);
default <T> Optional<T> wrapAsOptional(T value) {
return Optional.ofNullable( value );
}
}

View File

@ -0,0 +1,21 @@
/*
* 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._3163;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
@WithClasses({
Issue3163Mapper.class,
Source.class,
Target.class
})
class Issue3163Test {
@ProcessorTest
void shouldCompile() {
}
}

View File

@ -0,0 +1,34 @@
/*
* 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._3163;
/**
* @author Filip Hrisafov
*/
public class Source {
private final Nested nested;
public Source(Nested nested) {
this.nested = nested;
}
public Nested getNested() {
return nested;
}
public static class Nested {
private final String value;
public Nested(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
}

View File

@ -0,0 +1,38 @@
/*
* 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._3163;
import java.util.Optional;
/**
* @author Filip Hrisafov
*/
public class Target {
private Nested nested;
public Optional<Nested> getNested() {
return Optional.ofNullable( nested );
}
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public final void setNested(Optional<? extends Nested> nested) {
this.nested = nested.orElse( null );
}
public static class Nested {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}