JAVA序列化--Protobuf

发布时间:2023年12月20日

目录

前言

一、Protobuf入门

Protobuf的优势

Protobuf 编译器

安装使用Protobuf

编写.proto文件?

编译proto文件

总结


前言

Protocol Buffers(简称protobuf)是一种轻便高效的数据序列化格式,可用于结构化数据的序列化和反序列化。它由Google开发并开源,用于解决数据交换和存储的通用问题。protobuf和json、xml如出一辙,之所以要学习protobuf是因为这种序列化方式得到的数据体积是很小的,这就导致了它在网络传输性能以及存储方面的优势;因为体积小,让protobuf序列化的数据传输性能高,磁盘占用率也低;所以protobuf序列化的场景一般出现在高性能业务中,基本上是通过tcp+protobuf的通信方式取代http+json。


一、Protobuf入门

Protobuf是Protocol Buffers的简称,它是Google公司开发的?种数据描述语?,并于2008年对外开 源。Protobuf刚开源时的定位类似于XML、JSON等数据描述语?,通过附带?具?成代码并实现将结 构化数据序列化的功能。但是我们更关注的是Protobuf作为接?规范的描述语?,可以作为设计安全的 跨语?PRC接?的基础?具。

Protobuf的优势

  • 编解码效率
  • 高压缩比
  • 多语言支持

Protobuf 编译器

Protobuf的编译器叫做:protoc(protobuf compiler)

我们在写protobuf之前需要先安装protoc这个工具,这个工具的作用就是将我们编写的.proto文件转换成目标语言对应的代码;比如我们现在是java语言,那么我们就要使用protoc这个工具把我们编写的.proto文件转成我们需要的java类文件;同样的,如果需要转成go语言、python语言同样也需要借助protoc这个编译工具;

java栗子:

./protoc --java_out=./  DmpDataProto.proto 

安装使用Protobuf

我们可以点击protoc工具下载链接,目前的最新版本是v25.1

找到protoc工具,根据自己的操作系统选择合适的安装包,我的操作系统是macos,那么我就选择protoc-25.0-rc-1-osx-x86_64.zip。解压文件后,进入bin目录查看protoc的版本。

yangyanpingdeMacBook-Air:bin yangyanping$ ./protoc --version
libprotoc 25.0-rc1

编写.proto文件?

我们先来编写一个User.proto文件作为示例,我们的目标语言是java

syntax = "proto3";

// 是否生成多个java文件
option java_multiple_files = false;
// 指定生成的java文件的包名
option java_package = "com.yyp.test.proto";
// 指定生成java文件类名
option java_outer_classname = "UserProto";
// 是否生成equals和hash方法
option java_generate_equals_and_hash = true;
// 生成的java文件编码格式
option java_string_check_utf8 = true;
// protocol buffer编译器是否应该 基于服务定义 产生 抽象服务代码
option java_generic_services = false;

