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


Java MessageOrBuilder.hasField方法代码示例

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


在下文中一共展示了MessageOrBuilder.hasField方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: ensureAllFields

import com.google.protobuf.MessageOrBuilder; //导入方法依赖的package包/类
/**
 * Checks that all possible fields for this message are set.
 * If a field is of String type, it will also ensure it is != ""
 *
 * This method will throw an exception if a field is missing, else does nothing.
 *
 * @param message the message object to check
 * @param errorHeader an optional prefix to the exception string
 * @param excludes a list of excluded field names
 * @throws Exception
 */
static public void ensureAllFields(MessageOrBuilder message, String errorHeader, Set<String> excludes) throws Exception {
    for (FieldDescriptor f : message.getDescriptorForType().getFields()) {
        if ((!message.hasField(f) || 
                (f.getJavaType().equals(JavaType.STRING) && message.getField(f).equals("")) ) 
            && !excludes.contains(f.getName()) ) {
                if (errorHeader!=null && !errorHeader.equals("")) {
                    errorHeader+=" ";
                }
                throw new Exception(errorHeader+"Missing field: "+f.getName());
        }
    }
}
 
开发者ID:rhlabs,项目名称:louie,代码行数:24,代码来源:PBUtils.java


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