Skip to content

Commit eb10ebd

Browse files
zhangskzcopybara-github
authored andcommitted
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
1 parent baaf402 commit eb10ebd

2 files changed

Lines changed: 1117 additions & 1074 deletions

File tree

java/core/src/main/java/com/google/protobuf/Descriptors.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ public final class Descriptors {
7575
@SuppressWarnings("NonFinalStaticField")
7676
private static volatile FeatureSetDefaults javaEditionDefaults = null;
7777

78-
private static FeatureSet getEditionDefaults(Edition edition) {
78+
/** Sets the default feature mappings used during the build. Exposed for tests. */
79+
static void setTestJavaEditionDefaults(FeatureSetDefaults defaults) {
80+
javaEditionDefaults = defaults;
81+
}
82+
83+
/** Gets the default feature mappings used during the build. */
84+
static FeatureSetDefaults getJavaEditionDefaults() {
7985
// Force explicit initialization before synchronized block which can trigger initialization in
8086
// `JavaFeaturesProto.registerAllExtensions()` and `FeatureSetdefaults.parseFrom()` calls.
8187
// Otherwise, this can result in deadlock if another threads holds the static init block's
@@ -88,18 +94,22 @@ private static FeatureSet getEditionDefaults(Edition edition) {
8894
try {
8995
ExtensionRegistry registry = ExtensionRegistry.newInstance();
9096
registry.add(JavaFeaturesProto.java);
91-
javaEditionDefaults =
97+
setTestJavaEditionDefaults(
9298
FeatureSetDefaults.parseFrom(
9399
JavaEditionDefaults.PROTOBUF_INTERNAL_JAVA_EDITION_DEFAULTS.getBytes(
94100
Internal.ISO_8859_1),
95-
registry);
101+
registry));
96102
} catch (Exception e) {
97103
throw new AssertionError(e);
98104
}
99105
}
100106
}
101107
}
108+
return javaEditionDefaults;
109+
}
102110

111+
static FeatureSet getEditionDefaults(Edition edition) {
112+
FeatureSetDefaults javaEditionDefaults = getJavaEditionDefaults();
103113
if (edition.getNumber() < javaEditionDefaults.getMinimumEdition().getNumber()) {
104114
throw new IllegalArgumentException(
105115
"Edition "
@@ -1116,17 +1126,18 @@ private void resolveAllFeatures() {
11161126
enumType.resolveAllFeatures();
11171127
}
11181128

1129+
// Oneofs must be resolved before any children oneof fields.
1130+
for (OneofDescriptor oneof : oneofs) {
1131+
oneof.resolveAllFeatures();
1132+
}
1133+
11191134
for (FieldDescriptor field : fields) {
11201135
field.resolveAllFeatures();
11211136
}
11221137

11231138
for (FieldDescriptor extension : extensions) {
11241139
extension.resolveAllFeatures();
11251140
}
1126-
1127-
for (OneofDescriptor oneof : oneofs) {
1128-
oneof.resolveAllFeatures();
1129-
}
11301141
}
11311142

11321143
/** Look up and cross-link all field types, etc. */
@@ -1703,6 +1714,7 @@ private FieldDescriptor(
17031714
extensionScope = parent;
17041715
} else {
17051716
extensionScope = null;
1717+
this.parent = file;
17061718
}
17071719

17081720
if (proto.hasOneofIndex()) {
@@ -1726,6 +1738,7 @@ private FieldDescriptor(
17261738
}
17271739
containingOneof = parent.getOneofs().get(proto.getOneofIndex());
17281740
containingOneof.fieldCount++;
1741+
this.parent = containingOneof;
17291742
} else {
17301743
containingOneof = null;
17311744
}

0 commit comments

Comments
 (0)