#2541 fix incorrect name for TypeVar with ElementType.TYPE_USE for javac-with-errorprone

This commit is contained in:
Filip Hrisafov 2021-08-14 13:50:20 +02:00
parent f0a13bb306
commit 2c23b935db
4 changed files with 108 additions and 1 deletions

View File

@ -278,7 +278,17 @@ public class TypeFactory {
isInterface = false;
// When the component type is primitive and is annotated with ElementType.TYPE_USE then
// the typeMirror#toString returns (@CustomAnnotation :: byte) for the javac compiler
name = mirror.getKind().isPrimitive() ? NativeTypes.getName( mirror.getKind() ) : mirror.toString();
if ( mirror.getKind().isPrimitive() ) {
name = NativeTypes.getName( mirror.getKind() );
}
// When the component type is type var and is annotated with ElementType.TYPE_USE then
// the typeMirror#toString returns (@CustomAnnotation T) for the errorprone javac compiler
else if ( mirror.getKind() == TypeKind.TYPEVAR ) {
name = ( (TypeVariable) mirror ).asElement().getSimpleName().toString();
}
else {
name = mirror.toString();
}
packageName = null;
qualifiedName = name;
typeElement = null;

View File

@ -0,0 +1,47 @@
/*
* 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._2541;
import java.util.Optional;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface Issue2541Mapper {
Issue2541Mapper INSTANCE = Mappers.getMapper( Issue2541Mapper.class );
Target map(Source source);
default <T> Optional<T> toOptional(@Nullable T value) {
return Optional.ofNullable( value );
}
class Target {
private Optional<String> value;
public Optional<String> getValue() {
return value;
}
public void setValue(Optional<String> value) {
this.value = value;
}
}
class Source {
private final String value;
public Source(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
}

View File

@ -0,0 +1,28 @@
/*
* 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._2541;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Filip Hrisafov
*/
@WithClasses({
Issue2541Mapper.class,
Nullable.class,
})
class Issue2541Test {
@ProcessorTest
void shouldGenerateCorrectCode() {
Issue2541Mapper.Target target = Issue2541Mapper.INSTANCE.map( new Issue2541Mapper.Source( null ) );
assertThat( target.getValue() ).isEmpty();
}
}

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._2541;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({
ElementType.METHOD,
ElementType.TYPE_USE
})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Nullable {
}