diff --git a/pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/Common.java b/pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/Common.java index 2c934c00..15888434 100644 --- a/pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/Common.java +++ b/pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/Common.java @@ -143,7 +143,7 @@ public static String cleanJavaDocComment(String fieldComment) { return cleanDocStr(fieldComment .replaceAll("/\\*\\*[\n\r\s\t]*\\*[\t\s]*|[\n\r\s\t]*\\*/","") // remove java doc .replaceAll("\n\s+\\*\s+","\n") // remove indenting and * - .replaceAll("\n\s+\\*\s*\n","\n
\n") // remove indenting and * + .replaceAll("\n\s+\\*\s*\n","\n\n") // remove indenting and * .replaceAll("/\\*\\*","") // remove indenting and /** at beginning of comment. .trim() // Remove leading and trailing spaces. ); @@ -162,12 +162,15 @@ public static String cleanDocStr(String docStr) { .replaceAll(" < ", " < ") // escape loose less than .replaceAll(" > ", " > ") // escape loose less than .replaceAll(" & ", " & ") // - .replaceAll("
((.|\n)*?)
", "%%%%%$1%%%%") // replace closed paragraphs temporarily - .replaceAll("((\s|\n)*?)($|<[^>]+>)","$1$2$3") // remove
at end of paragraph - .replaceAll("
((.|\n)*?)(
|$|<[^>]+>)","
$1
$2$3") // clean up loose paragraphs + .replaceAll("([^<]*?)
", "%%%%%$1%%%%") // replace closed paragraphs temporarily + .replaceAll("((\\s|\\n)*?)($|<[^>]+>)","$1$2$3") // remove
at end of paragraph + .replaceAll("
((.|\\n)*?)([\\s\\n]*)(%%%%%|
|\\n@\\w+ |$|<[^>]+>)","
$1
$3$4") // clean up loose paragraphs + // Do second pass as we can miss somethat were caught as closers in first pass + .replaceAll("
([^<]*?)
", "%%%%%$1%%%%") // replace closed paragraphs temporarily + .replaceAll("((.|\\n)*?)([\\s\\n]*)(%%%%%|
|\\n@\\w+ |$|<[^>]+>)","
$1
$3$4") // clean up loose paragraphs + // restore completed paragraphs .replaceAll("%%%%%", "") // replace back to paragraphs .replaceAll("%%%%", "
") // replace back to paragraphs - ; } @@ -482,18 +485,8 @@ else if (f.repeated()) { @NonNull private static String getPrimitiveWrapperEqualsGeneration(String generatedCodeSoFar, Field f) { switch (f.messageType()) { - case "StringValue" -> - generatedCodeSoFar += ( - """ - if (this.$fieldName == null && thatObj.$fieldName != null) { - return false; - } - if (this.$fieldName != null && !$fieldName.equals(thatObj.$fieldName)) { - return false; - } - """).replace("$fieldName", f.nameCamelFirstLower()); - case "BoolValue" -> - + case "StringValue", "BoolValue", "Int32Value", "UInt32Value", "Int64Value", "UInt64Value", "FloatValue", + "DoubleValue", "BytesValue" -> generatedCodeSoFar += ( """ if (this.$fieldName == null && thatObj.$fieldName != null) { @@ -503,27 +496,7 @@ private static String getPrimitiveWrapperEqualsGeneration(String generatedCodeSo return false; } """).replace("$fieldName", f.nameCamelFirstLower()); - case "Int32Value", "UInt32Value", "Int64Value", "UInt64Value", "FloatValue", "DoubleValue" -> - generatedCodeSoFar += ( - """ - if (this.$fieldName == null && thatObj.$fieldName != null) { - return false; - } - if (this.$fieldName != null && !$fieldName.equals(thatObj.$fieldName)) { - return false; - } - """).replace("$fieldName", f.nameCamelFirstLower()); - case "BytesValue" -> - generatedCodeSoFar += ( - """ - if (this.$fieldName == null && thatObj.$fieldName != null) { - return false; - } - if (this.$fieldName != null && !$fieldName.equals(thatObj.$fieldName)) { - return false; - } - """).replace("$fieldName", f.nameCamelFirstLower()); - default -> throw new UnsupportedOperationException("Unhandled optional message type:" + f.messageType()); + default -> throw new UnsupportedOperationException("Unhandled optional message type:" + f.messageType()); } return generatedCodeSoFar; } diff --git a/pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/generators/EnumGenerator.java b/pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/generators/EnumGenerator.java index 6942a27c..c1591d9c 100644 --- a/pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/generators/EnumGenerator.java +++ b/pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/impl/generators/EnumGenerator.java @@ -57,7 +57,18 @@ public static void generateEnumFile(Protobuf3Parser.EnumDefContext enumDef, File .replaceAll("/n\s*/n","/n") // remove empty lines ); maxIndex = Math.max(maxIndex, enumNumber); - enumValues.put(enumNumber, new EnumValue(enumValueName, false,enumValueJavaDoc)); + // extract if the enum is marks as deprecated + boolean deprecatedEnumValue = false; + if(item.enumField().enumValueOptions() != null && item.enumField().enumValueOptions().enumValueOption() != null) { + for(var option:item.enumField().enumValueOptions().enumValueOption()) { + if ("deprecated".equals(option.optionName().getText())) { + deprecatedEnumValue = true; + } else { + System.err.println("Unhandled Option: "+option.getText()); + } + } + } + enumValues.put(enumNumber, new EnumValue(enumValueName, deprecatedEnumValue,enumValueJavaDoc)); } else if (item.optionStatement() != null){ if ("deprecated".equals(item.optionStatement().optionName().getText())) { deprecated = "@Deprecated "; @@ -102,10 +113,16 @@ static String createEnum(String javaDocComment, String deprecated, String enumNa for (int i = 0; i <= maxIndex; i++) { final EnumValue enumValue = enumValues.get(i); if (enumValue != null) { - final String cleanedEnumComment = + final String cleanedEnumComment = enumValue.javaDoc.contains("\n") ? + """ + /** + * $enumJavadoc + */ + """ + .replace("$enumJavadoc", + enumValue.javaDoc.replaceAll("\n\s*","\n * ")) : """ - /**$enumJavadoc - */ + /** $enumJavadoc */ """ .replace("$enumJavadoc", enumValue.javaDoc); final String deprecatedText = enumValue.deprecated ? "@Deprecated\n" : ""; diff --git a/pbj-core/pbj-compiler/src/test/java/com/hedera/pbj/compiler/impl/CommonTest.java b/pbj-core/pbj-compiler/src/test/java/com/hedera/pbj/compiler/impl/CommonTest.java index c03343eb..a8fc75c8 100644 --- a/pbj-core/pbj-compiler/src/test/java/com/hedera/pbj/compiler/impl/CommonTest.java +++ b/pbj-core/pbj-compiler/src/test/java/com/hedera/pbj/compiler/impl/CommonTest.java @@ -5,6 +5,9 @@ import static org.junit.jupiter.api.Assertions.*; +/** + * Test the common utility methods. + */ final class CommonTest { // ================================================================================================================ @@ -29,9 +32,11 @@ void commentWithParamsAndReturn() { @Test @DisplayName("Test one line comment on lultiple lines") void oneLineOnMultipleLines() { - String str = "/**\n" + - " * The capacity of this sequence will be the difference between the initial position and the length of the delegate\n" + - " */\n"; + String str = """ + /** + * The capacity of this sequence will be the difference between the initial position and the length of the delegate + */ + """; String result = Common.cleanJavaDocComment(str); String expected = "The capacity of this sequence will be the difference between the initial position and the length of the delegate"; assertEquals(expected, result); @@ -40,139 +45,185 @@ void oneLineOnMultipleLines() { @Test @DisplayName("Test params, throws and returns") void oneParamsThrowsAndReturns() { - String str = "/**\n" + - " * Reads the signed byte at current {@link #position()}, and then increments the {@link #position()} by 1.\n" + - " *\n" + - " * @return The signed byte at the current {@link #position()}\n" + - " * @throws BufferUnderflowException If there are no bytes remaining in this sequence\n" + - " * @throws DataAccessException If an I/O error occurs\n" + - " */"; + String str = """ + /** + * Reads the signed byte at current {@link #position()}, and then increments the {@link #position()} by 1. + * + * @return The signed byte at the current {@link #position()} + * @throws BufferUnderflowException If there are no bytes remaining in this sequence + * @throws DataAccessException If an I/O error occurs + */"""; String result = Common.cleanJavaDocComment(str); - String expected = "Reads the signed byte at current {@link #position()}, and then increments the {@link #position()} by 1.\n" + - " *\n" + - "@return The signed byte at the current {@link #position()}\n" + - "@throws BufferUnderflowException If there are no bytes remaining in this sequence\n" + - "@throws DataAccessException If an I/O error occurs"; + String expected = """ + Reads the signed byte at current {@link #position()}, and then increments the {@link #position()} by 1. + + @return The signed byte at the current {@link #position()} + @throws BufferUnderflowException If there are no bytes remaining in this sequence + @throws DataAccessException If an I/O error occurs"""; assertEquals(expected, result); } @Test @DisplayName("Test params, throws and returns") void oneParamsThrowsAndReturnsWithMore() { - String str = " /**\n" + - " * Read bytes starting at current {@link #position()} into the {@code dst} array, up to the size of the {@code dst}\n" + - " * array. If {@code dst} is larger than the remaining bytes in the sequence, only the remaining bytes are read.\n" + - " * The total number of bytes actually read are returned. The bytes will be placed starting at index 0 of the array.\n" + - " * The {@link #position()} will be incremented by the number of bytes read. If no bytes are available in the\n" + - " * sequence, then 0 is returned.\n" + - " *\n" + - " *The {@code dst} array may be partially written to at the time that any of the declared exceptions are thrown.\n" + - " *\n" + - " *
Bytes are read from the sequence one at a time. If there are not {@code length} bytes remaining in this\n" + - " * sequence, then a {@link BufferUnderflowException} will be thrown. The {@link #position()} will be\n" + - " * incremented by the number of bytes read prior to the exception.\n" + - " *\n" + - " * @param dst The destination array. Cannot be null.\n" + - " * @throws NullPointerException if {@code dst} is null\n" + - " * @throws DataAccessException If an I/O error occurs\n" + - " * @return The number of bytes read actually read and placed into {@code dst}\n" + - " */"; + String str = """ + /** + * Read bytes starting at current {@link #position()} into the {@code dst} array, up to the size of the {@code dst} + * array. If {@code dst} is larger than the remaining bytes in the sequence, only the remaining bytes are read. + * The total number of bytes actually read are returned. The bytes will be placed starting at index 0 of the array. + * The {@link #position()} will be incremented by the number of bytes read. If no bytes are available in the + * sequence, then 0 is returned. + *
+ * Non-closed P between two paragraphs. + * + *
P at beginning of paragraph.With lots of text. Lipsum dolor sit amet, consectetur adipiscing elit. + * Nulla nec purus nec. + * + *
P at beginning of paragraph 2.With lots of text. Lipsum dolor sit amet, consectetur adipiscing elit. + * Nulla nec purus nec. + *
+ *
Simple closed paragraph.
+ * + *+ * New line closed paragraph. + *
+ * + *Bytes are read from the sequence one at a time. If there are not {@code length} bytes remaining in this + * sequence, then a {@link BufferUnderflowException} will be thrown. The {@link #position()} will be + * incremented by the number of bytes read prior to the exception. + * + * @param dst The destination array. Cannot be null. + * @throws NullPointerException if {@code dst} is null + * @throws DataAccessException If an I/O error occurs + * @return The number of bytes read actually read and placed into {@code dst} + */ + """; String result = Common.cleanJavaDocComment(str); - String expected = "Read bytes starting at current {@link #position()} into the {@code dst} array, up to the size of the {@code dst}\n" + - "array. If {@code dst} is larger than the remaining bytes in the sequence, only the remaining bytes are read.\n" + - "The total number of bytes actually read are returned. The bytes will be placed starting at index 0 of the array.\n" + - "The {@link #position()} will be incremented by the number of bytes read. If no bytes are available in the\n" + - "sequence, then 0 is returned.\n" + - " *\n" + - "
The {@code dst} array may be partially written to at the time that any of the declared exceptions are thrown.\n" + - " *\n" + - "
Bytes are read from the sequence one at a time. If there are not {@code length} bytes remaining in this\n" + - "sequence, then a {@link BufferUnderflowException} will be thrown. The {@link #position()} will be\n" + - "incremented by the number of bytes read prior to the exception.\n" + - " *\n" + - "@param dst The destination array. Cannot be null.\n" + - "@throws NullPointerException if {@code dst} is null\n" + - "@throws DataAccessException If an I/O error occurs\n" + - "@return The number of bytes read actually read and placed into {@code dst}"; + String expected = """ + Read bytes starting at current {@link #position()} into the {@code dst} array, up to the size of the {@code dst} + array. If {@code dst} is larger than the remaining bytes in the sequence, only the remaining bytes are read. + The total number of bytes actually read are returned. The bytes will be placed starting at index 0 of the array. + The {@link #position()} will be incremented by the number of bytes read. If no bytes are available in the + sequence, then 0 is returned. +
+ Non-closed P between two paragraphs.
+ +P at beginning of paragraph.With lots of text. Lipsum dolor sit amet, consectetur adipiscing elit. + Nulla nec purus nec.
+ +P at beginning of paragraph 2.With lots of text. Lipsum dolor sit amet, consectetur adipiscing elit. + Nulla nec purus nec.
+ + +Simple closed paragraph.
+ ++ New line closed paragraph. +
+ +Bytes are read from the sequence one at a time. If there are not {@code length} bytes remaining in this + sequence, then a {@link BufferUnderflowException} will be thrown. The {@link #position()} will be + incremented by the number of bytes read prior to the exception.
+ + @param dst The destination array. Cannot be null. + @throws NullPointerException if {@code dst} is null + @throws DataAccessException If an I/O error occurs + @return The number of bytes read actually read and placed into {@code dst}"""; assertEquals(expected, result); } @Test @DisplayName("Test params, throws and returns more") void oneParamsThrowsAndReturnsWithMore2() { - String str = "\n" + - " /**\n" + - " * Read bytes starting at the current {@link #position()} into the {@code dst} array, up to {@code maxLength}\n" + - " * number of bytes. If {@code maxLength} is larger than the remaining bytes in the sequence, only the remaining\n" + - " * bytes are read. The total number of bytes actually read are returned. The bytes will be placed starting at index\n" + - " * {@code offset} of the array. The {@link #position()} will be incremented by the number of bytes read. If no\n" + - " * bytes are available in the sequence, then 0 is returned.\n" + - " *\n" + - " *The {@code dst} array may be partially written to at the time that any of the declared exceptions are thrown.\n" + - " *\n" + - " *
Bytes are read from the sequence one at a time. If there are not {@code length} bytes remaining in this\n" + - " * sequence, then a {@link BufferUnderflowException} will be thrown. The {@link #position()} will be\n" + - " * incremented by the number of bytes read prior to the exception.\n" + - " *\n" + - " * @param dst The array into which bytes are to be written\n" + - " * @param offset The offset within the {@code dst} array of the first byte to be written; must be non-negative and\n" + - " * no larger than {@code dst.length - maxLength}.\n" + - " * @param maxLength The maximum number of bytes to be written to the given {@code dst} array; must be non-negative\n" + - " * and no larger than {@code dst.length - offset}\n" + - " * @throws NullPointerException If {@code dst} is null\n" + - " * @throws IndexOutOfBoundsException If {@code offset} is out of bounds of {@code dst} or if\n" + - " * {@code offset + maxLength} is not less than {@code dst.length}\n" + - " * @throws IllegalArgumentException If {@code maxLength} is negative\n" + - " * @throws DataAccessException If an I/O error occurs\n" + - " * @return The number of bytes read actually read and placed into {@code dst}\n" + - " */"; + String str = """ + + /** + * Read bytes starting at the current {@link #position()} into the {@code dst} array, up to {@code maxLength} + * number of bytes. If {@code maxLength} is larger than the remaining bytes in the sequence, only the remaining + * bytes are read. The total number of bytes actually read are returned. The bytes will be placed starting at index + * {@code offset} of the array. The {@link #position()} will be incremented by the number of bytes read. If no + * bytes are available in the sequence, then 0 is returned. + * + *
The {@code dst} array may be partially written to at the time that any of the declared exceptions are thrown. + * + *
Bytes are read from the sequence one at a time. If there are not {@code length} bytes remaining in this + * sequence, then a {@link BufferUnderflowException} will be thrown. The {@link #position()} will be + * incremented by the number of bytes read prior to the exception. + * + * @param dst The array into which bytes are to be written + * @param offset The offset within the {@code dst} array of the first byte to be written; must be non-negative and + * no larger than {@code dst.length - maxLength}. + * @param maxLength The maximum number of bytes to be written to the given {@code dst} array; must be non-negative + * and no larger than {@code dst.length - offset} + * @throws NullPointerException If {@code dst} is null + * @throws IndexOutOfBoundsException If {@code offset} is out of bounds of {@code dst} or if + * {@code offset + maxLength} is not less than {@code dst.length} + * @throws IllegalArgumentException If {@code maxLength} is negative + * @throws DataAccessException If an I/O error occurs + * @return The number of bytes read actually read and placed into {@code dst} + */ + """; String result = Common.cleanJavaDocComment(str); - String expected = "Read bytes starting at the current {@link #position()} into the {@code dst} array, up to {@code maxLength}\n" + - "number of bytes. If {@code maxLength} is larger than the remaining bytes in the sequence, only the remaining\n" + - "bytes are read. The total number of bytes actually read are returned. The bytes will be placed starting at index\n" + - "{@code offset} of the array. The {@link #position()} will be incremented by the number of bytes read. If no\n" + - "bytes are available in the sequence, then 0 is returned.\n" + - " *\n" + - "
The {@code dst} array may be partially written to at the time that any of the declared exceptions are thrown.\n" + - " *\n" + - "
Bytes are read from the sequence one at a time. If there are not {@code length} bytes remaining in this\n" + - "sequence, then a {@link BufferUnderflowException} will be thrown. The {@link #position()} will be\n" + - "incremented by the number of bytes read prior to the exception.\n" + - " *\n" + - "@param dst The array into which bytes are to be written\n" + - "@param offset The offset within the {@code dst} array of the first byte to be written; must be non-negative and\n" + - "no larger than {@code dst.length - maxLength}.\n" + - "@param maxLength The maximum number of bytes to be written to the given {@code dst} array; must be non-negative\n" + - "and no larger than {@code dst.length - offset}\n" + - "@throws NullPointerException If {@code dst} is null\n" + - "@throws IndexOutOfBoundsException If {@code offset} is out of bounds of {@code dst} or if\n" + - "{@code offset + maxLength} is not less than {@code dst.length}\n" + - "@throws IllegalArgumentException If {@code maxLength} is negative\n" + - "@throws DataAccessException If an I/O error occurs\n" + - "@return The number of bytes read actually read and placed into {@code dst}"; + String expected = """ + Read bytes starting at the current {@link #position()} into the {@code dst} array, up to {@code maxLength} + number of bytes. If {@code maxLength} is larger than the remaining bytes in the sequence, only the remaining + bytes are read. The total number of bytes actually read are returned. The bytes will be placed starting at index + {@code offset} of the array. The {@link #position()} will be incremented by the number of bytes read. If no + bytes are available in the sequence, then 0 is returned. + +
The {@code dst} array may be partially written to at the time that any of the declared exceptions are thrown.
+ +Bytes are read from the sequence one at a time. If there are not {@code length} bytes remaining in this + sequence, then a {@link BufferUnderflowException} will be thrown. The {@link #position()} will be + incremented by the number of bytes read prior to the exception.
+ + @param dst The array into which bytes are to be written + @param offset The offset within the {@code dst} array of the first byte to be written; must be non-negative and + no larger than {@code dst.length - maxLength}. + @param maxLength The maximum number of bytes to be written to the given {@code dst} array; must be non-negative + and no larger than {@code dst.length - offset} + @throws NullPointerException If {@code dst} is null + @throws IndexOutOfBoundsException If {@code offset} is out of bounds of {@code dst} or if + {@code offset + maxLength} is not less than {@code dst.length} + @throws IllegalArgumentException If {@code maxLength} is negative + @throws DataAccessException If an I/O error occurs + @return The number of bytes read actually read and placed into {@code dst}"""; assertEquals(expected, result); } @Test @DisplayName("Test params, throws and returns more 2") void oneParamsThrowsAndReturnsWithMore3() { - String str = " /**\n" + - " * Reads the next four bytes at the current {@link #position()}, composing them into an int value according to\n" + - " * specified byte order, and then increments the {@link #position()} by four.\n" + - " *\n" + - " * @param byteOrder the byte order, aka endian to use. Should never be null. If it is null, BIG_ENDIAN is used.\n" + - " * @return The int value at the current {@link #position()}\n" + - " * @throws BufferUnderflowException If there are fewer than four bytes remaining\n" + - " * @throws DataAccessException if an I/O error occurs\n" + - " */"; + String str = """ + /** + * Reads the next four bytes at the current {@link #position()}, composing them into an int value according to + * specified byte order, and then increments the {@link #position()} by four. + * + * @param byteOrder the byte order, aka endian to use. Should never be null. If it is null, BIG_ENDIAN is used. + * @return The int value at the current {@link #position()} + * @throws BufferUnderflowException If there are fewer than four bytes remaining + * @throws DataAccessException if an I/O error occurs + */\ + """; String result = Common.cleanJavaDocComment(str); - String expected = "Reads the next four bytes at the current {@link #position()}, composing them into an int value according to\n" + - "specified byte order, and then increments the {@link #position()} by four.\n" + - " *\n" + - "@param byteOrder the byte order, aka endian to use. Should never be null. If it is null, BIG_ENDIAN is used.\n" + - "@return The int value at the current {@link #position()}\n" + - "@throws BufferUnderflowException If there are fewer than four bytes remaining\n" + - "@throws DataAccessException if an I/O error occurs"; + String expected = """ + Reads the next four bytes at the current {@link #position()}, composing them into an int value according to + specified byte order, and then increments the {@link #position()} by four. + + @param byteOrder the byte order, aka endian to use. Should never be null. If it is null, BIG_ENDIAN is used. + @return The int value at the current {@link #position()} + @throws BufferUnderflowException If there are fewer than four bytes remaining + @throws DataAccessException if an I/O error occurs"""; assertEquals(expected, result); } } \ No newline at end of file