Add isClosed() to EnumDescriptor in Java.

PiperOrigin-RevId: 507489971
pull/11594/head
Sandy Zhang 2 years ago committed by Copybara-Service
parent 3becebb82a
commit d1b86601a2
  1. 15
      java/core/src/main/java/com/google/protobuf/Descriptors.java
  2. 1
      java/core/src/test/java/com/google/protobuf/DescriptorsTest.java
  3. 2
      java/core/src/test/java/com/google/protobuf/UnknownEnumValueTest.java

@ -1772,6 +1772,21 @@ public final class Descriptors {
return file;
}
/**
* Determines if the given enum is closed.
*
* <p>Closed enum means that it:
*
* <ul>
* <li>Has a fixed set of named values. *
* <li>Encountering values not in this set causes them to be treated as unknown fields.
* <li>The first value (i.e., the default) may be nonzero.
* </ul>
*/
public boolean isClosed() {
return getFile().getSyntax() != Syntax.PROTO3;
}
/** If this is a nested type, get the outer descriptor, otherwise null. */
public Descriptor getContainingType() {
return containingType;

@ -297,6 +297,7 @@ public class DescriptorsTest {
assertThat(enumType.getName()).isEqualTo("ForeignEnum");
assertThat(enumType.getFullName()).isEqualTo("protobuf_unittest.ForeignEnum");
assertThat(enumType.getFile()).isEqualTo(UnittestProto.getDescriptor());
assertThat(enumType.isClosed()).isTrue();
assertThat(enumType.getContainingType()).isNull();
assertThat(enumType.getOptions()).isEqualTo(DescriptorProtos.EnumOptions.getDefaultInstance());

@ -56,6 +56,7 @@ public class UnknownEnumValueTest {
@Test
@SuppressWarnings("ProtoNewBuilderMergeFrom")
public void testUnknownEnumValues() throws Exception {
assertThat(TestAllTypes.NestedEnum.getDescriptor().isClosed()).isFalse();
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
builder.setOptionalNestedEnumValue(4321);
builder.addRepeatedNestedEnumValue(5432);
@ -270,6 +271,7 @@ public class UnknownEnumValueTest {
@Test
public void testUnknownEnumValuesInProto2() throws Exception {
assertThat(Proto2TestEnum.getDescriptor().isClosed()).isTrue();
Proto2EnumMessage.Builder sourceMessage = Proto2EnumMessage.newBuilder();
sourceMessage
.addRepeatedPackedEnum(Proto2TestEnum.ZERO)

Loading…
Cancel
Save