Skip to content

Commit

Permalink
Added patch from Jasper. Addressed Jasper's comments. Changed generat…
Browse files Browse the repository at this point in the history
…ed code directory layout to match Maven.

Signed-off-by: Ivan Malygin <ivan@swirldslabs.com>
  • Loading branch information
imalygin committed Nov 20, 2023
1 parent 921e6c6 commit e21de96
Show file tree
Hide file tree
Showing 11 changed files with 608 additions and 416 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public void apply(Project project) {
// get java src sets
final var mainSrcSet = javaPlugin.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
final var testSrcSet = javaPlugin.getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME);
final String outputDirectory = "generated/source/pbj-proto/main/";
final String outputDirectory = "generated/source/pbj-proto/";
final Provider<Directory> outputDirectoryMain =
project.getLayout().getBuildDirectory().dir(outputDirectory + "java");
project.getLayout().getBuildDirectory().dir(outputDirectory + "main/java");
final Provider<Directory> outputDirectoryTest =
project.getLayout().getBuildDirectory().dir(outputDirectory + "test");
project.getLayout().getBuildDirectory().dir(outputDirectory + "test/java");

// for the 'main' source set we:
// 1) Add a new 'pbj' virtual directory mapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ else if (f.nameCamelFirstLower() != null) {
generatedCodeSoFar += (
"""
if ($fieldName != DEFAULT.$fieldName) {
result = 31 * result + Integer.hashCode($fieldName);
result = 31 * result + Integer.hashCode($fieldName);
}
""").replace("$fieldName", f.nameCamelFirstLower());
} else if (f.type() == Field.FieldType.FIXED64 ||
Expand All @@ -214,57 +214,57 @@ else if (f.nameCamelFirstLower() != null) {
generatedCodeSoFar += (
"""
if ($fieldName != DEFAULT.$fieldName) {
result = 31 * result + Long.hashCode($fieldName);
result = 31 * result + Long.hashCode($fieldName);
}
""").replace("$fieldName", f.nameCamelFirstLower());
} else if (f.type() == Field.FieldType.ENUM) {
generatedCodeSoFar += (
"""
if ($fieldName != DEFAULT.$fieldName) {
result = 31 * result + $fieldName.hashCode();
if ($fieldName != null && !$fieldName.equals(DEFAULT.$fieldName)) {
result = 31 * result + $fieldName.hashCode();
}
""").replace("$fieldName", f.nameCamelFirstLower());
} else if (f.type() == Field.FieldType.BOOL) {
generatedCodeSoFar += (
"""
if ($fieldName != DEFAULT.$fieldName) {
result = 31 * result + Boolean.hashCode($fieldName);
result = 31 * result + Boolean.hashCode($fieldName);
}
""").replace("$fieldName", f.nameCamelFirstLower());
} else if (f.type() == Field.FieldType.FLOAT) {
generatedCodeSoFar += (
"""
if ($fieldName != DEFAULT.$fieldName) {
result = 31 * result + Float.hashCode($fieldName);
"""
if ($fieldName != DEFAULT.$fieldName) {
result = 31 * result + Float.hashCode($fieldName);
}
""").replace("$fieldName", f.nameCamelFirstLower());
} else if (f.type() == Field.FieldType.DOUBLE) {
generatedCodeSoFar += (
"""
if ($fieldName != DEFAULT.$fieldName) {
result = 31 * result + Double.hashCode($fieldName);
result = 31 * result + Double.hashCode($fieldName);
}
""").replace("$fieldName", f.nameCamelFirstLower());
} else if (f.type() == Field.FieldType.STRING) {
generatedCodeSoFar += (
"""
if ($fieldName != DEFAULT.$fieldName) {
result = 31 * result + $fieldName.hashCode();
if ($fieldName != null && !$fieldName.equals(DEFAULT.$fieldName)) {
result = 31 * result + $fieldName.hashCode();
}
""").replace("$fieldName", f.nameCamelFirstLower());
} else if (f.type() == Field.FieldType.BYTES) {
generatedCodeSoFar += (
"""
if ($fieldName != DEFAULT.$fieldName) {
result = 31 * result + ($fieldName == null ? 0 : $fieldName.hashCode());
if ($fieldName != null && !$fieldName.equals(DEFAULT.$fieldName)) {
result = 31 * result + ($fieldName == null ? 0 : $fieldName.hashCode());
}
""").replace("$fieldName", f.nameCamelFirstLower());
}
else if (f.parent() == null) { // process sub message
generatedCodeSoFar += (
"""
if ($fieldName != null && $fieldName != DEFAULT.$fieldName) {
result = 31 * result + $fieldName.hashCode();
if ($fieldName != null && !$fieldName.equals(DEFAULT.$fieldName)) {
result = 31 * result + $fieldName.hashCode();
}
""").replace("$fieldName", f.nameCamelFirstLower());
}
Expand All @@ -287,44 +287,44 @@ private static String getOptionalHashCodeGeneration(String generatedCodeSoFar, F
switch (f.messageType()) {
case "StringValue" -> generatedCodeSoFar += (

Check warning on line 288 in pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/Common.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/Common.java#L288

Avoid reassigning parameters such as 'generatedCodeSoFar'
"""
if (!DEFAULT.$fieldName.equals($fieldName)) {
result = 31 * result + $fieldName.hashCode();
if ($fieldName != null && !$fieldName.equals(DEFAULT.$fieldName)) {
result = 31 * result + $fieldName.hashCode();
}
""").replace("$fieldName", f.nameCamelFirstLower());
case "BoolValue" -> generatedCodeSoFar += (
"""
if (!DEFAULT.$fieldName.equals($fieldName)) {
result = 31 * result + Boolean.hashCode($fieldName);
if ($fieldName != null && !$fieldName.equals(DEFAULT.$fieldName)) {
result = 31 * result + Boolean.hashCode($fieldName);
}
""").replace("$fieldName", f.nameCamelFirstLower());
case "Int32Value", "UInt32Value" -> generatedCodeSoFar += (
"""
if (DEFAULT.$fieldName.equals($fieldName)) {
result = 31 * result + Integer.hashCode($fieldName);
if ($fieldName != null && !$fieldName.equals(DEFAULT.$fieldName)) {
result = 31 * result + Integer.hashCode($fieldName);
}
""").replace("$fieldName", f.nameCamelFirstLower());
case "Int64Value", "UInt64Value" -> generatedCodeSoFar += (
"""
if (DEFAULT.$fieldName.equals($fieldName)) {
result = 31 * result + Long.hashCode($fieldName);
if ($fieldName != null && !$fieldName.equals(DEFAULT.$fieldName)) {
result = 31 * result + Long.hashCode($fieldName);
}
""").replace("$fieldName", f.nameCamelFirstLower());
case "FloatValue" -> generatedCodeSoFar += (
"""
if (DEFAULT.$fieldName.equals($fieldName)) {
result = 31 * result + Float.hashCode($fieldName);
if ($fieldName != null && !$fieldName.equals(DEFAULT.$fieldName)) {
result = 31 * result + Float.hashCode($fieldName);
}
""").replace("$fieldName", f.nameCamelFirstLower());
case "DoubleValue" -> generatedCodeSoFar += (
"""
if (DEFAULT.$fieldName.equals($fieldName)) {
result = 31 * result + Double.hashCode($fieldName);
if ($fieldName != null && !$fieldName.equals(DEFAULT.$fieldName)) {
result = 31 * result + Double.hashCode($fieldName);
}
""").replace("$fieldName", f.nameCamelFirstLower());
case "BytesValue" -> generatedCodeSoFar += (
"""
if (DEFAULT.$fieldName.equals($fieldName)){
result = 31 * result + ($fieldName == null ? 0 : $fieldName.hashCode());
if ($fieldName != null && !$fieldName.equals(DEFAULT.$fieldName)) {
result = 31 * result + ($fieldName == null ? 0 : $fieldName.hashCode());
}
""").replace("$fieldName", f.nameCamelFirstLower());
default -> throw new UnsupportedOperationException("Unhandled optional message type:" + f.messageType());

Check notice on line 330 in pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/Common.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/Common.java#L330

Line is longer than 120 characters (found 129).
Expand All @@ -344,11 +344,11 @@ private static String getRepeatedHashCodeGeneration(String generatedCodeSoFar, F
"""
java.util.List list$$fieldName = $fieldName;
for (Object o : list$$fieldName) {
if (o != null) {
result = 31 * result + o.hashCode();
} else {
result = 31 * result;
}
if (o != null) {
result = 31 * result + o.hashCode();
} else {
result = 31 * result;
}
}
""").replace("$fieldName", f.nameCamelFirstLower());
return generatedCodeSoFar;
Expand Down Expand Up @@ -383,7 +383,7 @@ else if (f.nameCamelFirstLower() != null) {
generatedCodeSoFar +=
"""
if ($fieldName != thatObj.$fieldName) {
return false;
return false;
}
""".replace("$fieldName", f.nameCamelFirstLower());
} else if (f.type() == Field.FieldType.FIXED64 ||
Expand All @@ -394,29 +394,29 @@ else if (f.nameCamelFirstLower() != null) {
generatedCodeSoFar +=
"""
if ($fieldName != thatObj.$fieldName) {
return false;
return false;
}
""".replace("$fieldName", f.nameCamelFirstLower());
} else if (f.type() == Field.FieldType.BOOL) {
generatedCodeSoFar +=
"""
if ($fieldName != thatObj.$fieldName) {
return false;
return false;
}
""".replace("$fieldName", f.nameCamelFirstLower());
} else if (f.type() == Field.FieldType.DOUBLE) {

Check notice on line 407 in pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/Common.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/Common.java#L407

Must have at least one statement.
} else if (f.type() == Field.FieldType.FLOAT) {
generatedCodeSoFar +=
"""
if ($fieldName != thatObj.$fieldName) {
return false;
return false;
}
""".replace("$fieldName", f.nameCamelFirstLower());
} else if (f.type() == Field.FieldType.DOUBLE) {
generatedCodeSoFar +=
"""
if ($fieldName != thatObj.$fieldName) {
return false;
return false;
}
""".replace("$fieldName", f.nameCamelFirstLower());
} else if (f.type() == Field.FieldType.STRING ||
Expand All @@ -426,10 +426,10 @@ else if (f.nameCamelFirstLower() != null) {
generatedCodeSoFar += (
"""
if ($fieldName == null && thatObj.$fieldName != null) {
return false;
return false;
}
if ($fieldName != null && !$fieldName.equals(thatObj.$fieldName)) {
return false;
return false;
}
""").replace("$fieldName", f.nameCamelFirstLower());
}
Expand All @@ -454,48 +454,48 @@ private static String getOptionalEqualsGeneration(String generatedCodeSoFar, Fie
generatedCodeSoFar += (
"""
if (this.$fieldName == null && thatObj.$fieldName != null) {
return false;
return false;
}
if (this.$fieldName != null && !$fieldName.equals(thatObj.$fieldName)) {
return false;
return false;
}
""").replace("$fieldName", f.nameCamelFirstLower());
case "BoolValue" ->
generatedCodeSoFar += (
"""
if ($fieldName instanceof Object) {
if (this.$fieldName == null && thatObj.$fieldName != null) {
return false;
}
if (!$fieldName.equals(thatObj.$fieldName)) {
return false;
}
if (this.$fieldName == null && thatObj.$fieldName != null) {
return false;
}
if (!$fieldName.equals(thatObj.$fieldName)) {
return false;
}
} else if ($fieldName != thatObj.$fieldName) {
return false;
return false;
}
""").replace("$fieldName", f.nameCamelFirstLower());
case "Int32Value", "UInt32Value", "Int64Value", "UInt64Value", "FloatValue", "DoubleValue" ->
generatedCodeSoFar += (
"""
if ($fieldName instanceof Object) {
if (this.$fieldName == null && thatObj.$fieldName != null) {
return false;
}
if (this.$fieldName != null && !$fieldName.equals(thatObj.$fieldName)) {
return false;
}
if (this.$fieldName == null && thatObj.$fieldName != null) {
return false;
}
if (this.$fieldName != null && !$fieldName.equals(thatObj.$fieldName)) {
return false;
}
} else if ($fieldName != thatObj.$fieldName) {
return false;
return false;
}
""").replace("$fieldName", f.nameCamelFirstLower());
case "BytesValue" ->
generatedCodeSoFar += (
"""
if (this.$fieldName == null && thatObj.$fieldName != null) {
return false;
return false;
}
if (this.$fieldName != null && !$fieldName.equals(thatObj.$fieldName)) {
return false;
return false;
}
""").replace("$fieldName", f.nameCamelFirstLower());
default -> throw new UnsupportedOperationException("Unhandled optional message type:" + f.messageType());
Expand All @@ -515,11 +515,11 @@ private static String getRepeatedEqualsGeneration(String generatedCodeSoFar, Fie
generatedCodeSoFar += (
"""
if (this.$fieldName == null && this.$fieldName != null) {
return false;
return false;
}
if (this.$fieldName != null && !$fieldName.equals(thatObj.$fieldName)) {
return false;
return false;
}
""").replace("$fieldName", f.nameCamelFirstLower());
return generatedCodeSoFar;
Expand Down
Loading

0 comments on commit e21de96

Please sign in to comment.