Skip to content

Commit

Permalink
mapstruct#3163: Strip wild card when checking for type assignability
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Zegveld authored and filiphr committed Aug 1, 2023
1 parent b2dc641 commit 7eb5f06
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,14 @@ public Type withoutBounds() {
* @return {@code true} if and only if this type is assignable to the given other type.
*/
public boolean isAssignableTo(Type other) {
TypeMirror otherMirror = other.typeMirror;
if ( otherMirror.getKind() == TypeKind.WILDCARD ) {
otherMirror = typeUtils.erasure( other.typeMirror );
}
if ( TypeKind.WILDCARD == typeMirror.getKind() ) {
return typeUtils.contains( typeMirror, other.typeMirror );
return typeUtils.contains( typeMirror, otherMirror );
}

return typeUtils.isAssignable( typeMirror, other.typeMirror );
return typeUtils.isAssignable( typeMirror, otherMirror );
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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 );
}
}
Original file line number Diff line number Diff line change
@@ -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() {
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 7eb5f06

Please sign in to comment.