Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit be537857 authored by Joe Onorato's avatar Joe Onorato Committed by Android (Google) Code Review
Browse files

Merge changes from topic 'proto convenience methods'

* changes:
  ProtoOutputStream convenience methods.
  Give protoc-gen-javastream the ability to output multiple java files.
parents 56832046 fb9f736a
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -41525,11 +41525,15 @@ package android.util.proto {
  public final class ProtoOutputStream {
    ctor public ProtoOutputStream();
    ctor public ProtoOutputStream(int);
    ctor public ProtoOutputStream(java.io.OutputStream);
    ctor public ProtoOutputStream(java.io.FileDescriptor);
    method public static int checkFieldId(long, long);
    method public static int convertObjectIdToOrdinal(int);
    method public void dump(java.lang.String);
    method public void end(long);
    method public void endObject(long);
    method public void endRepeatedObject(long);
    method public void flush();
    method public byte[] getBytes();
    method public static int getDepthFromToken(long);
    method public static int getObjectIdFromToken(long);
@@ -41538,9 +41542,17 @@ package android.util.proto {
    method public static int getTagSizeFromToken(long);
    method public static long makeFieldId(int, long);
    method public static long makeToken(int, boolean, int, int, int);
    method public long start(long);
    method public long startObject(long);
    method public long startRepeatedObject(long);
    method public static java.lang.String token2String(long);
    method public void write(long, double);
    method public void write(long, float);
    method public void write(long, int);
    method public void write(long, long);
    method public void write(long, boolean);
    method public void write(long, java.lang.String);
    method public void write(long, byte[]);
    method public void writeBool(long, boolean);
    method public void writeBytes(long, byte[]);
    method public void writeDouble(long, double);
@@ -41550,6 +41562,8 @@ package android.util.proto {
    method public void writeFloat(long, float);
    method public void writeInt32(long, int);
    method public void writeInt64(long, long);
    method public void writeObject(long, byte[]);
    method public void writeObjectImpl(int, byte[]);
    method public void writePackedBool(long, boolean[]);
    method public void writePackedDouble(long, double[]);
    method public void writePackedEnum(long, int[]);
@@ -41573,6 +41587,8 @@ package android.util.proto {
    method public void writeRepeatedFloat(long, float);
    method public void writeRepeatedInt32(long, int);
    method public void writeRepeatedInt64(long, long);
    method public void writeRepeatedObject(long, byte[]);
    method public void writeRepeatedObjectImpl(int, byte[]);
    method public void writeRepeatedSFixed32(long, int);
    method public void writeRepeatedSFixed64(long, long);
    method public void writeRepeatedSInt32(long, int);
+903 −0

File changed.

Preview size limit exceeded, changes collapsed.

+88 −22
Original line number Diff line number Diff line
@@ -94,7 +94,7 @@ make_java_package(const FileDescriptorProto& file_descriptor) {
 * Figure out the name of the file we are generating.
 */
static string
make_file_name(const FileDescriptorProto& file_descriptor)
make_file_name(const FileDescriptorProto& file_descriptor, const string& class_name)
{
    string const package = make_java_package(file_descriptor);
    string result;
@@ -103,7 +103,7 @@ make_file_name(const FileDescriptorProto& file_descriptor)
        result += '/';
    }

    result += make_outer_class_name(file_descriptor);
    result += class_name;
    result += ".java";

    return result;
@@ -320,10 +320,16 @@ write_message(stringstream& text, const DescriptorProto& message, const string&

/**
 * Write the contents of a file.
 *
 * If there are enums and generate_outer is false, invalid java code will be generated.
 */
static void
write_file(stringstream& text, const FileDescriptorProto& file_descriptor)
write_file(CodeGeneratorResponse* response, const FileDescriptorProto& file_descriptor,
        const string& filename, bool generate_outer,
        const vector<EnumDescriptorProto>& enums, const vector<DescriptorProto>& messages)
{
    stringstream text;

    string const package_name = make_java_package(file_descriptor);
    string const outer_class_name = make_outer_class_name(file_descriptor);

@@ -338,27 +344,92 @@ write_file(stringstream& text, const FileDescriptorProto& file_descriptor)
    }

    // This bit of policy is android api rules specific: Raw proto classes
    // must never be in the API, but they should all be available for testing.
    // must never be in the API
    text << "/** @hide */" << endl;
    text << "@android.annotation.TestApi" << 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; i<N; i++) {
        write_enum(text, enums[i], indented);
    }

    N = messages.size();
    for (size_t i=0; i<N; i++) {
        write_message(text, messages[i], indented);
    }

    if (generate_outer) {
        text << "}" << endl;
    }

    CodeGeneratorResponse::File* file_response = response->add_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<EnumDescriptorProto> enums;
        int N = file_descriptor.enum_type_size();
        for (int i=0; i<N; i++) {
            enums.push_back(file_descriptor.enum_type(i));
        }

        vector<DescriptorProto> 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<N; i++) {
        vector<EnumDescriptorProto> enums;

        vector<DescriptorProto> 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;
    const string indented = indent_more("");

    vector<EnumDescriptorProto> enums;
    N = file_descriptor.enum_type_size();
    for (int i=0; i<N; i++) {
        write_enum(text, file_descriptor.enum_type(i), indented);
        enums.push_back(file_descriptor.enum_type(i));
    }

    vector<DescriptorProto> messages;
    N = file_descriptor.message_type_size();
    for (int i=0; i<N; i++) {
        write_message(text, file_descriptor.message_type(i), indented);
        messages.push_back(file_descriptor.message_type(i));
    }

    text << "}" << endl;
    write_file(response, file_descriptor,
            make_file_name(file_descriptor, make_outer_class_name(file_descriptor)),
            true, enums, messages);
}

/**
@@ -383,16 +454,11 @@ main(int argc, char const*const* argv)
    for (int i=0; i<N; i++) {
        const FileDescriptorProto& file_descriptor = request.proto_file(i);
        if (should_generate_for_file(request, file_descriptor.name())) {
            // Generate the text
            stringstream text;
            write_file(text, file_descriptor);

            // Put the text in the response
            CodeGeneratorResponse::File* file_response = response.add_file();
            file_response->set_name(make_file_name(file_descriptor));
            file_response->set_content(text.str());

            cerr << "writing file: " << file_response->name() << endl;
            if (file_descriptor.options().java_multiple_files()) {
                write_multiple_files(&response, file_descriptor);
            } else {
                write_single_file(&response, file_descriptor);
            }
        }
    }