本文整理匯總了Java中com.google.protobuf.Descriptors.Descriptor類的典型用法代碼示例。如果您正苦於以下問題:Java Descriptor類的具體用法?Java Descriptor怎麽用?Java Descriptor使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Descriptor類屬於com.google.protobuf.Descriptors包,在下文中一共展示了Descriptor類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: buildMessage
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的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();
}
示例2: mergeMapField
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的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());
}
}
示例3: createProtoBuf
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的package包/類
Message createProtoBuf(
Descriptor descriptor, Message.Builder builder, Map<String, Object> input) {
if (input == null) {
return builder.build();
}
for (FieldDescriptor field : descriptor.getFields()) {
String fieldName = field.getName();
if (!input.containsKey(fieldName)) {
// TODO: validate required fields
continue;
}
if (field.isRepeated()) {
List<Object> values = (List<Object>) input.get(fieldName);
for (Object value : values) {
builder.addRepeatedField(field, getValueForField(field, value, builder));
}
} else {
builder.setField(field, getValueForField(field, input.get(fieldName), builder));
}
}
return builder.build();
}
示例4: build
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的package包/類
GqlInputConverter build() {
HashBiMap<String, Descriptor> mapping = HashBiMap.create();
HashBiMap<String, EnumDescriptor> enumMapping = HashBiMap.create(getEnumMap(enumDescriptors));
LinkedList<Descriptor> loop = new LinkedList<>(descriptors);
Set<FileDescriptor> fileDescriptorSet = ProtoRegistry.extractDependencies(fileDescriptors);
for (FileDescriptor fileDescriptor : fileDescriptorSet) {
loop.addAll(fileDescriptor.getMessageTypes());
enumMapping.putAll(getEnumMap(fileDescriptor.getEnumTypes()));
}
while (!loop.isEmpty()) {
Descriptor descriptor = loop.pop();
if (!mapping.containsKey(descriptor.getFullName())) {
mapping.put(getReferenceName(descriptor), descriptor);
loop.addAll(descriptor.getNestedTypes());
enumMapping.putAll(getEnumMap(descriptor.getEnumTypes()));
}
}
return new GqlInputConverter(
ImmutableBiMap.copyOf(mapping), ImmutableBiMap.copyOf(enumMapping));
}
示例5: getAllFieldsMutable
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的package包/類
/** Internal helper which returns a mutable map. */
private Map<FieldDescriptor, Object> getAllFieldsMutable() {
final TreeMap<FieldDescriptor, Object> result =
new TreeMap<FieldDescriptor, Object>();
final Descriptor descriptor = internalGetFieldAccessorTable().descriptor;
for (final FieldDescriptor field : descriptor.getFields()) {
if (field.isRepeated()) {
final List value = (List) getField(field);
if (!value.isEmpty()) {
result.put(field, value);
}
} else {
if (hasField(field)) {
result.put(field, getField(field));
}
}
}
return result;
}
示例6: ProtoFieldInfo
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的package包/類
ProtoFieldInfo(FieldDescriptor field, Message containingPrototype) {
this.field = checkNotNull(field, "field");
this.containingPrototype = checkNotNull(containingPrototype, "containingPrototype");
builderClass = containingPrototype.newBuilderForType().getClass();
camelCaseName = underscoresToUpperCamelCase(field.getName());
if (field.isMapField()) {
Descriptor mapType = field.getMessageType();
mapKeyField = new ProtoFieldInfo(mapType.findFieldByName("key"), containingPrototype);
mapValueField = new ProtoFieldInfo(mapType.findFieldByName("value"), containingPrototype);
} else {
mapKeyField = null;
mapValueField = null;
}
}
示例7: DynamicMessage
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的package包/類
/**
* Construct a {@code DynamicMessage} using the given {@code FieldSet}.
*/
private DynamicMessage(Descriptor type, FieldSet<FieldDescriptor> fields,
UnknownFieldSet unknownFields) {
this.type = type;
this.fields = fields;
this.unknownFields = unknownFields;
}
示例8: mergeListValue
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的package包/類
private void mergeListValue(JsonElement json, Message.Builder builder)
throws InvalidProtocolBufferException {
Descriptor descriptor = builder.getDescriptorForType();
FieldDescriptor field = descriptor.findFieldByName("values");
if (field == null) {
throw new InvalidProtocolBufferException("Invalid ListValue type.");
}
mergeRepeatedField(field, json, builder);
}
示例9: parseFrom
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的package包/類
/** Parse a message of the given type from the given input stream. */
public static DynamicMessage parseFrom(
Descriptor type,
CodedInputStream input,
ExtensionRegistry extensionRegistry)
throws IOException {
return newBuilder(type).mergeFrom(input, extensionRegistry).buildParsed();
}
示例10: mergeWrapper
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的package包/類
private void mergeWrapper(JsonElement json, Message.Builder builder)
throws InvalidProtocolBufferException {
Descriptor type = builder.getDescriptorForType();
FieldDescriptor field = type.findFieldByName("value");
if (field == null) {
throw new InvalidProtocolBufferException("Invalid wrapper type: " + type.getFullName());
}
builder.setField(field, parseFieldValue(field, json, builder));
}
示例11: isInitialized
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的package包/類
private static boolean isInitialized(Descriptor type,
FieldSet<FieldDescriptor> fields) {
// Check that all required fields are present.
for (final FieldDescriptor field : type.getFields()) {
if (field.isRequired()) {
if (!fields.hasField(field)) {
return false;
}
}
}
// Check that embedded messages are initialized.
return fields.isInitialized();
}
示例12: mergeStruct
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的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);
}
示例13: addMessage
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的package包/類
private void addMessage(Descriptor message) {
for (Descriptor nestedType : message.getNestedTypes()) {
addMessage(nestedType);
}
if (types.containsKey(message.getFullName())) {
logger.warning("Type " + message.getFullName() + " is added multiple times.");
return;
}
types.put(message.getFullName(), message);
}
示例14: testFieldDescriptorDefault
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的package包/類
public void testFieldDescriptorDefault() throws Exception {
Descriptor d = TestAllTypes.getDescriptor();
assertFalse(d.findFieldByName("optional_int32").hasDefaultValue());
assertEquals(0, d.findFieldByName("optional_int32").getDefaultValue());
assertTrue(d.findFieldByName("default_int32").hasDefaultValue());
assertEquals(41, d.findFieldByName("default_int32").getDefaultValue());
d = TestExtremeDefaultValues.getDescriptor();
assertEquals(
ByteString.copyFrom(
"\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes("ISO-8859-1")),
d.findFieldByName("escaped_bytes").getDefaultValue());
assertEquals(-1, d.findFieldByName("large_uint32").getDefaultValue());
assertEquals(-1L, d.findFieldByName("large_uint64").getDefaultValue());
}
示例15: add
import com.google.protobuf.Descriptors.Descriptor; //導入依賴的package包/類
/**
* Adds message types and all types defined in the same .proto file as
* well as all transitively imported .proto files to this {@link Builder}.
*/
public Builder add(Iterable<Descriptor> messageTypes) {
if (types == null) {
throw new IllegalStateException("A TypeRegistry.Builer can only be used once.");
}
for (Descriptor type : messageTypes) {
addFile(type.getFile());
}
return this;
}