本文整理匯總了Java中com.google.protobuf.Descriptors.FieldDescriptor.getJavaType方法的典型用法代碼示例。如果您正苦於以下問題:Java FieldDescriptor.getJavaType方法的具體用法?Java FieldDescriptor.getJavaType怎麽用?Java FieldDescriptor.getJavaType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.protobuf.Descriptors.FieldDescriptor
的用法示例。
在下文中一共展示了FieldDescriptor.getJavaType方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getField
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
@Override
public Object getField(final FieldDescriptor field) {
if (field.isExtension()) {
verifyContainingType(field);
final Object value = extensions.getField(field);
if (value == null) {
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
// Lacking an ExtensionRegistry, we have no way to determine the
// extension's real type, so we return a DynamicMessage.
return DynamicMessage.getDefaultInstance(field.getMessageType());
} else {
return field.getDefaultValue();
}
} else {
return value;
}
} else {
return super.getField(field);
}
}
示例2: getExtension
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
/** Get the value of an extension. */
@SuppressWarnings("unchecked")
public final <Type> Type getExtension(
final GeneratedExtension<MessageType, Type> extension) {
verifyExtensionContainingType(extension);
FieldDescriptor descriptor = extension.getDescriptor();
final Object value = extensions.getField(descriptor);
if (value == null) {
if (descriptor.isRepeated()) {
return (Type) Collections.emptyList();
} else if (descriptor.getJavaType() ==
FieldDescriptor.JavaType.MESSAGE) {
return (Type) extension.getMessageDefaultInstance();
} else {
return (Type) extension.fromReflectionType(
descriptor.getDefaultValue());
}
} else {
return (Type) extension.fromReflectionType(value);
}
}
示例3: add
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
/** Add a message-type extension to the registry by descriptor. */
public void add(final FieldDescriptor type, final Message defaultInstance) {
if (type.getJavaType() != FieldDescriptor.JavaType.MESSAGE) {
throw new IllegalArgumentException(
"ExtensionRegistry.add() provided a default instance for a " +
"non-message extension.");
}
add(new ExtensionInfo(type, defaultInstance));
}
示例4: newBuilderForField
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
public Builder newBuilderForField(FieldDescriptor field) {
verifyContainingType(field);
if (field.getJavaType() != FieldDescriptor.JavaType.MESSAGE) {
throw new IllegalArgumentException(
"newBuilderForField is only valid for fields with message type.");
}
return new Builder(field.getMessageType());
}
示例5: getField
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
public Object getField(FieldDescriptor field) {
verifyContainingType(field);
Object result = fields.getField(field);
if (result == null) {
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
result = getDefaultInstance(field.getMessageType());
} else {
result = field.getDefaultValue();
}
}
return result;
}
示例6: isInitialized
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
@Override
public boolean isInitialized() {
for (final FieldDescriptor field : getDescriptorForType().getFields()) {
// Check that all required fields are present.
if (field.isRequired()) {
if (!hasField(field)) {
return false;
}
}
// Check that embedded messages are initialized.
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
if (field.isRepeated()) {
@SuppressWarnings("unchecked") final
List<Message> messageList = (List<Message>) getField(field);
for (final Message element : messageList) {
if (!element.isInitialized()) {
return false;
}
}
} else {
if (hasField(field) && !((Message) getField(field)).isInitialized()) {
return false;
}
}
}
}
return true;
}
示例7: internalInit
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
/** For use by generated code only. */
public void internalInit(final FieldDescriptor descriptor,
final Class type) {
if (this.descriptor != null) {
throw new IllegalStateException("Already initialized.");
}
if (!descriptor.isExtension()) {
throw new IllegalArgumentException(
"GeneratedExtension given a regular (non-extension) field.");
}
this.descriptor = descriptor;
this.type = type;
switch (descriptor.getJavaType()) {
case MESSAGE:
enumValueOf = null;
enumGetValueDescriptor = null;
messageDefaultInstance =
(Message) invokeOrDie(getMethodOrDie(type, "getDefaultInstance"),
null);
if (messageDefaultInstance == null) {
throw new IllegalStateException(
type.getName() + ".getDefaultInstance() returned null.");
}
break;
case ENUM:
enumValueOf = getMethodOrDie(type, "valueOf",
EnumValueDescriptor.class);
enumGetValueDescriptor = getMethodOrDie(type, "getValueDescriptor");
messageDefaultInstance = null;
break;
default:
enumValueOf = null;
enumGetValueDescriptor = null;
messageDefaultInstance = null;
break;
}
}
示例8: FieldAccessorTable
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
/**
* Construct a FieldAccessorTable for a particular message class. Only
* one FieldAccessorTable should ever be constructed per class.
*
* @param descriptor The type's descriptor.
* @param camelCaseNames The camelcase names of all fields in the message.
* These are used to derive the accessor method names.
* @param messageClass The message type.
* @param builderClass The builder type.
*/
public FieldAccessorTable(
final Descriptor descriptor,
final String[] camelCaseNames,
final Class<? extends GeneratedMessage> messageClass,
final Class<? extends Builder> builderClass) {
this.descriptor = descriptor;
fields = new FieldAccessor[descriptor.getFields().size()];
for (int i = 0; i < fields.length; i++) {
final FieldDescriptor field = descriptor.getFields().get(i);
if (field.isRepeated()) {
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
fields[i] = new RepeatedMessageFieldAccessor(
field, camelCaseNames[i], messageClass, builderClass);
} else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM) {
fields[i] = new RepeatedEnumFieldAccessor(
field, camelCaseNames[i], messageClass, builderClass);
} else {
fields[i] = new RepeatedFieldAccessor(
field, camelCaseNames[i], messageClass, builderClass);
}
} else {
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
fields[i] = new SingularMessageFieldAccessor(
field, camelCaseNames[i], messageClass, builderClass);
} else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM) {
fields[i] = new SingularEnumFieldAccessor(
field, camelCaseNames[i], messageClass, builderClass);
} else {
fields[i] = new SingularFieldAccessor(
field, camelCaseNames[i], messageClass, builderClass);
}
}
}
}
示例9: add
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
/** Add a non-message-type extension to the registry by descriptor. */
public void add(final FieldDescriptor type) {
if (type.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
throw new IllegalArgumentException(
"ExtensionRegistry.add() must be provided a default instance when " +
"adding an embedded message extension.");
}
add(new ExtensionInfo(type, null));
}
示例10: handleValue
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
private Object handleValue(Tokenizer tokenizer,
ExtensionRegistry extensionRegistry,
Message.Builder builder,
FieldDescriptor field,
ExtensionRegistry.ExtensionInfo extension) throws ParseException {
Object value = null;
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
value = handleObject(tokenizer, extensionRegistry, builder, field, extension);
} else {
value = handlePrimitive(tokenizer, field);
}
return value;
}
示例11: printSingleField
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
private static void printSingleField(final FieldDescriptor field,
final Object value,
final TextGenerator generator)
throws IOException {
if (field.isExtension()) {
generator.print("[");
// We special-case MessageSet elements for compatibility with proto1.
if (field.getContainingType().getOptions().getMessageSetWireFormat()
&& (field.getType() == FieldDescriptor.Type.MESSAGE)
&& (field.isOptional())
// object equality
&& (field.getExtensionScope() == field.getMessageType())) {
generator.print(field.getMessageType().getFullName());
} else {
generator.print(field.getFullName());
}
generator.print("]");
} else {
if (field.getType() == FieldDescriptor.Type.GROUP) {
// Groups must be serialized with their original capitalization.
generator.print(field.getMessageType().getName());
} else {
generator.print(field.getName());
}
}
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
generator.print(" {\n");
generator.indent();
} else {
generator.print(": ");
}
printFieldValue(field, value, generator);
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
generator.outdent();
generator.print("}");
}
generator.print("\n");
}
示例12: mergeFrom
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
public BuilderType mergeFrom(final Message other) {
if (other.getDescriptorForType() != getDescriptorForType()) {
throw new IllegalArgumentException(
"mergeFrom(Message) can only merge messages of the same type.");
}
// Note: We don't attempt to verify that other's fields have valid
// types. Doing so would be a losing battle. We'd have to verify
// all sub-messages as well, and we'd have to make copies of all of
// them to insure that they don't change after verification (since
// the Message interface itself cannot enforce immutability of
// implementations).
// TODO(kenton): Provide a function somewhere called makeDeepCopy()
// which allows people to make secure deep copies of messages.
for (final Map.Entry<FieldDescriptor, Object> entry :
other.getAllFields().entrySet()) {
final FieldDescriptor field = entry.getKey();
if (field.isRepeated()) {
for (final Object element : (List)entry.getValue()) {
addRepeatedField(field, element);
}
} else if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
final Message existingValue = (Message)getField(field);
if (existingValue == existingValue.getDefaultInstanceForType()) {
setField(field, entry.getValue());
} else {
setField(field,
existingValue.newBuilderForType()
.mergeFrom(existingValue)
.mergeFrom((Message)entry.getValue())
.build());
}
} else {
setField(field, entry.getValue());
}
}
mergeUnknownFields(other.getUnknownFields());
return (BuilderType) this;
}
示例13: parseFieldValue
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
private Object parseFieldValue(FieldDescriptor field, JsonElement json, Message.Builder builder)
throws InvalidProtocolBufferException {
if (json instanceof JsonNull) {
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE
&& field.getMessageType().getFullName().equals(Value.getDescriptor().getFullName())) {
// For every other type, "null" means absence, but for the special
// Value message, it means the "null_value" field has been set.
Value value = Value.newBuilder().setNullValueValue(0).build();
return builder.newBuilderForField(field).mergeFrom(value.toByteString()).build();
} else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM
&& field.getEnumType().getFullName().equals(NullValue.getDescriptor().getFullName())) {
// If the type of the field is a NullValue, then the value should be explicitly set.
return field.getEnumType().findValueByNumber(0);
}
return null;
}
switch (field.getType()) {
case INT32:
case SINT32:
case SFIXED32:
return parseInt32(json);
case INT64:
case SINT64:
case SFIXED64:
return parseInt64(json);
case BOOL:
return parseBool(json);
case FLOAT:
return parseFloat(json);
case DOUBLE:
return parseDouble(json);
case UINT32:
case FIXED32:
return parseUint32(json);
case UINT64:
case FIXED64:
return parseUint64(json);
case STRING:
return parseString(json);
case BYTES:
return parseBytes(json);
case ENUM:
return parseEnum(field.getEnumType(), json);
case MESSAGE:
case GROUP:
if (currentDepth >= recursionLimit) {
throw new InvalidProtocolBufferException("Hit recursion limit.");
}
++currentDepth;
Message.Builder subBuilder = builder.newBuilderForField(field);
merge(json, subBuilder);
--currentDepth;
return subBuilder.build();
default:
throw new InvalidProtocolBufferException("Invalid field type: " + field.getType());
}
}
示例14: printSingleField
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
private void printSingleField(FieldDescriptor field,
Object value,
JsonGenerator generator) throws IOException {
if (field.isExtension()) {
generator.print("\"");
// We special-case MessageSet elements for compatibility with proto1.
if (field.getContainingType().getOptions().getMessageSetWireFormat()
&& (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional())
// object equality
&& (field.getExtensionScope() == field.getMessageType())) {
generator.print(field.getMessageType().getFullName());
} else {
generator.print(field.getFullName());
}
generator.print("\"");
} else {
generator.print("\"");
if (field.getType() == FieldDescriptor.Type.GROUP) {
// Groups must be serialized with their original capitalization.
generator.print(field.getMessageType().getName());
} else {
generator.print(field.getName());
}
generator.print("\"");
}
// Done with the name, on to the value
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
generator.print(": ");
generator.indent();
} else {
generator.print(": ");
}
if (field.isRepeated()) {
// Repeated field. Print each element.
generator.print("[");
for (Iterator<?> iter = ((List<?>) value).iterator(); iter.hasNext();) {
printFieldValue(field, iter.next(), generator);
if (iter.hasNext()) {
generator.print(",");
}
}
generator.print("]");
} else {
printFieldValue(field, value, generator);
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
generator.outdent();
}
}
}