本文整理汇总了Java中com.google.protobuf.Descriptors.Descriptor方法的典型用法代码示例。如果您正苦于以下问题:Java Descriptors.Descriptor方法的具体用法?Java Descriptors.Descriptor怎么用?Java Descriptors.Descriptor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.protobuf.Descriptors
的用法示例。
在下文中一共展示了Descriptors.Descriptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decodeProtobuf
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
/**
* Handle all the logic leading to the decoding of a Protobuf-encoded binary given a schema file path.
* @param schema Schema used to decode the binary data
* @param messageType Type of Protobuf Message
* @param encodedData Encoded data source
* @return A JSON representation of the data, contained in a Java String
* @throws InvalidProtocolBufferException Thrown when an error occurs during the encoding of the decoded data into JSON
* @throws Descriptors.DescriptorValidationException Thrown when the schema is invalid
* @throws UnknownMessageTypeException Thrown when the given message type is not contained in the schema
* @throws MessageDecodingException Thrown when an error occurs during the binary decoding
* @throws SchemaLoadingException Thrown when an error occurs while reading the schema file
*/
public static String decodeProtobuf(DynamicSchema schema, String messageType, InputStream encodedData) throws InvalidProtocolBufferException, Descriptors.DescriptorValidationException, UnknownMessageTypeException, MessageDecodingException, SchemaLoadingException {
Descriptors.Descriptor descriptor;
DynamicMessage message;
descriptor = schema.getMessageDescriptor(messageType);
if (descriptor == null) {
throw new UnknownMessageTypeException(messageType);
}
try {
message = DynamicMessage.parseFrom(descriptor, encodedData);
} catch (IOException e) {
throw new MessageDecodingException(e);
}
return JSONMapper.toJSON(message);
}
示例2: testNullRepeated
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
@Test
public void testNullRepeated() throws DataGeneratorException {
Record r = RecordCreator.create();
Map<String, Field> repeated = new HashMap<>();
repeated.put("samples", Field.create(Field.Type.LIST, null));
r.set(Field.create(repeated));
Descriptors.Descriptor descriptor = RepeatedProto.getDescriptor().findMessageTypeByName("Repeated");
// repeated field samples is null and ignored
DynamicMessage dynamicMessage = ProtobufTypeUtil.sdcFieldToProtobufMsg(
r,
descriptor,
typeToExtensionMap,
defaultValueMap
);
// null repeated fields are treated as empty arrays
Object samples = dynamicMessage.getField(descriptor.findFieldByName("samples"));
Assert.assertNotNull(samples);
Assert.assertTrue(samples instanceof List);
Assert.assertEquals(0, ((List) samples).size());
}
示例3: create_masks_for_given_field_tags
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
@Test
public void create_masks_for_given_field_tags() {
final Descriptors.Descriptor descriptor = Project.getDescriptor();
final int[] fieldNumbers = {1, 2, 3};
@SuppressWarnings("DuplicateStringLiteralInspection")
final String[] fieldNames = {"id", "name", "task"};
final FieldMask mask = FieldMasks.maskOf(descriptor, fieldNumbers);
final List<String> paths = mask.getPathsList();
assertSize(fieldNumbers.length, paths);
for (int i = 0; i < paths.size(); i++) {
final String expectedPath = descriptor.getFullName() + '.' + fieldNames[i];
assertEquals(expectedPath, paths.get(i));
}
}
示例4: ProtobufDataParser
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
public ProtobufDataParser(
ProtoConfigurableEntity.Context context,
String messageId,
Descriptors.Descriptor descriptor,
Map<String, Set<Descriptors.FieldDescriptor>> messageTypeToExtensionMap,
ExtensionRegistry extensionRegistry,
InputStream inputStream,
String readerOffset,
int maxObjectLength,
boolean isDelimited
) throws IOException, Descriptors.DescriptorValidationException, DataParserException {
this.context = context;
this.inputStream = new OverrunInputStream(inputStream, maxObjectLength, true);
this.messageId = messageId;
this.messageTypeToExtensionMap = messageTypeToExtensionMap;
this.extensionRegistry = extensionRegistry;
this.descriptor = descriptor;
this.builder = DynamicMessage.newBuilder(descriptor);
this.isDelimited = isDelimited;
// skip to the required location
if (readerOffset != null && !readerOffset.isEmpty() && !readerOffset.equals("0")) {
int offset = Integer.parseInt(readerOffset);
this.inputStream.skip(offset);
}
}
示例5: populateDefaultsAndExtensions
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
/**
* Populates a map of protobuf extensions and map with the default values for
* each message field from a map of file descriptors.
*
* @param fileDescriptorMap Map of file descriptors
* @param typeToExtensionMap Map of extensions to populate
* @param defaultValueMap Map of default values to populate
*/
public static void populateDefaultsAndExtensions(
Map<String, Descriptors.FileDescriptor> fileDescriptorMap,
Map<String, Set<Descriptors.FieldDescriptor>> typeToExtensionMap,
Map<String, Object> defaultValueMap
) {
for (Descriptors.FileDescriptor f : fileDescriptorMap.values()) {
// go over every file descriptor and look for extensions and default values of those extensions
for (Descriptors.FieldDescriptor fieldDescriptor : f.getExtensions()) {
String containingType = fieldDescriptor.getContainingType().getFullName();
Set<Descriptors.FieldDescriptor> fieldDescriptors = typeToExtensionMap.get(containingType);
if (fieldDescriptors == null) {
fieldDescriptors = new LinkedHashSet<>();
typeToExtensionMap.put(containingType, fieldDescriptors);
}
fieldDescriptors.add(fieldDescriptor);
if (fieldDescriptor.hasDefaultValue()) {
defaultValueMap.put(containingType + "." + fieldDescriptor.getName(), fieldDescriptor.getDefaultValue());
}
}
// go over messages within file descriptor and look for all fields and extensions and their defaults
for (Descriptors.Descriptor d : f.getMessageTypes()) {
addDefaultsAndExtensions(typeToExtensionMap, defaultValueMap, d);
}
}
}
示例6: getDescriptor
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
private Descriptors.Descriptor getDescriptor(Class<M> messageClass) {
try {
return (Descriptors.Descriptor) MethodUtils.invokeStaticMethod(
messageClass, "getDescriptor");
} catch (NoSuchMethodException | IllegalAccessException
| InvocationTargetException ex) {
throw new RuntimeException(ex);
}
}
示例7: testNonEmptyRepeated
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
@Test
public void testNonEmptyRepeated() throws DataGeneratorException {
Record r = RecordCreator.create();
Map<String, Field> repeated = new HashMap<>();
repeated.put(
"samples",
Field.create(
Field.Type.LIST,
Arrays.asList(
Field.create(1),
Field.create(2),
Field.create(3),
Field.create(4),
Field.create(5)
)
)
);
r.set(Field.create(repeated));
Descriptors.Descriptor descriptor = RepeatedProto.getDescriptor().findMessageTypeByName("Repeated");
// repeated field samples is null and ignored
DynamicMessage dynamicMessage = ProtobufTypeUtil.sdcFieldToProtobufMsg(
r,
descriptor,
typeToExtensionMap,
defaultValueMap
);
// null repeated fields are treated as empty arrays
Object samples = dynamicMessage.getField(descriptor.findFieldByName("samples"));
Assert.assertNotNull(samples);
Assert.assertTrue(samples instanceof List);
Assert.assertEquals(5, ((List)samples).size());
}
示例8: testOneofProtoToSdc
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
@Test
public void testOneofProtoToSdc() throws DataParserException, IOException, DataGeneratorException {
Descriptors.Descriptor descriptor = OneofProto.getDescriptor().findMessageTypeByName("Oneof");
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
OneofProto.Oneof.Builder builder = OneofProto.Oneof.newBuilder();
OneofProto.Oneof build = builder.setOneofInt(5).build();
build.writeDelimitedTo(bOut);
bOut.close();
DynamicMessage.Builder dynBldr = DynamicMessage.newBuilder(descriptor);
dynBldr.mergeDelimitedFrom(new ByteArrayInputStream(bOut.toByteArray()), null);
Record record = RecordCreator.create();
Field field = ProtobufTypeUtil.protobufToSdcField(record, "", descriptor, typeToExtensionMap, dynBldr.build());
Assert.assertNotNull(field);
Assert.assertEquals("", field.getValueAsMap().get("oneofString").getValue());
Assert.assertEquals(Field.Type.INTEGER, field.getValueAsListMap().get("oneofInt").getType());
Assert.assertEquals(5, field.getValueAsMap().get("oneofInt").getValueAsInteger());
bOut.reset();
builder.clear();
build = builder.setOneofString("Hello").build();
build.writeDelimitedTo(bOut);
bOut.close();
dynBldr = DynamicMessage.newBuilder(descriptor);
dynBldr.mergeDelimitedFrom(new ByteArrayInputStream(bOut.toByteArray()), null);
record = RecordCreator.create();
field = ProtobufTypeUtil.protobufToSdcField(record, "", descriptor, typeToExtensionMap, dynBldr.build());
Assert.assertNotNull(field);
Assert.assertEquals(0, field.getValueAsMap().get("oneofInt").getValue());
Assert.assertEquals(Field.Type.STRING, field.getValueAsListMap().get("oneofString").getType());
Assert.assertEquals("Hello", field.getValueAsMap().get("oneofString").getValueAsString());
}
示例9: ProtobufDataGenerator
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
public ProtobufDataGenerator(
OutputStream outputStream,
Descriptors.Descriptor descriptor,
Map<String, Set<Descriptors.FieldDescriptor>> messageTypeToExtensionMap,
Map<String, Object> defaultValueMap,
boolean isDelimited
) {
this.outputStream = outputStream;
this.descriptor = descriptor;
this.messageTypeToExtensionMap = messageTypeToExtensionMap;
this.defaultValueMap = defaultValueMap;
this.isDelimited = isDelimited;
}
示例10: handleNonRepeatedField
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
private static void handleNonRepeatedField(
Record record,
Map<String, Field> valueAsMap,
String fieldPath,
Map<String, Set<Descriptors.FieldDescriptor>> messageTypeToExtensionMap,
Map<String, Object> defaultValueMap,
Descriptors.Descriptor desc,
Descriptors.FieldDescriptor f,
DynamicMessage.Builder builder
) throws DataGeneratorException {
Object val;
String keyName = f.getName();
if (valueAsMap.containsKey(keyName)) {
val = getValue(
f,
valueAsMap.get(keyName),
record,
fieldPath + FORWARD_SLASH + f.getName(),
messageTypeToExtensionMap,
defaultValueMap
);
} else {
// record does not contain field, look up default value
String key = desc.getFullName() + "." + f.getName();
if (!defaultValueMap.containsKey(key) && !f.isOptional()) {
throw new DataGeneratorException(
Errors.PROTOBUF_04,
record.getHeader().getSourceId(),
key
);
}
val = defaultValueMap.get(key);
}
if (val != null) {
builder.setField(f, val);
}
}
示例11: getProtobufClassFieldDescriptions
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
protected String getProtobufClassFieldDescriptions(Class<? extends Message> messageClass)
throws Exception {
StringBuilder sb = new StringBuilder();
Constructor<?> constructor = null;
try {
constructor = messageClass.getDeclaredConstructor();
} catch (NoSuchMethodException nsmex) {
//Issue #35
logger.info("Unsupported protobuf field: {}", messageClass.getName());
return sb.toString();
}
constructor.setAccessible(true);
Object instance = constructor.newInstance();
Message.Builder builder = ((Message)instance).newBuilderForType();
Message message = builder.build();
Descriptors.Descriptor requestDesc = message.getDescriptorForType();
List<Descriptors.FieldDescriptor> requestFields = requestDesc.getFields();
Iterator<Descriptors.FieldDescriptor> iter = requestFields.iterator();
while (iter.hasNext()) {
Descriptors.FieldDescriptor fd = iter.next();
//TODO: deal with repeated fields
sb.append("{\"name\":\"");
sb.append(fd.getName());
sb.append("\",\"type\":\"");
if (fd.getType().toString().equalsIgnoreCase("message")) {
sb.append(getLastComponent(fd.getMessageType().getFullName()));
sb.append("\",\"values\":[");
Descriptors.FieldDescriptor childDescriptor = requestDesc.findFieldByName(fd.getName());
Message.Builder subMessageBuilder = builder.newBuilderForField(childDescriptor);
Message subMessage = subMessageBuilder.build();
sb.append(getProtobufClassFieldDescriptions(subMessage.getClass()));
sb.append("]}");
} else {
sb.append(fd.getType().toString().toLowerCase());
sb.append("\",\"values\":null}");
}
if (iter.hasNext()) {
sb.append(",");
}
}
return sb.toString();
}
示例12: retrieve_default_field_mask_if_no_field_tags_requested
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
@Test
public void retrieve_default_field_mask_if_no_field_tags_requested() {
final Descriptors.Descriptor descriptor = Project.getDescriptor();
final FieldMask mask = FieldMasks.maskOf(descriptor);
assertEquals(FieldMask.getDefaultInstance(), mask);
}
示例13: getDescriptor
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
public static final Descriptors.Descriptor getDescriptor() {
return PushSMPMessageProtos.internal_static_textsecure_IncomingPushMessageSignal_descriptor;
}
示例14: getDescriptorForType
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
public Descriptors.Descriptor getDescriptorForType() {
return PushSMPMessageProtos.internal_static_textsecure_PushSMPMessageContent_descriptor;
}
示例15: getValue
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
private static Object getValue(
Descriptors.FieldDescriptor f,
Field field,
Record record,
String protoFieldPath,
Map<String, Set<Descriptors.FieldDescriptor>> messageTypeToExtensionMap,
Map<String, Object> defaultValueMap
) throws DataGeneratorException {
Object value = null;
try {
if (field.getValue() != null) {
switch (f.getJavaType()) {
case BOOLEAN:
value = field.getValueAsBoolean();
break;
case BYTE_STRING:
value = ByteString.copyFrom(field.getValueAsByteArray());
break;
case DOUBLE:
value = field.getValueAsDouble();
break;
case ENUM:
value = f.getEnumType().findValueByName(field.getValueAsString());
break;
case FLOAT:
value = field.getValueAsFloat();
break;
case INT:
value = field.getValueAsInteger();
break;
case LONG:
value = field.getValueAsLong();
break;
case STRING:
value = field.getValueAsString();
break;
case MESSAGE:
Descriptors.Descriptor messageType = f.getMessageType();
value = sdcFieldToProtobufMsg(
record, field, protoFieldPath, messageType, messageTypeToExtensionMap, defaultValueMap
);
break;
default:
throw new DataGeneratorException(Errors.PROTOBUF_03, f.getJavaType().name());
}
}
} catch (IllegalArgumentException e) {
throw new DataGeneratorException(Errors.PROTOBUF_11, field.getValue(), f.getJavaType().name(), e);
}
return value;
}