Emit comments as javadoc in the Java backend

While AIDL treats /** */ and /* */ alike, but Java tools don't.
So /* */ in AIDL should be formatted as Javadoc style (/** */) so that
Java tools recognize tags correctly.

Bug: 177276893
Test: aidl_unittests
Change-Id: I609660bf7f5e875ed99df7950d8b199613cdb7e1
diff --git a/comments.cpp b/comments.cpp
index 4b724b2..afbdc8f 100644
--- a/comments.cpp
+++ b/comments.cpp
@@ -41,6 +41,7 @@
 static const std::string_view kLineCommentBegin = "//";
 static const std::string_view kBlockCommentBegin = "/*";
 static const std::string_view kBlockCommentEnd = "*/";
+static const std::string_view kDocCommentBegin = "/**";
 static const std::string kTagDeprecated = "@deprecated";
 static const std::regex kTagHideRegex{"@hide\\b"};
 
@@ -214,5 +215,22 @@
   return std::nullopt;
 }
 
+// Formats comments for the Java backend.
+// The last/block comment is transformed into javadoc(/** */)
+// and others are used as they are.
+std::string FormatCommentsForJava(const Comments& comments) {
+  std::stringstream out;
+  for (auto it = begin(comments); it != end(comments); it++) {
+    const bool last = next(it) == end(comments);
+    // We only re-format the last/block comment which is not already a doc-style comment.
+    if (last && it->type == Comment::Type::BLOCK && !StartsWith(it->body, kDocCommentBegin)) {
+      out << kDocCommentBegin << ConsumePrefix(it->body, kBlockCommentBegin);
+    } else {
+      out << it->body;
+    }
+  }
+  return out.str();
+}
+
 }  // namespace aidl
 }  // namespace android
\ No newline at end of file