当前位置: 首页>>代码示例>>Java>>正文


Java MessageOrBuilder.getField方法代码示例

本文整理汇总了Java中com.google.protobuf.MessageOrBuilder.getField方法的典型用法代码示例。如果您正苦于以下问题:Java MessageOrBuilder.getField方法的具体用法?Java MessageOrBuilder.getField怎么用?Java MessageOrBuilder.getField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.protobuf.MessageOrBuilder的用法示例。


在下文中一共展示了MessageOrBuilder.getField方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getFieldIfPresent

import com.google.protobuf.MessageOrBuilder; //导入方法依赖的package包/类
@Nullable
public static <Type> Type getFieldIfPresent(
    MessageOrBuilder mob, FieldDescriptor fieldDesc, Class<Type> clazz) {
  if (mob.hasField(fieldDesc)) {
    Object fieldValue = null;
    try {
      fieldValue = mob.getField(fieldDesc);
      if (null == fieldValue) {
        return null;
      }

      if (fieldValue instanceof EnumValueDescriptor && clazz.isEnum()) {
        // Do some sanity checks and convert the EnumValueDescriptor into the (Type) class (which
        // has to be an enum)
        EnumValueDescriptor fieldEnumValue = (EnumValueDescriptor) fieldValue;
        if (clazz.getSimpleName().equals(fieldEnumValue.getType().getName())) {
          Type[] enumValues = clazz.getEnumConstants();
          if (fieldEnumValue.getIndex() >= 0 && fieldEnumValue.getIndex() < enumValues.length) {
            Type value = enumValues[fieldEnumValue.getIndex()];
            return value;
          }
        }
        throw new RuntimeException(
            String.format("Couldn't convert '%s' to class '%s'", fieldValue, clazz.getName()));
      }

      return clazz.cast(fieldValue);
    } catch (ClassCastException ex) {
      throw new RuntimeException(
          String.format(
              "Expected (%s) type, not (%s), for field '%s' of (%s)%s",
              clazz, fieldValue.getClass(), fieldDesc.getName(), mob.getClass(), getName(mob)),
          ex);
    }
  }
  return null;
}
 
开发者ID:googleapis,项目名称:api-compiler,代码行数:38,代码来源:ProtoHelpers.java

示例2: getFieldBoolean

import com.google.protobuf.MessageOrBuilder; //导入方法依赖的package包/类
public static boolean getFieldBoolean(
    MessageOrBuilder mob, String fieldName, boolean defaultValue) {
  boolean value = defaultValue;
  FieldDescriptor fieldDesc = mob.getDescriptorForType().findFieldByName(fieldName);
  if (null != fieldDesc) {
    if (mob.hasField(fieldDesc)) {
      Object fieldValue = mob.getField(fieldDesc);
      if (fieldValue instanceof Boolean) {
        value = (Boolean) fieldValue;
      }
    }
  }
  return value;
}
 
开发者ID:googleapis,项目名称:api-compiler,代码行数:15,代码来源:ProtoHelpers.java

示例3: printToString

import com.google.protobuf.MessageOrBuilder; //导入方法依赖的package包/类
/**
 * Converts a message into a string.
 */
public String printToString(MessageOrBuilder message) {
  StringBuilder result = new StringBuilder();
  for (FieldDescriptor field : getFieldsInNumberOrder(message.getDescriptorForType())) {

    // Skip empty fields.
    if ((field.isRepeated() && message.getRepeatedFieldCount(field) == 0)
        || (!field.isRepeated() && !message.hasField(field))) {
      continue;
    }

    // Normalize repeated and singleton fields.
    Object rawValue = message.getField(field);
    @SuppressWarnings("unchecked")
    List<Object> values =
        field.isMapField()
        ? sortMapEntries(field, rawValue)
        : field.isRepeated()
        ? (List<Object>) rawValue
        : ImmutableList.of(rawValue);

    // Print field values.
    for (Object value : values) {
      result.append(printFieldToString(field, value));
    }

  }
  return result.toString();
}
 
开发者ID:googleapis,项目名称:api-compiler,代码行数:32,代码来源:TextFormatForTest.java

示例4: getValueByFieldNameOrNull

import com.google.protobuf.MessageOrBuilder; //导入方法依赖的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;
}
 
开发者ID:taojoe,项目名称:protobuf-transformer,代码行数:8,代码来源:Transformer.java

示例5: printAny

import com.google.protobuf.MessageOrBuilder; //导入方法依赖的package包/类
/** Prints google.protobuf.Any */
private void printAny(MessageOrBuilder message) throws IOException {
  if (Any.getDefaultInstance().equals(message)) {
    generator.print("{}");
    return;
  }
  Descriptor descriptor = message.getDescriptorForType();
  FieldDescriptor typeUrlField = descriptor.findFieldByName("type_url");
  FieldDescriptor valueField = descriptor.findFieldByName("value");
  // Validates type of the message. Note that we can't just cast the message
  // to com.google.protobuf.Any because it might be a DynamicMessage.
  if (typeUrlField == null
      || valueField == null
      || typeUrlField.getType() != FieldDescriptor.Type.STRING
      || valueField.getType() != FieldDescriptor.Type.BYTES) {
    throw new InvalidProtocolBufferException("Invalid Any type.");
  }
  String typeUrl = (String) message.getField(typeUrlField);
  String typeName = getTypeName(typeUrl);
  Descriptor type = registry.find(typeName);
  if (type == null) {
    throw new InvalidProtocolBufferException("Cannot find type for url: " + typeUrl);
  }
  ByteString content = (ByteString) message.getField(valueField);
  Message contentMessage =
      DynamicMessage.getDefaultInstance(type).getParserForType().parseFrom(content);
  WellKnownTypePrinter printer = wellKnownTypePrinters.get(typeName);
  if (printer != null) {
    // If the type is one of the well-known types, we use a special
    // formatting.
    generator.print("{" + blankOrNewLine);
    generator.indent();
    generator.print("\"@type\":" + blankOrSpace + gson.toJson(typeUrl) + "," + blankOrNewLine);
    generator.print("\"value\":" + blankOrSpace);
    printer.print(this, contentMessage);
    generator.print(blankOrNewLine);
    generator.outdent();
    generator.print("}");
  } else {
    // Print the content message instead (with a "@type" field added).
    print(contentMessage, typeUrl);
  }
}
 
开发者ID:SeldonIO,项目名称:seldon-core,代码行数:44,代码来源:JsonFormat.java

示例6: getAttribute

import com.google.protobuf.MessageOrBuilder; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static <T> T getAttribute(final FieldDescriptor key, final MessageOrBuilder message) {
  return (T) message.getField(verifyFieldIsAttribute(key));
}
 
开发者ID:protobufel,项目名称:protobuf-el,代码行数:5,代码来源:MessageAdapter.java

示例7: getChild

import com.google.protobuf.MessageOrBuilder; //导入方法依赖的package包/类
public static Message getChild(final FieldDescriptor key, final MessageOrBuilder message) {
  return (Message) message.getField(verifyFieldIsChild(key));
}
 
开发者ID:protobufel,项目名称:protobuf-el,代码行数:4,代码来源:MessageAdapter.java

示例8: getField

import com.google.protobuf.MessageOrBuilder; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static <T> T getField(final FieldDescriptor key, final int index,
    final MessageOrBuilder message) {
  verifyIndex(key, index);
  return (T) (key.isRepeated() ? message.getRepeatedField(key, index) : message.getField(key));
}
 
开发者ID:protobufel,项目名称:protobuf-el,代码行数:7,代码来源:MessageAdapter.java


注:本文中的com.google.protobuf.MessageOrBuilder.getField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。