本文整理汇总了Java中com.google.protobuf.Descriptors.FieldDescriptor方法的典型用法代码示例。如果您正苦于以下问题:Java Descriptors.FieldDescriptor方法的具体用法?Java Descriptors.FieldDescriptor怎么用?Java Descriptors.FieldDescriptor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.protobuf.Descriptors
的用法示例。
在下文中一共展示了Descriptors.FieldDescriptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: requireAllFieldsExcept
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
private static void requireAllFieldsExcept(AbstractMessage message, int... fieldNumbersNotRequired) {
Collection<Descriptors.FieldDescriptor> required = new ArrayList<>(message.getDescriptorForType().getFields());
Collection<Descriptors.FieldDescriptor> actual = message.getAllFields().keySet();
required.removeAll(actual);
if(fieldNumbersNotRequired != null) {
for(int fieldNumber : fieldNumbersNotRequired) {
required.remove(message.getDescriptorForType().findFieldByNumber(fieldNumber));
}
}
if(!required.isEmpty()) {
Collection<String> names = new ArrayList<>(required.size());
for(Descriptors.FieldDescriptor desc : required) {
names.add(desc.getName());
}
throw new ProtobufReadException(message.getDescriptorForType().getFullName(),
"Missing required fields: " + names.toString());
}
}
示例2: messageForFilter
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
private static <M extends Message, B extends Message.Builder> M messageForFilter(
ProtocolStringList filter,
Constructor<B> builderConstructor, Message wholeMessage)
throws InstantiationException,
IllegalAccessException,
InvocationTargetException {
final B builder = builderConstructor.newInstance();
final List<Descriptors.FieldDescriptor> fields = wholeMessage.getDescriptorForType()
.getFields();
for (Descriptors.FieldDescriptor field : fields) {
if (filter.contains(field.getFullName())) {
builder.setField(field, wholeMessage.getField(field));
}
}
@SuppressWarnings("unchecked")
// It's fine as the constructor is of {@code MessageCls.Builder} type.
final M result = (M) builder.build();
return result;
}
示例3: addMapField
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
private static void addMapField(final JsonObjectBuilder json, final Message msg, final Descriptors.FieldDescriptor field) {
final JsonObjectBuilder map = Json.createObjectBuilder();
final List<MapEntry<String, Object>> mapEntries = (List<MapEntry<String, Object>>) msg.getField(field);
mapEntries.forEach(entry -> {
final Object value = entry.getValue();
if (value instanceof Message) {
map.add(entry.getKey(), protobuMessageToJson((Message) value));
} else if (value instanceof String) {
map.add(entry.getKey(), (String) value);
} else if (value instanceof Integer) {
map.add(entry.getKey(), (Integer) value);
} else if (value instanceof Long) {
map.add(entry.getKey(), (Long) value);
} else if (value instanceof Double) {
map.add(entry.getKey(), (Double) value);
} else if (value instanceof Float) {
map.add(entry.getKey(), (Float) value);
} else if (value instanceof Boolean) {
map.add(entry.getKey(), (Boolean) value);
} else if (value instanceof ByteString) {
map.add(entry.getKey(), Base64.getEncoder().encodeToString(((ByteString) value).toByteArray()));
}
});
json.add(field.getName(), map);
}
示例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: print
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
private void print(final Message message,
final JavaPropsGenerator generator)
throws IOException {
for (final Map.Entry<Descriptors.FieldDescriptor, Object> field :
message.getAllFields().entrySet()) {
printField(field.getKey(), field.getValue(), generator);
}
printUnknownFields(message.getUnknownFields(), generator);
}
示例7: printField
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
public void printField(final Descriptors.FieldDescriptor field,
final Object value,
final Appendable output)
throws IOException {
final JavaPropsGenerator generator = new JavaPropsGenerator(output);
printField(field, value, generator);
}
示例8: printFieldToString
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
public String printFieldToString(final Descriptors.FieldDescriptor field,
final Object value) {
try {
final StringBuilder text = new StringBuilder();
printField(field, value, text);
return text.toString();
} catch (IOException e) {
throw new RuntimeException(
"Writing to a StringBuilder threw an IOException (should never " +
"happen).", e);
}
}
示例9: populate
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
/**
*
* @param rs
* @param builder
* @throws SQLException
*/
private void populate(ResultSet rs, Message.Builder builder)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();// 列个数
String columnLabel = null;// 列名
Object columnValue = null;// 列值
Descriptors.FieldDescriptor fieldDescriptor = null;
for (int i = 1; i <= columnCount; i++) {
columnLabel = metaData.getColumnLabel(i);
columnValue = rs.getObject(i);
if (columnValue == null)
continue;// 如果为空,继续下一个
fieldDescriptor = descriptor.findFieldByName(columnLabel);
if (fieldDescriptor == null)
continue;// 如果为空,继续下一个
// 转换为相应的类型 ,会自动将 date 类型转换为long
if (fieldDescriptor.getType().equals(FieldDescriptor.Type.ENUM)) {
columnValue = fieldDescriptor.getEnumType().findValueByNumber(
(int) columnValue);
} else {
columnValue = ConvertUtils.convert(columnValue, fieldDescriptor
.getDefaultValue().getClass());
}
builder.setField(fieldDescriptor, columnValue);
}
}
示例10: convert
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
public static SharedPreferences.Editor convert(Message message, SharedPreferences.Editor editor) {
Map<Descriptors.FieldDescriptor,Object> fields = message.getAllFields();
for (Descriptors.FieldDescriptor desc : fields.keySet()) {
Log.i(TAG,"processing \""+desc.getName()+"\" with value \""+ fields.get(desc)+"\" ");
switch (desc.getJavaType()) {
case INT:
editor.putInt(desc.getName(),(Integer)fields.get(desc));
break;
case LONG:
editor.putLong(desc.getName(),(Long)fields.get(desc));
break;
case FLOAT:
break;
case DOUBLE:
break;
case BOOLEAN:
editor.putBoolean(desc.getName(),(Boolean)fields.get(desc));
break;
case STRING:
editor.putString(desc.getName(),(String)fields.get(desc));
break;
case BYTE_STRING:
break;
case ENUM:
break;
case MESSAGE:
break;
}
}
return editor;
}
示例11: convertFromPrefs
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
public static Message.Builder convertFromPrefs(Message.Builder builder, SharedPreferences prefs) {
List<Descriptors.FieldDescriptor> fields = builder.getDescriptorForType().getFields();
for (Descriptors.FieldDescriptor desc : fields) {
Log.i(TAG,"processing \""+desc.getName()+"\"");
switch (desc.getJavaType()) {
case INT:
builder.setField(desc,prefs.getInt(desc.getName(),0));
break;
case LONG:
builder.setField(desc,prefs.getLong(desc.getName(),0L));
break;
case FLOAT:
break;
case DOUBLE:
break;
case BOOLEAN:
builder.setField(desc,prefs.getBoolean(desc.getName(),false));
break;
case STRING:
builder.setField(desc,prefs.getString(desc.getName(),""));
break;
case BYTE_STRING:
break;
case ENUM:
break;
case MESSAGE:
break;
}
}
return builder;
}
示例12: getValueByFieldNameOrNull
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
public Object getValueByFieldNameOrNull(MessageOrBuilder message, String fieldName, Object defaultValue){
Descriptors.FieldDescriptor field=message.getDescriptorForType().findFieldByName(fieldName);
if(field!=null && message.hasField(field)){
return message.getField(field);
}
return defaultValue;
}
示例13: maskOf
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
/**
* Creates a new instance of {@code FieldMask} basing on the target type
* {@link Descriptor descriptor} and field tags defined in the Protobuf message.
*
* @param typeDescriptor {@link Descriptor descriptor} of the type to create a mask for.
* @param fieldTags field tags to include into the mask.
* @return an instance of {@code FieldMask} for the target type with the fields specified.
*/
public static FieldMask maskOf(Descriptor typeDescriptor, int... fieldTags) {
if (fieldTags.length == 0) {
return FieldMask.getDefaultInstance();
}
final FieldMask.Builder result = FieldMask.newBuilder();
for (int fieldNumber : fieldTags) {
final Descriptors.FieldDescriptor field = typeDescriptor.findFieldByNumber(fieldNumber);
final String fieldPath = field.getFullName();
result.addPaths(fieldPath);
}
return result.build();
}
示例14: return_multiple_results_for_projection_batch_read_by_ids_with_field_mask
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
@Test
public void return_multiple_results_for_projection_batch_read_by_ids_with_field_mask() {
final List<Descriptors.FieldDescriptor> projectFields = Project.getDescriptor()
.getFields();
doCheckReadingCustomersByIdAndFieldMask(
projectFields.get(0)
.getFullName(), // ID
projectFields.get(1)
.getFullName()); // Name
}
示例15: retrieve_all_data_if_field_mask_is_not_set
import com.google.protobuf.Descriptors; //导入方法依赖的package包/类
@Test
public void retrieve_all_data_if_field_mask_is_not_set() {
final Stand stand = prepareStandWithAggregateRepo(createStandStorage());
final Customer sampleCustomer = getSampleCustomer();
final Version stateVersion = GivenVersion.withNumber(1);
stand.update(asEnvelope(sampleCustomer.getId(), sampleCustomer, stateVersion));
final Query customerQuery = requestFactory.query().all(Customer.class);
//noinspection OverlyComplexAnonymousInnerClass
final MemoizeQueryResponseObserver observer = new MemoizeQueryResponseObserver() {
@Override
public void onNext(QueryResponse value) {
super.onNext(value);
final List<Any> messages = value.getMessagesList();
assertFalse(messages.isEmpty());
final Customer customer = unpack(messages.get(0));
for (Descriptors.FieldDescriptor field : customer.getDescriptorForType()
.getFields()) {
assertTrue(customer.getField(field)
.equals(sampleCustomer.getField(field)));
}
}
};
stand.execute(customerQuery, observer);
verifyObserver(observer);
}