#include <utility>
#include <vector>
-#include <flatbuffers/util.h>
+#include "flatbuffers/util.h"
#define to_string flatbuffers::NumToString
// Stringify helpers used solely to cast GRPC_VERSION
#ifndef STR
-#define STR(s) #s
+# define STR(s) # s
#endif
#ifndef XSTR
-#define XSTR(s) STR(s)
+# define XSTR(s) STR(s)
#endif
-
typedef grpc_generator::Printer Printer;
typedef std::map<grpc::string, grpc::string> VARS;
typedef grpc_generator::Service ServiceDescriptor;
namespace grpc_java_generator {
typedef std::string string;
// Generates imports for the service
-void GenerateImports(grpc_generator::File* file,
- grpc_generator::Printer* printer, VARS& vars) {
+void GenerateImports(grpc_generator::File *file,
+ grpc_generator::Printer *printer, VARS &vars) {
vars["filename"] = file->filename();
- printer->Print(
- vars,
- "//Generated by flatc compiler (version $flatc_version$)\n");
+ printer->Print(vars,
+ "//Generated by flatc compiler (version $flatc_version$)\n");
printer->Print("//If you make any local changes, they will be lost\n");
printer->Print(vars, "//source: $filename$.fbs\n\n");
printer->Print(vars, "package $Package$;\n\n");
// Adjust a method name prefix identifier to follow the JavaBean spec:
// - decapitalize the first letter
// - remove embedded underscores & capitalize the following letter
-static string MixedLower(const string& word) {
+static string MixedLower(const string &word) {
string w;
w += static_cast<string::value_type>(tolower(word[0]));
bool after_underscore = false;
// - An underscore is inserted where a lower case letter is followed by an
// upper case letter.
// - All letters are converted to upper case
-static string ToAllUpperCase(const string& word) {
+static string ToAllUpperCase(const string &word) {
string w;
for (size_t i = 0; i < word.length(); ++i) {
w += static_cast<string::value_type>(toupper(word[i]));
return w;
}
-static inline string LowerMethodName(const MethodDescriptor* method) {
+static inline string LowerMethodName(const MethodDescriptor *method) {
return MixedLower(method->name());
}
-static inline string MethodPropertiesFieldName(const MethodDescriptor* method) {
+static inline string MethodPropertiesFieldName(const MethodDescriptor *method) {
return "METHOD_" + ToAllUpperCase(method->name());
}
static inline string MethodPropertiesGetterName(
- const MethodDescriptor* method) {
+ const MethodDescriptor *method) {
return MixedLower("get_" + method->name() + "_method");
}
-static inline string MethodIdFieldName(const MethodDescriptor* method) {
+static inline string MethodIdFieldName(const MethodDescriptor *method) {
return "METHODID_" + ToAllUpperCase(method->name());
}
-static inline string JavaClassName(VARS& vars, const string& name) {
+static inline string JavaClassName(VARS &vars, const string &name) {
// string name = google::protobuf::compiler::java::ClassName(desc);
return vars["Package"] + name;
}
-static inline string ServiceClassName(const string& service_name) {
+static inline string ServiceClassName(const string &service_name) {
return service_name + "Grpc";
}
// TODO(nmittler): Remove once protobuf includes javadoc methods in
// distribution.
-template <typename ITR>
-static void GrpcSplitStringToIteratorUsing(const string& full,
- const char* delim, ITR& result) {
+template<typename ITR>
+static void GrpcSplitStringToIteratorUsing(const string &full,
+ const char *delim, ITR &result) {
// Optimize the common case where delim is a single character.
if (delim[0] != '\0' && delim[1] == '\0') {
char c = delim[0];
- const char* p = full.data();
- const char* end = p + full.size();
+ const char *p = full.data();
+ const char *end = p + full.size();
while (p != end) {
if (*p == c) {
++p;
} else {
- const char* start = p;
+ const char *start = p;
while (++p != end && *p != c)
;
*result++ = string(start, p - start);
}
}
-static void GrpcSplitStringUsing(const string& full, const char* delim,
- std::vector<string>* result) {
+static void GrpcSplitStringUsing(const string &full, const char *delim,
+ std::vector<string> *result) {
std::back_insert_iterator<std::vector<string>> it(*result);
GrpcSplitStringToIteratorUsing(full, delim, it);
}
-static std::vector<string> GrpcSplit(const string& full, const char* delim) {
+static std::vector<string> GrpcSplit(const string &full, const char *delim) {
std::vector<string> result;
GrpcSplitStringUsing(full, delim, &result);
return result;
// TODO(nmittler): Remove once protobuf includes javadoc methods in
// distribution.
-static string GrpcEscapeJavadoc(const string& input) {
+static string GrpcEscapeJavadoc(const string &input) {
string result;
result.reserve(input.size() * 2);
// Java interprets Unicode escape sequences anywhere!
result.append("\");
break;
- default:
- result.push_back(c);
- break;
+ default: result.push_back(c); break;
}
prev = c;
return result;
}
-static std::vector<string> GrpcGetDocLines(const string& comments) {
+static std::vector<string> GrpcGetDocLines(const string &comments) {
if (!comments.empty()) {
// TODO(kenton): Ideally we should parse the comment text as Markdown and
// write it back as HTML, but this requires a Markdown parser. For now
string escapedComments = GrpcEscapeJavadoc(comments);
std::vector<string> lines = GrpcSplit(escapedComments, "\n");
- while (!lines.empty() && lines.back().empty()) {
- lines.pop_back();
- }
+ while (!lines.empty() && lines.back().empty()) { lines.pop_back(); }
return lines;
}
return std::vector<string>();
}
static std::vector<string> GrpcGetDocLinesForDescriptor(
- const DescriptorType* descriptor) {
+ const DescriptorType *descriptor) {
return descriptor->GetAllComments();
// return GrpcGetDocLines(descriptor->GetLeadingComments("///"));
}
-static void GrpcWriteDocCommentBody(Printer* printer, VARS& vars,
- const std::vector<string>& lines,
+static void GrpcWriteDocCommentBody(Printer *printer, VARS &vars,
+ const std::vector<string> &lines,
bool surroundWithPreTag) {
if (!lines.empty()) {
- if (surroundWithPreTag) {
- printer->Print(" * <pre>\n");
- }
+ if (surroundWithPreTag) { printer->Print(" * <pre>\n"); }
for (size_t i = 0; i < lines.size(); i++) {
// Most lines should start with a space. Watch out for lines that start
}
}
- if (surroundWithPreTag) {
- printer->Print(" * </pre>\n");
- }
+ if (surroundWithPreTag) { printer->Print(" * </pre>\n"); }
}
}
-static void GrpcWriteDocComment(Printer* printer, VARS& vars,
- const string& comments) {
+static void GrpcWriteDocComment(Printer *printer, VARS &vars,
+ const string &comments) {
printer->Print("/**\n");
std::vector<string> lines = GrpcGetDocLines(comments);
GrpcWriteDocCommentBody(printer, vars, lines, false);
printer->Print(" */\n");
}
-static void GrpcWriteServiceDocComment(Printer* printer, VARS& vars,
- const ServiceDescriptor* service) {
+static void GrpcWriteServiceDocComment(Printer *printer, VARS &vars,
+ const ServiceDescriptor *service) {
printer->Print("/**\n");
std::vector<string> lines = GrpcGetDocLinesForDescriptor(service);
GrpcWriteDocCommentBody(printer, vars, lines, true);
printer->Print(" */\n");
}
-void GrpcWriteMethodDocComment(Printer* printer, VARS& vars,
- const MethodDescriptor* method) {
+void GrpcWriteMethodDocComment(Printer *printer, VARS &vars,
+ const MethodDescriptor *method) {
printer->Print("/**\n");
std::vector<string> lines = GrpcGetDocLinesForDescriptor(method);
GrpcWriteDocCommentBody(printer, vars, lines, true);
printer->Print(" */\n");
}
-//outputs static singleton extractor for type stored in "extr_type" and "extr_type_name" vars
-static void PrintTypeExtractor(Printer* p, VARS& vars) {
- p->Print(
- vars,
- "private static volatile FlatbuffersUtils.FBExtactor<$extr_type$> "
- "extractorOf$extr_type_name$;\n"
- "private static FlatbuffersUtils.FBExtactor<$extr_type$> "
- "getExtractorOf$extr_type_name$() {\n"
- " if (extractorOf$extr_type_name$ != null) return "
- "extractorOf$extr_type_name$;\n"
- " synchronized ($service_class_name$.class) {\n"
- " if (extractorOf$extr_type_name$ != null) return "
- "extractorOf$extr_type_name$;\n"
- " extractorOf$extr_type_name$ = new "
- "FlatbuffersUtils.FBExtactor<$extr_type$>() {\n"
- " public $extr_type$ extract (ByteBuffer buffer) {\n"
- " return "
- "$extr_type$.getRootAs$extr_type_name$(buffer);\n"
- " }\n"
- " };\n"
- " return extractorOf$extr_type_name$;\n"
- " }\n"
- "}\n\n");
+// outputs static singleton extractor for type stored in "extr_type" and
+// "extr_type_name" vars
+static void PrintTypeExtractor(Printer *p, VARS &vars) {
+ p->Print(vars,
+ "private static volatile FlatbuffersUtils.FBExtactor<$extr_type$> "
+ "extractorOf$extr_type_name$;\n"
+ "private static FlatbuffersUtils.FBExtactor<$extr_type$> "
+ "getExtractorOf$extr_type_name$() {\n"
+ " if (extractorOf$extr_type_name$ != null) return "
+ "extractorOf$extr_type_name$;\n"
+ " synchronized ($service_class_name$.class) {\n"
+ " if (extractorOf$extr_type_name$ != null) return "
+ "extractorOf$extr_type_name$;\n"
+ " extractorOf$extr_type_name$ = new "
+ "FlatbuffersUtils.FBExtactor<$extr_type$>() {\n"
+ " public $extr_type$ extract (ByteBuffer buffer) {\n"
+ " return "
+ "$extr_type$.getRootAs$extr_type_name$(buffer);\n"
+ " }\n"
+ " };\n"
+ " return extractorOf$extr_type_name$;\n"
+ " }\n"
+ "}\n\n");
}
-static void PrintMethodFields(Printer* p, VARS& vars,
- const ServiceDescriptor* service) {
+static void PrintMethodFields(Printer *p, VARS &vars,
+ const ServiceDescriptor *service) {
p->Print("// Static method descriptors that strictly reflect the proto.\n");
vars["service_name"] = service->name();
- //set of names of rpc input- and output- types that were already encountered.
- //this is needed to avoid duplicating type extractor since it's possible that
- //the same type is used as an input or output type of more than a single RPC method
+ // set of names of rpc input- and output- types that were already encountered.
+ // this is needed to avoid duplicating type extractor since it's possible that
+ // the same type is used as an input or output type of more than a single RPC
+ // method
std::set<std::string> encounteredTypes;
for (int i = 0; i < service->method_count(); ++i) {
auto method = service->method(i);
- vars["arg_in_id"] = to_string(2L * i); //trying to make msvc 10 happy
+ vars["arg_in_id"] = to_string(2L * i); // trying to make msvc 10 happy
vars["arg_out_id"] = to_string(2L * i + 1);
vars["method_name"] = method->name();
vars["input_type_name"] = method->get_input_type_name();
vars["method_field_name"] = MethodPropertiesFieldName(method.get());
vars["method_new_field_name"] = MethodPropertiesGetterName(method.get());
vars["method_method_name"] = MethodPropertiesGetterName(method.get());
- bool client_streaming = method->ClientStreaming() || method->BidiStreaming();
- bool server_streaming = method->ServerStreaming() || method->BidiStreaming();
+ bool client_streaming =
+ method->ClientStreaming() || method->BidiStreaming();
+ bool server_streaming =
+ method->ServerStreaming() || method->BidiStreaming();
if (client_streaming) {
if (server_streaming) {
vars["method_type"] = "BIDI_STREAMING";
}
p->Print(
- vars,
- "@$ExperimentalApi$(\"https://github.com/grpc/grpc-java/issues/"
- "1901\")\n"
- "public static $MethodDescriptor$<$input_type$,\n"
- " $output_type$> $method_method_name$() {\n"
- " $MethodDescriptor$<$input_type$, $output_type$> "
- "$method_new_field_name$;\n"
- " if (($method_new_field_name$ = "
- "$service_class_name$.$method_new_field_name$) == null) {\n"
- " synchronized ($service_class_name$.class) {\n"
- " if (($method_new_field_name$ = "
- "$service_class_name$.$method_new_field_name$) == null) {\n"
- " $service_class_name$.$method_new_field_name$ = "
- "$method_new_field_name$ = \n"
- " $MethodDescriptor$.<$input_type$, "
- "$output_type$>newBuilder()\n"
- " .setType($MethodType$.$method_type$)\n"
- " .setFullMethodName(generateFullMethodName(\n"
- " \"$Package$$service_name$\", \"$method_name$\"))\n"
- " .setSampledToLocalTracing(true)\n"
- " .setRequestMarshaller(FlatbuffersUtils.marshaller(\n"
- " $input_type$.class, "
- "getExtractorOf$input_type_name$()))\n"
- " .setResponseMarshaller(FlatbuffersUtils.marshaller(\n"
- " $output_type$.class, "
- "getExtractorOf$output_type_name$()))\n");
+ vars,
+ "@$ExperimentalApi$(\"https://github.com/grpc/grpc-java/issues/"
+ "1901\")\n"
+ "public static $MethodDescriptor$<$input_type$,\n"
+ " $output_type$> $method_method_name$() {\n"
+ " $MethodDescriptor$<$input_type$, $output_type$> "
+ "$method_new_field_name$;\n"
+ " if (($method_new_field_name$ = "
+ "$service_class_name$.$method_new_field_name$) == null) {\n"
+ " synchronized ($service_class_name$.class) {\n"
+ " if (($method_new_field_name$ = "
+ "$service_class_name$.$method_new_field_name$) == null) {\n"
+ " $service_class_name$.$method_new_field_name$ = "
+ "$method_new_field_name$ = \n"
+ " $MethodDescriptor$.<$input_type$, "
+ "$output_type$>newBuilder()\n"
+ " .setType($MethodType$.$method_type$)\n"
+ " .setFullMethodName(generateFullMethodName(\n"
+ " \"$Package$$service_name$\", \"$method_name$\"))\n"
+ " .setSampledToLocalTracing(true)\n"
+ " .setRequestMarshaller(FlatbuffersUtils.marshaller(\n"
+ " $input_type$.class, "
+ "getExtractorOf$input_type_name$()))\n"
+ " .setResponseMarshaller(FlatbuffersUtils.marshaller(\n"
+ " $output_type$.class, "
+ "getExtractorOf$output_type_name$()))\n");
// vars["proto_method_descriptor_supplier"] = service->name() +
// "MethodDescriptorSupplier";
enum CallType { ASYNC_CALL = 0, BLOCKING_CALL = 1, FUTURE_CALL = 2 };
-static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
- const ServiceDescriptor* service);
+static void PrintBindServiceMethodBody(Printer *p, VARS &vars,
+ const ServiceDescriptor *service);
// Prints a client interface or implementation class, or a server interface.
-static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
+static void PrintStub(Printer *p, VARS &vars, const ServiceDescriptor *service,
StubType type) {
const string service_name = service->name();
vars["service_name"] = service_name;
break;
case BLOCKING_CLIENT_INTERFACE:
interface = true;
- FLATBUFFERS_FALLTHROUGH(); // fall thru
+ FLATBUFFERS_FALLTHROUGH(); // fall thru
case BLOCKING_CLIENT_IMPL:
call_type = BLOCKING_CALL;
stub_name += "BlockingStub";
break;
case FUTURE_CLIENT_INTERFACE:
interface = true;
- FLATBUFFERS_FALLTHROUGH(); // fall thru
+ FLATBUFFERS_FALLTHROUGH(); // fall thru
case FUTURE_CLIENT_IMPL:
call_type = FUTURE_CALL;
stub_name += "FutureStub";
vars["client_name"] = client_name;
// Class head
- if (!interface) {
- GrpcWriteServiceDocComment(p, vars, service);
- }
+ if (!interface) { GrpcWriteServiceDocComment(p, vars, service); }
if (impl_base) {
p->Print(vars,
"public static abstract class $abstract_name$ implements "
vars["output_type"] = JavaClassName(vars, method->get_output_type_name());
vars["lower_method_name"] = LowerMethodName(&*method);
vars["method_method_name"] = MethodPropertiesGetterName(&*method);
- bool client_streaming = method->ClientStreaming() || method->BidiStreaming();
- bool server_streaming = method->ServerStreaming() || method->BidiStreaming();
+ bool client_streaming =
+ method->ClientStreaming() || method->BidiStreaming();
+ bool server_streaming =
+ method->ServerStreaming() || method->BidiStreaming();
if (call_type == BLOCKING_CALL && client_streaming) {
// Blocking client interface with client streaming is not available
p->Print("\n");
// TODO(nmittler): Replace with WriteMethodDocComment once included by the
// protobuf distro.
- if (!interface) {
- GrpcWriteMethodDocComment(p, vars, &*method);
- }
+ if (!interface) { GrpcWriteMethodDocComment(p, vars, &*method); }
p->Print("public ");
switch (call_type) {
case BLOCKING_CALL:
"responseObserver);\n");
}
break;
- default:
- break;
+ default: break;
}
} else if (!interface) {
switch (call_type) {
}
static bool CompareMethodClientStreaming(
- const std::unique_ptr<const grpc_generator::Method>& method1,
- const std::unique_ptr<const grpc_generator::Method>& method2) {
+ const std::unique_ptr<const grpc_generator::Method> &method1,
+ const std::unique_ptr<const grpc_generator::Method> &method2) {
return method1->ClientStreaming() < method2->ClientStreaming();
}
// Place all method invocations into a single class to reduce memory footprint
// on Android.
-static void PrintMethodHandlerClass(Printer* p, VARS& vars,
- const ServiceDescriptor* service) {
+static void PrintMethodHandlerClass(Printer *p, VARS &vars,
+ const ServiceDescriptor *service) {
// Sort method ids based on ClientStreaming() so switch tables are compact.
std::vector<std::unique_ptr<const grpc_generator::Method>> sorted_methods(
service->method_count());
stable_sort(sorted_methods.begin(), sorted_methods.end(),
CompareMethodClientStreaming);
for (size_t i = 0; i < sorted_methods.size(); i++) {
- auto& method = sorted_methods[i];
+ auto &method = sorted_methods[i];
vars["method_id"] = to_string(i);
vars["method_id_name"] = MethodIdFieldName(&*method);
p->Print(vars,
for (int i = 0; i < service->method_count(); ++i) {
auto method = service->method(i);
- if (method->ClientStreaming() || method->BidiStreaming()) {
- continue;
- }
+ if (method->ClientStreaming() || method->BidiStreaming()) { continue; }
vars["method_id_name"] = MethodIdFieldName(&*method);
vars["lower_method_name"] = LowerMethodName(&*method);
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
for (int i = 0; i < service->method_count(); ++i) {
auto method = service->method(i);
- if (!(method->ClientStreaming() || method->BidiStreaming())) {
- continue;
- }
+ if (!(method->ClientStreaming() || method->BidiStreaming())) { continue; }
vars["method_id_name"] = MethodIdFieldName(&*method);
vars["lower_method_name"] = LowerMethodName(&*method);
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
p->Print("}\n\n");
}
-static void PrintGetServiceDescriptorMethod(Printer* p, VARS& vars,
- const ServiceDescriptor* service) {
+static void PrintGetServiceDescriptorMethod(Printer *p, VARS &vars,
+ const ServiceDescriptor *service) {
vars["service_name"] = service->name();
// vars["proto_base_descriptor_supplier"] = service->name() +
// "BaseDescriptorSupplier"; vars["proto_file_descriptor_supplier"] =
p->Print("}\n");
}
-static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
- const ServiceDescriptor* service) {
+static void PrintBindServiceMethodBody(Printer *p, VARS &vars,
+ const ServiceDescriptor *service) {
vars["service_name"] = service->name();
p->Indent();
p->Print(vars,
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
vars["output_type"] = JavaClassName(vars, method->get_output_type_name());
vars["method_id_name"] = MethodIdFieldName(&*method);
- bool client_streaming = method->ClientStreaming() || method->BidiStreaming();
- bool server_streaming = method->ServerStreaming() || method->BidiStreaming();
+ bool client_streaming =
+ method->ClientStreaming() || method->BidiStreaming();
+ bool server_streaming =
+ method->ServerStreaming() || method->BidiStreaming();
if (client_streaming) {
if (server_streaming) {
vars["calls_method"] = "asyncBidiStreamingCall";
p->Outdent();
}
-static void PrintService(Printer* p, VARS& vars,
- const ServiceDescriptor* service,
+static void PrintService(Printer *p, VARS &vars,
+ const ServiceDescriptor *service,
bool disable_version) {
vars["service_name"] = service->name();
vars["service_class_name"] = ServiceClassName(service->name());
p->Print("}\n");
}
-void PrintStaticImports(Printer* p) {
+void PrintStaticImports(Printer *p) {
p->Print(
"import java.nio.ByteBuffer;\n"
"import static "
"io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;\n\n");
}
-void GenerateService(const grpc_generator::Service* service,
- grpc_generator::Printer* printer, VARS& vars,
+void GenerateService(const grpc_generator::Service *service,
+ grpc_generator::Printer *printer, VARS &vars,
bool disable_version) {
// All non-generated classes must be referred by fully qualified names to
// avoid collision with generated classes.
}
grpc::string GenerateServiceSource(
- grpc_generator::File* file, const grpc_generator::Service* service,
- grpc_java_generator::Parameters* parameters) {
+ grpc_generator::File *file, const grpc_generator::Service *service,
+ grpc_java_generator::Parameters *parameters) {
grpc::string out;
auto printer = file->CreatePrinter(&out);
VARS vars;