Skip to content

Commit

Permalink
[Java] Prevent collision when field name is 'value'.
Browse files Browse the repository at this point in the history
`value` is used as a parameter name in some DTO methods. When an SBE
field was named `value` it caused an issue due to a collision. We now
make sure to reference fields more explicitly via `this.fieldName`.
  • Loading branch information
ZachBray committed Dec 6, 2024
1 parent eb24bb0 commit 68aeb1a
Showing 1 changed file with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,15 @@ private void generateGroups(
.append(indent).append("public List<").append(qualifiedDtoClassName).append("> ")
.append(formattedPropertyName).append("()\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append("return ").append(fieldName).append(";\n")
.append(indent).append(INDENT).append("return this.").append(fieldName).append(";\n")
.append(indent).append("}\n");

classBuilder.appendPublic().append("\n")
.append(generateDocumentation(indent, groupToken))
.append(indent).append("public void ").append(formattedPropertyName).append("(")
.append("List<").append(qualifiedDtoClassName).append("> value)")
.append(indent).append("{\n")
.append(indent).append(INDENT).append(fieldName).append(" = value;\n")
.append(indent).append(INDENT).append("this.").append(fieldName).append(" = value;\n")
.append(indent).append("}\n");
}
}
Expand Down Expand Up @@ -350,7 +350,7 @@ private void generateComputeEncodedLength(
.append(indent).append(INDENT).append("encodedLength += ")
.append(groupDecoderClassName).append(".sbeHeaderSize();\n\n")
.append(indent).append(INDENT).append("for (").append(groupDtoClassName).append(" group : ")
.append(fieldName).append(")\n")
.append("this.").append(fieldName).append(")\n")
.append(indent).append(INDENT).append("{\n")
.append(indent).append(INDENT).append(INDENT)
.append("encodedLength += group.computeEncodedLength();\n")
Expand All @@ -373,7 +373,7 @@ private void generateComputeEncodedLength(
final String characterEncoding = varDataToken.encoding().characterEncoding();
final String lengthAccessor = characterEncoding == null ? ".length" : ".length()";
lengthBuilder.append(indent).append(INDENT).append("encodedLength += ")
.append(fieldName).append(lengthAccessor);
.append("this.").append(fieldName).append(lengthAccessor);

final int elementByteLength = varDataToken.encoding().primitiveType().size();
if (elementByteLength != 1)
Expand Down Expand Up @@ -1255,15 +1255,15 @@ private void generateComplexProperty(
.append(indent).append("public ").append(typeName).append(" ")
.append(formattedPropertyName).append("()\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append("return ").append(fieldName).append(";\n")
.append(indent).append(INDENT).append("return this.").append(fieldName).append(";\n")
.append(indent).append("}\n");

classBuilder.appendPublic().append("\n")
.append(generateDocumentation(indent, fieldToken))
.append(indent).append("public void ")
.append(formattedPropertyName).append("(").append(typeName).append(" value)\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append(fieldName).append(" = value;\n")
.append(indent).append(INDENT).append("this.").append(fieldName).append(" = value;\n")
.append(indent).append("}\n");
}

Expand Down Expand Up @@ -1304,15 +1304,15 @@ private void generateEnumProperty(
.append(indent).append("public ").append(enumName).append(" ")
.append(formattedPropertyName).append("()\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append("return ").append(fieldName).append(";\n")
.append(indent).append(INDENT).append("return this.").append(fieldName).append(";\n")
.append(indent).append("}\n");

classBuilder.appendPublic().append("\n")
.append(generateDocumentation(indent, fieldToken))
.append(indent).append("public void ").append(formattedPropertyName)
.append("(").append(enumName).append(" value)\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append(fieldName).append(" = value;\n")
.append(indent).append(INDENT).append("this.").append(fieldName).append(" = value;\n")
.append(indent).append("}\n");
}
}
Expand Down Expand Up @@ -1382,7 +1382,7 @@ private void generateArrayProperty(
.append(indent).append("public ").append(typeName).append(" ")
.append(formattedPropertyName).append("()\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append("return ").append(fieldName).append(";\n")
.append(indent).append(INDENT).append("return this.").append(fieldName).append(";\n")
.append(indent).append("}\n");

classBuilder.appendPublic().append("\n")
Expand All @@ -1391,7 +1391,7 @@ private void generateArrayProperty(
.append("(").append(typeName).append(" value)\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append(validateMethod).append("(value);\n")
.append(indent).append(INDENT).append(fieldName).append(" = value;\n")
.append(indent).append(INDENT).append("this.").append(fieldName).append(" = value;\n")
.append(indent).append("}\n");

generateArrayValidateMethod(
Expand All @@ -1416,7 +1416,7 @@ private void generateArrayProperty(
.append(indent).append("public ").append(typeName).append(" ")
.append(formattedPropertyName).append("()\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append("return ").append(fieldName).append(";\n")
.append(indent).append(INDENT).append("return this.").append(fieldName).append(";\n")
.append(indent).append("}\n");

classBuilder.appendPublic().append("\n")
Expand All @@ -1425,7 +1425,7 @@ private void generateArrayProperty(
.append(typeName).append(" value").append(")\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append(validateMethod).append("(value);\n")
.append(indent).append(INDENT).append(fieldName).append(" = value;\n")
.append(indent).append(INDENT).append("this.").append(fieldName).append(" = value;\n")
.append(indent).append("}\n");

generateArrayValidateMethod(
Expand Down Expand Up @@ -1519,7 +1519,7 @@ private void generateSingleValueProperty(
.append(indent).append("public ").append(typeName).append(" ")
.append(formattedPropertyName).append("()\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append("return ").append(fieldName).append(";\n")
.append(indent).append(INDENT).append("return this.").append(fieldName).append(";\n")
.append(indent).append("}\n");

classBuilder.appendPublic().append("\n")
Expand All @@ -1528,7 +1528,7 @@ private void generateSingleValueProperty(
.append(typeName).append(" value)\n")
.append(indent).append("{\n")
.append(validationCall)
.append(indent).append(INDENT).append(fieldName).append(" = value;\n")
.append(indent).append(INDENT).append("this.").append(fieldName).append(" = value;\n")
.append(indent).append("}\n");
}

Expand Down Expand Up @@ -1590,14 +1590,14 @@ private void generateVarData(
.append(indent).append("public ").append(dtoType).append(" ")
.append(formattedPropertyName).append("()\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append("return ").append(fieldName).append(";\n")
.append(indent).append(INDENT).append("return this.").append(fieldName).append(";\n")
.append(indent).append("}\n");

classBuilder.appendPublic().append("\n")
.append(indent).append("public void ").append(formattedPropertyName)
.append("(").append(dtoType).append(" value)\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append(fieldName).append(" = value;\n")
.append(indent).append(INDENT).append("this.").append(fieldName).append(" = value;\n")
.append(indent).append("}\n");
}
}
Expand Down Expand Up @@ -1777,15 +1777,15 @@ private void generateChoices(
.append("\n")
.append(indent).append("public boolean ").append(formattedPropertyName).append("()\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append("return ").append(fieldName).append(";\n")
.append(indent).append(INDENT).append("return this.").append(fieldName).append(";\n")
.append(indent).append("}\n");

classBuilder.appendPublic()
.append("\n")
.append(indent).append(dtoClassName).append(" ")
.append(formattedPropertyName).append("(boolean value)\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append(fieldName).append(" = value;\n")
.append(indent).append(INDENT).append("this.").append(fieldName).append(" = value;\n")
.append(indent).append(INDENT).append("return this;\n")
.append(indent).append("}\n");
}
Expand Down

0 comments on commit 68aeb1a

Please sign in to comment.