Package com.google.protobuf
Class Descriptors.FieldDescriptor
- java.lang.Object
-
- com.google.protobuf.Descriptors.GenericDescriptor
-
- com.google.protobuf.Descriptors.FieldDescriptor
-
- All Implemented Interfaces:
FieldSet.FieldDescriptorLite<Descriptors.FieldDescriptor>,java.lang.Comparable<Descriptors.FieldDescriptor>
- Enclosing class:
- Descriptors
public static final class Descriptors.FieldDescriptor extends Descriptors.GenericDescriptor implements java.lang.Comparable<Descriptors.FieldDescriptor>, FieldSet.FieldDescriptorLite<Descriptors.FieldDescriptor>
Describes a field of a message type.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDescriptors.FieldDescriptor.JavaTypestatic classDescriptors.FieldDescriptor.Type
-
Field Summary
Fields Modifier and Type Field Description private Descriptors.OneofDescriptorcontainingOneofprivate Descriptors.DescriptorcontainingTypeprivate java.lang.ObjectdefaultValueprivate Descriptors.EnumDescriptorenumTypeprivate Descriptors.DescriptorextensionScopeprivate Descriptors.FileDescriptorfileprivate java.lang.StringfullNameprivate intindexprivate booleanisProto3Optionalprivate java.lang.StringjsonNameprivate Descriptors.DescriptormessageTypeprivate static Descriptors.NumberGetter<Descriptors.FieldDescriptor>NUMBER_GETTERprivate DescriptorProtos.FieldOptionsoptionsprivate DescriptorProtos.FieldDescriptorProtoprotoprivate static WireFormat.FieldType[]tableprivate Descriptors.FieldDescriptor.Typetype-
Fields inherited from class com.google.protobuf.Descriptors.GenericDescriptor
features, parent
-
-
Constructor Summary
Constructors Modifier Constructor Description privateFieldDescriptor(DescriptorProtos.FieldDescriptorProto proto, Descriptors.FileDescriptor file, Descriptors.Descriptor parent, int index, boolean isExtension)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(Descriptors.FieldDescriptor other)Compare with anotherFieldDescriptor.private voidcrossLink()Look up and cross-link all field types, etc.private static java.lang.StringfieldNameToJsonName(java.lang.String name)Descriptors.OneofDescriptorgetContainingOneof()Get the field's containing oneof.Descriptors.DescriptorgetContainingType()Get the field's containing type.java.lang.ObjectgetDefaultValue()Returns the field's default value.Descriptors.EnumDescriptorgetEnumType()For enum fields, gets the field's type.Descriptors.DescriptorgetExtensionScope()For extensions defined nested within message types, gets the outer type.Descriptors.FileDescriptorgetFile()Get theFileDescriptorcontaining this descriptor.java.lang.StringgetFullName()Get the field's fully-qualified name.intgetIndex()Get the index of this descriptor within its parent.Descriptors.FieldDescriptor.JavaTypegetJavaType()Get the field's java type.java.lang.StringgetJsonName()Get the JSON name of this field.WireFormat.JavaTypegetLiteJavaType()For internal use only.WireFormat.FieldTypegetLiteType()For internal use only.Descriptors.DescriptorgetMessageType()For embedded message and group fields, gets the field's type.java.lang.StringgetName()Get the field's unqualified name.intgetNumber()Get the field's number.DescriptorProtos.FieldOptionsgetOptions()Get theFieldOptions, defined indescriptor.proto.Descriptors.OneofDescriptorgetRealContainingOneof()Get the field's containing oneof, only if non-synthetic.Descriptors.FieldDescriptor.TypegetType()Get the field's declared type.booleanhasDefaultValue()Returns true if the field had an explicitly-defined default value.(package private) booleanhasInferredLegacyProtoFeatures()(package private) booleanhasOptionalKeyword()Returns true if this field was syntactically written with "optional" in the .proto file.booleanhasPresence()Returns true if this field tracks presence, ie.(package private) DescriptorProtos.FeatureSetinferLegacyProtoFeatures()MessageLite.BuilderinternalMergeFrom(MessageLite.Builder to, MessageLite from)For internal use only.booleanisExtension()Is this field an extension?(package private) booleanisGroupLike()Returns true if this field is structured like the synthetic field of a proto2 group.booleanisMapField()booleanisOptional()Is this field declared optional?booleanisPackable()Can this field be packed? That is, is it a repeated primitive field?booleanisPacked()Does this field have the[packed = true]option or is this field packable in proto3 and not explicitly set to unpacked?booleanisRepeated()Is this field declared repeated?booleanisRequired()Is this field declared required?booleanlegacyEnumFieldTreatedAsClosed()Determines if the given enum field is treated as closed based on legacy non-conformant behavior.booleanneedsUtf8Check()For internal use only.private voidresolveAllFeatures()SeeFileDescriptor#resolveAllFeatures.private voidsetProto(DescriptorProtos.FieldDescriptorProto proto)DescriptorProtos.FieldDescriptorPrototoProto()Convert the descriptor to its protocol message representation.java.lang.StringtoString()(package private) voidvalidateFeatures()-
Methods inherited from class com.google.protobuf.Descriptors.GenericDescriptor
getFeatures, resolveFeatures
-
-
-
-
Field Detail
-
NUMBER_GETTER
private static final Descriptors.NumberGetter<Descriptors.FieldDescriptor> NUMBER_GETTER
-
table
private static final WireFormat.FieldType[] table
-
index
private final int index
-
proto
private DescriptorProtos.FieldDescriptorProto proto
-
options
private volatile DescriptorProtos.FieldOptions options
-
fullName
private final java.lang.String fullName
-
jsonName
private java.lang.String jsonName
-
file
private final Descriptors.FileDescriptor file
-
extensionScope
private final Descriptors.Descriptor extensionScope
-
isProto3Optional
private final boolean isProto3Optional
-
type
private Descriptors.FieldDescriptor.Type type
-
containingType
private Descriptors.Descriptor containingType
-
messageType
private Descriptors.Descriptor messageType
-
containingOneof
private Descriptors.OneofDescriptor containingOneof
-
enumType
private Descriptors.EnumDescriptor enumType
-
defaultValue
private java.lang.Object defaultValue
-
-
Constructor Detail
-
FieldDescriptor
private FieldDescriptor(DescriptorProtos.FieldDescriptorProto proto, Descriptors.FileDescriptor file, Descriptors.Descriptor parent, int index, boolean isExtension) throws Descriptors.DescriptorValidationException
-
-
Method Detail
-
getIndex
public int getIndex()
Get the index of this descriptor within its parent.- See Also:
Descriptors.Descriptor.getIndex()
-
toProto
public DescriptorProtos.FieldDescriptorProto toProto()
Convert the descriptor to its protocol message representation.- Specified by:
toProtoin classDescriptors.GenericDescriptor
-
getName
public java.lang.String getName()
Get the field's unqualified name.- Specified by:
getNamein classDescriptors.GenericDescriptor
-
getNumber
public int getNumber()
Get the field's number.- Specified by:
getNumberin interfaceFieldSet.FieldDescriptorLite<Descriptors.FieldDescriptor>
-
getFullName
public java.lang.String getFullName()
Get the field's fully-qualified name.- Specified by:
getFullNamein classDescriptors.GenericDescriptor- See Also:
Descriptors.Descriptor.getFullName()
-
getJsonName
public java.lang.String getJsonName()
Get the JSON name of this field.
-
getJavaType
public Descriptors.FieldDescriptor.JavaType getJavaType()
Get the field's java type. This is just for convenience. EveryFieldDescriptorProto.Typemaps to exactly one Java type.
-
getLiteJavaType
public WireFormat.JavaType getLiteJavaType()
For internal use only.- Specified by:
getLiteJavaTypein interfaceFieldSet.FieldDescriptorLite<Descriptors.FieldDescriptor>
-
getFile
public Descriptors.FileDescriptor getFile()
Get theFileDescriptorcontaining this descriptor.- Specified by:
getFilein classDescriptors.GenericDescriptor
-
getType
public Descriptors.FieldDescriptor.Type getType()
Get the field's declared type.
-
getLiteType
public WireFormat.FieldType getLiteType()
For internal use only.- Specified by:
getLiteTypein interfaceFieldSet.FieldDescriptorLite<Descriptors.FieldDescriptor>
-
needsUtf8Check
public boolean needsUtf8Check()
For internal use only.
-
isMapField
public boolean isMapField()
-
isRequired
public boolean isRequired()
Is this field declared required?
-
isOptional
public boolean isOptional()
Is this field declared optional?
-
isRepeated
public boolean isRepeated()
Is this field declared repeated?- Specified by:
isRepeatedin interfaceFieldSet.FieldDescriptorLite<Descriptors.FieldDescriptor>
-
isPacked
public boolean isPacked()
Does this field have the[packed = true]option or is this field packable in proto3 and not explicitly set to unpacked?- Specified by:
isPackedin interfaceFieldSet.FieldDescriptorLite<Descriptors.FieldDescriptor>
-
isPackable
public boolean isPackable()
Can this field be packed? That is, is it a repeated primitive field?
-
hasDefaultValue
public boolean hasDefaultValue()
Returns true if the field had an explicitly-defined default value.
-
getDefaultValue
public java.lang.Object getDefaultValue()
Returns the field's default value. Valid for all types except for messages and groups. For all other types, the object returned is of the same class that would returned by Message.getField(this).
-
getOptions
public DescriptorProtos.FieldOptions getOptions()
Get theFieldOptions, defined indescriptor.proto.
-
isExtension
public boolean isExtension()
Is this field an extension?
-
getContainingType
public Descriptors.Descriptor getContainingType()
Get the field's containing type. For extensions, this is the type being extended, not the location where the extension was defined. SeegetExtensionScope().
-
getContainingOneof
public Descriptors.OneofDescriptor getContainingOneof()
Get the field's containing oneof.
-
getRealContainingOneof
public Descriptors.OneofDescriptor getRealContainingOneof()
Get the field's containing oneof, only if non-synthetic.
-
hasOptionalKeyword
boolean hasOptionalKeyword()
Returns true if this field was syntactically written with "optional" in the .proto file. Excludes singular proto3 fields that do not have a label.
-
hasPresence
public boolean hasPresence()
Returns true if this field tracks presence, ie. does the field distinguish between "unset" and "present with default value."This includes required, optional, and oneof fields. It excludes maps, repeated fields, and singular proto3 fields without "optional".
For fields where hasPresence() == true, the return value of msg.hasField() is semantically meaningful.
-
isGroupLike
boolean isGroupLike()
Returns true if this field is structured like the synthetic field of a proto2 group. This allows us to expand our treatment of delimited fields without breaking proto2 files that have been upgraded to editions.
-
getExtensionScope
public Descriptors.Descriptor getExtensionScope()
For extensions defined nested within message types, gets the outer type. Not valid for non-extension fields. For example, consider this.protofile:message Foo { extensions 1000 to max; } extend Foo { optional int32 baz = 1234; } message Bar { extend Foo { optional int32 moo = 4321; } }Bothbaz's andmoo's containing type isFoo. However,baz's extension scope isnullwhilemoo's extension scope isBar.
-
getMessageType
public Descriptors.Descriptor getMessageType()
For embedded message and group fields, gets the field's type.
-
getEnumType
public Descriptors.EnumDescriptor getEnumType()
For enum fields, gets the field's type.- Specified by:
getEnumTypein interfaceFieldSet.FieldDescriptorLite<Descriptors.FieldDescriptor>
-
legacyEnumFieldTreatedAsClosed
public boolean legacyEnumFieldTreatedAsClosed()
Determines if the given enum field is treated as closed based on legacy non-conformant behavior.Conformant behavior determines closedness based on the enum and can be queried using
EnumDescriptor.isClosed().Some runtimes currently have a quirk where non-closed enums are treated as closed when used as the type of fields defined in a `syntax = proto2;` file. This quirk is not present in all runtimes; as of writing, we know that:
- C++, Java, and C++-based Python share this quirk.
- UPB and UPB-based Python do not.
- PHP and Ruby treat all enums as open regardless of declaration.
Care should be taken when using this function to respect the target runtime's enum handling quirks.
-
compareTo
public int compareTo(Descriptors.FieldDescriptor other)
Compare with anotherFieldDescriptor. This orders fields in "canonical" order, which simply means ascending order by field number.othermust be a field of the same type. That is,getContainingType()must return the sameDescriptorfor both fields.- Specified by:
compareToin interfacejava.lang.Comparable<Descriptors.FieldDescriptor>- Returns:
- negative, zero, or positive if
thisis less than, equal to, or greater thanother, respectively
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
fieldNameToJsonName
private static java.lang.String fieldNameToJsonName(java.lang.String name)
-
resolveAllFeatures
private void resolveAllFeatures() throws Descriptors.DescriptorValidationExceptionSeeFileDescriptor#resolveAllFeatures.
-
inferLegacyProtoFeatures
DescriptorProtos.FeatureSet inferLegacyProtoFeatures()
- Overrides:
inferLegacyProtoFeaturesin classDescriptors.GenericDescriptor
-
hasInferredLegacyProtoFeatures
boolean hasInferredLegacyProtoFeatures()
- Overrides:
hasInferredLegacyProtoFeaturesin classDescriptors.GenericDescriptor
-
validateFeatures
void validateFeatures() throws Descriptors.DescriptorValidationException- Overrides:
validateFeaturesin classDescriptors.GenericDescriptor- Throws:
Descriptors.DescriptorValidationException
-
crossLink
private void crossLink() throws Descriptors.DescriptorValidationExceptionLook up and cross-link all field types, etc.
-
setProto
private void setProto(DescriptorProtos.FieldDescriptorProto proto) throws Descriptors.DescriptorValidationException
-
internalMergeFrom
public MessageLite.Builder internalMergeFrom(MessageLite.Builder to, MessageLite from)
For internal use only. This is to satisfy the FieldDescriptorLite interface.- Specified by:
internalMergeFromin interfaceFieldSet.FieldDescriptorLite<Descriptors.FieldDescriptor>
-
-