Skip to content

Commit

Permalink
Fix features inheritance of oneof fields and extensions and fix/move …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
zhangskz authored and copybara-github committed Feb 23, 2024
1 parent baaf402 commit eb10ebd
Show file tree
Hide file tree
Showing 2 changed files with 1,117 additions and 1,074 deletions.
27 changes: 20 additions & 7 deletions java/core/src/main/java/com/google/protobuf/Descriptors.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -88,18 +94,22 @@ private static FeatureSet getEditionDefaults(Edition edition) {
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 "
Expand Down Expand Up @@ -1116,17 +1126,18 @@ private void resolveAllFeatures() {
enumType.resolveAllFeatures();
}

// Oneofs must be resolved before any children oneof fields.
for (OneofDescriptor oneof : oneofs) {
oneof.resolveAllFeatures();
}

for (FieldDescriptor field : fields) {
field.resolveAllFeatures();
}

for (FieldDescriptor extension : extensions) {
extension.resolveAllFeatures();
}

for (OneofDescriptor oneof : oneofs) {
oneof.resolveAllFeatures();
}
}

/** Look up and cross-link all field types, etc. */
Expand Down Expand Up @@ -1703,6 +1714,7 @@ private FieldDescriptor(
extensionScope = parent;
} else {
extensionScope = null;
this.parent = file;
}

if (proto.hasOneofIndex()) {
Expand All @@ -1726,6 +1738,7 @@ private FieldDescriptor(
}
containingOneof = parent.getOneofs().get(proto.getOneofIndex());
containingOneof.fieldCount++;
this.parent = containingOneof;
} else {
containingOneof = null;
}
Expand Down
Loading

0 comments on commit eb10ebd

Please sign in to comment.