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