#include "Errors.h" #include "stream_proto_utils.h" #include "string_utils.h" #include #include #include #include #include using namespace android::stream_proto; using namespace google::protobuf::io; using namespace std; /** * If the descriptor gives us a class name, use that. Otherwise make one up from * the filename of the .proto file. */ static string make_outer_class_name(const FileDescriptorProto& file_descriptor) { string name = file_descriptor.options().java_outer_classname(); if (name.size() == 0) { name = to_camel_case(file_base_name(file_descriptor.name())); if (name.size() == 0) { ERRORS.Add(UNKNOWN_FILE, UNKNOWN_LINE, "Unable to make an outer class name for file: %s", file_descriptor.name().c_str()); name = "Unknown"; } } return name; } /** * Figure out the package name that we are generating. */ static string make_java_package(const FileDescriptorProto& file_descriptor) { if (file_descriptor.options().has_java_package()) { return file_descriptor.options().java_package(); } else { return file_descriptor.package(); } } /** * Figure out the name of the file we are generating. */ static string make_file_name(const FileDescriptorProto& file_descriptor, const string& class_name) { string const package = make_java_package(file_descriptor); string result; if (package.size() > 0) { result = replace_string(package, '.', '/'); result += '/'; } result += class_name; result += ".java"; return result; } static string indent_more(const string& indent) { return indent + INDENT; } /** * Write the constants for an enum. */ static void write_enum(stringstream& text, const EnumDescriptorProto& enu, const string& indent) { const int N = enu.value_size(); text << indent << "// enum " << enu.name() << endl; for (int i=0; i& enums, const vector& messages) { stringstream text; string const package_name = make_java_package(file_descriptor); string const outer_class_name = make_outer_class_name(file_descriptor); text << "// Generated by protoc-gen-javastream. DO NOT MODIFY." << endl; text << "// source: " << file_descriptor.name() << endl << endl; if (package_name.size() > 0) { if (package_name.size() > 0) { text << "package " << package_name << ";" << endl; text << endl; } } // This bit of policy is android api rules specific: Raw proto classes // must never be in the API text << "/** @hide */" << endl; // text << "@android.annotation.TestApi" << endl; if (generate_outer) { text << "public final class " << outer_class_name << " {" << endl; text << endl; } size_t N; const string indented = generate_outer ? indent_more("") : string(); N = enums.size(); for (size_t i=0; iadd_file(); file_response->set_name(filename); file_response->set_content(text.str()); } /** * Write one file per class. Put all of the enums into the "outer" class. */ static void write_multiple_files(CodeGeneratorResponse* response, const FileDescriptorProto& file_descriptor) { // If there is anything to put in the outer class file, create one if (file_descriptor.enum_type_size() > 0) { vector enums; int N = file_descriptor.enum_type_size(); for (int i=0; i messages; write_file(response, file_descriptor, make_file_name(file_descriptor, make_outer_class_name(file_descriptor)), true, enums, messages); } // For each of the message types, make a file int N = file_descriptor.message_type_size(); for (int i=0; i enums; vector messages; messages.push_back(file_descriptor.message_type(i)); write_file(response, file_descriptor, make_file_name(file_descriptor, file_descriptor.message_type(i).name()), false, enums, messages); } } static void write_single_file(CodeGeneratorResponse* response, const FileDescriptorProto& file_descriptor) { int N; vector enums; N = file_descriptor.enum_type_size(); for (int i=0; i messages; N = file_descriptor.message_type_size(); for (int i=0; i