• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #include <google/protobuf/compiler/java/java_doc_comment.h>
36 
37 #include <vector>
38 
39 #include <google/protobuf/io/printer.h>
40 #include <google/protobuf/stubs/strutil.h>
41 
42 namespace google {
43 namespace protobuf {
44 namespace compiler {
45 namespace java {
46 
EscapeJavadoc(const std::string & input)47 std::string EscapeJavadoc(const std::string& input) {
48   std::string result;
49   result.reserve(input.size() * 2);
50 
51   char prev = '*';
52 
53   for (std::string::size_type i = 0; i < input.size(); i++) {
54     char c = input[i];
55     switch (c) {
56       case '*':
57         // Avoid "/*".
58         if (prev == '/') {
59           result.append("&#42;");
60         } else {
61           result.push_back(c);
62         }
63         break;
64       case '/':
65         // Avoid "*/".
66         if (prev == '*') {
67           result.append("&#47;");
68         } else {
69           result.push_back(c);
70         }
71         break;
72       case '@':
73         // '@' starts javadoc tags including the @deprecated tag, which will
74         // cause a compile-time error if inserted before a declaration that
75         // does not have a corresponding @Deprecated annotation.
76         result.append("&#64;");
77         break;
78       case '<':
79         // Avoid interpretation as HTML.
80         result.append("&lt;");
81         break;
82       case '>':
83         // Avoid interpretation as HTML.
84         result.append("&gt;");
85         break;
86       case '&':
87         // Avoid interpretation as HTML.
88         result.append("&amp;");
89         break;
90       case '\\':
91         // Java interprets Unicode escape sequences anywhere!
92         result.append("&#92;");
93         break;
94       default:
95         result.push_back(c);
96         break;
97     }
98 
99     prev = c;
100   }
101 
102   return result;
103 }
104 
WriteDocCommentBodyForLocation(io::Printer * printer,const SourceLocation & location)105 static void WriteDocCommentBodyForLocation(io::Printer* printer,
106                                            const SourceLocation& location) {
107   std::string comments = location.leading_comments.empty()
108                              ? location.trailing_comments
109                              : location.leading_comments;
110   if (!comments.empty()) {
111     // TODO(kenton):  Ideally we should parse the comment text as Markdown and
112     //   write it back as HTML, but this requires a Markdown parser.  For now
113     //   we just use <pre> to get fixed-width text formatting.
114 
115     // If the comment itself contains block comment start or end markers,
116     // HTML-escape them so that they don't accidentally close the doc comment.
117     comments = EscapeJavadoc(comments);
118 
119     std::vector<std::string> lines = Split(comments, "\n");
120     while (!lines.empty() && lines.back().empty()) {
121       lines.pop_back();
122     }
123 
124     printer->Print(" * <pre>\n");
125     for (int i = 0; i < lines.size(); i++) {
126       // Most lines should start with a space.  Watch out for lines that start
127       // with a /, since putting that right after the leading asterisk will
128       // close the comment.
129       if (!lines[i].empty() && lines[i][0] == '/') {
130         printer->Print(" * $line$\n", "line", lines[i]);
131       } else {
132         printer->Print(" *$line$\n", "line", lines[i]);
133       }
134     }
135     printer->Print(
136         " * </pre>\n"
137         " *\n");
138   }
139 }
140 
141 template <typename DescriptorType>
WriteDocCommentBody(io::Printer * printer,const DescriptorType * descriptor)142 static void WriteDocCommentBody(io::Printer* printer,
143                                 const DescriptorType* descriptor) {
144   SourceLocation location;
145   if (descriptor->GetSourceLocation(&location)) {
146     WriteDocCommentBodyForLocation(printer, location);
147   }
148 }
149 
FirstLineOf(const std::string & value)150 static std::string FirstLineOf(const std::string& value) {
151   std::string result = value;
152 
153   std::string::size_type pos = result.find_first_of('\n');
154   if (pos != std::string::npos) {
155     result.erase(pos);
156   }
157 
158   // If line ends in an opening brace, make it "{ ... }" so it looks nice.
159   if (!result.empty() && result[result.size() - 1] == '{') {
160     result.append(" ... }");
161   }
162 
163   return result;
164 }
165 
WriteMessageDocComment(io::Printer * printer,const Descriptor * message)166 void WriteMessageDocComment(io::Printer* printer, const Descriptor* message) {
167   printer->Print("/**\n");
168   WriteDocCommentBody(printer, message);
169   printer->Print(
170       " * Protobuf type {@code $fullname$}\n"
171       " */\n",
172       "fullname", EscapeJavadoc(message->full_name()));
173 }
174 
WriteFieldDocComment(io::Printer * printer,const FieldDescriptor * field)175 void WriteFieldDocComment(io::Printer* printer, const FieldDescriptor* field) {
176   // We start the comment with the main body based on the comments from the
177   // .proto file (if present). We then continue with the field declaration,
178   // e.g.:
179   //   optional string foo = 5;
180   // And then we end with the javadoc tags if applicable.
181   // If the field is a group, the debug string might end with {.
182   printer->Print("/**\n");
183   WriteDocCommentBody(printer, field);
184   printer->Print(" * <code>$def$</code>\n", "def",
185                  EscapeJavadoc(FirstLineOf(field->DebugString())));
186   printer->Print(" */\n");
187 }
188 
WriteFieldAccessorDocComment(io::Printer * printer,const FieldDescriptor * field,const FieldAccessorType type,const bool builder)189 void WriteFieldAccessorDocComment(io::Printer* printer,
190                                   const FieldDescriptor* field,
191                                   const FieldAccessorType type,
192                                   const bool builder) {
193   printer->Print("/**\n");
194   WriteDocCommentBody(printer, field);
195   printer->Print(" * <code>$def$</code>\n", "def",
196                  EscapeJavadoc(FirstLineOf(field->DebugString())));
197   switch (type) {
198     case HAZZER:
199       printer->Print(" * @return Whether the $name$ field is set.\n", "name",
200                      field->camelcase_name());
201       break;
202     case GETTER:
203       printer->Print(" * @return The $name$.\n", "name",
204                      field->camelcase_name());
205       break;
206     case SETTER:
207       printer->Print(" * @param value The $name$ to set.\n", "name",
208                      field->camelcase_name());
209       break;
210     case CLEARER:
211       // Print nothing
212       break;
213     // Repeated
214     case LIST_COUNT:
215       printer->Print(" * @return The count of $name$.\n", "name",
216                      field->camelcase_name());
217       break;
218     case LIST_GETTER:
219       printer->Print(" * @return A list containing the $name$.\n", "name",
220                      field->camelcase_name());
221       break;
222     case LIST_INDEXED_GETTER:
223       printer->Print(" * @param index The index of the element to return.\n");
224       printer->Print(" * @return The $name$ at the given index.\n", "name",
225                      field->camelcase_name());
226       break;
227     case LIST_INDEXED_SETTER:
228       printer->Print(" * @param index The index to set the value at.\n");
229       printer->Print(" * @param value The $name$ to set.\n", "name",
230                      field->camelcase_name());
231       break;
232     case LIST_ADDER:
233       printer->Print(" * @param value The $name$ to add.\n", "name",
234                      field->camelcase_name());
235       break;
236     case LIST_MULTI_ADDER:
237       printer->Print(" * @param values The $name$ to add.\n", "name",
238                      field->camelcase_name());
239       break;
240   }
241   if (builder) {
242     printer->Print(" * @return This builder for chaining.\n");
243   }
244   printer->Print(" */\n");
245 }
246 
WriteFieldEnumValueAccessorDocComment(io::Printer * printer,const FieldDescriptor * field,const FieldAccessorType type,const bool builder)247 void WriteFieldEnumValueAccessorDocComment(io::Printer* printer,
248                                            const FieldDescriptor* field,
249                                            const FieldAccessorType type,
250                                            const bool builder) {
251   printer->Print("/**\n");
252   WriteDocCommentBody(printer, field);
253   printer->Print(" * <code>$def$</code>\n", "def",
254                  EscapeJavadoc(FirstLineOf(field->DebugString())));
255   switch (type) {
256     case HAZZER:
257       // Should never happen
258       break;
259     case GETTER:
260       printer->Print(
261           " * @return The enum numeric value on the wire for $name$.\n", "name",
262           field->camelcase_name());
263       break;
264     case SETTER:
265       printer->Print(
266           " * @param value The enum numeric value on the wire for $name$ to "
267           "set.\n",
268           "name", field->camelcase_name());
269       break;
270     case CLEARER:
271       // Print nothing
272       break;
273     // Repeated
274     case LIST_COUNT:
275       // Should never happen
276       break;
277     case LIST_GETTER:
278       printer->Print(
279           " * @return A list containing the enum numeric values on the wire "
280           "for $name$.\n",
281           "name", field->camelcase_name());
282       break;
283     case LIST_INDEXED_GETTER:
284       printer->Print(" * @param index The index of the value to return.\n");
285       printer->Print(
286           " * @return The enum numeric value on the wire of $name$ at the "
287           "given index.\n",
288           "name", field->camelcase_name());
289       break;
290     case LIST_INDEXED_SETTER:
291       printer->Print(" * @param index The index to set the value at.\n");
292       printer->Print(
293           " * @param value The enum numeric value on the wire for $name$ to "
294           "set.\n",
295           "name", field->camelcase_name());
296       break;
297     case LIST_ADDER:
298       printer->Print(
299           " * @param value The enum numeric value on the wire for $name$ to "
300           "add.\n",
301           "name", field->camelcase_name());
302       break;
303     case LIST_MULTI_ADDER:
304       printer->Print(
305           " * @param values The enum numeric values on the wire for $name$ to "
306           "add.\n",
307           "name", field->camelcase_name());
308       break;
309   }
310   if (builder) {
311     printer->Print(" * @return This builder for chaining.\n");
312   }
313   printer->Print(" */\n");
314 }
315 
WriteFieldStringBytesAccessorDocComment(io::Printer * printer,const FieldDescriptor * field,const FieldAccessorType type,const bool builder)316 void WriteFieldStringBytesAccessorDocComment(io::Printer* printer,
317                                              const FieldDescriptor* field,
318                                              const FieldAccessorType type,
319                                              const bool builder) {
320   printer->Print("/**\n");
321   WriteDocCommentBody(printer, field);
322   printer->Print(" * <code>$def$</code>\n", "def",
323                  EscapeJavadoc(FirstLineOf(field->DebugString())));
324   switch (type) {
325     case HAZZER:
326       // Should never happen
327       break;
328     case GETTER:
329       printer->Print(" * @return The bytes for $name$.\n", "name",
330                      field->camelcase_name());
331       break;
332     case SETTER:
333       printer->Print(" * @param value The bytes for $name$ to set.\n", "name",
334                      field->camelcase_name());
335       break;
336     case CLEARER:
337       // Print nothing
338       break;
339     // Repeated
340     case LIST_COUNT:
341       // Should never happen
342       break;
343     case LIST_GETTER:
344       printer->Print(" * @return A list containing the bytes for $name$.\n",
345                      "name", field->camelcase_name());
346       break;
347     case LIST_INDEXED_GETTER:
348       printer->Print(" * @param index The index of the value to return.\n");
349       printer->Print(" * @return The bytes of the $name$ at the given index.\n",
350                      "name", field->camelcase_name());
351       break;
352     case LIST_INDEXED_SETTER:
353       printer->Print(" * @param index The index to set the value at.\n");
354       printer->Print(" * @param value The bytes of the $name$ to set.\n",
355                      "name", field->camelcase_name());
356       break;
357     case LIST_ADDER:
358       printer->Print(" * @param value The bytes of the $name$ to add.\n",
359                      "name", field->camelcase_name());
360       break;
361     case LIST_MULTI_ADDER:
362       printer->Print(" * @param values The bytes of the $name$ to add.\n",
363                      "name", field->camelcase_name());
364       break;
365   }
366   if (builder) {
367     printer->Print(" * @return This builder for chaining.\n");
368   }
369   printer->Print(" */\n");
370 }
371 
372 // Enum
373 
WriteEnumDocComment(io::Printer * printer,const EnumDescriptor * enum_)374 void WriteEnumDocComment(io::Printer* printer, const EnumDescriptor* enum_) {
375   printer->Print("/**\n");
376   WriteDocCommentBody(printer, enum_);
377   printer->Print(
378       " * Protobuf enum {@code $fullname$}\n"
379       " */\n",
380       "fullname", EscapeJavadoc(enum_->full_name()));
381 }
382 
WriteEnumValueDocComment(io::Printer * printer,const EnumValueDescriptor * value)383 void WriteEnumValueDocComment(io::Printer* printer,
384                               const EnumValueDescriptor* value) {
385   printer->Print("/**\n");
386   WriteDocCommentBody(printer, value);
387   printer->Print(
388       " * <code>$def$</code>\n"
389       " */\n",
390       "def", EscapeJavadoc(FirstLineOf(value->DebugString())));
391 }
392 
WriteServiceDocComment(io::Printer * printer,const ServiceDescriptor * service)393 void WriteServiceDocComment(io::Printer* printer,
394                             const ServiceDescriptor* service) {
395   printer->Print("/**\n");
396   WriteDocCommentBody(printer, service);
397   printer->Print(
398       " * Protobuf service {@code $fullname$}\n"
399       " */\n",
400       "fullname", EscapeJavadoc(service->full_name()));
401 }
402 
WriteMethodDocComment(io::Printer * printer,const MethodDescriptor * method)403 void WriteMethodDocComment(io::Printer* printer,
404                            const MethodDescriptor* method) {
405   printer->Print("/**\n");
406   WriteDocCommentBody(printer, method);
407   printer->Print(
408       " * <code>$def$</code>\n"
409       " */\n",
410       "def", EscapeJavadoc(FirstLineOf(method->DebugString())));
411 }
412 
413 }  // namespace java
414 }  // namespace compiler
415 }  // namespace protobuf
416 }  // namespace google
417