Skip to content

Commit f978ec2

Browse files
Add isPlaceholder() accessors to file, message, and enum descriptors
This aligns with C++ and Go. PiperOrigin-RevId: 803010915
1 parent ded5b46 commit f978ec2

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ GenericDescriptor getParent() {
177177
return null;
178178
}
179179

180+
public boolean isPlaceholder() {
181+
return placeholder;
182+
}
183+
180184
/** Returns the same as getName(). */
181185
@Override
182186
public String getFullName() {
@@ -574,6 +578,7 @@ public interface InternalDescriptorAssigner {
574578
private final FileDescriptor[] dependencies;
575579
private final FileDescriptor[] publicDependencies;
576580
private final FileDescriptorTables tables;
581+
private final boolean placeholder;
577582
private volatile boolean featuresResolved;
578583

579584
private FileDescriptor(
@@ -610,6 +615,8 @@ private FileDescriptor(
610615
this.publicDependencies = new FileDescriptor[publicDependencies.size()];
611616
publicDependencies.toArray(this.publicDependencies);
612617

618+
placeholder = false;
619+
613620
tables.addPackage(getPackage(), this);
614621

615622
messageTypes =
@@ -663,6 +670,8 @@ private FileDescriptor(
663670
services = EMPTY_SERVICE_DESCRIPTORS;
664671
extensions = EMPTY_FIELD_DESCRIPTORS;
665672

673+
placeholder = true;
674+
666675
tables.addPackage(packageName, this);
667676
tables.addSymbol(message);
668677
}
@@ -855,6 +864,10 @@ GenericDescriptor getParent() {
855864
return parent;
856865
}
857866

867+
public boolean isPlaceholder() {
868+
return placeholder;
869+
}
870+
858871
/** If this is a nested type, get the outer descriptor, otherwise null. */
859872
public Descriptor getContainingType() {
860873
if (parent instanceof Descriptor) {
@@ -1091,6 +1104,8 @@ public EnumDescriptor findEnumTypeByName(final String name) {
10911104
private final int[] extensionRangeLowerBounds;
10921105
private final int[] extensionRangeUpperBounds;
10931106

1107+
private final boolean placeholder;
1108+
10941109
// Used to create a placeholder when the type cannot be found.
10951110
Descriptor(final String fullname) throws DescriptorValidationException {
10961111
String name = fullname;
@@ -1122,6 +1137,8 @@ public EnumDescriptor findEnumTypeByName(final String name) {
11221137

11231138
extensionRangeLowerBounds = new int[] {1};
11241139
extensionRangeUpperBounds = new int[] {536870912};
1140+
1141+
placeholder = true;
11251142
}
11261143

11271144
private Descriptor(
@@ -1204,6 +1221,8 @@ private Descriptor(
12041221
}
12051222
this.realOneofCount = this.oneofs.length - syntheticOneofCount;
12061223

1224+
placeholder = false;
1225+
12071226
file.tables.addSymbol(this);
12081227

12091228
// NOTE: The defined extension ranges are guaranteed to be disjoint.
@@ -2306,6 +2325,10 @@ GenericDescriptor getParent() {
23062325
return parent;
23072326
}
23082327

2328+
public boolean isPlaceholder() {
2329+
return false;
2330+
}
2331+
23092332
/**
23102333
* Determines if the given enum is closed.
23112334
*

‎java/core/src/test/java/com/google/protobuf/DescriptorsTest.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public void testFieldTypeEnumMapping() throws Exception {
9292
public void testFileDescriptor() throws Exception {
9393
FileDescriptor file = UnittestProto.getDescriptor();
9494

95+
assertThat(file.isPlaceholder()).isFalse();
9596
assertThat(file.getName()).isEqualTo("google/protobuf/unittest.proto");
9697
assertThat(file.getPackage()).isEqualTo("proto2_unittest");
9798
assertThat(file.getOptions().getJavaOuterClassname()).isEqualTo("UnittestProto");
@@ -209,7 +210,10 @@ public void testFileDescriptorCopyHeadingTo() throws Exception {
209210
@Test
210211
public void testDescriptor() throws Exception {
211212
Descriptor messageType = TestAllTypes.getDescriptor();
213+
assertThat(messageType.isPlaceholder()).isFalse();
214+
212215
Descriptor nestedType = TestAllTypes.NestedMessage.getDescriptor();
216+
assertThat(nestedType.isPlaceholder()).isFalse();
213217

214218
assertThat(messageType.getName()).isEqualTo("TestAllTypes");
215219
assertThat(messageType.getFullName()).isEqualTo("proto2_unittest.TestAllTypes");
@@ -967,8 +971,21 @@ public void testUnknownFieldsAllowed() throws Exception {
967971
.setName("bar")
968972
.setNumber(1)))
969973
.build();
970-
FileDescriptor unused =
974+
FileDescriptor foo =
971975
Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0], true);
976+
assertThat(
977+
foo.findMessageTypeByName("Foo")
978+
.findFieldByName("bar")
979+
.getMessageType()
980+
.isPlaceholder())
981+
.isTrue();
982+
assertThat(
983+
foo.findMessageTypeByName("Foo")
984+
.findFieldByName("bar")
985+
.getMessageType()
986+
.getFile()
987+
.isPlaceholder())
988+
.isTrue();
972989
}
973990

974991
@Test

0 commit comments

Comments
 (0)