本文整理匯總了Java中com.google.protobuf.Message.Builder.setField方法的典型用法代碼示例。如果您正苦於以下問題:Java Builder.setField方法的具體用法?Java Builder.setField怎麽用?Java Builder.setField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.protobuf.Message.Builder
的用法示例。
在下文中一共展示了Builder.setField方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setTargetField
import com.google.protobuf.Message.Builder; //導入方法依賴的package包/類
private static void setTargetField(final Builder target, final Object sourceObject, final String targetField)
throws IllegalArgumentException {
Descriptors.FieldDescriptor fieldDescriptor = target.getDescriptorForType().findFieldByName(targetField);
if (null == fieldDescriptor) {
throw new RuntimeException("Unknown target field in protobuf: " + targetField);
}
if (fieldDescriptor.isRepeated()) {
target.addRepeatedField(fieldDescriptor, sourceObject);
} else {
target.setField(fieldDescriptor, sourceObject);
}
}
示例2: buildKey
import com.google.protobuf.Message.Builder; //導入方法依賴的package包/類
public T buildKey(T data) {
Builder b = data.newBuilderForType();
for (FieldDescriptor field : fields) {
if (!data.hasField(field)) {
if (requireFields) {
throw new IllegalStateException("Field not set: " + field.getFullName());
} else {
continue;
}
}
Object value = data.getField(field);
b.setField(field, value);
}
return (T) b.build();
}
示例3: buildMessage
import com.google.protobuf.Message.Builder; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
private static Object buildMessage(Builder builder, Map<String, Object> fields) {
Descriptor descriptor = builder.getDescriptorForType();
for (Map.Entry<String, Object> entry : fields.entrySet()) {
if (entry.getValue() == null) {
continue;
}
FieldDescriptor field = getField(descriptor, entry.getKey());
if (entry.getValue() instanceof List<?>) {
List<Object> values = (List<Object>) entry.getValue();
for (Object value : values) {
builder.addRepeatedField(field, buildValue(builder, field, value));
}
} else {
builder.setField(field, buildValue(builder, field, entry.getValue()));
}
}
return builder.build();
}
示例4: generate
import com.google.protobuf.Message.Builder; //導入方法依賴的package包/類
/**
* Generates a random protocol buffer, filling in all required fields but
* with a p chance of not setting an optional field and p chance of having
* an empty repeated field.
*/
@SuppressWarnings("unchecked")
public E generate(double p) {
Builder builder = instance.newBuilderForType();
Descriptor descriptor = instance.getDescriptorForType();
for (FieldDescriptor field : descriptor.getFields()) {
if (!field.isRequired() && random.nextDouble() < p) {
continue;
}
builder.setField(field, getRandomValue(field, p));
}
return (E) builder.build();
}