本文整理匯總了Java中com.google.protobuf.Descriptors.FieldDescriptor.getType方法的典型用法代碼示例。如果您正苦於以下問題:Java FieldDescriptor.getType方法的具體用法?Java FieldDescriptor.getType怎麽用?Java FieldDescriptor.getType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.protobuf.Descriptors.FieldDescriptor
的用法示例。
在下文中一共展示了FieldDescriptor.getType方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: convertType
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
/** Returns a GraphQLOutputType generated from a FieldDescriptor. */
static GraphQLOutputType convertType(FieldDescriptor fieldDescriptor) {
final GraphQLOutputType type;
if (fieldDescriptor.getType() == Type.MESSAGE) {
type = getReference(fieldDescriptor.getMessageType());
} else if (fieldDescriptor.getType() == Type.GROUP) {
type = getReference(fieldDescriptor.getMessageType());
} else if (fieldDescriptor.getType() == Type.ENUM) {
type = getReference(fieldDescriptor.getEnumType());
} else {
type = TYPE_MAP.get(fieldDescriptor.getType());
}
if (type == null) {
throw new RuntimeException("Unknown type: " + fieldDescriptor.getType());
}
if (fieldDescriptor.isRepeated()) {
return new GraphQLList(type);
} else {
return type;
}
}
示例2: writeTo
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
public void writeTo(final CodedOutputStream output) throws IOException {
final boolean isMessageSet =
getDescriptorForType().getOptions().getMessageSetWireFormat();
for (final Map.Entry<FieldDescriptor, Object> entry :
getAllFields().entrySet()) {
final FieldDescriptor field = entry.getKey();
final Object value = entry.getValue();
if (isMessageSet && field.isExtension() &&
field.getType() == FieldDescriptor.Type.MESSAGE &&
!field.isRepeated()) {
output.writeMessageSetExtension(field.getNumber(), (Message) value);
} else {
FieldSet.writeField(field, value, output);
}
}
final UnknownFieldSet unknownFields = getUnknownFields();
if (isMessageSet) {
unknownFields.writeAsMessageSetTo(output);
} else {
unknownFields.writeTo(output);
}
}
示例3: getSerializedSize
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
public int getSerializedSize() {
int size = memoizedSize;
if (size != -1) {
return size;
}
size = 0;
final boolean isMessageSet =
getDescriptorForType().getOptions().getMessageSetWireFormat();
for (final Map.Entry<FieldDescriptor, Object> entry :
getAllFields().entrySet()) {
final FieldDescriptor field = entry.getKey();
final Object value = entry.getValue();
if (isMessageSet && field.isExtension() &&
field.getType() == FieldDescriptor.Type.MESSAGE &&
!field.isRepeated()) {
size += CodedOutputStream.computeMessageSetExtensionSize(
field.getNumber(), (Message) value);
} else {
size += FieldSet.computeFieldSize(field, value);
}
}
final UnknownFieldSet unknownFields = getUnknownFields();
if (isMessageSet) {
size += unknownFields.getSerializedSizeAsMessageSet();
} else {
size += unknownFields.getSerializedSize();
}
memoizedSize = size;
return size;
}
示例4: printSingleField
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
private void printSingleField(FieldDescriptor field, Object value,
XMLStreamWriter generator) throws XMLStreamException, IOException {
if (field.isExtension()) {
generator.writeStartElement(EXTENSION_ELEMENT);
// 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.writeAttribute(EXTENSION_TYPE, field.getMessageType().getFullName());
} else {
// extensions will have '.' in them, while normal fields wont..
generator.writeAttribute(EXTENSION_TYPE, field.getFullName());
}
} else {
if (field.getType() == FieldDescriptor.Type.GROUP) {
// Groups must be serialized with their original capitalization.
generator.writeStartElement(field.getMessageType().getName());
} else {
generator.writeStartElement(field.getName());
}
}
// Done with the name, on to the value
printFieldValue(field, value, generator);
generator.writeEndElement();
}
示例5: getDefaultValue
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
/**
* Gets the default value for a field type. Note that we use proto3
* language defaults and ignore any default values set through the
* proto "default" option.
*/
private Object getDefaultValue(FieldDescriptor field, Message.Builder builder) {
switch (field.getType()) {
case INT32:
case SINT32:
case SFIXED32:
case UINT32:
case FIXED32:
return 0;
case INT64:
case SINT64:
case SFIXED64:
case UINT64:
case FIXED64:
return 0L;
case FLOAT:
return 0.0f;
case DOUBLE:
return 0.0;
case BOOL:
return false;
case STRING:
return "";
case BYTES:
return ByteString.EMPTY;
case ENUM:
return field.getEnumType().getValues().get(0);
case MESSAGE:
case GROUP:
return builder.newBuilderForField(field).getDefaultInstanceForType();
default:
throw new IllegalStateException("Invalid field type: " + field.getType());
}
}
示例6: printSingleField
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
private void printSingleField(FieldDescriptor field,
Object value,
JsonGenerator generator) throws IOException {
if (field.isExtension()) {
// 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.writeFieldName(field.getMessageType().getFullName());
} else {
// extensions will have '.' in them, while normal fields wont..
generator.writeFieldName(field.getFullName());
}
} else {
if (field.getType() == FieldDescriptor.Type.GROUP) {
// Groups must be serialized with their original capitalization.
generator.writeFieldName(field.getMessageType().getName());
} else {
generator.writeFieldName(field.getName());
}
}
// Done with the name, on to the value
if (field.isRepeated()) {
// Repeated field. Print each element.
generator.writeStartArray();
for (Iterator<?> iter = ((List<?>) value).iterator(); iter.hasNext();) {
printFieldValue(field, iter.next(), generator);
}
generator.writeEndArray();
} else {
printFieldValue(field, value, generator);
}
}
示例7: getFieldType
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
private GraphQLType getFieldType(FieldDescriptor field) {
if (field.getType() == FieldDescriptor.Type.MESSAGE
|| field.getType() == FieldDescriptor.Type.GROUP) {
return new GraphQLTypeReference(getReferenceName(field.getMessageType()));
}
if (field.getType() == FieldDescriptor.Type.ENUM) {
return new GraphQLTypeReference(ProtoToGql.getReferenceName(field.getEnumType()));
}
GraphQLType type = ProtoToGql.convertType(field);
if (type instanceof GraphQLList) {
return ((GraphQLList) type).getWrappedType();
}
return type;
}
示例8: 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");
}
示例9: 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());
}
}
示例10: printFieldValue
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
private void printFieldValue(FieldDescriptor field, Object value, XMLStreamWriter generator)
throws XMLStreamException, IOException {
// TODO: look at using field.getType().getJavaType(), to simplify this..
switch (field.getType()) {
case INT32:
case INT64:
case SINT32:
case SINT64:
case SFIXED32:
case SFIXED64:
case FLOAT:
case DOUBLE:
case BOOL:
case STRING:
// Good old toString() does what we want for these types.
generator.writeCharacters(value.toString());
break;
case UINT32:
case FIXED32:
generator.writeCharacters(unsignedToString((Integer) value));
break;
case UINT64:
case FIXED64:
generator.writeCharacters(unsignedToString((Long) value));
break;
case BYTES: {
// Here we break with JsonFormat - since there is an issue with non-utf8 bytes..
generator.writeCharacters(escapeBytes((ByteString)value));
break;
}
case ENUM: {
generator.writeCharacters(((EnumValueDescriptor) value).getName());
break;
}
case MESSAGE:
case GROUP:
printMessage((Message) value, generator);
break;
}
}
示例11: printSingleField
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
private void printSingleField(FieldDescriptor field, Object value, XmlGenerator generator) throws IOException {
if (field.isExtension()) {
generator.print("<extension type=\"");
// 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(">");
}
printFieldValue(field, value, generator);
if (!field.isExtension()) {
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(">");
} else {
generator.print("</extension>");
}
}
示例12: printFieldValue
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
private void printFieldValue(FieldDescriptor field, Object value, XmlGenerator generator) throws IOException {
switch (field.getType()) {
case INT32:
case INT64:
case SINT32:
case SINT64:
case SFIXED32:
case SFIXED64:
case FLOAT:
case DOUBLE:
case BOOL:
// Good old toString() does what we want for these types.
generator.print(value.toString());
break;
case UINT32:
case FIXED32:
generator.print(unsignedToString((Integer) value));
break;
case UINT64:
case FIXED64:
generator.print(unsignedToString((Long) value));
break;
case STRING:
generator.print(escapeText((String) value));
break;
case BYTES: {
generator.print(escapeBytes((ByteString) value));
break;
}
case ENUM: {
generator.print(((EnumValueDescriptor) value).getName());
break;
}
case MESSAGE:
case GROUP:
print((Message) value, generator);
break;
}
}
示例13: printFieldValue
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
private void printFieldValue(FieldDescriptor field, Object value, JsonGenerator generator) throws IOException {
// TODO: look at using field.getType().getJavaType(), to simplify this..
switch (field.getType()) {
case INT32:
case SINT32:
case SFIXED32:
generator.writeNumber((Integer)value);
break;
case INT64:
case SINT64:
case SFIXED64:
generator.writeNumber((Long)value);
break;
case FLOAT:
generator.writeNumber((Float)value);
break;
case DOUBLE:
generator.writeNumber((Double)value);
break;
case BOOL:
// Good old toString() does what we want for these types.
generator.writeBoolean((Boolean)value);
break;
case UINT32:
case FIXED32:
generator.writeNumber(Integer.toUnsignedLong((Integer)value));
break;
case UINT64:
case FIXED64:
generator.writeNumber(unsignedLong((Long) value));
break;
case STRING:
generator.writeString((String) value);
break;
case BYTES: {
// Here we break with JsonFormat - since there is an issue with non-utf8 bytes..
generator.writeBinary(((ByteString)value).toByteArray());
break;
}
case ENUM: {
generator.writeString(((EnumValueDescriptor) value).getName());
break;
}
case MESSAGE:
case GROUP:
generator.writeStartObject();
printMessage((Message) value, generator);
generator.writeEndObject();
break;
}
}
示例14: printSingleField
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
private void printSingleField(FieldDescriptor field,
Object value,
HtmlGenerator generator) throws IOException {
if (field.isExtension()) {
generator.print("[<span style=\"");
generator.print(FIELD_NAME_STYLE);
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("</span>]");
} else {
generator.print("<span style=\"");
generator.print(FIELD_NAME_STYLE);
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("</span>");
}
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
generator.print(" <span style=\"color: red;\">{</span><br/>");
generator.indent();
} else {
generator.print(": ");
}
printFieldValue(field, value, generator);
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
generator.outdent();
generator.print("<span style=\"color: red;\">}</span>");
}
generator.print("<br/>");
}
示例15: printFieldValue
import com.google.protobuf.Descriptors.FieldDescriptor; //導入方法依賴的package包/類
private static void printFieldValue(final FieldDescriptor field,
final Object value,
final TextGenerator generator)
throws IOException {
switch (field.getType()) {
case INT32:
case INT64:
case SINT32:
case SINT64:
case SFIXED32:
case SFIXED64:
case FLOAT:
case DOUBLE:
case BOOL:
// Good old toString() does what we want for these types.
generator.print(value.toString());
break;
case UINT32:
case FIXED32:
generator.print(unsignedToString((Integer) value));
break;
case UINT64:
case FIXED64:
generator.print(unsignedToString((Long) value));
break;
case STRING:
generator.print("\"");
generator.print(escapeText((String) value));
generator.print("\"");
break;
case BYTES:
generator.print("\"");
generator.print(escapeBytes((ByteString) value));
generator.print("\"");
break;
case ENUM:
generator.print(((EnumValueDescriptor) value).getName());
break;
case MESSAGE:
case GROUP:
print((Message) value, generator);
break;
}
}