本文整理汇总了Java中com.google.protobuf.Message.Builder方法的典型用法代码示例。如果您正苦于以下问题:Java Message.Builder方法的具体用法?Java Message.Builder怎么用?Java Message.Builder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.protobuf.Message
的用法示例。
在下文中一共展示了Message.Builder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mergeMapField
import com.google.protobuf.Message; //导入方法依赖的package包/类
private void mergeMapField(FieldDescriptor field, JsonElement json, Message.Builder builder)
throws InvalidProtocolBufferException {
if (!(json instanceof JsonObject)) {
throw new InvalidProtocolBufferException("Expect a map object but found: " + json);
}
Descriptor type = field.getMessageType();
FieldDescriptor keyField = type.findFieldByName("key");
FieldDescriptor valueField = type.findFieldByName("value");
if (keyField == null || valueField == null) {
throw new InvalidProtocolBufferException("Invalid map field: " + field.getFullName());
}
JsonObject object = (JsonObject) json;
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
Message.Builder entryBuilder = builder.newBuilderForField(field);
Object key = parseFieldValue(keyField, new JsonPrimitive(entry.getKey()), entryBuilder);
Object value = parseFieldValue(valueField, entry.getValue(), entryBuilder);
if (value == null) {
throw new InvalidProtocolBufferException("Map value cannot be null.");
}
entryBuilder.setField(keyField, key);
entryBuilder.setField(valueField, value);
builder.addRepeatedField(field, entryBuilder.build());
}
}
示例2: getValueForField
import com.google.protobuf.Message; //导入方法依赖的package包/类
private Object getValueForField(FieldDescriptor field, Object value, Message.Builder builder) {
// TODO: handle groups, oneof
if (field.getType() == FieldDescriptor.Type.MESSAGE) {
Descriptor fieldTypeDescriptor =
descriptorMapping.get(getReferenceName(field.getMessageType()));
return createProtoBuf(
fieldTypeDescriptor, builder.newBuilderForField(field), (Map<String, Object>) value);
}
if (field.getType() == FieldDescriptor.Type.ENUM) {
EnumDescriptor enumDescriptor =
enumMapping.get(ProtoToGql.getReferenceName(field.getEnumType()));
return enumDescriptor.findValueByNumber((int) value);
}
return value;
}
示例3: startRpc
import com.google.protobuf.Message; //导入方法依赖的package包/类
public void startRpc(EventLoop eventLoop, Message.Builder builder, RpcCallback<Message> callback) {
if (this.callback != null) {
throw new IllegalStateException(
"ProtoRpcController already in use by another RPC call; " +
"wait for callback before reusing.");
}
if (callback == null) {
throw new NullPointerException("callback cannot be null");
}
assert this.eventLoop == null;
assert eventLoop != null;
assert this.builder == null;
assert builder != null;
this.eventLoop = eventLoop;
this.builder = builder;
this.callback = callback;
status = Protocol.Status.INVALID;
}
示例4: mergeValue
import com.google.protobuf.Message; //导入方法依赖的package包/类
/**
* Merges the JSON {@link String} into the provided {@link Message.Builder}.
*
* @throws InvalidProtocolBufferException if the input is not valid JSON format or there are
* unknown fields in the input.
*/
public void mergeValue(String json, Message.Builder builder) throws IOException {
checkNotNull(json, "json");
checkNotNull(builder, "builder");
JsonParser parser = jsonFactory.createParser(json);
mergeValue(parser, builder);
}
示例5: handleObject
import com.google.protobuf.Message; //导入方法依赖的package包/类
private Object handleObject(XMLEventReader parser,
XMLEvent startEvent,
ExtensionRegistry extensionRegistry,
Message.Builder builder,
FieldDescriptor field,
ExtensionRegistry.ExtensionInfo extension) throws XMLStreamException {
Message.Builder subBuilder = createSubBuilder(builder, field, extension);
XMLEvent event = startEvent;
int depth = 0; // initialize to 0
do {
if (event.isStartElement()) {
depth++; // we're inside the element
mergeField(parser, event,
extensionRegistry, subBuilder);
XMLEvent nextEvent = parser.nextTag();
if (nextEvent.isEndElement()) {
depth--;
// if we're back under the top level obj, and there is another close, we're done.
if (depth <= 0 && parser.peek().isEndElement()) {
break;
}
} else if (nextEvent.isStartElement()) {
depth++;
}
} else {
// something is potentially wrong..
break;
}
} while (parser.hasNext() && (event = parser.nextTag()) != null);
return subBuilder.build();
}
示例6: doMerge
import com.google.protobuf.Message; //导入方法依赖的package包/类
@Override
public void doMerge(JsonParser parser, int unused, Message.Builder messageBuilder)
throws IOException {
Duration.Builder builder = (Duration.Builder) messageBuilder;
try {
builder.mergeFrom(Durations.parse(ParseSupport.parseString(parser)));
} catch (ParseException e) {
throw new InvalidProtocolBufferException(
"Failed to readValue duration: " + parser.getText());
}
}
示例7: mergeStruct
import com.google.protobuf.Message; //导入方法依赖的package包/类
private void mergeStruct(JsonElement json, Message.Builder builder)
throws InvalidProtocolBufferException {
Descriptor descriptor = builder.getDescriptorForType();
FieldDescriptor field = descriptor.findFieldByName("fields");
if (field == null) {
throw new InvalidProtocolBufferException("Invalid Struct type.");
}
mergeMapField(field, json, builder);
}
示例8: mergeRepeatedField
import com.google.protobuf.Message; //导入方法依赖的package包/类
private void mergeRepeatedField(
FieldDescriptor field, JsonElement json, Message.Builder builder)
throws InvalidProtocolBufferException {
if (!(json instanceof JsonArray)) {
throw new InvalidProtocolBufferException("Expect an array but found: " + json);
}
JsonArray array = (JsonArray) json;
for (int i = 0; i < array.size(); ++i) {
Object value = parseFieldValue(field, array.get(i), builder);
if (value == null) {
throw new InvalidProtocolBufferException("Repeated field elements cannot be null");
}
builder.addRepeatedField(field, value);
}
}
示例9: callExecService
import com.google.protobuf.Message; //导入方法依赖的package包/类
@Override
protected Message callExecService(RpcController controller, Descriptors.MethodDescriptor method,
Message request, Message responsePrototype)
throws IOException {
if (LOG.isTraceEnabled()) {
LOG.trace("Call: "+method.getName()+", "+request.toString());
}
final ClientProtos.CoprocessorServiceCall call =
ClientProtos.CoprocessorServiceCall.newBuilder()
.setRow(ByteStringer.wrap(HConstants.EMPTY_BYTE_ARRAY))
.setServiceName(method.getService().getFullName())
.setMethodName(method.getName())
.setRequest(request.toByteString()).build();
// TODO: Are we retrying here? Does not seem so. We should use RetryingRpcCaller
CoprocessorServiceResponse result = ProtobufUtil.execService(controller,
connection.getMaster(), call);
Message response = null;
if (result.getValue().hasValue()) {
Message.Builder builder = responsePrototype.newBuilderForType();
ProtobufUtil.mergeFrom(builder, result.getValue().getValue());
response = builder.build();
} else {
response = responsePrototype.getDefaultInstanceForType();
}
if (LOG.isTraceEnabled()) {
LOG.trace("Master Result is value=" + response);
}
return response;
}
示例10: newEmptyMessage
import com.google.protobuf.Message; //导入方法依赖的package包/类
/**
* Creates an empty protobuf message of the specified type
*
* @param klass the protobuf message type
* @return the generated protobuf message
*/
public static <TYPE extends Message> TYPE newEmptyMessage(Class<TYPE> klass) {
try {
Message.Builder builder = getBuilder(klass);
return (TYPE) builder.build();
} catch (Exception e) {
throw new RuntimeException("Error deserializing byte array to protobuf", e);
}
}
示例11: merge
import com.google.protobuf.Message; //导入方法依赖的package包/类
void merge(Reader json, Message.Builder builder) throws IOException {
JsonReader reader = new JsonReader(json);
reader.setLenient(false);
merge(jsonParser.parse(reader), builder);
}
示例12: mergeFromJson
import com.google.protobuf.Message; //导入方法依赖的package包/类
private void mergeFromJson(String json, Message.Builder builder, Message... additionalTypes)
throws IOException {
mergeFromJson(json, false, builder, additionalTypes);
}
示例13: mergeField
import com.google.protobuf.Message; //导入方法依赖的package包/类
/**
* Parse a single field from {@code tokenizer} and merge it into {@code builder}. If a ',' is
* detected after the field ends, the next field will be parsed automatically
*/
protected void mergeField(Tokenizer tokenizer,
ExtensionRegistry extensionRegistry,
Message.Builder builder) throws ParseException {
FieldDescriptor field;
Descriptor type = builder.getDescriptorForType();
ExtensionRegistry.ExtensionInfo extension = null;
boolean unknown = false;
String name = tokenizer.consumeIdentifier();
field = type.findFieldByName(name);
// Group names are expected to be capitalized as they appear in the
// .proto file, which actually matches their type names, not their field
// names.
if (field == null) {
// Explicitly specify US locale so that this code does not break when
// executing in Turkey.
String lowerName = name.toLowerCase(Locale.US);
field = type.findFieldByName(lowerName);
// If the case-insensitive match worked but the field is NOT a group,
if ((field != null) && (field.getType() != FieldDescriptor.Type.GROUP)) {
field = null;
}
}
// Again, special-case group names as described above.
if ((field != null) && (field.getType() == FieldDescriptor.Type.GROUP)
&& !field.getMessageType().getName().equals(name)) {
field = null;
}
// Last try to lookup by field-index if 'name' is numeric,
// which indicates a possible unknown field
if (field == null && TextUtils.isDigits(name)) {
field = type.findFieldByNumber(Integer.parseInt(name));
unknown = true;
}
// Finally, look for extensions
extension = extensionRegistry.findExtensionByName(name);
if (extension != null) {
if (extension.descriptor.getContainingType() != type) {
throw tokenizer.parseExceptionPreviousToken("Extension \"" + name
+ "\" does not extend message type \""
+ type.getFullName() + "\".");
}
field = extension.descriptor;
}
// Disabled throwing exception if field not found, since it could be a different version.
if (field == null) {
handleMissingField(tokenizer, extensionRegistry, builder);
//throw tokenizer.parseExceptionPreviousToken("Message type \"" + type.getFullName()
// + "\" has no field named \"" + name
// + "\".");
}
if (field != null) {
tokenizer.consume(":");
boolean array = tokenizer.tryConsume("[");
if (array) {
while (!tokenizer.tryConsume("]")) {
handleValue(tokenizer, extensionRegistry, builder, field, extension, unknown);
tokenizer.tryConsume(",");
}
} else {
handleValue(tokenizer, extensionRegistry, builder, field, extension, unknown);
}
}
if (tokenizer.tryConsume(",")) {
// Continue with the next field
mergeField(tokenizer, extensionRegistry, builder);
}
}
示例14: merge
import com.google.protobuf.Message; //导入方法依赖的package包/类
public void merge(final InputStream input,
ExtensionRegistry extensionRegistry,
final Message.Builder builder) throws IOException {
merge(input, defaultCharset, extensionRegistry, builder);
}
示例15: parseFieldValue
import com.google.protobuf.Message; //导入方法依赖的package包/类
private Object parseFieldValue(FieldDescriptor field, JsonElement json, Message.Builder builder)
throws InvalidProtocolBufferException {
if (json instanceof JsonNull) {
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE
&& field.getMessageType().getFullName().equals(Value.getDescriptor().getFullName())) {
// For every other type, "null" means absence, but for the special
// Value message, it means the "null_value" field has been set.
Value value = Value.newBuilder().setNullValueValue(0).build();
return builder.newBuilderForField(field).mergeFrom(value.toByteString()).build();
} else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM
&& field.getEnumType().getFullName().equals(NullValue.getDescriptor().getFullName())) {
// If the type of the field is a NullValue, then the value should be explicitly set.
return field.getEnumType().findValueByNumber(0);
}
return null;
}
switch (field.getType()) {
case INT32:
case SINT32:
case SFIXED32:
return parseInt32(json);
case INT64:
case SINT64:
case SFIXED64:
return parseInt64(json);
case BOOL:
return parseBool(json);
case FLOAT:
return parseFloat(json);
case DOUBLE:
return parseDouble(json);
case UINT32:
case FIXED32:
return parseUint32(json);
case UINT64:
case FIXED64:
return parseUint64(json);
case STRING:
return parseString(json);
case BYTES:
return parseBytes(json);
case ENUM:
return parseEnum(field.getEnumType(), json);
case MESSAGE:
case GROUP:
if (currentDepth >= recursionLimit) {
throw new InvalidProtocolBufferException("Hit recursion limit.");
}
++currentDepth;
Message.Builder subBuilder = builder.newBuilderForField(field);
merge(json, subBuilder);
--currentDepth;
return subBuilder.build();
default:
throw new InvalidProtocolBufferException("Invalid field type: " + field.getType());
}
}