本文整理汇总了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);
}
}
示例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();
}