mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-28 07:58:05 +00:00
replace framework include of flatbuffers (#7079)
This commit is contained in:
@@ -23,19 +23,18 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <flatbuffers/util.h>
|
#include "flatbuffers/util.h"
|
||||||
#define to_string flatbuffers::NumToString
|
#define to_string flatbuffers::NumToString
|
||||||
|
|
||||||
// Stringify helpers used solely to cast GRPC_VERSION
|
// Stringify helpers used solely to cast GRPC_VERSION
|
||||||
#ifndef STR
|
#ifndef STR
|
||||||
#define STR(s) #s
|
# define STR(s) # s
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef XSTR
|
#ifndef XSTR
|
||||||
#define XSTR(s) STR(s)
|
# define XSTR(s) STR(s)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
typedef grpc_generator::Printer Printer;
|
typedef grpc_generator::Printer Printer;
|
||||||
typedef std::map<grpc::string, grpc::string> VARS;
|
typedef std::map<grpc::string, grpc::string> VARS;
|
||||||
typedef grpc_generator::Service ServiceDescriptor;
|
typedef grpc_generator::Service ServiceDescriptor;
|
||||||
@@ -46,12 +45,11 @@ typedef grpc_generator::Method MethodDescriptor;
|
|||||||
namespace grpc_java_generator {
|
namespace grpc_java_generator {
|
||||||
typedef std::string string;
|
typedef std::string string;
|
||||||
// Generates imports for the service
|
// Generates imports for the service
|
||||||
void GenerateImports(grpc_generator::File* file,
|
void GenerateImports(grpc_generator::File *file,
|
||||||
grpc_generator::Printer* printer, VARS& vars) {
|
grpc_generator::Printer *printer, VARS &vars) {
|
||||||
vars["filename"] = file->filename();
|
vars["filename"] = file->filename();
|
||||||
printer->Print(
|
printer->Print(vars,
|
||||||
vars,
|
"//Generated by flatc compiler (version $flatc_version$)\n");
|
||||||
"//Generated by flatc compiler (version $flatc_version$)\n");
|
|
||||||
printer->Print("//If you make any local changes, they will be lost\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, "//source: $filename$.fbs\n\n");
|
||||||
printer->Print(vars, "package $Package$;\n\n");
|
printer->Print(vars, "package $Package$;\n\n");
|
||||||
@@ -65,7 +63,7 @@ void GenerateImports(grpc_generator::File* file,
|
|||||||
// Adjust a method name prefix identifier to follow the JavaBean spec:
|
// Adjust a method name prefix identifier to follow the JavaBean spec:
|
||||||
// - decapitalize the first letter
|
// - decapitalize the first letter
|
||||||
// - remove embedded underscores & capitalize the following letter
|
// - remove embedded underscores & capitalize the following letter
|
||||||
static string MixedLower(const string& word) {
|
static string MixedLower(const string &word) {
|
||||||
string w;
|
string w;
|
||||||
w += static_cast<string::value_type>(tolower(word[0]));
|
w += static_cast<string::value_type>(tolower(word[0]));
|
||||||
bool after_underscore = false;
|
bool after_underscore = false;
|
||||||
@@ -85,7 +83,7 @@ static string MixedLower(const string& word) {
|
|||||||
// - An underscore is inserted where a lower case letter is followed by an
|
// - An underscore is inserted where a lower case letter is followed by an
|
||||||
// upper case letter.
|
// upper case letter.
|
||||||
// - All letters are converted to upper case
|
// - All letters are converted to upper case
|
||||||
static string ToAllUpperCase(const string& word) {
|
static string ToAllUpperCase(const string &word) {
|
||||||
string w;
|
string w;
|
||||||
for (size_t i = 0; i < word.length(); ++i) {
|
for (size_t i = 0; i < word.length(); ++i) {
|
||||||
w += static_cast<string::value_type>(toupper(word[i]));
|
w += static_cast<string::value_type>(toupper(word[i]));
|
||||||
@@ -96,47 +94,47 @@ static string ToAllUpperCase(const string& word) {
|
|||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline string LowerMethodName(const MethodDescriptor* method) {
|
static inline string LowerMethodName(const MethodDescriptor *method) {
|
||||||
return MixedLower(method->name());
|
return MixedLower(method->name());
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline string MethodPropertiesFieldName(const MethodDescriptor* method) {
|
static inline string MethodPropertiesFieldName(const MethodDescriptor *method) {
|
||||||
return "METHOD_" + ToAllUpperCase(method->name());
|
return "METHOD_" + ToAllUpperCase(method->name());
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline string MethodPropertiesGetterName(
|
static inline string MethodPropertiesGetterName(
|
||||||
const MethodDescriptor* method) {
|
const MethodDescriptor *method) {
|
||||||
return MixedLower("get_" + method->name() + "_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());
|
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);
|
// string name = google::protobuf::compiler::java::ClassName(desc);
|
||||||
return vars["Package"] + name;
|
return vars["Package"] + name;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline string ServiceClassName(const string& service_name) {
|
static inline string ServiceClassName(const string &service_name) {
|
||||||
return service_name + "Grpc";
|
return service_name + "Grpc";
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(nmittler): Remove once protobuf includes javadoc methods in
|
// TODO(nmittler): Remove once protobuf includes javadoc methods in
|
||||||
// distribution.
|
// distribution.
|
||||||
template <typename ITR>
|
template<typename ITR>
|
||||||
static void GrpcSplitStringToIteratorUsing(const string& full,
|
static void GrpcSplitStringToIteratorUsing(const string &full,
|
||||||
const char* delim, ITR& result) {
|
const char *delim, ITR &result) {
|
||||||
// Optimize the common case where delim is a single character.
|
// Optimize the common case where delim is a single character.
|
||||||
if (delim[0] != '\0' && delim[1] == '\0') {
|
if (delim[0] != '\0' && delim[1] == '\0') {
|
||||||
char c = delim[0];
|
char c = delim[0];
|
||||||
const char* p = full.data();
|
const char *p = full.data();
|
||||||
const char* end = p + full.size();
|
const char *end = p + full.size();
|
||||||
while (p != end) {
|
while (p != end) {
|
||||||
if (*p == c) {
|
if (*p == c) {
|
||||||
++p;
|
++p;
|
||||||
} else {
|
} else {
|
||||||
const char* start = p;
|
const char *start = p;
|
||||||
while (++p != end && *p != c)
|
while (++p != end && *p != c)
|
||||||
;
|
;
|
||||||
*result++ = string(start, p - start);
|
*result++ = string(start, p - start);
|
||||||
@@ -158,13 +156,13 @@ static void GrpcSplitStringToIteratorUsing(const string& full,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void GrpcSplitStringUsing(const string& full, const char* delim,
|
static void GrpcSplitStringUsing(const string &full, const char *delim,
|
||||||
std::vector<string>* result) {
|
std::vector<string> *result) {
|
||||||
std::back_insert_iterator<std::vector<string>> it(*result);
|
std::back_insert_iterator<std::vector<string>> it(*result);
|
||||||
GrpcSplitStringToIteratorUsing(full, delim, it);
|
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;
|
std::vector<string> result;
|
||||||
GrpcSplitStringUsing(full, delim, &result);
|
GrpcSplitStringUsing(full, delim, &result);
|
||||||
return result;
|
return result;
|
||||||
@@ -172,7 +170,7 @@ static std::vector<string> GrpcSplit(const string& full, const char* delim) {
|
|||||||
|
|
||||||
// TODO(nmittler): Remove once protobuf includes javadoc methods in
|
// TODO(nmittler): Remove once protobuf includes javadoc methods in
|
||||||
// distribution.
|
// distribution.
|
||||||
static string GrpcEscapeJavadoc(const string& input) {
|
static string GrpcEscapeJavadoc(const string &input) {
|
||||||
string result;
|
string result;
|
||||||
result.reserve(input.size() * 2);
|
result.reserve(input.size() * 2);
|
||||||
|
|
||||||
@@ -219,9 +217,7 @@ static string GrpcEscapeJavadoc(const string& input) {
|
|||||||
// Java interprets Unicode escape sequences anywhere!
|
// Java interprets Unicode escape sequences anywhere!
|
||||||
result.append("\");
|
result.append("\");
|
||||||
break;
|
break;
|
||||||
default:
|
default: result.push_back(c); break;
|
||||||
result.push_back(c);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prev = c;
|
prev = c;
|
||||||
@@ -230,7 +226,7 @@ static string GrpcEscapeJavadoc(const string& input) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::vector<string> GrpcGetDocLines(const string& comments) {
|
static std::vector<string> GrpcGetDocLines(const string &comments) {
|
||||||
if (!comments.empty()) {
|
if (!comments.empty()) {
|
||||||
// TODO(kenton): Ideally we should parse the comment text as Markdown and
|
// 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
|
// write it back as HTML, but this requires a Markdown parser. For now
|
||||||
@@ -241,27 +237,23 @@ static std::vector<string> GrpcGetDocLines(const string& comments) {
|
|||||||
string escapedComments = GrpcEscapeJavadoc(comments);
|
string escapedComments = GrpcEscapeJavadoc(comments);
|
||||||
|
|
||||||
std::vector<string> lines = GrpcSplit(escapedComments, "\n");
|
std::vector<string> lines = GrpcSplit(escapedComments, "\n");
|
||||||
while (!lines.empty() && lines.back().empty()) {
|
while (!lines.empty() && lines.back().empty()) { lines.pop_back(); }
|
||||||
lines.pop_back();
|
|
||||||
}
|
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
return std::vector<string>();
|
return std::vector<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::vector<string> GrpcGetDocLinesForDescriptor(
|
static std::vector<string> GrpcGetDocLinesForDescriptor(
|
||||||
const DescriptorType* descriptor) {
|
const DescriptorType *descriptor) {
|
||||||
return descriptor->GetAllComments();
|
return descriptor->GetAllComments();
|
||||||
// return GrpcGetDocLines(descriptor->GetLeadingComments("///"));
|
// return GrpcGetDocLines(descriptor->GetLeadingComments("///"));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void GrpcWriteDocCommentBody(Printer* printer, VARS& vars,
|
static void GrpcWriteDocCommentBody(Printer *printer, VARS &vars,
|
||||||
const std::vector<string>& lines,
|
const std::vector<string> &lines,
|
||||||
bool surroundWithPreTag) {
|
bool surroundWithPreTag) {
|
||||||
if (!lines.empty()) {
|
if (!lines.empty()) {
|
||||||
if (surroundWithPreTag) {
|
if (surroundWithPreTag) { printer->Print(" * <pre>\n"); }
|
||||||
printer->Print(" * <pre>\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t i = 0; i < lines.size(); i++) {
|
for (size_t i = 0; i < lines.size(); i++) {
|
||||||
// Most lines should start with a space. Watch out for lines that start
|
// Most lines should start with a space. Watch out for lines that start
|
||||||
@@ -275,73 +267,72 @@ static void GrpcWriteDocCommentBody(Printer* printer, VARS& vars,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (surroundWithPreTag) {
|
if (surroundWithPreTag) { printer->Print(" * </pre>\n"); }
|
||||||
printer->Print(" * </pre>\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void GrpcWriteDocComment(Printer* printer, VARS& vars,
|
static void GrpcWriteDocComment(Printer *printer, VARS &vars,
|
||||||
const string& comments) {
|
const string &comments) {
|
||||||
printer->Print("/**\n");
|
printer->Print("/**\n");
|
||||||
std::vector<string> lines = GrpcGetDocLines(comments);
|
std::vector<string> lines = GrpcGetDocLines(comments);
|
||||||
GrpcWriteDocCommentBody(printer, vars, lines, false);
|
GrpcWriteDocCommentBody(printer, vars, lines, false);
|
||||||
printer->Print(" */\n");
|
printer->Print(" */\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void GrpcWriteServiceDocComment(Printer* printer, VARS& vars,
|
static void GrpcWriteServiceDocComment(Printer *printer, VARS &vars,
|
||||||
const ServiceDescriptor* service) {
|
const ServiceDescriptor *service) {
|
||||||
printer->Print("/**\n");
|
printer->Print("/**\n");
|
||||||
std::vector<string> lines = GrpcGetDocLinesForDescriptor(service);
|
std::vector<string> lines = GrpcGetDocLinesForDescriptor(service);
|
||||||
GrpcWriteDocCommentBody(printer, vars, lines, true);
|
GrpcWriteDocCommentBody(printer, vars, lines, true);
|
||||||
printer->Print(" */\n");
|
printer->Print(" */\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void GrpcWriteMethodDocComment(Printer* printer, VARS& vars,
|
void GrpcWriteMethodDocComment(Printer *printer, VARS &vars,
|
||||||
const MethodDescriptor* method) {
|
const MethodDescriptor *method) {
|
||||||
printer->Print("/**\n");
|
printer->Print("/**\n");
|
||||||
std::vector<string> lines = GrpcGetDocLinesForDescriptor(method);
|
std::vector<string> lines = GrpcGetDocLinesForDescriptor(method);
|
||||||
GrpcWriteDocCommentBody(printer, vars, lines, true);
|
GrpcWriteDocCommentBody(printer, vars, lines, true);
|
||||||
printer->Print(" */\n");
|
printer->Print(" */\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
//outputs static singleton extractor for type stored in "extr_type" and "extr_type_name" vars
|
// outputs static singleton extractor for type stored in "extr_type" and
|
||||||
static void PrintTypeExtractor(Printer* p, VARS& vars) {
|
// "extr_type_name" vars
|
||||||
p->Print(
|
static void PrintTypeExtractor(Printer *p, VARS &vars) {
|
||||||
vars,
|
p->Print(vars,
|
||||||
"private static volatile FlatbuffersUtils.FBExtactor<$extr_type$> "
|
"private static volatile FlatbuffersUtils.FBExtactor<$extr_type$> "
|
||||||
"extractorOf$extr_type_name$;\n"
|
"extractorOf$extr_type_name$;\n"
|
||||||
"private static FlatbuffersUtils.FBExtactor<$extr_type$> "
|
"private static FlatbuffersUtils.FBExtactor<$extr_type$> "
|
||||||
"getExtractorOf$extr_type_name$() {\n"
|
"getExtractorOf$extr_type_name$() {\n"
|
||||||
" if (extractorOf$extr_type_name$ != null) return "
|
" if (extractorOf$extr_type_name$ != null) return "
|
||||||
"extractorOf$extr_type_name$;\n"
|
"extractorOf$extr_type_name$;\n"
|
||||||
" synchronized ($service_class_name$.class) {\n"
|
" synchronized ($service_class_name$.class) {\n"
|
||||||
" if (extractorOf$extr_type_name$ != null) return "
|
" if (extractorOf$extr_type_name$ != null) return "
|
||||||
"extractorOf$extr_type_name$;\n"
|
"extractorOf$extr_type_name$;\n"
|
||||||
" extractorOf$extr_type_name$ = new "
|
" extractorOf$extr_type_name$ = new "
|
||||||
"FlatbuffersUtils.FBExtactor<$extr_type$>() {\n"
|
"FlatbuffersUtils.FBExtactor<$extr_type$>() {\n"
|
||||||
" public $extr_type$ extract (ByteBuffer buffer) {\n"
|
" public $extr_type$ extract (ByteBuffer buffer) {\n"
|
||||||
" return "
|
" return "
|
||||||
"$extr_type$.getRootAs$extr_type_name$(buffer);\n"
|
"$extr_type$.getRootAs$extr_type_name$(buffer);\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
" };\n"
|
" };\n"
|
||||||
" return extractorOf$extr_type_name$;\n"
|
" return extractorOf$extr_type_name$;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n\n");
|
"}\n\n");
|
||||||
}
|
}
|
||||||
static void PrintMethodFields(Printer* p, VARS& vars,
|
static void PrintMethodFields(Printer *p, VARS &vars,
|
||||||
const ServiceDescriptor* service) {
|
const ServiceDescriptor *service) {
|
||||||
p->Print("// Static method descriptors that strictly reflect the proto.\n");
|
p->Print("// Static method descriptors that strictly reflect the proto.\n");
|
||||||
vars["service_name"] = service->name();
|
vars["service_name"] = service->name();
|
||||||
|
|
||||||
//set of names of rpc input- and output- types that were already encountered.
|
// 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
|
// 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
|
// the same type is used as an input or output type of more than a single RPC
|
||||||
|
// method
|
||||||
std::set<std::string> encounteredTypes;
|
std::set<std::string> encounteredTypes;
|
||||||
|
|
||||||
for (int i = 0; i < service->method_count(); ++i) {
|
for (int i = 0; i < service->method_count(); ++i) {
|
||||||
auto method = service->method(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["arg_out_id"] = to_string(2L * i + 1);
|
||||||
vars["method_name"] = method->name();
|
vars["method_name"] = method->name();
|
||||||
vars["input_type_name"] = method->get_input_type_name();
|
vars["input_type_name"] = method->get_input_type_name();
|
||||||
@@ -351,8 +342,10 @@ static void PrintMethodFields(Printer* p, VARS& vars,
|
|||||||
vars["method_field_name"] = MethodPropertiesFieldName(method.get());
|
vars["method_field_name"] = MethodPropertiesFieldName(method.get());
|
||||||
vars["method_new_field_name"] = MethodPropertiesGetterName(method.get());
|
vars["method_new_field_name"] = MethodPropertiesGetterName(method.get());
|
||||||
vars["method_method_name"] = MethodPropertiesGetterName(method.get());
|
vars["method_method_name"] = MethodPropertiesGetterName(method.get());
|
||||||
bool client_streaming = method->ClientStreaming() || method->BidiStreaming();
|
bool client_streaming =
|
||||||
bool server_streaming = method->ServerStreaming() || method->BidiStreaming();
|
method->ClientStreaming() || method->BidiStreaming();
|
||||||
|
bool server_streaming =
|
||||||
|
method->ServerStreaming() || method->BidiStreaming();
|
||||||
if (client_streaming) {
|
if (client_streaming) {
|
||||||
if (server_streaming) {
|
if (server_streaming) {
|
||||||
vars["method_type"] = "BIDI_STREAMING";
|
vars["method_type"] = "BIDI_STREAMING";
|
||||||
@@ -392,32 +385,32 @@ static void PrintMethodFields(Printer* p, VARS& vars,
|
|||||||
}
|
}
|
||||||
|
|
||||||
p->Print(
|
p->Print(
|
||||||
vars,
|
vars,
|
||||||
"@$ExperimentalApi$(\"https://github.com/grpc/grpc-java/issues/"
|
"@$ExperimentalApi$(\"https://github.com/grpc/grpc-java/issues/"
|
||||||
"1901\")\n"
|
"1901\")\n"
|
||||||
"public static $MethodDescriptor$<$input_type$,\n"
|
"public static $MethodDescriptor$<$input_type$,\n"
|
||||||
" $output_type$> $method_method_name$() {\n"
|
" $output_type$> $method_method_name$() {\n"
|
||||||
" $MethodDescriptor$<$input_type$, $output_type$> "
|
" $MethodDescriptor$<$input_type$, $output_type$> "
|
||||||
"$method_new_field_name$;\n"
|
"$method_new_field_name$;\n"
|
||||||
" if (($method_new_field_name$ = "
|
" if (($method_new_field_name$ = "
|
||||||
"$service_class_name$.$method_new_field_name$) == null) {\n"
|
"$service_class_name$.$method_new_field_name$) == null) {\n"
|
||||||
" synchronized ($service_class_name$.class) {\n"
|
" synchronized ($service_class_name$.class) {\n"
|
||||||
" if (($method_new_field_name$ = "
|
" if (($method_new_field_name$ = "
|
||||||
"$service_class_name$.$method_new_field_name$) == null) {\n"
|
"$service_class_name$.$method_new_field_name$) == null) {\n"
|
||||||
" $service_class_name$.$method_new_field_name$ = "
|
" $service_class_name$.$method_new_field_name$ = "
|
||||||
"$method_new_field_name$ = \n"
|
"$method_new_field_name$ = \n"
|
||||||
" $MethodDescriptor$.<$input_type$, "
|
" $MethodDescriptor$.<$input_type$, "
|
||||||
"$output_type$>newBuilder()\n"
|
"$output_type$>newBuilder()\n"
|
||||||
" .setType($MethodType$.$method_type$)\n"
|
" .setType($MethodType$.$method_type$)\n"
|
||||||
" .setFullMethodName(generateFullMethodName(\n"
|
" .setFullMethodName(generateFullMethodName(\n"
|
||||||
" \"$Package$$service_name$\", \"$method_name$\"))\n"
|
" \"$Package$$service_name$\", \"$method_name$\"))\n"
|
||||||
" .setSampledToLocalTracing(true)\n"
|
" .setSampledToLocalTracing(true)\n"
|
||||||
" .setRequestMarshaller(FlatbuffersUtils.marshaller(\n"
|
" .setRequestMarshaller(FlatbuffersUtils.marshaller(\n"
|
||||||
" $input_type$.class, "
|
" $input_type$.class, "
|
||||||
"getExtractorOf$input_type_name$()))\n"
|
"getExtractorOf$input_type_name$()))\n"
|
||||||
" .setResponseMarshaller(FlatbuffersUtils.marshaller(\n"
|
" .setResponseMarshaller(FlatbuffersUtils.marshaller(\n"
|
||||||
" $output_type$.class, "
|
" $output_type$.class, "
|
||||||
"getExtractorOf$output_type_name$()))\n");
|
"getExtractorOf$output_type_name$()))\n");
|
||||||
|
|
||||||
// vars["proto_method_descriptor_supplier"] = service->name() +
|
// vars["proto_method_descriptor_supplier"] = service->name() +
|
||||||
// "MethodDescriptorSupplier";
|
// "MethodDescriptorSupplier";
|
||||||
@@ -449,11 +442,11 @@ enum StubType {
|
|||||||
|
|
||||||
enum CallType { ASYNC_CALL = 0, BLOCKING_CALL = 1, FUTURE_CALL = 2 };
|
enum CallType { ASYNC_CALL = 0, BLOCKING_CALL = 1, FUTURE_CALL = 2 };
|
||||||
|
|
||||||
static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
|
static void PrintBindServiceMethodBody(Printer *p, VARS &vars,
|
||||||
const ServiceDescriptor* service);
|
const ServiceDescriptor *service);
|
||||||
|
|
||||||
// Prints a client interface or implementation class, or a server interface.
|
// 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) {
|
StubType type) {
|
||||||
const string service_name = service->name();
|
const string service_name = service->name();
|
||||||
vars["service_name"] = service_name;
|
vars["service_name"] = service_name;
|
||||||
@@ -474,7 +467,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
|||||||
break;
|
break;
|
||||||
case BLOCKING_CLIENT_INTERFACE:
|
case BLOCKING_CLIENT_INTERFACE:
|
||||||
interface = true;
|
interface = true;
|
||||||
FLATBUFFERS_FALLTHROUGH(); // fall thru
|
FLATBUFFERS_FALLTHROUGH(); // fall thru
|
||||||
case BLOCKING_CLIENT_IMPL:
|
case BLOCKING_CLIENT_IMPL:
|
||||||
call_type = BLOCKING_CALL;
|
call_type = BLOCKING_CALL;
|
||||||
stub_name += "BlockingStub";
|
stub_name += "BlockingStub";
|
||||||
@@ -482,7 +475,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
|||||||
break;
|
break;
|
||||||
case FUTURE_CLIENT_INTERFACE:
|
case FUTURE_CLIENT_INTERFACE:
|
||||||
interface = true;
|
interface = true;
|
||||||
FLATBUFFERS_FALLTHROUGH(); // fall thru
|
FLATBUFFERS_FALLTHROUGH(); // fall thru
|
||||||
case FUTURE_CLIENT_IMPL:
|
case FUTURE_CLIENT_IMPL:
|
||||||
call_type = FUTURE_CALL;
|
call_type = FUTURE_CALL;
|
||||||
stub_name += "FutureStub";
|
stub_name += "FutureStub";
|
||||||
@@ -499,9 +492,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
|||||||
vars["client_name"] = client_name;
|
vars["client_name"] = client_name;
|
||||||
|
|
||||||
// Class head
|
// Class head
|
||||||
if (!interface) {
|
if (!interface) { GrpcWriteServiceDocComment(p, vars, service); }
|
||||||
GrpcWriteServiceDocComment(p, vars, service);
|
|
||||||
}
|
|
||||||
if (impl_base) {
|
if (impl_base) {
|
||||||
p->Print(vars,
|
p->Print(vars,
|
||||||
"public static abstract class $abstract_name$ implements "
|
"public static abstract class $abstract_name$ implements "
|
||||||
@@ -544,8 +535,10 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
|||||||
vars["output_type"] = JavaClassName(vars, method->get_output_type_name());
|
vars["output_type"] = JavaClassName(vars, method->get_output_type_name());
|
||||||
vars["lower_method_name"] = LowerMethodName(&*method);
|
vars["lower_method_name"] = LowerMethodName(&*method);
|
||||||
vars["method_method_name"] = MethodPropertiesGetterName(&*method);
|
vars["method_method_name"] = MethodPropertiesGetterName(&*method);
|
||||||
bool client_streaming = method->ClientStreaming() || method->BidiStreaming();
|
bool client_streaming =
|
||||||
bool server_streaming = method->ServerStreaming() || method->BidiStreaming();
|
method->ClientStreaming() || method->BidiStreaming();
|
||||||
|
bool server_streaming =
|
||||||
|
method->ServerStreaming() || method->BidiStreaming();
|
||||||
|
|
||||||
if (call_type == BLOCKING_CALL && client_streaming) {
|
if (call_type == BLOCKING_CALL && client_streaming) {
|
||||||
// Blocking client interface with client streaming is not available
|
// Blocking client interface with client streaming is not available
|
||||||
@@ -561,9 +554,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
|||||||
p->Print("\n");
|
p->Print("\n");
|
||||||
// TODO(nmittler): Replace with WriteMethodDocComment once included by the
|
// TODO(nmittler): Replace with WriteMethodDocComment once included by the
|
||||||
// protobuf distro.
|
// protobuf distro.
|
||||||
if (!interface) {
|
if (!interface) { GrpcWriteMethodDocComment(p, vars, &*method); }
|
||||||
GrpcWriteMethodDocComment(p, vars, &*method);
|
|
||||||
}
|
|
||||||
p->Print("public ");
|
p->Print("public ");
|
||||||
switch (call_type) {
|
switch (call_type) {
|
||||||
case BLOCKING_CALL:
|
case BLOCKING_CALL:
|
||||||
@@ -628,8 +619,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
|||||||
"responseObserver);\n");
|
"responseObserver);\n");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default: break;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} else if (!interface) {
|
} else if (!interface) {
|
||||||
switch (call_type) {
|
switch (call_type) {
|
||||||
@@ -704,15 +694,15 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool CompareMethodClientStreaming(
|
static bool CompareMethodClientStreaming(
|
||||||
const std::unique_ptr<const grpc_generator::Method>& method1,
|
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> &method2) {
|
||||||
return method1->ClientStreaming() < method2->ClientStreaming();
|
return method1->ClientStreaming() < method2->ClientStreaming();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Place all method invocations into a single class to reduce memory footprint
|
// Place all method invocations into a single class to reduce memory footprint
|
||||||
// on Android.
|
// on Android.
|
||||||
static void PrintMethodHandlerClass(Printer* p, VARS& vars,
|
static void PrintMethodHandlerClass(Printer *p, VARS &vars,
|
||||||
const ServiceDescriptor* service) {
|
const ServiceDescriptor *service) {
|
||||||
// Sort method ids based on ClientStreaming() so switch tables are compact.
|
// Sort method ids based on ClientStreaming() so switch tables are compact.
|
||||||
std::vector<std::unique_ptr<const grpc_generator::Method>> sorted_methods(
|
std::vector<std::unique_ptr<const grpc_generator::Method>> sorted_methods(
|
||||||
service->method_count());
|
service->method_count());
|
||||||
@@ -722,7 +712,7 @@ static void PrintMethodHandlerClass(Printer* p, VARS& vars,
|
|||||||
stable_sort(sorted_methods.begin(), sorted_methods.end(),
|
stable_sort(sorted_methods.begin(), sorted_methods.end(),
|
||||||
CompareMethodClientStreaming);
|
CompareMethodClientStreaming);
|
||||||
for (size_t i = 0; i < sorted_methods.size(); i++) {
|
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"] = to_string(i);
|
||||||
vars["method_id_name"] = MethodIdFieldName(&*method);
|
vars["method_id_name"] = MethodIdFieldName(&*method);
|
||||||
p->Print(vars,
|
p->Print(vars,
|
||||||
@@ -755,9 +745,7 @@ static void PrintMethodHandlerClass(Printer* p, VARS& vars,
|
|||||||
|
|
||||||
for (int i = 0; i < service->method_count(); ++i) {
|
for (int i = 0; i < service->method_count(); ++i) {
|
||||||
auto method = service->method(i);
|
auto method = service->method(i);
|
||||||
if (method->ClientStreaming() || method->BidiStreaming()) {
|
if (method->ClientStreaming() || method->BidiStreaming()) { continue; }
|
||||||
continue;
|
|
||||||
}
|
|
||||||
vars["method_id_name"] = MethodIdFieldName(&*method);
|
vars["method_id_name"] = MethodIdFieldName(&*method);
|
||||||
vars["lower_method_name"] = LowerMethodName(&*method);
|
vars["lower_method_name"] = LowerMethodName(&*method);
|
||||||
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
|
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
|
||||||
@@ -789,9 +777,7 @@ static void PrintMethodHandlerClass(Printer* p, VARS& vars,
|
|||||||
|
|
||||||
for (int i = 0; i < service->method_count(); ++i) {
|
for (int i = 0; i < service->method_count(); ++i) {
|
||||||
auto method = service->method(i);
|
auto method = service->method(i);
|
||||||
if (!(method->ClientStreaming() || method->BidiStreaming())) {
|
if (!(method->ClientStreaming() || method->BidiStreaming())) { continue; }
|
||||||
continue;
|
|
||||||
}
|
|
||||||
vars["method_id_name"] = MethodIdFieldName(&*method);
|
vars["method_id_name"] = MethodIdFieldName(&*method);
|
||||||
vars["lower_method_name"] = LowerMethodName(&*method);
|
vars["lower_method_name"] = LowerMethodName(&*method);
|
||||||
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
|
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
|
||||||
@@ -816,8 +802,8 @@ static void PrintMethodHandlerClass(Printer* p, VARS& vars,
|
|||||||
p->Print("}\n\n");
|
p->Print("}\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void PrintGetServiceDescriptorMethod(Printer* p, VARS& vars,
|
static void PrintGetServiceDescriptorMethod(Printer *p, VARS &vars,
|
||||||
const ServiceDescriptor* service) {
|
const ServiceDescriptor *service) {
|
||||||
vars["service_name"] = service->name();
|
vars["service_name"] = service->name();
|
||||||
// vars["proto_base_descriptor_supplier"] = service->name() +
|
// vars["proto_base_descriptor_supplier"] = service->name() +
|
||||||
// "BaseDescriptorSupplier"; vars["proto_file_descriptor_supplier"] =
|
// "BaseDescriptorSupplier"; vars["proto_file_descriptor_supplier"] =
|
||||||
@@ -909,8 +895,8 @@ static void PrintGetServiceDescriptorMethod(Printer* p, VARS& vars,
|
|||||||
p->Print("}\n");
|
p->Print("}\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
|
static void PrintBindServiceMethodBody(Printer *p, VARS &vars,
|
||||||
const ServiceDescriptor* service) {
|
const ServiceDescriptor *service) {
|
||||||
vars["service_name"] = service->name();
|
vars["service_name"] = service->name();
|
||||||
p->Indent();
|
p->Indent();
|
||||||
p->Print(vars,
|
p->Print(vars,
|
||||||
@@ -925,8 +911,10 @@ static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
|
|||||||
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
|
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
|
||||||
vars["output_type"] = JavaClassName(vars, method->get_output_type_name());
|
vars["output_type"] = JavaClassName(vars, method->get_output_type_name());
|
||||||
vars["method_id_name"] = MethodIdFieldName(&*method);
|
vars["method_id_name"] = MethodIdFieldName(&*method);
|
||||||
bool client_streaming = method->ClientStreaming() || method->BidiStreaming();
|
bool client_streaming =
|
||||||
bool server_streaming = method->ServerStreaming() || method->BidiStreaming();
|
method->ClientStreaming() || method->BidiStreaming();
|
||||||
|
bool server_streaming =
|
||||||
|
method->ServerStreaming() || method->BidiStreaming();
|
||||||
if (client_streaming) {
|
if (client_streaming) {
|
||||||
if (server_streaming) {
|
if (server_streaming) {
|
||||||
vars["calls_method"] = "asyncBidiStreamingCall";
|
vars["calls_method"] = "asyncBidiStreamingCall";
|
||||||
@@ -960,8 +948,8 @@ static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
|
|||||||
p->Outdent();
|
p->Outdent();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void PrintService(Printer* p, VARS& vars,
|
static void PrintService(Printer *p, VARS &vars,
|
||||||
const ServiceDescriptor* service,
|
const ServiceDescriptor *service,
|
||||||
bool disable_version) {
|
bool disable_version) {
|
||||||
vars["service_name"] = service->name();
|
vars["service_name"] = service->name();
|
||||||
vars["service_class_name"] = ServiceClassName(service->name());
|
vars["service_class_name"] = ServiceClassName(service->name());
|
||||||
@@ -1041,7 +1029,7 @@ static void PrintService(Printer* p, VARS& vars,
|
|||||||
p->Print("}\n");
|
p->Print("}\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintStaticImports(Printer* p) {
|
void PrintStaticImports(Printer *p) {
|
||||||
p->Print(
|
p->Print(
|
||||||
"import java.nio.ByteBuffer;\n"
|
"import java.nio.ByteBuffer;\n"
|
||||||
"import static "
|
"import static "
|
||||||
@@ -1074,8 +1062,8 @@ void PrintStaticImports(Printer* p) {
|
|||||||
"io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;\n\n");
|
"io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenerateService(const grpc_generator::Service* service,
|
void GenerateService(const grpc_generator::Service *service,
|
||||||
grpc_generator::Printer* printer, VARS& vars,
|
grpc_generator::Printer *printer, VARS &vars,
|
||||||
bool disable_version) {
|
bool disable_version) {
|
||||||
// All non-generated classes must be referred by fully qualified names to
|
// All non-generated classes must be referred by fully qualified names to
|
||||||
// avoid collision with generated classes.
|
// avoid collision with generated classes.
|
||||||
@@ -1111,8 +1099,8 @@ void GenerateService(const grpc_generator::Service* service,
|
|||||||
}
|
}
|
||||||
|
|
||||||
grpc::string GenerateServiceSource(
|
grpc::string GenerateServiceSource(
|
||||||
grpc_generator::File* file, const grpc_generator::Service* service,
|
grpc_generator::File *file, const grpc_generator::Service *service,
|
||||||
grpc_java_generator::Parameters* parameters) {
|
grpc_java_generator::Parameters *parameters) {
|
||||||
grpc::string out;
|
grpc::string out;
|
||||||
auto printer = file->CreatePrinter(&out);
|
auto printer = file->CreatePrinter(&out);
|
||||||
VARS vars;
|
VARS vars;
|
||||||
|
|||||||
Reference in New Issue
Block a user