|
2 | 2 |
|
3 | 3 | import com.google.protobuf.DescriptorProtos; |
4 | 4 |
|
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
5 | 10 | public class ConstructorHelperGenerator { |
6 | | - public String generateConstructorHelper(String protoPackage, String originalClassName, DescriptorProtos.DescriptorProto message) { |
7 | | - StringBuilder sb = new StringBuilder(); |
| 11 | + public String generateConstructorHelper(String protoPackage, String originalClassName, DescriptorProtos.DescriptorProto descriptorProto) { |
| 12 | + StringBuilder builder = new StringBuilder(); |
| 13 | + String packageDecl = protoPackage.isEmpty() ? "" : "package " + protoPackage + ";\n\n"; |
| 14 | + |
| 15 | + builder.append(packageDecl); |
| 16 | + builder.append(generateImports(descriptorProto, protoPackage)); |
| 17 | + builder.append("public final class ") |
| 18 | + .append(originalClassName) |
| 19 | + .append("Constructor {\n\n") |
| 20 | + .append(" private ") |
| 21 | + .append(originalClassName) |
| 22 | + .append("Constructor() {}\n\n") |
| 23 | + .append(" public static ") |
| 24 | + .append(originalClassName) |
| 25 | + .append(" from("); |
8 | 26 |
|
9 | | - sb.append("package ").append(protoPackage).append(";\n\n"); |
10 | | - sb.append("public final class ").append(originalClassName).append("Constructor {\n\n"); |
| 27 | + for (int i = 0; i < descriptorProto.getFieldCount(); i++) { |
| 28 | + DescriptorProtos.FieldDescriptorProto field = descriptorProto.getField(i); |
| 29 | + if (i > 0) builder.append(", "); |
| 30 | + builder.append(JavaTypeMapper.map(field)).append(" ").append(field.getName()); |
| 31 | + } |
11 | 32 |
|
12 | | - sb.append(" private ").append(originalClassName).append("Constructor() {}\n\n"); |
| 33 | + builder.append(") {\n") |
| 34 | + .append(" ") |
| 35 | + .append(originalClassName) |
| 36 | + .append(".Builder builder = ") |
| 37 | + .append(originalClassName) |
| 38 | + .append(".newBuilder();\n"); |
13 | 39 |
|
14 | | - sb.append(" public static ").append(originalClassName).append(" from("); |
15 | | - for (int i = 0; i < message.getFieldCount(); i++) { |
16 | | - DescriptorProtos.FieldDescriptorProto field = message.getField(i); |
17 | | - sb.append(JavaTypeMapper.map(field.getType())).append(" ").append(field.getName()); |
18 | | - if (i < message.getFieldCount() - 1) { |
19 | | - sb.append(", "); |
| 40 | + for (DescriptorProtos.FieldDescriptorProto field : descriptorProto.getFieldList()) { |
| 41 | + String fieldName = field.getName(); |
| 42 | + String setter = "set" + StringUtil.capitalize(fieldName); |
| 43 | + if (field.hasProto3Optional()) { |
| 44 | + builder.append(" if (") |
| 45 | + .append(fieldName) |
| 46 | + .append(" != null) {\n") |
| 47 | + .append(" builder.") |
| 48 | + .append(setter) |
| 49 | + .append("(") |
| 50 | + .append(fieldName) |
| 51 | + .append(");\n") |
| 52 | + .append(" }\n"); |
| 53 | + } else { |
| 54 | + builder.append(" builder.") |
| 55 | + .append(setter) |
| 56 | + .append("(") |
| 57 | + .append(fieldName) |
| 58 | + .append(");\n"); |
20 | 59 | } |
21 | 60 | } |
22 | | - sb.append(") {\n"); |
23 | 61 |
|
24 | | - sb.append(" return ").append(originalClassName).append(".newBuilder()\n"); |
25 | | - for (DescriptorProtos.FieldDescriptorProto field : message.getFieldList()) { |
26 | | - sb.append(" .set").append(StringUtil.capitalize(field.getName())).append("(").append(field.getName()).append(")\n"); |
27 | | - } |
28 | | - sb.append(" .build();\n"); |
29 | | - sb.append(" }\n"); |
| 62 | + builder.append(" return builder.build();\n") |
| 63 | + .append(" }\n") |
| 64 | + .append("}\n"); |
| 65 | + |
| 66 | + return builder.toString(); |
| 67 | + } |
30 | 68 |
|
31 | | - sb.append("}\n"); |
| 69 | + private String generateImports(DescriptorProtos.DescriptorProto descriptorProto, String packageName) { |
| 70 | + Set<String> imports = new HashSet<>(); |
| 71 | + |
| 72 | + for (DescriptorProtos.FieldDescriptorProto field : descriptorProto.getFieldList()) { |
| 73 | + DescriptorProtos.FieldDescriptorProto.Type type = field.getType(); |
| 74 | + if (type == DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE || |
| 75 | + type == DescriptorProtos.FieldDescriptorProto.Type.TYPE_ENUM) { |
| 76 | + |
| 77 | + String fieldType = JavaTypeMapper.map(field); |
| 78 | + |
| 79 | + if (!List.of("int", "long", "float", "double", "boolean", "String", "Object").contains(fieldType)) { |
| 80 | + imports.add("import " + packageName + "." + fieldType + ";"); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
32 | 84 |
|
33 | | - return sb.toString(); |
| 85 | + return imports.stream() |
| 86 | + .sorted() |
| 87 | + .collect(Collectors.joining("\n")) + "\n\n"; |
34 | 88 | } |
35 | 89 | } |
0 commit comments