本文整理汇总了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;
}
示例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;
}
示例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();
}
示例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;
}
示例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());
}
}
}