Skip to content

Commit

Permalink
feat[pb]: generates an all-args constructor. If no field in the class…
Browse files Browse the repository at this point in the history
…, no constructor will be generated.
  • Loading branch information
jaysunxiao committed Jan 19, 2024
1 parent 3459d41 commit 30790b6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public static void generate(PbGenerateOperation pbGenerateOperation, List<Proto>
var recordBody = buildRecordBody(pbMessage);
builder.append(GenerateProtocolFile.addTabs(recordBody, 1));
} else {
var classBody = buildClassBody(pbMessage);
var classBody = buildClassBody(pbGenerateOperation, pbMessage);
classBody = classBody.replaceFirst("public class ", "public static class ");
builder.append(GenerateProtocolFile.addTabs(classBody, 1));
}
Expand Down Expand Up @@ -247,7 +247,7 @@ public static String buildMessage(PbGenerateOperation pbGenerateOperation, List<
var recordBody = buildRecordBody(pbMessage);
builder.append(recordBody);
} else {
var classBody = buildClassBody(pbMessage);
var classBody = buildClassBody(pbGenerateOperation, pbMessage);
builder.append(classBody);
}
return builder.toString();
Expand Down Expand Up @@ -398,7 +398,7 @@ private static String buildRecordBody(PbMessage pbMessage) {
return builder.toString();
}

private static String buildClassBody(PbMessage pbMessage) {
private static String buildClassBody(PbGenerateOperation pbGenerateOperation, PbMessage pbMessage) {
var builder = new StringBuilder();
builder.append(StringUtils.format("@Protocol(id = {})", pbMessage.getProtocolId())).append(LS);
builder.append(StringUtils.format("public class {} {", pbMessage.getName())).append(LS);
Expand All @@ -408,37 +408,56 @@ private static String buildClassBody(PbMessage pbMessage) {
.sorted((a, b) -> a.getTag() - b.getTag())
.toList();

var builderMethod = new StringBuilder();
var fieldBuilder = new StringBuilder();
var valueOfBuilder = new StringBuilder();
var methodBuilder = new StringBuilder();

valueOfBuilder.append(TAB).append(StringUtils.format("public {} {", pbMessage.getName())).append(LS);
for (var pbField : pbFields) {
var type = getJavaType(pbField);
var name = pbField.getName();

var fieldComment = buildFieldComment(pbField);
builder.append(fieldComment);
fieldBuilder.append(fieldComment);
if (isCompatiblePbField(pbField)) {
var tag = pbField.getTag() - COMPATIBLE_FIELD_TAG;
builder.append(TAB).append(StringUtils.format("@Compatible({})", tag)).append(LS);
fieldBuilder.append(TAB).append(StringUtils.format("@Compatible({})", tag)).append(LS);
}
builder.append(TAB).append(StringUtils.format("private {} {};", type, name)).append(LS);
fieldBuilder.append(TAB).append(StringUtils.format("private {} {};", type, name)).append(LS);

// valueOf
valueOfBuilder.append(TAB + TAB).append(StringUtils.format("this.{} = {};", name, name)).append(LS);

// method
String getMethod;
if (!"bool".equalsIgnoreCase(pbField.getType())) {
getMethod = StringUtils.format("get{}", StringUtils.capitalize(pbField.getName()));
} else {
getMethod = StringUtils.format("is{}", StringUtils.capitalize(pbField.getName()));
}

builderMethod.append(TAB).append(StringUtils.format("public {} {}() {", type, getMethod)).append(LS);
builderMethod.append(TAB + TAB).append(StringUtils.format("return {};", pbField.getName())).append(LS);
builderMethod.append(TAB).append("}").append(LS);
methodBuilder.append(TAB).append(StringUtils.format("public {} {}() {", type, getMethod)).append(LS);
methodBuilder.append(TAB + TAB).append(StringUtils.format("return {};", pbField.getName())).append(LS);
methodBuilder.append(TAB).append("}").append(LS);

String setMethod = StringUtils.format("set{}", StringUtils.capitalize(pbField.getName()));
builderMethod.append(TAB).append(StringUtils.format("public void {}({} {}) {", setMethod, type, pbField.getName())).append(LS);
builderMethod.append(TAB + TAB).append(StringUtils.format("this.{} = {};", pbField.getName(), pbField.getName())).append(LS);
builderMethod.append(TAB).append("}").append(LS);
methodBuilder.append(TAB).append(StringUtils.format("public void {}({} {}) {", setMethod, type, pbField.getName())).append(LS);
methodBuilder.append(TAB + TAB).append(StringUtils.format("this.{} = {};", pbField.getName(), pbField.getName())).append(LS);
methodBuilder.append(TAB).append("}").append(LS);
}
valueOfBuilder.append(TAB).append("}").append(LS);

builder.append(LS).append(fieldBuilder);

if (pbGenerateOperation.isAllArgsConstructor() && CollectionUtils.isNotEmpty(pbFields)) {
// no args constructor
builder.append(TAB).append(StringUtils.format("public {} {", pbMessage.getName())).append(LS)
.append(TAB).append("}").append(LS);
// all args constructor
builder.append(valueOfBuilder);
}

builder.append(LS).append(builderMethod);
builder.append(LS).append(methodBuilder);
builder.append("}");
return builder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class PbGenerateOperation {
private String protoPath;
private String outputPath;
private String javaPackage;
/**
* Generates an all-args constructor. If no field in the class, no constructor will be generated.
*/
private boolean allArgsConstructor;
/**
* Whether generated class is record
*/
Expand Down Expand Up @@ -65,10 +69,22 @@ public void setJavaPackage(String javaPackage) {
this.javaPackage = javaPackage;
}

public boolean isAllArgsConstructor() {
return allArgsConstructor;
}

public void setAllArgsConstructor(boolean allArgsConstructor) {
this.allArgsConstructor = allArgsConstructor;
}

public boolean isRecordClass() {
return recordClass;
}

public void setRecordClass(boolean recordClass) {
this.recordClass = recordClass;
}

public List<String> getRecordExcludes() {
return recordExcludes;
}
Expand All @@ -77,10 +93,6 @@ public void setRecordExcludes(List<String> recordExcludes) {
this.recordExcludes = recordExcludes;
}

public void setRecordClass(boolean recordClass) {
this.recordClass = recordClass;
}

public boolean isOneProtocol() {
return oneProtocol;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public void generateClassFromProto() {
buildOption.setProtoPath("src\\test\\protobuf");
buildOption.setOutputPath("zfoopb");
buildOption.setJavaPackage("com.zfoo.protocol.generate.test");
// buildOption.setRecordClass(true);
buildOption.setRecordClass(true);
// buildOption.setAllArgsConstructor(true);
// buildOption.getRecordExcludes().add("SimpleObject");
// buildOption.setOneProtocol(true);
GeneratePbUtils.create(buildOption);
Expand Down

0 comments on commit 30790b6

Please sign in to comment.