Skip to content

Commit

Permalink
Fix validateFeatures to be called after resolved features are actuall…
Browse files Browse the repository at this point in the history
…y set to this.features

PiperOrigin-RevId: 619261163
  • Loading branch information
zhangskz committed Mar 26, 2024
1 parent 2e51ff6 commit 9bf69ec
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions java/core/src/main/java/com/google/protobuf/Descriptors.java
Expand Up @@ -639,7 +639,7 @@ private void resolveAllFeaturesInternal() throws DescriptorValidationException {
if (this.features != null) {
return;
}
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());

for (Descriptor messageType : messageTypes) {
messageType.resolveAllFeatures();
Expand Down Expand Up @@ -718,7 +718,7 @@ private synchronized void setProto(final FileDescriptorProto proto) {
this.proto = proto;
this.options = null;
try {
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());

for (int i = 0; i < messageTypes.length; i++) {
messageTypes[i].setProto(proto.getMessageType(i));
Expand Down Expand Up @@ -1112,7 +1112,7 @@ private Descriptor(

/** See {@link FileDescriptor#resolveAllFeatures}. */
private void resolveAllFeatures() throws DescriptorValidationException {
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());

for (Descriptor nestedType : nestedTypes) {
nestedType.resolveAllFeatures();
Expand Down Expand Up @@ -1174,7 +1174,7 @@ private void validateNoDuplicateFieldNumbers() throws DescriptorValidationExcept
private void setProto(final DescriptorProto proto) throws DescriptorValidationException {
this.proto = proto;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());

for (int i = 0; i < nestedTypes.length; i++) {
nestedTypes[i].setProto(proto.getNestedType(i));
Expand Down Expand Up @@ -1744,7 +1744,7 @@ private FieldDescriptor(

/** See {@link FileDescriptor#resolveAllFeatures}. */
private void resolveAllFeatures() throws DescriptorValidationException {
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());
}

@Override
Expand Down Expand Up @@ -1803,7 +1803,7 @@ boolean hasInferredLegacyProtoFeatures() {
}

@Override
void validateFeatures(FeatureSet features) throws DescriptorValidationException {
void validateFeatures() throws DescriptorValidationException {
if (containingType != null
&& containingType.toProto().getOptions().getMessageSetWireFormat()) {
if (isExtension()) {
Expand Down Expand Up @@ -1992,7 +1992,7 @@ private void crossLink() throws DescriptorValidationException {
private void setProto(final FieldDescriptorProto proto) throws DescriptorValidationException {
this.proto = proto;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());
}

/** For internal use only. This is to satisfy the FieldDescriptorLite interface. */
Expand Down Expand Up @@ -2262,7 +2262,8 @@ private EnumDescriptor(

/** See {@link FileDescriptor#resolveAllFeatures}. */
private void resolveAllFeatures() throws DescriptorValidationException {
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());

for (EnumValueDescriptor value : values) {
value.resolveAllFeatures();
}
Expand All @@ -2272,7 +2273,7 @@ private void resolveAllFeatures() throws DescriptorValidationException {
private void setProto(final EnumDescriptorProto proto) throws DescriptorValidationException {
this.proto = proto;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());

for (int i = 0; i < values.length; i++) {
values[i].setProto(proto.getValue(i));
Expand Down Expand Up @@ -2414,15 +2415,15 @@ private EnumValueDescriptor(final EnumDescriptor parent, final Integer number) {

/** See {@link FileDescriptor#resolveAllFeatures}. */
private void resolveAllFeatures() throws DescriptorValidationException {
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());
}

/** See {@link FileDescriptor#setProto}. */
private void setProto(final EnumValueDescriptorProto proto)
throws DescriptorValidationException {
this.proto = proto;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());
}
}

Expand Down Expand Up @@ -2530,7 +2531,7 @@ private ServiceDescriptor(

/** See {@link FileDescriptor#resolveAllFeatures}. */
private void resolveAllFeatures() throws DescriptorValidationException {
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());

for (MethodDescriptor method : methods) {
method.resolveAllFeatures();
Expand All @@ -2547,7 +2548,7 @@ private void crossLink() throws DescriptorValidationException {
private void setProto(final ServiceDescriptorProto proto) throws DescriptorValidationException {
this.proto = proto;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());

for (int i = 0; i < methods.length; i++) {
methods[i].setProto(proto.getMethod(i));
Expand Down Expand Up @@ -2668,7 +2669,7 @@ private MethodDescriptor(

/** See {@link FileDescriptor#resolveAllFeatures}. */
private void resolveAllFeatures() throws DescriptorValidationException {
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());
}

private void crossLink() throws DescriptorValidationException {
Expand Down Expand Up @@ -2697,7 +2698,7 @@ private void crossLink() throws DescriptorValidationException {
private void setProto(final MethodDescriptorProto proto) throws DescriptorValidationException {
this.proto = proto;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());
}
}

Expand Down Expand Up @@ -2735,11 +2736,13 @@ private GenericDescriptor() {}

public abstract FileDescriptor getFile();

FeatureSet resolveFeatures(FeatureSet unresolvedFeatures) throws DescriptorValidationException {
void resolveFeatures(FeatureSet unresolvedFeatures) throws DescriptorValidationException {
if (this.parent != null
&& unresolvedFeatures.equals(FeatureSet.getDefaultInstance())
&& !hasInferredLegacyProtoFeatures()) {
return this.parent.features;
this.features = this.parent.features;
validateFeatures();
return;
}
FeatureSet.Builder features;
if (this.parent == null) {
Expand All @@ -2750,8 +2753,8 @@ FeatureSet resolveFeatures(FeatureSet unresolvedFeatures) throws DescriptorValid
}
features.mergeFrom(inferLegacyProtoFeatures());
features.mergeFrom(unresolvedFeatures);
validateFeatures(features.build());
return internFeatures(features.build());
this.features = internFeatures(features.build());
validateFeatures();
}

FeatureSet inferLegacyProtoFeatures() {
Expand All @@ -2762,8 +2765,7 @@ boolean hasInferredLegacyProtoFeatures() {
return false;
}

void validateFeatures(FeatureSet features) throws DescriptorValidationException {
}
void validateFeatures() throws DescriptorValidationException {}

GenericDescriptor parent;
volatile FeatureSet features;
Expand Down Expand Up @@ -3231,13 +3233,13 @@ boolean isSynthetic() {

/** See {@link FileDescriptor#resolveAllFeatures}. */
private void resolveAllFeatures() throws DescriptorValidationException {
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());
}

private void setProto(final OneofDescriptorProto proto) throws DescriptorValidationException {
this.proto = proto;
this.options = null;
this.features = resolveFeatures(proto.getOptions().getFeatures());
resolveFeatures(proto.getOptions().getFeatures());
}

private OneofDescriptor(
Expand Down

0 comments on commit 9bf69ec

Please sign in to comment.