message User {
  int32 id = 1;
  string name = 2;
  string email = 3;

  enum PhoneType{
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber{
    PhoneType type = 1;
    string number = 2;
  }

  repeated PhoneNumber phoneNumber = 4;
}

来简单解释一下User.proto的内容,该文件将会生成一个UserProto.java文件,不过在该UserProto.java文件中才会生成User内部类等;

在message User中,有4个属性,分别是id、name、email、phoneNumber,每个属性都需要一个唯一编号,范围从【1~536,870,911】,但是【19000~19999】号段是保留编号,保留编号是不能被使用的;

每个属性都有一个对应的数据类型,int32对应java中的int类型,string对应的String类型;repeated修饰符代表该数据类型可以重复,repeated PhoneNumber phoneNumber翻译成java语言如下:

List<PhoneNumber> phoneNumber;

编译proto文件

我们接下来需要通过protoc工具来编译.proto文件,把.proto文件编译成我们需要的目标语言对应的文件,当前pwd位置处于User.proto文件同一层:

/Users/yangyanping/Downloads/protoc-25.0-rc-1-osx-x86_64/bin
./protoc --java_out=./  User.proto 

上面的命令中,--java_out表示生成的结果放在哪个文件夹中,.代表当前文件夹;?

生产的java 类UserProto 源代码

// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: User.proto

// Protobuf Java Version: 3.25.0-rc1
package com.yyp.test.proto;

public final class UserProto {
  private UserProto() {}
  public static void registerAllExtensions(
      com.google.protobuf.ExtensionRegistryLite registry) {
  }

  public static void registerAllExtensions(
      com.google.protobuf.ExtensionRegistry registry) {
    registerAllExtensions(
        (com.google.protobuf.ExtensionRegistryLite) registry);
  }
  public interface UserOrBuilder extends
      // @@protoc_insertion_point(interface_extends:User)
      com.google.protobuf.MessageOrBuilder {

    /**
     * <code>int32 id = 1;</code>
     * @return The id.
     */
    int getId();

    /**
     * <code>string name = 2;</code>
     * @return The name.
     */
    java.lang.String getName();
    /**
     * <code>string name = 2;</code>
     * @return The bytes for name.
     */
    com.google.protobuf.ByteString
        getNameBytes();

    /**
     * <code>string email = 3;</code>
     * @return The email.
     */
    java.lang.String getEmail();
    /**
     * <code>string email = 3;</code>
     * @return The bytes for email.
     */
    com.google.protobuf.ByteString
        getEmailBytes();

    /**
     * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
     */
    java.util.List<com.yyp.test.proto.UserProto.User.PhoneNumber> 
        getPhoneNumberList();
    /**
     * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
     */
    com.yyp.test.proto.UserProto.User.PhoneNumber getPhoneNumber(int index);
    /**
     * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
     */
    int getPhoneNumberCount();
    /**
     * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
     */
    java.util.List<? extends com.yyp.test.proto.UserProto.User.PhoneNumberOrBuilder> 
        getPhoneNumberOrBuilderList();
    /**
     * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
     */
    com.yyp.test.proto.UserProto.User.PhoneNumberOrBuilder getPhoneNumberOrBuilder(
        int index);
  }
  /**
   * Protobuf type {@code User}
   */
  public static final class User extends
      com.google.protobuf.GeneratedMessageV3 implements
      // @@protoc_insertion_point(message_implements:User)
      UserOrBuilder {
  private static final long serialVersionUID = 0L;
    // Use User.newBuilder() to construct.
    private User(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
      super(builder);
    }
    private User() {
      name_ = "";
      email_ = "";
      phoneNumber_ = java.util.Collections.emptyList();
    }

    @java.lang.Override
    @SuppressWarnings({"unused"})
    protected java.lang.Object newInstance(
        UnusedPrivateParameter unused) {
      return new User();
    }

    public static final com.google.protobuf.Descriptors.Descriptor
        getDescriptor() {
      return com.yyp.test.proto.UserProto.internal_static_User_descriptor;
    }

    @java.lang.Override
    protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
        internalGetFieldAccessorTable() {
      return com.yyp.test.proto.UserProto.internal_static_User_fieldAccessorTable
          .ensureFieldAccessorsInitialized(
              com.yyp.test.proto.UserProto.User.class, com.yyp.test.proto.UserProto.User.Builder.class);
    }

    /**
     * Protobuf enum {@code User.PhoneType}
     */
    public enum PhoneType
        implements com.google.protobuf.ProtocolMessageEnum {
      /**
       * <code>MOBILE = 0;</code>
       */
      MOBILE(0),
      /**
       * <code>HOME = 1;</code>
       */
      HOME(1),
      /**
       * <code>WORK = 2;</code>
       */
      WORK(2),
      UNRECOGNIZED(-1),
      ;

      /**
       * <code>MOBILE = 0;</code>
       */
      public static final int MOBILE_VALUE = 0;
      /**
       * <code>HOME = 1;</code>
       */
      public static final int HOME_VALUE = 1;
      /**
       * <code>WORK = 2;</code>
       */
      public static final int WORK_VALUE = 2;


      public final int getNumber() {
        if (this == UNRECOGNIZED) {
          throw new java.lang.IllegalArgumentException(
              "Can't get the number of an unknown enum value.");
        }
        return value;
      }

      /**
       * @param value The numeric wire value of the corresponding enum entry.
       * @return The enum associated with the given numeric wire value.
       * @deprecated Use {@link #forNumber(int)} instead.
       */
      @java.lang.Deprecated
      public static PhoneType valueOf(int value) {
        return forNumber(value);
      }

      /**
       * @param value The numeric wire value of the corresponding enum entry.
       * @return The enum associated with the given numeric wire value.
       */
      public static PhoneType forNumber(int value) {
        switch (value) {
          case 0: return MOBILE;
          case 1: return HOME;
          case 2: return WORK;
          default: return null;
        }
      }

      public static com.google.protobuf.Internal.EnumLiteMap<PhoneType>
          internalGetValueMap() {
        return internalValueMap;
      }
      private static final com.google.protobuf.Internal.EnumLiteMap<
          PhoneType> internalValueMap =
            new com.google.protobuf.Internal.EnumLiteMap<PhoneType>() {
              public PhoneType findValueByNumber(int number) {
                return PhoneType.forNumber(number);
              }
            };

      public final com.google.protobuf.Descriptors.EnumValueDescriptor
          getValueDescriptor() {
        if (this == UNRECOGNIZED) {
          throw new java.lang.IllegalStateException(
              "Can't get the descriptor of an unrecognized enum value.");
        }
        return getDescriptor().getValues().get(ordinal());
      }
      public final com.google.protobuf.Descriptors.EnumDescriptor
          getDescriptorForType() {
        return getDescriptor();
      }
      public static final com.google.protobuf.Descriptors.EnumDescriptor
          getDescriptor() {
        return com.yyp.test.proto.UserProto.User.getDescriptor().getEnumTypes().get(0);
      }

      private static final PhoneType[] VALUES = values();

      public static PhoneType valueOf(
          com.google.protobuf.Descriptors.EnumValueDescriptor desc) {
        if (desc.getType() != getDescriptor()) {
          throw new java.lang.IllegalArgumentException(
            "EnumValueDescriptor is not for this type.");
        }
        if (desc.getIndex() == -1) {
          return UNRECOGNIZED;
        }
        return VALUES[desc.getIndex()];
      }

      private final int value;

      private PhoneType(int value) {
        this.value = value;
      }

      // @@protoc_insertion_point(enum_scope:User.PhoneType)
    }

    public interface PhoneNumberOrBuilder extends
        // @@protoc_insertion_point(interface_extends:User.PhoneNumber)
        com.google.protobuf.MessageOrBuilder {

      /**
       * <code>.User.PhoneType type = 1;</code>
       * @return The enum numeric value on the wire for type.
       */
      int getTypeValue();
      /**
       * <code>.User.PhoneType type = 1;</code>
       * @return The type.
       */
      com.yyp.test.proto.UserProto.User.PhoneType getType();

      /**
       * <code>string number = 2;</code>
       * @return The number.
       */
      java.lang.String getNumber();
      /**
       * <code>string number = 2;</code>
       * @return The bytes for number.
       */
      com.google.protobuf.ByteString
          getNumberBytes();
    }
    /**
     * Protobuf type {@code User.PhoneNumber}
     */
    public static final class PhoneNumber extends
        com.google.protobuf.GeneratedMessageV3 implements
        // @@protoc_insertion_point(message_implements:User.PhoneNumber)
        PhoneNumberOrBuilder {
    private static final long serialVersionUID = 0L;
      // Use PhoneNumber.newBuilder() to construct.
      private PhoneNumber(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
        super(builder);
      }
      private PhoneNumber() {
        type_ = 0;
        number_ = "";
      }

      @java.lang.Override
      @SuppressWarnings({"unused"})
      protected java.lang.Object newInstance(
          UnusedPrivateParameter unused) {
        return new PhoneNumber();
      }

      public static final com.google.protobuf.Descriptors.Descriptor
          getDescriptor() {
        return com.yyp.test.proto.UserProto.internal_static_User_PhoneNumber_descriptor;
      }

      @java.lang.Override
      protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
          internalGetFieldAccessorTable() {
        return com.yyp.test.proto.UserProto.internal_static_User_PhoneNumber_fieldAccessorTable
            .ensureFieldAccessorsInitialized(
                com.yyp.test.proto.UserProto.User.PhoneNumber.class, com.yyp.test.proto.UserProto.User.PhoneNumber.Builder.class);
      }

      public static final int TYPE_FIELD_NUMBER = 1;
      private int type_ = 0;
      /**
       * <code>.User.PhoneType type = 1;</code>
       * @return The enum numeric value on the wire for type.
       */
      @java.lang.Override public int getTypeValue() {
        return type_;
      }
      /**
       * <code>.User.PhoneType type = 1;</code>
       * @return The type.
       */
      @java.lang.Override public com.yyp.test.proto.UserProto.User.PhoneType getType() {
        com.yyp.test.proto.UserProto.User.PhoneType result = com.yyp.test.proto.UserProto.User.PhoneType.forNumber(type_);
        return result == null ? com.yyp.test.proto.UserProto.User.PhoneType.UNRECOGNIZED : result;
      }

      public static final int NUMBER_FIELD_NUMBER = 2;
      @SuppressWarnings("serial")
      private volatile java.lang.Object number_ = "";
      /**
       * <code>string number = 2;</code>
       * @return The number.
       */
      @java.lang.Override
      public java.lang.String getNumber() {
        java.lang.Object ref = number_;
        if (ref instanceof java.lang.String) {
          return (java.lang.String) ref;
        } else {
          com.google.protobuf.ByteString bs = 
              (com.google.protobuf.ByteString) ref;
          java.lang.String s = bs.toStringUtf8();
          number_ = s;
          return s;
        }
      }
      /**
       * <code>string number = 2;</code>
       * @return The bytes for number.
       */
      @java.lang.Override
      public com.google.protobuf.ByteString
          getNumberBytes() {
        java.lang.Object ref = number_;
        if (ref instanceof java.lang.String) {
          com.google.protobuf.ByteString b = 
              com.google.protobuf.ByteString.copyFromUtf8(
                  (java.lang.String) ref);
          number_ = b;
          return b;
        } else {
          return (com.google.protobuf.ByteString) ref;
        }
      }

      private byte memoizedIsInitialized = -1;
      @java.lang.Override
      public final boolean isInitialized() {
        byte isInitialized = memoizedIsInitialized;
        if (isInitialized == 1) return true;
        if (isInitialized == 0) return false;

        memoizedIsInitialized = 1;
        return true;
      }

      @java.lang.Override
      public void writeTo(com.google.protobuf.CodedOutputStream output)
                          throws java.io.IOException {
        if (type_ != com.yyp.test.proto.UserProto.User.PhoneType.MOBILE.getNumber()) {
          output.writeEnum(1, type_);
        }
        if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(number_)) {
          com.google.protobuf.GeneratedMessageV3.writeString(output, 2, number_);
        }
        getUnknownFields().writeTo(output);
      }

      @java.lang.Override
      public int getSerializedSize() {
        int size = memoizedSize;
        if (size != -1) return size;

        size = 0;
        if (type_ != com.yyp.test.proto.UserProto.User.PhoneType.MOBILE.getNumber()) {
          size += com.google.protobuf.CodedOutputStream
            .computeEnumSize(1, type_);
        }
        if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(number_)) {
          size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, number_);
        }
        size += getUnknownFields().getSerializedSize();
        memoizedSize = size;
        return size;
      }

      @java.lang.Override
      public boolean equals(final java.lang.Object obj) {
        if (obj == this) {
         return true;
        }
        if (!(obj instanceof com.yyp.test.proto.UserProto.User.PhoneNumber)) {
          return super.equals(obj);
        }
        com.yyp.test.proto.UserProto.User.PhoneNumber other = (com.yyp.test.proto.UserProto.User.PhoneNumber) obj;

        if (type_ != other.type_) return false;
        if (!getNumber()
            .equals(other.getNumber())) return false;
        if (!getUnknownFields().equals(other.getUnknownFields())) return false;
        return true;
      }

      @java.lang.Override
      public int hashCode() {
        if (memoizedHashCode != 0) {
          return memoizedHashCode;
        }
        int hash = 41;
        hash = (19 * hash) + getDescriptor().hashCode();
        hash = (37 * hash) + TYPE_FIELD_NUMBER;
        hash = (53 * hash) + type_;
        hash = (37 * hash) + NUMBER_FIELD_NUMBER;
        hash = (53 * hash) + getNumber().hashCode();
        hash = (29 * hash) + getUnknownFields().hashCode();
        memoizedHashCode = hash;
        return hash;
      }

      public static com.yyp.test.proto.UserProto.User.PhoneNumber parseFrom(
          java.nio.ByteBuffer data)
          throws com.google.protobuf.InvalidProtocolBufferException {
        return PARSER.parseFrom(data);
      }
      public static com.yyp.test.proto.UserProto.User.PhoneNumber parseFrom(
          java.nio.ByteBuffer data,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws com.google.protobuf.InvalidProtocolBufferException {
        return PARSER.parseFrom(data, extensionRegistry);
      }
      public static com.yyp.test.proto.UserProto.User.PhoneNumber parseFrom(
          com.google.protobuf.ByteString data)
          throws com.google.protobuf.InvalidProtocolBufferException {
        return PARSER.parseFrom(data);
      }
      public static com.yyp.test.proto.UserProto.User.PhoneNumber parseFrom(
          com.google.protobuf.ByteString data,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws com.google.protobuf.InvalidProtocolBufferException {
        return PARSER.parseFrom(data, extensionRegistry);
      }
      public static com.yyp.test.proto.UserProto.User.PhoneNumber parseFrom(byte[] data)
          throws com.google.protobuf.InvalidProtocolBufferException {
        return PARSER.parseFrom(data);
      }
      public static com.yyp.test.proto.UserProto.User.PhoneNumber parseFrom(
          byte[] data,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws com.google.protobuf.InvalidProtocolBufferException {
        return PARSER.parseFrom(data, extensionRegistry);
      }
      public static com.yyp.test.proto.UserProto.User.PhoneNumber parseFrom(java.io.InputStream input)
          throws java.io.IOException {
        return com.google.protobuf.GeneratedMessageV3
            .parseWithIOException(PARSER, input);
      }
      public static com.yyp.test.proto.UserProto.User.PhoneNumber parseFrom(
          java.io.InputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws java.io.IOException {
        return com.google.protobuf.GeneratedMessageV3
            .parseWithIOException(PARSER, input, extensionRegistry);
      }

      public static com.yyp.test.proto.UserProto.User.PhoneNumber parseDelimitedFrom(java.io.InputStream input)
          throws java.io.IOException {
        return com.google.protobuf.GeneratedMessageV3
            .parseDelimitedWithIOException(PARSER, input);
      }

      public static com.yyp.test.proto.UserProto.User.PhoneNumber parseDelimitedFrom(
          java.io.InputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws java.io.IOException {
        return com.google.protobuf.GeneratedMessageV3
            .parseDelimitedWithIOException(PARSER, input, extensionRegistry);
      }
      public static com.yyp.test.proto.UserProto.User.PhoneNumber parseFrom(
          com.google.protobuf.CodedInputStream input)
          throws java.io.IOException {
        return com.google.protobuf.GeneratedMessageV3
            .parseWithIOException(PARSER, input);
      }
      public static com.yyp.test.proto.UserProto.User.PhoneNumber parseFrom(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws java.io.IOException {
        return com.google.protobuf.GeneratedMessageV3
            .parseWithIOException(PARSER, input, extensionRegistry);
      }

      @java.lang.Override
      public Builder newBuilderForType() { return newBuilder(); }
      public static Builder newBuilder() {
        return DEFAULT_INSTANCE.toBuilder();
      }
      public static Builder newBuilder(com.yyp.test.proto.UserProto.User.PhoneNumber prototype) {
        return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
      }
      @java.lang.Override
      public Builder toBuilder() {
        return this == DEFAULT_INSTANCE
            ? new Builder() : new Builder().mergeFrom(this);
      }

      @java.lang.Override
      protected Builder newBuilderForType(
          com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
        Builder builder = new Builder(parent);
        return builder;
      }
      /**
       * Protobuf type {@code User.PhoneNumber}
       */
      public static final class Builder extends
          com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
          // @@protoc_insertion_point(builder_implements:User.PhoneNumber)
          com.yyp.test.proto.UserProto.User.PhoneNumberOrBuilder {
        public static final com.google.protobuf.Descriptors.Descriptor
            getDescriptor() {
          return com.yyp.test.proto.UserProto.internal_static_User_PhoneNumber_descriptor;
        }

        @java.lang.Override
        protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
            internalGetFieldAccessorTable() {
          return com.yyp.test.proto.UserProto.internal_static_User_PhoneNumber_fieldAccessorTable
              .ensureFieldAccessorsInitialized(
                  com.yyp.test.proto.UserProto.User.PhoneNumber.class, com.yyp.test.proto.UserProto.User.PhoneNumber.Builder.class);
        }

        // Construct using com.yyp.test.proto.UserProto.User.PhoneNumber.newBuilder()
        private Builder() {

        }

        private Builder(
            com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
          super(parent);

        }
        @java.lang.Override
        public Builder clear() {
          super.clear();
          bitField0_ = 0;
          type_ = 0;
          number_ = "";
          return this;
        }

        @java.lang.Override
        public com.google.protobuf.Descriptors.Descriptor
            getDescriptorForType() {
          return com.yyp.test.proto.UserProto.internal_static_User_PhoneNumber_descriptor;
        }

        @java.lang.Override
        public com.yyp.test.proto.UserProto.User.PhoneNumber getDefaultInstanceForType() {
          return com.yyp.test.proto.UserProto.User.PhoneNumber.getDefaultInstance();
        }

        @java.lang.Override
        public com.yyp.test.proto.UserProto.User.PhoneNumber build() {
          com.yyp.test.proto.UserProto.User.PhoneNumber result = buildPartial();
          if (!result.isInitialized()) {
            throw newUninitializedMessageException(result);
          }
          return result;
        }

        @java.lang.Override
        public com.yyp.test.proto.UserProto.User.PhoneNumber buildPartial() {
          com.yyp.test.proto.UserProto.User.PhoneNumber result = new com.yyp.test.proto.UserProto.User.PhoneNumber(this);
          if (bitField0_ != 0) { buildPartial0(result); }
          onBuilt();
          return result;
        }

        private void buildPartial0(com.yyp.test.proto.UserProto.User.PhoneNumber result) {
          int from_bitField0_ = bitField0_;
          if (((from_bitField0_ & 0x00000001) != 0)) {
            result.type_ = type_;
          }
          if (((from_bitField0_ & 0x00000002) != 0)) {
            result.number_ = number_;
          }
        }

        @java.lang.Override
        public Builder clone() {
          return super.clone();
        }
        @java.lang.Override
        public Builder setField(
            com.google.protobuf.Descriptors.FieldDescriptor field,
            java.lang.Object value) {
          return super.setField(field, value);
        }
        @java.lang.Override
        public Builder clearField(
            com.google.protobuf.Descriptors.FieldDescriptor field) {
          return super.clearField(field);
        }
        @java.lang.Override
        public Builder clearOneof(
            com.google.protobuf.Descriptors.OneofDescriptor oneof) {
          return super.clearOneof(oneof);
        }
        @java.lang.Override
        public Builder setRepeatedField(
            com.google.protobuf.Descriptors.FieldDescriptor field,
            int index, java.lang.Object value) {
          return super.setRepeatedField(field, index, value);
        }
        @java.lang.Override
        public Builder addRepeatedField(
            com.google.protobuf.Descriptors.FieldDescriptor field,
            java.lang.Object value) {
          return super.addRepeatedField(field, value);
        }
        @java.lang.Override
        public Builder mergeFrom(com.google.protobuf.Message other) {
          if (other instanceof com.yyp.test.proto.UserProto.User.PhoneNumber) {
            return mergeFrom((com.yyp.test.proto.UserProto.User.PhoneNumber)other);
          } else {
            super.mergeFrom(other);
            return this;
          }
        }

        public Builder mergeFrom(com.yyp.test.proto.UserProto.User.PhoneNumber other) {
          if (other == com.yyp.test.proto.UserProto.User.PhoneNumber.getDefaultInstance()) return this;
          if (other.type_ != 0) {
            setTypeValue(other.getTypeValue());
          }
          if (!other.getNumber().isEmpty()) {
            number_ = other.number_;
            bitField0_ |= 0x00000002;
            onChanged();
          }
          this.mergeUnknownFields(other.getUnknownFields());
          onChanged();
          return this;
        }

        @java.lang.Override
        public final boolean isInitialized() {
          return true;
        }

        @java.lang.Override
        public Builder mergeFrom(
            com.google.protobuf.CodedInputStream input,
            com.google.protobuf.ExtensionRegistryLite extensionRegistry)
            throws java.io.IOException {
          if (extensionRegistry == null) {
            throw new java.lang.NullPointerException();
          }
          try {
            boolean done = false;
            while (!done) {
              int tag = input.readTag();
              switch (tag) {
                case 0:
                  done = true;
                  break;
                case 8: {
                  type_ = input.readEnum();
                  bitField0_ |= 0x00000001;
                  break;
                } // case 8
                case 18: {
                  number_ = input.readStringRequireUtf8();
                  bitField0_ |= 0x00000002;
                  break;
                } // case 18
                default: {
                  if (!super.parseUnknownField(input, extensionRegistry, tag)) {
                    done = true; // was an endgroup tag
                  }
                  break;
                } // default:
              } // switch (tag)
            } // while (!done)
          } catch (com.google.protobuf.InvalidProtocolBufferException e) {
            throw e.unwrapIOException();
          } finally {
            onChanged();
          } // finally
          return this;
        }
        private int bitField0_;

        private int type_ = 0;
        /**
         * <code>.User.PhoneType type = 1;</code>
         * @return The enum numeric value on the wire for type.
         */
        @java.lang.Override public int getTypeValue() {
          return type_;
        }
        /**
         * <code>.User.PhoneType type = 1;</code>
         * @param value The enum numeric value on the wire for type to set.
         * @return This builder for chaining.
         */
        public Builder setTypeValue(int value) {
          type_ = value;
          bitField0_ |= 0x00000001;
          onChanged();
          return this;
        }
        /**
         * <code>.User.PhoneType type = 1;</code>
         * @return The type.
         */
        @java.lang.Override
        public com.yyp.test.proto.UserProto.User.PhoneType getType() {
          com.yyp.test.proto.UserProto.User.PhoneType result = com.yyp.test.proto.UserProto.User.PhoneType.forNumber(type_);
          return result == null ? com.yyp.test.proto.UserProto.User.PhoneType.UNRECOGNIZED : result;
        }
        /**
         * <code>.User.PhoneType type = 1;</code>
         * @param value The type to set.
         * @return This builder for chaining.
         */
        public Builder setType(com.yyp.test.proto.UserProto.User.PhoneType value) {
          if (value == null) {
            throw new NullPointerException();
          }
          bitField0_ |= 0x00000001;
          type_ = value.getNumber();
          onChanged();
          return this;
        }
        /**
         * <code>.User.PhoneType type = 1;</code>
         * @return This builder for chaining.
         */
        public Builder clearType() {
          bitField0_ = (bitField0_ & ~0x00000001);
          type_ = 0;
          onChanged();
          return this;
        }

        private java.lang.Object number_ = "";
        /**
         * <code>string number = 2;</code>
         * @return The number.
         */
        public java.lang.String getNumber() {
          java.lang.Object ref = number_;
          if (!(ref instanceof java.lang.String)) {
            com.google.protobuf.ByteString bs =
                (com.google.protobuf.ByteString) ref;
            java.lang.String s = bs.toStringUtf8();
            number_ = s;
            return s;
          } else {
            return (java.lang.String) ref;
          }
        }
        /**
         * <code>string number = 2;</code>
         * @return The bytes for number.
         */
        public com.google.protobuf.ByteString
            getNumberBytes() {
          java.lang.Object ref = number_;
          if (ref instanceof String) {
            com.google.protobuf.ByteString b = 
                com.google.protobuf.ByteString.copyFromUtf8(
                    (java.lang.String) ref);
            number_ = b;
            return b;
          } else {
            return (com.google.protobuf.ByteString) ref;
          }
        }
        /**
         * <code>string number = 2;</code>
         * @param value The number to set.
         * @return This builder for chaining.
         */
        public Builder setNumber(
            java.lang.String value) {
          if (value == null) { throw new NullPointerException(); }
          number_ = value;
          bitField0_ |= 0x00000002;
          onChanged();
          return this;
        }
        /**
         * <code>string number = 2;</code>
         * @return This builder for chaining.
         */
        public Builder clearNumber() {
          number_ = getDefaultInstance().getNumber();
          bitField0_ = (bitField0_ & ~0x00000002);
          onChanged();
          return this;
        }
        /**
         * <code>string number = 2;</code>
         * @param value The bytes for number to set.
         * @return This builder for chaining.
         */
        public Builder setNumberBytes(
            com.google.protobuf.ByteString value) {
          if (value == null) { throw new NullPointerException(); }
          checkByteStringIsUtf8(value);
          number_ = value;
          bitField0_ |= 0x00000002;
          onChanged();
          return this;
        }
        @java.lang.Override
        public final Builder setUnknownFields(
            final com.google.protobuf.UnknownFieldSet unknownFields) {
          return super.setUnknownFields(unknownFields);
        }

        @java.lang.Override
        public final Builder mergeUnknownFields(
            final com.google.protobuf.UnknownFieldSet unknownFields) {
          return super.mergeUnknownFields(unknownFields);
        }


        // @@protoc_insertion_point(builder_scope:User.PhoneNumber)
      }

      // @@protoc_insertion_point(class_scope:User.PhoneNumber)
      private static final com.yyp.test.proto.UserProto.User.PhoneNumber DEFAULT_INSTANCE;
      static {
        DEFAULT_INSTANCE = new com.yyp.test.proto.UserProto.User.PhoneNumber();
      }

      public static com.yyp.test.proto.UserProto.User.PhoneNumber getDefaultInstance() {
        return DEFAULT_INSTANCE;
      }

      private static final com.google.protobuf.Parser<PhoneNumber>
          PARSER = new com.google.protobuf.AbstractParser<PhoneNumber>() {
        @java.lang.Override
        public PhoneNumber parsePartialFrom(
            com.google.protobuf.CodedInputStream input,
            com.google.protobuf.ExtensionRegistryLite extensionRegistry)
            throws com.google.protobuf.InvalidProtocolBufferException {
          Builder builder = newBuilder();
          try {
            builder.mergeFrom(input, extensionRegistry);
          } catch (com.google.protobuf.InvalidProtocolBufferException e) {
            throw e.setUnfinishedMessage(builder.buildPartial());
          } catch (com.google.protobuf.UninitializedMessageException e) {
            throw e.asInvalidProtocolBufferException().setUnfinishedMessage(builder.buildPartial());
          } catch (java.io.IOException e) {
            throw new com.google.protobuf.InvalidProtocolBufferException(e)
                .setUnfinishedMessage(builder.buildPartial());
          }
          return builder.buildPartial();
        }
      };

      public static com.google.protobuf.Parser<PhoneNumber> parser() {
        return PARSER;
      }

      @java.lang.Override
      public com.google.protobuf.Parser<PhoneNumber> getParserForType() {
        return PARSER;
      }

      @java.lang.Override
      public com.yyp.test.proto.UserProto.User.PhoneNumber getDefaultInstanceForType() {
        return DEFAULT_INSTANCE;
      }

    }

    public static final int ID_FIELD_NUMBER = 1;
    private int id_ = 0;
    /**
     * <code>int32 id = 1;</code>
     * @return The id.
     */
    @java.lang.Override
    public int getId() {
      return id_;
    }

    public static final int NAME_FIELD_NUMBER = 2;
    @SuppressWarnings("serial")
    private volatile java.lang.Object name_ = "";
    /**
     * <code>string name = 2;</code>
     * @return The name.
     */
    @java.lang.Override
    public java.lang.String getName() {
      java.lang.Object ref = name_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        name_ = s;
        return s;
      }
    }
    /**
     * <code>string name = 2;</code>
     * @return The bytes for name.
     */
    @java.lang.Override
    public com.google.protobuf.ByteString
        getNameBytes() {
      java.lang.Object ref = name_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        name_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    public static final int EMAIL_FIELD_NUMBER = 3;
    @SuppressWarnings("serial")
    private volatile java.lang.Object email_ = "";
    /**
     * <code>string email = 3;</code>
     * @return The email.
     */
    @java.lang.Override
    public java.lang.String getEmail() {
      java.lang.Object ref = email_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        email_ = s;
        return s;
      }
    }
    /**
     * <code>string email = 3;</code>
     * @return The bytes for email.
     */
    @java.lang.Override
    public com.google.protobuf.ByteString
        getEmailBytes() {
      java.lang.Object ref = email_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        email_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    public static final int PHONENUMBER_FIELD_NUMBER = 4;
    @SuppressWarnings("serial")
    private java.util.List<com.yyp.test.proto.UserProto.User.PhoneNumber> phoneNumber_;
    /**
     * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
     */
    @java.lang.Override
    public java.util.List<com.yyp.test.proto.UserProto.User.PhoneNumber> getPhoneNumberList() {
      return phoneNumber_;
    }
    /**
     * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
     */
    @java.lang.Override
    public java.util.List<? extends com.yyp.test.proto.UserProto.User.PhoneNumberOrBuilder> 
        getPhoneNumberOrBuilderList() {
      return phoneNumber_;
    }
    /**
     * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
     */
    @java.lang.Override
    public int getPhoneNumberCount() {
      return phoneNumber_.size();
    }
    /**
     * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
     */
    @java.lang.Override
    public com.yyp.test.proto.UserProto.User.PhoneNumber getPhoneNumber(int index) {
      return phoneNumber_.get(index);
    }
    /**
     * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
     */
    @java.lang.Override
    public com.yyp.test.proto.UserProto.User.PhoneNumberOrBuilder getPhoneNumberOrBuilder(
        int index) {
      return phoneNumber_.get(index);
    }

    private byte memoizedIsInitialized = -1;
    @java.lang.Override
    public final boolean isInitialized() {
      byte isInitialized = memoizedIsInitialized;
      if (isInitialized == 1) return true;
      if (isInitialized == 0) return false;

      memoizedIsInitialized = 1;
      return true;
    }

    @java.lang.Override
    public void writeTo(com.google.protobuf.CodedOutputStream output)
                        throws java.io.IOException {
      if (id_ != 0) {
        output.writeInt32(1, id_);
      }
      if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(name_)) {
        com.google.protobuf.GeneratedMessageV3.writeString(output, 2, name_);
      }
      if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(email_)) {
        com.google.protobuf.GeneratedMessageV3.writeString(output, 3, email_);
      }
      for (int i = 0; i < phoneNumber_.size(); i++) {
        output.writeMessage(4, phoneNumber_.get(i));
      }
      getUnknownFields().writeTo(output);
    }

    @java.lang.Override
    public int getSerializedSize() {
      int size = memoizedSize;
      if (size != -1) return size;

      size = 0;
      if (id_ != 0) {
        size += com.google.protobuf.CodedOutputStream
          .computeInt32Size(1, id_);
      }
      if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(name_)) {
        size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, name_);
      }
      if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(email_)) {
        size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, email_);
      }
      for (int i = 0; i < phoneNumber_.size(); i++) {
        size += com.google.protobuf.CodedOutputStream
          .computeMessageSize(4, phoneNumber_.get(i));
      }
      size += getUnknownFields().getSerializedSize();
      memoizedSize = size;
      return size;
    }

    @java.lang.Override
    public boolean equals(final java.lang.Object obj) {
      if (obj == this) {
       return true;
      }
      if (!(obj instanceof com.yyp.test.proto.UserProto.User)) {
        return super.equals(obj);
      }
      com.yyp.test.proto.UserProto.User other = (com.yyp.test.proto.UserProto.User) obj;

      if (getId()
          != other.getId()) return false;
      if (!getName()
          .equals(other.getName())) return false;
      if (!getEmail()
          .equals(other.getEmail())) return false;
      if (!getPhoneNumberList()
          .equals(other.getPhoneNumberList())) return false;
      if (!getUnknownFields().equals(other.getUnknownFields())) return false;
      return true;
    }

    @java.lang.Override
    public int hashCode() {
      if (memoizedHashCode != 0) {
        return memoizedHashCode;
      }
      int hash = 41;
      hash = (19 * hash) + getDescriptor().hashCode();
      hash = (37 * hash) + ID_FIELD_NUMBER;
      hash = (53 * hash) + getId();
      hash = (37 * hash) + NAME_FIELD_NUMBER;
      hash = (53 * hash) + getName().hashCode();
      hash = (37 * hash) + EMAIL_FIELD_NUMBER;
      hash = (53 * hash) + getEmail().hashCode();
      if (getPhoneNumberCount() > 0) {
        hash = (37 * hash) + PHONENUMBER_FIELD_NUMBER;
        hash = (53 * hash) + getPhoneNumberList().hashCode();
      }
      hash = (29 * hash) + getUnknownFields().hashCode();
      memoizedHashCode = hash;
      return hash;
    }

    public static com.yyp.test.proto.UserProto.User parseFrom(
        java.nio.ByteBuffer data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static com.yyp.test.proto.UserProto.User parseFrom(
        java.nio.ByteBuffer data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static com.yyp.test.proto.UserProto.User parseFrom(
        com.google.protobuf.ByteString data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static com.yyp.test.proto.UserProto.User parseFrom(
        com.google.protobuf.ByteString data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static com.yyp.test.proto.UserProto.User parseFrom(byte[] data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static com.yyp.test.proto.UserProto.User parseFrom(
        byte[] data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static com.yyp.test.proto.UserProto.User parseFrom(java.io.InputStream input)
        throws java.io.IOException {
      return com.google.protobuf.GeneratedMessageV3
          .parseWithIOException(PARSER, input);
    }
    public static com.yyp.test.proto.UserProto.User parseFrom(
        java.io.InputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return com.google.protobuf.GeneratedMessageV3
          .parseWithIOException(PARSER, input, extensionRegistry);
    }

    public static com.yyp.test.proto.UserProto.User parseDelimitedFrom(java.io.InputStream input)
        throws java.io.IOException {
      return com.google.protobuf.GeneratedMessageV3
          .parseDelimitedWithIOException(PARSER, input);
    }

    public static com.yyp.test.proto.UserProto.User parseDelimitedFrom(
        java.io.InputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return com.google.protobuf.GeneratedMessageV3
          .parseDelimitedWithIOException(PARSER, input, extensionRegistry);
    }
    public static com.yyp.test.proto.UserProto.User parseFrom(
        com.google.protobuf.CodedInputStream input)
        throws java.io.IOException {
      return com.google.protobuf.GeneratedMessageV3
          .parseWithIOException(PARSER, input);
    }
    public static com.yyp.test.proto.UserProto.User parseFrom(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return com.google.protobuf.GeneratedMessageV3
          .parseWithIOException(PARSER, input, extensionRegistry);
    }

    @java.lang.Override
    public Builder newBuilderForType() { return newBuilder(); }
    public static Builder newBuilder() {
      return DEFAULT_INSTANCE.toBuilder();
    }
    public static Builder newBuilder(com.yyp.test.proto.UserProto.User prototype) {
      return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
    }
    @java.lang.Override
    public Builder toBuilder() {
      return this == DEFAULT_INSTANCE
          ? new Builder() : new Builder().mergeFrom(this);
    }

    @java.lang.Override
    protected Builder newBuilderForType(
        com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
      Builder builder = new Builder(parent);
      return builder;
    }
    /**
     * Protobuf type {@code User}
     */
    public static final class Builder extends
        com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
        // @@protoc_insertion_point(builder_implements:User)
        com.yyp.test.proto.UserProto.UserOrBuilder {
      public static final com.google.protobuf.Descriptors.Descriptor
          getDescriptor() {
        return com.yyp.test.proto.UserProto.internal_static_User_descriptor;
      }

      @java.lang.Override
      protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
          internalGetFieldAccessorTable() {
        return com.yyp.test.proto.UserProto.internal_static_User_fieldAccessorTable
            .ensureFieldAccessorsInitialized(
                com.yyp.test.proto.UserProto.User.class, com.yyp.test.proto.UserProto.User.Builder.class);
      }

      // Construct using com.yyp.test.proto.UserProto.User.newBuilder()
      private Builder() {

      }

      private Builder(
          com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
        super(parent);

      }
      @java.lang.Override
      public Builder clear() {
        super.clear();
        bitField0_ = 0;
        id_ = 0;
        name_ = "";
        email_ = "";
        if (phoneNumberBuilder_ == null) {
          phoneNumber_ = java.util.Collections.emptyList();
        } else {
          phoneNumber_ = null;
          phoneNumberBuilder_.clear();
        }
        bitField0_ = (bitField0_ & ~0x00000008);
        return this;
      }

      @java.lang.Override
      public com.google.protobuf.Descriptors.Descriptor
          getDescriptorForType() {
        return com.yyp.test.proto.UserProto.internal_static_User_descriptor;
      }

      @java.lang.Override
      public com.yyp.test.proto.UserProto.User getDefaultInstanceForType() {
        return com.yyp.test.proto.UserProto.User.getDefaultInstance();
      }

      @java.lang.Override
      public com.yyp.test.proto.UserProto.User build() {
        com.yyp.test.proto.UserProto.User result = buildPartial();
        if (!result.isInitialized()) {
          throw newUninitializedMessageException(result);
        }
        return result;
      }

      @java.lang.Override
      public com.yyp.test.proto.UserProto.User buildPartial() {
        com.yyp.test.proto.UserProto.User result = new com.yyp.test.proto.UserProto.User(this);
        buildPartialRepeatedFields(result);
        if (bitField0_ != 0) { buildPartial0(result); }
        onBuilt();
        return result;
      }

      private void buildPartialRepeatedFields(com.yyp.test.proto.UserProto.User result) {
        if (phoneNumberBuilder_ == null) {
          if (((bitField0_ & 0x00000008) != 0)) {
            phoneNumber_ = java.util.Collections.unmodifiableList(phoneNumber_);
            bitField0_ = (bitField0_ & ~0x00000008);
          }
          result.phoneNumber_ = phoneNumber_;
        } else {
          result.phoneNumber_ = phoneNumberBuilder_.build();
        }
      }

      private void buildPartial0(com.yyp.test.proto.UserProto.User result) {
        int from_bitField0_ = bitField0_;
        if (((from_bitField0_ & 0x00000001) != 0)) {
          result.id_ = id_;
        }
        if (((from_bitField0_ & 0x00000002) != 0)) {
          result.name_ = name_;
        }
        if (((from_bitField0_ & 0x00000004) != 0)) {
          result.email_ = email_;
        }
      }

      @java.lang.Override
      public Builder clone() {
        return super.clone();
      }
      @java.lang.Override
      public Builder setField(
          com.google.protobuf.Descriptors.FieldDescriptor field,
          java.lang.Object value) {
        return super.setField(field, value);
      }
      @java.lang.Override
      public Builder clearField(
          com.google.protobuf.Descriptors.FieldDescriptor field) {
        return super.clearField(field);
      }
      @java.lang.Override
      public Builder clearOneof(
          com.google.protobuf.Descriptors.OneofDescriptor oneof) {
        return super.clearOneof(oneof);
      }
      @java.lang.Override
      public Builder setRepeatedField(
          com.google.protobuf.Descriptors.FieldDescriptor field,
          int index, java.lang.Object value) {
        return super.setRepeatedField(field, index, value);
      }
      @java.lang.Override
      public Builder addRepeatedField(
          com.google.protobuf.Descriptors.FieldDescriptor field,
          java.lang.Object value) {
        return super.addRepeatedField(field, value);
      }
      @java.lang.Override
      public Builder mergeFrom(com.google.protobuf.Message other) {
        if (other instanceof com.yyp.test.proto.UserProto.User) {
          return mergeFrom((com.yyp.test.proto.UserProto.User)other);
        } else {
          super.mergeFrom(other);
          return this;
        }
      }

      public Builder mergeFrom(com.yyp.test.proto.UserProto.User other) {
        if (other == com.yyp.test.proto.UserProto.User.getDefaultInstance()) return this;
        if (other.getId() != 0) {
          setId(other.getId());
        }
        if (!other.getName().isEmpty()) {
          name_ = other.name_;
          bitField0_ |= 0x00000002;
          onChanged();
        }
        if (!other.getEmail().isEmpty()) {
          email_ = other.email_;
          bitField0_ |= 0x00000004;
          onChanged();
        }
        if (phoneNumberBuilder_ == null) {
          if (!other.phoneNumber_.isEmpty()) {
            if (phoneNumber_.isEmpty()) {
              phoneNumber_ = other.phoneNumber_;
              bitField0_ = (bitField0_ & ~0x00000008);
            } else {
              ensurePhoneNumberIsMutable();
              phoneNumber_.addAll(other.phoneNumber_);
            }
            onChanged();
          }
        } else {
          if (!other.phoneNumber_.isEmpty()) {
            if (phoneNumberBuilder_.isEmpty()) {
              phoneNumberBuilder_.dispose();
              phoneNumberBuilder_ = null;
              phoneNumber_ = other.phoneNumber_;
              bitField0_ = (bitField0_ & ~0x00000008);
              phoneNumberBuilder_ = 
                com.google.protobuf.GeneratedMessageV3.alwaysUseFieldBuilders ?
                   getPhoneNumberFieldBuilder() : null;
            } else {
              phoneNumberBuilder_.addAllMessages(other.phoneNumber_);
            }
          }
        }
        this.mergeUnknownFields(other.getUnknownFields());
        onChanged();
        return this;
      }

      @java.lang.Override
      public final boolean isInitialized() {
        return true;
      }

      @java.lang.Override
      public Builder mergeFrom(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws java.io.IOException {
        if (extensionRegistry == null) {
          throw new java.lang.NullPointerException();
        }
        try {
          boolean done = false;
          while (!done) {
            int tag = input.readTag();
            switch (tag) {
              case 0:
                done = true;
                break;
              case 8: {
                id_ = input.readInt32();
                bitField0_ |= 0x00000001;
                break;
              } // case 8
              case 18: {
                name_ = input.readStringRequireUtf8();
                bitField0_ |= 0x00000002;
                break;
              } // case 18
              case 26: {
                email_ = input.readStringRequireUtf8();
                bitField0_ |= 0x00000004;
                break;
              } // case 26
              case 34: {
                com.yyp.test.proto.UserProto.User.PhoneNumber m =
                    input.readMessage(
                        com.yyp.test.proto.UserProto.User.PhoneNumber.parser(),
                        extensionRegistry);
                if (phoneNumberBuilder_ == null) {
                  ensurePhoneNumberIsMutable();
                  phoneNumber_.add(m);
                } else {
                  phoneNumberBuilder_.addMessage(m);
                }
                break;
              } // case 34
              default: {
                if (!super.parseUnknownField(input, extensionRegistry, tag)) {
                  done = true; // was an endgroup tag
                }
                break;
              } // default:
            } // switch (tag)
          } // while (!done)
        } catch (com.google.protobuf.InvalidProtocolBufferException e) {
          throw e.unwrapIOException();
        } finally {
          onChanged();
        } // finally
        return this;
      }
      private int bitField0_;

      private int id_ ;
      /**
       * <code>int32 id = 1;</code>
       * @return The id.
       */
      @java.lang.Override
      public int getId() {
        return id_;
      }
      /**
       * <code>int32 id = 1;</code>
       * @param value The id to set.
       * @return This builder for chaining.
       */
      public Builder setId(int value) {

        id_ = value;
        bitField0_ |= 0x00000001;
        onChanged();
        return this;
      }
      /**
       * <code>int32 id = 1;</code>
       * @return This builder for chaining.
       */
      public Builder clearId() {
        bitField0_ = (bitField0_ & ~0x00000001);
        id_ = 0;
        onChanged();
        return this;
      }

      private java.lang.Object name_ = "";
      /**
       * <code>string name = 2;</code>
       * @return The name.
       */
      public java.lang.String getName() {
        java.lang.Object ref = name_;
        if (!(ref instanceof java.lang.String)) {
          com.google.protobuf.ByteString bs =
              (com.google.protobuf.ByteString) ref;
          java.lang.String s = bs.toStringUtf8();
          name_ = s;
          return s;
        } else {
          return (java.lang.String) ref;
        }
      }
      /**
       * <code>string name = 2;</code>
       * @return The bytes for name.
       */
      public com.google.protobuf.ByteString
          getNameBytes() {
        java.lang.Object ref = name_;
        if (ref instanceof String) {
          com.google.protobuf.ByteString b = 
              com.google.protobuf.ByteString.copyFromUtf8(
                  (java.lang.String) ref);
          name_ = b;
          return b;
        } else {
          return (com.google.protobuf.ByteString) ref;
        }
      }
      /**
       * <code>string name = 2;</code>
       * @param value The name to set.
       * @return This builder for chaining.
       */
      public Builder setName(
          java.lang.String value) {
        if (value == null) { throw new NullPointerException(); }
        name_ = value;
        bitField0_ |= 0x00000002;
        onChanged();
        return this;
      }
      /**
       * <code>string name = 2;</code>
       * @return This builder for chaining.
       */
      public Builder clearName() {
        name_ = getDefaultInstance().getName();
        bitField0_ = (bitField0_ & ~0x00000002);
        onChanged();
        return this;
      }
      /**
       * <code>string name = 2;</code>
       * @param value The bytes for name to set.
       * @return This builder for chaining.
       */
      public Builder setNameBytes(
          com.google.protobuf.ByteString value) {
        if (value == null) { throw new NullPointerException(); }
        checkByteStringIsUtf8(value);
        name_ = value;
        bitField0_ |= 0x00000002;
        onChanged();
        return this;
      }

      private java.lang.Object email_ = "";
      /**
       * <code>string email = 3;</code>
       * @return The email.
       */
      public java.lang.String getEmail() {
        java.lang.Object ref = email_;
        if (!(ref instanceof java.lang.String)) {
          com.google.protobuf.ByteString bs =
              (com.google.protobuf.ByteString) ref;
          java.lang.String s = bs.toStringUtf8();
          email_ = s;
          return s;
        } else {
          return (java.lang.String) ref;
        }
      }
      /**
       * <code>string email = 3;</code>
       * @return The bytes for email.
       */
      public com.google.protobuf.ByteString
          getEmailBytes() {
        java.lang.Object ref = email_;
        if (ref instanceof String) {
          com.google.protobuf.ByteString b = 
              com.google.protobuf.ByteString.copyFromUtf8(
                  (java.lang.String) ref);
          email_ = b;
          return b;
        } else {
          return (com.google.protobuf.ByteString) ref;
        }
      }
      /**
       * <code>string email = 3;</code>
       * @param value The email to set.
       * @return This builder for chaining.
       */
      public Builder setEmail(
          java.lang.String value) {
        if (value == null) { throw new NullPointerException(); }
        email_ = value;
        bitField0_ |= 0x00000004;
        onChanged();
        return this;
      }
      /**
       * <code>string email = 3;</code>
       * @return This builder for chaining.
       */
      public Builder clearEmail() {
        email_ = getDefaultInstance().getEmail();
        bitField0_ = (bitField0_ & ~0x00000004);
        onChanged();
        return this;
      }
      /**
       * <code>string email = 3;</code>
       * @param value The bytes for email to set.
       * @return This builder for chaining.
       */
      public Builder setEmailBytes(
          com.google.protobuf.ByteString value) {
        if (value == null) { throw new NullPointerException(); }
        checkByteStringIsUtf8(value);
        email_ = value;
        bitField0_ |= 0x00000004;
        onChanged();
        return this;
      }

      private java.util.List<com.yyp.test.proto.UserProto.User.PhoneNumber> phoneNumber_ =
        java.util.Collections.emptyList();
      private void ensurePhoneNumberIsMutable() {
        if (!((bitField0_ & 0x00000008) != 0)) {
          phoneNumber_ = new java.util.ArrayList<com.yyp.test.proto.UserProto.User.PhoneNumber>(phoneNumber_);
          bitField0_ |= 0x00000008;
         }
      }

      private com.google.protobuf.RepeatedFieldBuilderV3<
          com.yyp.test.proto.UserProto.User.PhoneNumber, com.yyp.test.proto.UserProto.User.PhoneNumber.Builder, com.yyp.test.proto.UserProto.User.PhoneNumberOrBuilder> phoneNumberBuilder_;

      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public java.util.List<com.yyp.test.proto.UserProto.User.PhoneNumber> getPhoneNumberList() {
        if (phoneNumberBuilder_ == null) {
          return java.util.Collections.unmodifiableList(phoneNumber_);
        } else {
          return phoneNumberBuilder_.getMessageList();
        }
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public int getPhoneNumberCount() {
        if (phoneNumberBuilder_ == null) {
          return phoneNumber_.size();
        } else {
          return phoneNumberBuilder_.getCount();
        }
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public com.yyp.test.proto.UserProto.User.PhoneNumber getPhoneNumber(int index) {
        if (phoneNumberBuilder_ == null) {
          return phoneNumber_.get(index);
        } else {
          return phoneNumberBuilder_.getMessage(index);
        }
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public Builder setPhoneNumber(
          int index, com.yyp.test.proto.UserProto.User.PhoneNumber value) {
        if (phoneNumberBuilder_ == null) {
          if (value == null) {
            throw new NullPointerException();
          }
          ensurePhoneNumberIsMutable();
          phoneNumber_.set(index, value);
          onChanged();
        } else {
          phoneNumberBuilder_.setMessage(index, value);
        }
        return this;
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public Builder setPhoneNumber(
          int index, com.yyp.test.proto.UserProto.User.PhoneNumber.Builder builderForValue) {
        if (phoneNumberBuilder_ == null) {
          ensurePhoneNumberIsMutable();
          phoneNumber_.set(index, builderForValue.build());
          onChanged();
        } else {
          phoneNumberBuilder_.setMessage(index, builderForValue.build());
        }
        return this;
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public Builder addPhoneNumber(com.yyp.test.proto.UserProto.User.PhoneNumber value) {
        if (phoneNumberBuilder_ == null) {
          if (value == null) {
            throw new NullPointerException();
          }
          ensurePhoneNumberIsMutable();
          phoneNumber_.add(value);
          onChanged();
        } else {
          phoneNumberBuilder_.addMessage(value);
        }
        return this;
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public Builder addPhoneNumber(
          int index, com.yyp.test.proto.UserProto.User.PhoneNumber value) {
        if (phoneNumberBuilder_ == null) {
          if (value == null) {
            throw new NullPointerException();
          }
          ensurePhoneNumberIsMutable();
          phoneNumber_.add(index, value);
          onChanged();
        } else {
          phoneNumberBuilder_.addMessage(index, value);
        }
        return this;
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public Builder addPhoneNumber(
          com.yyp.test.proto.UserProto.User.PhoneNumber.Builder builderForValue) {
        if (phoneNumberBuilder_ == null) {
          ensurePhoneNumberIsMutable();
          phoneNumber_.add(builderForValue.build());
          onChanged();
        } else {
          phoneNumberBuilder_.addMessage(builderForValue.build());
        }
        return this;
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public Builder addPhoneNumber(
          int index, com.yyp.test.proto.UserProto.User.PhoneNumber.Builder builderForValue) {
        if (phoneNumberBuilder_ == null) {
          ensurePhoneNumberIsMutable();
          phoneNumber_.add(index, builderForValue.build());
          onChanged();
        } else {
          phoneNumberBuilder_.addMessage(index, builderForValue.build());
        }
        return this;
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public Builder addAllPhoneNumber(
          java.lang.Iterable<? extends com.yyp.test.proto.UserProto.User.PhoneNumber> values) {
        if (phoneNumberBuilder_ == null) {
          ensurePhoneNumberIsMutable();
          com.google.protobuf.AbstractMessageLite.Builder.addAll(
              values, phoneNumber_);
          onChanged();
        } else {
          phoneNumberBuilder_.addAllMessages(values);
        }
        return this;
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public Builder clearPhoneNumber() {
        if (phoneNumberBuilder_ == null) {
          phoneNumber_ = java.util.Collections.emptyList();
          bitField0_ = (bitField0_ & ~0x00000008);
          onChanged();
        } else {
          phoneNumberBuilder_.clear();
        }
        return this;
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public Builder removePhoneNumber(int index) {
        if (phoneNumberBuilder_ == null) {
          ensurePhoneNumberIsMutable();
          phoneNumber_.remove(index);
          onChanged();
        } else {
          phoneNumberBuilder_.remove(index);
        }
        return this;
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public com.yyp.test.proto.UserProto.User.PhoneNumber.Builder getPhoneNumberBuilder(
          int index) {
        return getPhoneNumberFieldBuilder().getBuilder(index);
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public com.yyp.test.proto.UserProto.User.PhoneNumberOrBuilder getPhoneNumberOrBuilder(
          int index) {
        if (phoneNumberBuilder_ == null) {
          return phoneNumber_.get(index);  } else {
          return phoneNumberBuilder_.getMessageOrBuilder(index);
        }
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public java.util.List<? extends com.yyp.test.proto.UserProto.User.PhoneNumberOrBuilder> 
           getPhoneNumberOrBuilderList() {
        if (phoneNumberBuilder_ != null) {
          return phoneNumberBuilder_.getMessageOrBuilderList();
        } else {
          return java.util.Collections.unmodifiableList(phoneNumber_);
        }
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public com.yyp.test.proto.UserProto.User.PhoneNumber.Builder addPhoneNumberBuilder() {
        return getPhoneNumberFieldBuilder().addBuilder(
            com.yyp.test.proto.UserProto.User.PhoneNumber.getDefaultInstance());
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public com.yyp.test.proto.UserProto.User.PhoneNumber.Builder addPhoneNumberBuilder(
          int index) {
        return getPhoneNumberFieldBuilder().addBuilder(
            index, com.yyp.test.proto.UserProto.User.PhoneNumber.getDefaultInstance());
      }
      /**
       * <code>repeated .User.PhoneNumber phoneNumber = 4;</code>
       */
      public java.util.List<com.yyp.test.proto.UserProto.User.PhoneNumber.Builder> 
           getPhoneNumberBuilderList() {
        return getPhoneNumberFieldBuilder().getBuilderList();
      }
      private com.google.protobuf.RepeatedFieldBuilderV3<
          com.yyp.test.proto.UserProto.User.PhoneNumber, com.yyp.test.proto.UserProto.User.PhoneNumber.Builder, com.yyp.test.proto.UserProto.User.PhoneNumberOrBuilder> 
          getPhoneNumberFieldBuilder() {
        if (phoneNumberBuilder_ == null) {
          phoneNumberBuilder_ = new com.google.protobuf.RepeatedFieldBuilderV3<
              com.yyp.test.proto.UserProto.User.PhoneNumber, com.yyp.test.proto.UserProto.User.PhoneNumber.Builder, com.yyp.test.proto.UserProto.User.PhoneNumberOrBuilder>(
                  phoneNumber_,
                  ((bitField0_ & 0x00000008) != 0),
                  getParentForChildren(),
                  isClean());
          phoneNumber_ = null;
        }
        return phoneNumberBuilder_;
      }
      @java.lang.Override
      public final Builder setUnknownFields(
          final com.google.protobuf.UnknownFieldSet unknownFields) {
        return super.setUnknownFields(unknownFields);
      }

      @java.lang.Override
      public final Builder mergeUnknownFields(
          final com.google.protobuf.UnknownFieldSet unknownFields) {
        return super.mergeUnknownFields(unknownFields);
      }


      // @@protoc_insertion_point(builder_scope:User)
    }

    // @@protoc_insertion_point(class_scope:User)
    private static final com.yyp.test.proto.UserProto.User DEFAULT_INSTANCE;
    static {
      DEFAULT_INSTANCE = new com.yyp.test.proto.UserProto.User();
    }

    public static com.yyp.test.proto.UserProto.User getDefaultInstance() {
      return DEFAULT_INSTANCE;
    }

    private static final com.google.protobuf.Parser<User>
        PARSER = new com.google.protobuf.AbstractParser<User>() {
      @java.lang.Override
      public User parsePartialFrom(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws com.google.protobuf.InvalidProtocolBufferException {
        Builder builder = newBuilder();
        try {
          builder.mergeFrom(input, extensionRegistry);
        } catch (com.google.protobuf.InvalidProtocolBufferException e) {
          throw e.setUnfinishedMessage(builder.buildPartial());
        } catch (com.google.protobuf.UninitializedMessageException e) {
          throw e.asInvalidProtocolBufferException().setUnfinishedMessage(builder.buildPartial());
        } catch (java.io.IOException e) {
          throw new com.google.protobuf.InvalidProtocolBufferException(e)
              .setUnfinishedMessage(builder.buildPartial());
        }
        return builder.buildPartial();
      }
    };

    public static com.google.protobuf.Parser<User> parser() {
      return PARSER;
    }

    @java.lang.Override
    public com.google.protobuf.Parser<User> getParserForType() {
      return PARSER;
    }

    @java.lang.Override
    public com.yyp.test.proto.UserProto.User getDefaultInstanceForType() {
      return DEFAULT_INSTANCE;
    }

  }

  private static final com.google.protobuf.Descriptors.Descriptor
    internal_static_User_descriptor;
  private static final 
    com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
      internal_static_User_fieldAccessorTable;
  private static final com.google.protobuf.Descriptors.Descriptor
    internal_static_User_PhoneNumber_descriptor;
  private static final 
    com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
      internal_static_User_PhoneNumber_fieldAccessorTable;

  public static com.google.protobuf.Descriptors.FileDescriptor
      getDescriptor() {
    return descriptor;
  }
  private static  com.google.protobuf.Descriptors.FileDescriptor
      descriptor;
  static {
    java.lang.String[] descriptorData = {
      "\n\nUser.proto\"\302\001\n\004User\022\n\n\002id\030\001 \001(\005\022\014\n\004nam" +
      "e\030\002 \001(\t\022\r\n\005email\030\003 \001(\t\022&\n\013phoneNumber\030\004 " +
      "\003(\0132\021.User.PhoneNumber\032<\n\013PhoneNumber\022\035\n" +
      "\004type\030\001 \001(\0162\017.User.PhoneType\022\016\n\006number\030\002" +
      " \001(\t\"+\n\tPhoneType\022\n\n\006MOBILE\020\000\022\010\n\004HOME\020\001\022" +
      "\010\n\004WORK\020\002B*\n\022com.yyp.test.protoB\tUserPro" +
      "toP\000\210\001\000\240\001\001\330\001\001b\006proto3"
    };
    descriptor = com.google.protobuf.Descriptors.FileDescriptor
      .internalBuildGeneratedFileFrom(descriptorData,
        new com.google.protobuf.Descriptors.FileDescriptor[] {
        });
    internal_static_User_descriptor =
      getDescriptor().getMessageTypes().get(0);
    internal_static_User_fieldAccessorTable = new
      com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
        internal_static_User_descriptor,
        new java.lang.String[] { "Id", "Name", "Email", "PhoneNumber", });
    internal_static_User_PhoneNumber_descriptor =
      internal_static_User_descriptor.getNestedTypes().get(0);
    internal_static_User_PhoneNumber_fieldAccessorTable = new
      com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
        internal_static_User_PhoneNumber_descriptor,
        new java.lang.String[] { "Type", "Number", });
  }

  // @@protoc_insertion_point(outer_class_scope)
}


总结

https://github.com/protocolbuffers/protobuf/releases/

Go微服务(二)——Protobuf详细入门-腾讯云开发者社区-腾讯云

文章来源:https://blog.csdn.net/yangyanping20108/article/details/135114573
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。