当前位置: 首页>>代码示例>>Java>>正文


Java Builder.build方法代码示例

本文整理汇总了Java中com.google.protobuf.AbstractMessage.Builder.build方法的典型用法代码示例。如果您正苦于以下问题:Java Builder.build方法的具体用法?Java Builder.build怎么用?Java Builder.build使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.protobuf.AbstractMessage.Builder的用法示例。


在下文中一共展示了Builder.build方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: toProtobuf

import com.google.protobuf.AbstractMessage.Builder; //导入方法依赖的package包/类
/**
 * @see com.quancheng.saluki.serializer.IProtobufSerializer#toProtobuf(java.lang.Object)
 */
@Override
@SuppressWarnings({"unchecked", "rawtypes", "unused"})
public Message toProtobuf(Object pojo) throws ProtobufException {
  try {
    final Class<?> fromClazz = (Class<?>) pojo.getClass();
    final Class<? extends GeneratedMessageV3> protoClazz =
        ProtobufSerializerUtils.getProtobufClassFromPojoAnno(fromClazz);
    if (protoClazz == null) {
      throw new ProtobufAnnotationException(
          "Doesn't seem like " + fromClazz + " is ProtobufEntity");
    }
    final Map<Field, ProtobufAttribute> protoBufFields =
        ProtobufSerializerUtils.getAllProtbufFields(fromClazz);
    if (protoBufFields.isEmpty()) {
      return null;
    }
    final Method newBuilderMethod = protoClazz.getMethod("newBuilder");
    final Builder protoObjBuilder = (Builder) newBuilderMethod.invoke(null);
    for (Entry<Field, ProtobufAttribute> entry : protoBufFields.entrySet()) {
      final Field field = entry.getKey();
      final ProtobufAttribute gpbAnnotation = entry.getValue();
      final String fieldName = field.getName();
      // 1. Determine validity of value
      Object value = Pojo2ProtobufHelp.getPojoFieldValue(pojo, gpbAnnotation, field);
      // If value is null and it is not required, skip, as the default for Protobuf values is null
      if (value == null) {
        continue;
      }
      // 2. Call recursively if this is a ProtobufEntity
      value = Pojo2ProtobufHelp.serializeToProtobufEntity(value);
      // 3. Special recursively if this is a ProtobufEntity
      if (value instanceof Collection) {
        value = Pojo2ProtobufHelp.convertCollectionToProtobufs((Collection<Object>) value);
        if (((Collection) value).isEmpty()) {
          continue;
        }
      }
      if (value instanceof Map) {
        value = Pojo2ProtobufHelp.convertMapToProtobufs((Map) value);
        if (((Map) value).isEmpty()) {
          continue;
        }
      }
      String setter = ProtobufSerializerUtils.getProtobufSetter(gpbAnnotation, field, value);
      if (value instanceof Enum) {
        value = JReflectionUtils.runMethod(value, "getNumber");
        setter = setter + "Value";
      }
      Pojo2ProtobufHelp.setProtobufFieldValue(gpbAnnotation, protoObjBuilder, setter, value);
    }
    return protoObjBuilder.build();
  } catch (Exception e) {
    throw new ProtobufException(
        "Could not generate Protobuf object for " + pojo.getClass() + ": " + e, e);
  }
}
 
开发者ID:venus-boot,项目名称:saluki,代码行数:60,代码来源:ProtobufSerializer.java

示例2: HttpPBPacket

import com.google.protobuf.AbstractMessage.Builder; //导入方法依赖的package包/类
public HttpPBPacket(int opcode, Builder<?> builder) {
		this.opcode = opcode;
		Message msg = builder.build();
//		log.info("[HttpPBPacket][new] \nopode:{}, message:\n[\n{}]" , opcode,  msg);
		this.data = msg.toByteArray();
	}
 
开发者ID:xuerong,项目名称:MMServerEngine,代码行数:7,代码来源:HttpPBPacket.java


注:本文中的com.google.protobuf.AbstractMessage.Builder.build方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。