#3786: Improve error message when mapping non-iterable to array

This commit is contained in:
Zegveld 2025-01-24 14:41:35 +01:00 committed by GitHub
parent c08ba4ca7e
commit 39551242d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 55 additions and 0 deletions

View File

@ -555,6 +555,11 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
return false; return false;
} }
if ( !parameterType.isIterableOrStreamType() && resultType.isArrayType() ) {
messager.printMessage( method, Message.RETRIEVAL_NON_ITERABLE_TO_ARRAY );
return false;
}
if ( parameterType.isPrimitive() ) { if ( parameterType.isPrimitive() ) {
messager.printMessage( method, Message.RETRIEVAL_PRIMITIVE_PARAMETER ); messager.printMessage( method, Message.RETRIEVAL_PRIMITIVE_PARAMETER );
return false; return false;

View File

@ -175,6 +175,7 @@ public enum Message {
RETRIEVAL_ITERABLE_TO_NON_ITERABLE( "Can't generate mapping method from iterable type from java stdlib to non-iterable type." ), RETRIEVAL_ITERABLE_TO_NON_ITERABLE( "Can't generate mapping method from iterable type from java stdlib to non-iterable type." ),
RETRIEVAL_MAPPING_HAS_TARGET_TYPE_PARAMETER( "Can't generate mapping method that has a parameter annotated with @TargetType." ), RETRIEVAL_MAPPING_HAS_TARGET_TYPE_PARAMETER( "Can't generate mapping method that has a parameter annotated with @TargetType." ),
RETRIEVAL_NON_ITERABLE_TO_ITERABLE( "Can't generate mapping method from non-iterable type to iterable type from java stdlib." ), RETRIEVAL_NON_ITERABLE_TO_ITERABLE( "Can't generate mapping method from non-iterable type to iterable type from java stdlib." ),
RETRIEVAL_NON_ITERABLE_TO_ARRAY( "Can't generate mapping method from non-iterable type to array." ),
RETRIEVAL_PRIMITIVE_PARAMETER( "Can't generate mapping method with primitive parameter type." ), RETRIEVAL_PRIMITIVE_PARAMETER( "Can't generate mapping method with primitive parameter type." ),
RETRIEVAL_PRIMITIVE_RETURN( "Can't generate mapping method with primitive return type." ), RETRIEVAL_PRIMITIVE_RETURN( "Can't generate mapping method with primitive return type." ),
RETRIEVAL_TYPE_VAR_SOURCE( "Can't generate mapping method for a generic type variable source." ), RETRIEVAL_TYPE_VAR_SOURCE( "Can't generate mapping method for a generic type variable source." ),

View File

@ -0,0 +1,13 @@
/*
* 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._3786;
import org.mapstruct.Mapper;
@Mapper
public interface ErroneousByteArrayMapper {
byte[] map( String something );
}

View File

@ -0,0 +1,36 @@
/*
* 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._3786;
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 Ben Zegveld
*/
@IssueKey( "3786" )
public class Issue3786Test {
@WithClasses( ErroneousByteArrayMapper.class )
@ProcessorTest
@ExpectedCompilationOutcome(
value = CompilationResult.FAILED,
diagnostics = {
@Diagnostic(
type = ErroneousByteArrayMapper.class,
kind = javax.tools.Diagnostic.Kind.ERROR,
line = 12,
message = "Can't generate mapping method from non-iterable type to array."
)
}
)
void byteArrayReturnTypeShouldGiveInaccessibleContructorError() {
}
}