|
1 | 1 | package org.mapstruct.example.protobuf; |
2 | 2 |
|
3 | | -import org.mapstruct.ap.spi.DefaultAccessorNamingStrategy; |
4 | | - |
5 | 3 | import javax.lang.model.element.Element; |
6 | | -import javax.lang.model.element.ElementKind; |
7 | 4 | import javax.lang.model.element.ExecutableElement; |
8 | 5 | import javax.lang.model.element.TypeElement; |
9 | 6 | import javax.lang.model.type.TypeMirror; |
10 | 7 |
|
| 8 | +import org.mapstruct.ap.spi.DefaultAccessorNamingStrategy; |
| 9 | +import org.mapstruct.ap.spi.MapStructProcessingEnvironment; |
| 10 | + |
11 | 11 | /** |
12 | 12 | * @author Thomas Kratz |
| 13 | + * @author Filip Hrisafov |
13 | 14 | */ |
14 | 15 | public class ProtobufAccessorNamingStrategy extends DefaultAccessorNamingStrategy { |
15 | 16 |
|
16 | | - public static final String PROTOBUF_GENERATED_MESSAGE_V3 = "com.google.protobuf.GeneratedMessageV3"; |
| 17 | + public static final String PROTOBUF_MESSAGE_OR_BUILDER = "com.google.protobuf.MessageOrBuilder"; |
17 | 18 | public static final String LIST_SUFFIX = "List"; |
18 | 19 |
|
| 20 | + protected TypeMirror protobufMesageOrBuilderType; |
| 21 | + |
| 22 | + @Override |
| 23 | + public void init(MapStructProcessingEnvironment processingEnvironment) { |
| 24 | + super.init(processingEnvironment); |
| 25 | + |
| 26 | + TypeElement typeElement = elementUtils.getTypeElement(PROTOBUF_MESSAGE_OR_BUILDER); |
| 27 | + if (typeElement != null) { |
| 28 | + protobufMesageOrBuilderType = typeElement.asType(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
19 | 32 | @Override |
20 | 33 | public String getElementName(ExecutableElement adderMethod) { |
21 | 34 |
|
22 | 35 | String methodName = super.getElementName(adderMethod); |
23 | 36 | Element receiver = adderMethod.getEnclosingElement(); |
24 | | - if (receiver != null && receiver.getKind() == ElementKind.CLASS) { |
25 | | - TypeElement type = (TypeElement) receiver; |
26 | | - TypeMirror superType = type.getSuperclass(); |
27 | | - if (superType != null && PROTOBUF_GENERATED_MESSAGE_V3.equals(superType.toString())) { |
28 | | - methodName += LIST_SUFFIX; |
29 | | - } |
| 37 | + if (receiver != null && protobufMesageOrBuilderType != null && typeUtils.isAssignable(receiver.asType(), protobufMesageOrBuilderType)) { |
| 38 | + methodName += LIST_SUFFIX; |
30 | 39 | } |
31 | 40 | return methodName; |
32 | 41 | } |
|
0 commit comments