#2867 Fix NPE when reporting message on parent mappers

This commit is contained in:
Filip Hrisafov 2022-06-04 21:52:59 +02:00
parent 0726563024
commit 46b78bfe59
4 changed files with 96 additions and 2 deletions

View File

@ -5,6 +5,7 @@
*/
package org.mapstruct.ap.internal.processor;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
@ -12,6 +13,7 @@ import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic.Kind;
import org.mapstruct.ap.internal.util.FormattingMessager;
@ -97,7 +99,7 @@ public class MapperAnnotatedFormattingMessenger implements FormattingMessager {
if ( e instanceof ExecutableElement ) {
ExecutableElement ee = (ExecutableElement) e;
StringBuilder method = new StringBuilder();
method.append( typeUtils.asElement( ee.getReturnType() ).getSimpleName() );
method.append( typeMirrorToString( ee.getReturnType() ) );
method.append( " " );
method.append( ee.getSimpleName() );
method.append( "(" );
@ -112,7 +114,12 @@ public class MapperAnnotatedFormattingMessenger implements FormattingMessager {
}
private String parameterToString(VariableElement element) {
return typeUtils.asElement( element.asType() ).getSimpleName() + " " + element.getSimpleName();
return typeMirrorToString( element.asType() ) + " " + element.getSimpleName();
}
private String typeMirrorToString(TypeMirror type) {
Element element = typeUtils.asElement( type );
return element != null ? element.getSimpleName().toString() : Objects.toString( type );
}
private Message determineDelegationMessage(Element e, Message msg) {

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._2867;
import org.mapstruct.MappingTarget;
/**
* @author Filip Hrisafov
*/
public interface Issue2867BaseMapper<T, S> {
void update(@MappingTarget T target, S source);
}

View File

@ -0,0 +1,31 @@
/*
* 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._2867;
import org.mapstruct.Mapper;
/**
* @author Filip Hrisafov
*/
@Mapper
public interface Issue2867Mapper extends Issue2867BaseMapper<Issue2867Mapper.Target, Issue2867Mapper.Source> {
class Target {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Source {
}
}

View File

@ -0,0 +1,39 @@
/*
* 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._2867;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
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;
/**
* @author Filip Hrisafov
*/
@WithClasses({
Issue2867BaseMapper.class,
Issue2867Mapper.class,
})
@IssueKey("2867")
class Issue2867Test {
@ExpectedCompilationOutcome(
value = CompilationResult.SUCCEEDED,
diagnostics = @Diagnostic(
type = Issue2867Mapper.class,
kind = javax.tools.Diagnostic.Kind.WARNING,
line = 14,
message = "Unmapped target property: \"name\"." +
" Occured at 'void update(T target, S source)' in 'Issue2867BaseMapper'."
)
)
@ProcessorTest
void shouldCompile() {
}
}