Skip to content

Commit 3955124

Browse files
authored
mapstruct#3786: Improve error message when mapping non-iterable to array
1 parent c08ba4c commit 3955124

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

processor/src/main/java/org/mapstruct/ap/internal/processor/MethodRetrievalProcessor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,11 @@ private boolean checkParameterAndReturnType(ExecutableElement method, List<Param
555555
return false;
556556
}
557557

558+
if ( !parameterType.isIterableOrStreamType() && resultType.isArrayType() ) {
559+
messager.printMessage( method, Message.RETRIEVAL_NON_ITERABLE_TO_ARRAY );
560+
return false;
561+
}
562+
558563
if ( parameterType.isPrimitive() ) {
559564
messager.printMessage( method, Message.RETRIEVAL_PRIMITIVE_PARAMETER );
560565
return false;

processor/src/main/java/org/mapstruct/ap/internal/util/Message.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public enum Message {
175175
RETRIEVAL_ITERABLE_TO_NON_ITERABLE( "Can't generate mapping method from iterable type from java stdlib to non-iterable type." ),
176176
RETRIEVAL_MAPPING_HAS_TARGET_TYPE_PARAMETER( "Can't generate mapping method that has a parameter annotated with @TargetType." ),
177177
RETRIEVAL_NON_ITERABLE_TO_ITERABLE( "Can't generate mapping method from non-iterable type to iterable type from java stdlib." ),
178+
RETRIEVAL_NON_ITERABLE_TO_ARRAY( "Can't generate mapping method from non-iterable type to array." ),
178179
RETRIEVAL_PRIMITIVE_PARAMETER( "Can't generate mapping method with primitive parameter type." ),
179180
RETRIEVAL_PRIMITIVE_RETURN( "Can't generate mapping method with primitive return type." ),
180181
RETRIEVAL_TYPE_VAR_SOURCE( "Can't generate mapping method for a generic type variable source." ),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._3786;
7+
8+
import org.mapstruct.Mapper;
9+
10+
@Mapper
11+
public interface ErroneousByteArrayMapper {
12+
byte[] map( String something );
13+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._3786;
7+
8+
import org.mapstruct.ap.testutil.IssueKey;
9+
import org.mapstruct.ap.testutil.ProcessorTest;
10+
import org.mapstruct.ap.testutil.WithClasses;
11+
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
12+
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
13+
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
14+
15+
/**
16+
* @author Ben Zegveld
17+
*/
18+
@IssueKey( "3786" )
19+
public class Issue3786Test {
20+
21+
@WithClasses( ErroneousByteArrayMapper.class )
22+
@ProcessorTest
23+
@ExpectedCompilationOutcome(
24+
value = CompilationResult.FAILED,
25+
diagnostics = {
26+
@Diagnostic(
27+
type = ErroneousByteArrayMapper.class,
28+
kind = javax.tools.Diagnostic.Kind.ERROR,
29+
line = 12,
30+
message = "Can't generate mapping method from non-iterable type to array."
31+
)
32+
}
33+
)
34+
void byteArrayReturnTypeShouldGiveInaccessibleContructorError() {
35+
}
36+
}

0 commit comments

Comments
 (0)