Skip to content

Commit

Permalink
Lazily resolve features for proto2 and proto3 for compatibility with …
Browse files Browse the repository at this point in the history
…old open source gencode that does not invoke feature resolution from gencode static init. (#16907)

PiperOrigin-RevId: 634804242
  • Loading branch information
zhangskz authored May 21, 2024
1 parent 0a05aa8 commit 4483c6b
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions java/core/src/main/java/com/google/protobuf/Descriptors.java
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ public Type getType() {
// (custom options) into unknown fields.
if (type == Type.MESSAGE
&& this.features != null
&& this.features.getMessageEncoding() == FeatureSet.MessageEncoding.DELIMITED) {
&& getFeatures().getMessageEncoding() == FeatureSet.MessageEncoding.DELIMITED) {
return Type.GROUP;
}
return type;
Expand All @@ -1319,13 +1319,13 @@ public boolean needsUtf8Check() {
// Always enforce strict UTF-8 checking for map fields.
return true;
}
if (this.features
if (getFeatures()
.getExtension(JavaFeaturesProto.java_)
.getUtf8Validation()
.equals(JavaFeatures.Utf8Validation.VERIFY)) {
return true;
}
return this.features.getUtf8Validation().equals(FeatureSet.Utf8Validation.VERIFY);
return getFeatures().getUtf8Validation().equals(FeatureSet.Utf8Validation.VERIFY);
}

public boolean isMapField() {
Expand All @@ -1341,14 +1341,14 @@ && isRepeated()

/** Is this field declared required? */
public boolean isRequired() {
return this.features.getFieldPresence()
return getFeatures().getFieldPresence()
== DescriptorProtos.FeatureSet.FieldPresence.LEGACY_REQUIRED;
}

/** Is this field declared optional? */
public boolean isOptional() {
return proto.getLabel() == FieldDescriptorProto.Label.LABEL_OPTIONAL
&& this.features.getFieldPresence()
&& getFeatures().getFieldPresence()
!= DescriptorProtos.FeatureSet.FieldPresence.LEGACY_REQUIRED;
}

Expand All @@ -1367,7 +1367,7 @@ public boolean isPacked() {
if (!isPackable()) {
return false;
}
return this.features
return getFeatures()
.getRepeatedFieldEncoding()
.equals(FeatureSet.RepeatedFieldEncoding.PACKED);
}
Expand Down Expand Up @@ -1467,7 +1467,7 @@ public boolean hasPresence() {
|| getType() == Type.GROUP
|| isExtension()
|| getContainingOneof() != null
|| this.features.getFieldPresence() != DescriptorProtos.FeatureSet.FieldPresence.IMPLICIT;
|| getFeatures().getFieldPresence() != DescriptorProtos.FeatureSet.FieldPresence.IMPLICIT;
}

/**
Expand All @@ -1476,7 +1476,8 @@ public boolean hasPresence() {
* been upgraded to editions.
*/
boolean isGroupLike() {
if (features.getMessageEncoding() != DescriptorProtos.FeatureSet.MessageEncoding.DELIMITED) {
if (getFeatures().getMessageEncoding()
!= DescriptorProtos.FeatureSet.MessageEncoding.DELIMITED) {
// Groups are always tag-delimited.
return false;
}
Expand Down Expand Up @@ -1577,7 +1578,7 @@ public boolean legacyEnumFieldTreatedAsClosed() {
}

return getType() == Type.ENUM
&& (this.features.getExtension(JavaFeaturesProto.java_).getLegacyClosedEnum()
&& (getFeatures().getExtension(JavaFeaturesProto.java_).getLegacyClosedEnum()
|| enumType.isClosed());
}

Expand Down Expand Up @@ -2115,7 +2116,7 @@ public FileDescriptor getFile() {
* handling quirks.
*/
public boolean isClosed() {
return this.features.getEnumType() == DescriptorProtos.FeatureSet.EnumType.CLOSED;
return getFeatures().getEnumType() == DescriptorProtos.FeatureSet.EnumType.CLOSED;
}

/** If this is a nested type, get the outer descriptor, otherwise null. */
Expand Down Expand Up @@ -2811,6 +2812,17 @@ boolean hasInferredLegacyProtoFeatures() {

void validateFeatures() throws DescriptorValidationException {}

FeatureSet getFeatures() {
// TODO: Remove lazy resolution of unresolved features for legacy syntax for
// compatibility with older <4.26.x gencode in the next breaking release.
if (this.features == null
&& (getFile().getEdition() == Edition.EDITION_PROTO2
|| getFile().getEdition() == Edition.EDITION_PROTO3)) {
getFile().resolveAllFeaturesImmutable();
}
return this.features;
}

GenericDescriptor parent;
volatile FeatureSet features;
}
Expand Down

0 comments on commit 4483c6b

Please sign in to comment.