luozongle01 commented on code in PR #2206: URL: https://github.com/apache/zookeeper/pull/2206#discussion_r1863136846
########## zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java: ########## @@ -767,4 +795,135 @@ public static String getCsharpFQName(String name) { } return fQName.toString(); } + + public String getJavaFieldComments(JField jField) { + return getFieldComments(jField, " "); + } + + public String getCFieldComments(JField jField) { + return getFieldComments(jField, " "); + } + + private String getFieldComments(JField jField, String indent) { + if (jField == null || jField.getTypeToken() == null || jField.getNextToken() == null) { + return ""; + } + + // get the comment before the line + Token beforeTheLineCommentToken = getCommentToken(jField.getPreviousToken(), jField.getTypeToken()); + List<String> comments = extractLeadingComments(beforeTheLineCommentToken, null); + + Token endOfLineCommentToken = getCommentToken(null, jField.getNextToken()); + if (endOfLineCommentToken != null && jField.getTypeToken().beginLine == endOfLineCommentToken.beginLine) { + + comments.addAll(extractLeadingComments(endOfLineCommentToken, endOfLineCommentToken.next)); + } + + return formatComments(indent, comments); + } + + private Token getCommentToken(Token previousToken, Token token) { + if (token == null || token.specialToken == null || belongsToThePreviousToken(previousToken, token.specialToken)) { + return null; + } + + Token tmpToken = token.specialToken; + while (tmpToken.specialToken != null) { + + if (belongsToThePreviousToken(previousToken, tmpToken.specialToken)) { + return tmpToken; + } + tmpToken = tmpToken.specialToken; + } + return tmpToken; + } + + /** + * Determine whether the current commentToken belongs to the previousToken. + * + * @return true: If the current commentToken should belong to the previousToken. + */ + private boolean belongsToThePreviousToken(Token previousToken, Token commentToken) { + if (previousToken == null || commentToken == null) { + return false; + } + + return previousToken.beginLine == commentToken.beginLine; + } + + public String getRecordComments() { + if (getRecordToken() == null || getRecordToken().specialToken == null) { + return ""; + } + + // get the comments before the class + Token beforeTheClassToken = getCommentToken(null, getRecordToken()); Review Comment: > I think `commentToken` is good enough. Also I suggest renaming `extractLeadingComments` to simple `extractComments`. Oh, that's true -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@zookeeper.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org