Fix features inheritance of oneof fields and extensions and fix/move unit tests to actually run.

JUnit4 does not support nested tests so these weren't running. Fixes setup problems and test logic. Oneof fields now inherit from their oneof, and top-level extensions inherit from top-level file when parent descriptor is null.

PiperOrigin-RevId: 609840087
pull/15928/head
Sandy Zhang 1 year ago committed by Copybara-Service
parent baaf402b29
commit eb10ebd169
  1. 27
      java/core/src/main/java/com/google/protobuf/Descriptors.java
  2. 2164
      java/core/src/test/java/com/google/protobuf/DescriptorsTest.java

@ -75,7 +75,13 @@ public final class Descriptors {
@SuppressWarnings("NonFinalStaticField")
private static volatile FeatureSetDefaults javaEditionDefaults = null;
private static FeatureSet getEditionDefaults(Edition edition) {
/** Sets the default feature mappings used during the build. Exposed for tests. */
static void setTestJavaEditionDefaults(FeatureSetDefaults defaults) {
javaEditionDefaults = defaults;
}
/** Gets the default feature mappings used during the build. */
static FeatureSetDefaults getJavaEditionDefaults() {
// Force explicit initialization before synchronized block which can trigger initialization in
// `JavaFeaturesProto.registerAllExtensions()` and `FeatureSetdefaults.parseFrom()` calls.
// Otherwise, this can result in deadlock if another threads holds the static init block's
@ -88,18 +94,22 @@ public final class Descriptors {
try {
ExtensionRegistry registry = ExtensionRegistry.newInstance();
registry.add(JavaFeaturesProto.java);
javaEditionDefaults =
setTestJavaEditionDefaults(
FeatureSetDefaults.parseFrom(
JavaEditionDefaults.PROTOBUF_INTERNAL_JAVA_EDITION_DEFAULTS.getBytes(
Internal.ISO_8859_1),
registry);
registry));
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
}
return javaEditionDefaults;
}
static FeatureSet getEditionDefaults(Edition edition) {
FeatureSetDefaults javaEditionDefaults = getJavaEditionDefaults();
if (edition.getNumber() < javaEditionDefaults.getMinimumEdition().getNumber()) {
throw new IllegalArgumentException(
"Edition "
@ -1116,6 +1126,11 @@ public final class Descriptors {
enumType.resolveAllFeatures();
}
// Oneofs must be resolved before any children oneof fields.
for (OneofDescriptor oneof : oneofs) {
oneof.resolveAllFeatures();
}
for (FieldDescriptor field : fields) {
field.resolveAllFeatures();
}
@ -1123,10 +1138,6 @@ public final class Descriptors {
for (FieldDescriptor extension : extensions) {
extension.resolveAllFeatures();
}
for (OneofDescriptor oneof : oneofs) {
oneof.resolveAllFeatures();
}
}
/** Look up and cross-link all field types, etc. */
@ -1703,6 +1714,7 @@ public final class Descriptors {
extensionScope = parent;
} else {
extensionScope = null;
this.parent = file;
}
if (proto.hasOneofIndex()) {
@ -1726,6 +1738,7 @@ public final class Descriptors {
}
containingOneof = parent.getOneofs().get(proto.getOneofIndex());
containingOneof.fieldCount++;
this.parent = containingOneof;
} else {
containingOneof = null;
}

Loading…
Cancel
Save