Remove public access to Java Edition APIs e.g. getEdition(), getEditionName().

Editions should not be used to make semantic decisions -- specific helpers (e.g. FieldDescriptor.hasPresence()) should be used instead. FileDescriptorProto can be used to access editions for *non semantic* purposes, incl. with ProtoFileUti.

These APIs have not been released yet and are thus non-breaking.

PiperOrigin-RevId: 597362543
pull/15380/head
Sandy Zhang 11 months ago committed by Copybara-Service
parent 592ee9b192
commit 63623a688c
  1. 10
      java/core/src/main/java/com/google/protobuf/Descriptors.java
  2. 21
      java/util/src/main/java/com/google/protobuf/util/ProtoFileUtil.java
  3. 24
      java/util/src/test/java/com/google/protobuf/util/ProtoFileUtilTest.java

@ -164,18 +164,10 @@ public final class Descriptors {
}
/** Get the edition of the .proto file. */
public Edition getEdition() {
Edition getEdition() {
return proto.getEdition();
}
/** Gets the name of the edition as specified in the .proto file. */
public String getEditionName() {
if (proto.getEdition().equals(Edition.EDITION_UNKNOWN)) {
return "";
}
return proto.getEdition().name().substring("EDITION_".length());
}
public void copyHeadingTo(FileDescriptorProto.Builder protoBuilder) {
protoBuilder.setName(getName()).setSyntax(getSyntax().name);
if (!getPackage().isEmpty()) {

@ -0,0 +1,21 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package com.google.protobuf.util;
import com.google.protobuf.DescriptorProtos.Edition;
/** Utility helper functions to work with {@link com.google.protobuf.FileDescriptorProto}. */
public final class ProtoFileUtil {
private ProtoFileUtil() {}
/** Converts an Edition to its string representation as specified in ".proto" file. */
public static String getEditionString(Edition edition) {
return edition.toString().substring("EDITION_".length());
}
}

@ -0,0 +1,24 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package com.google.protobuf.util;
import static com.google.common.truth.Truth.assertThat;
import com.google.protobuf.DescriptorProtos.Edition;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link ProtoFileUtil}. */
@RunWith(JUnit4.class)
public class ProtoFileUtilTest {
@Test
public void testGetEditionFromString() throws Exception {
assertThat(ProtoFileUtil.getEditionString(Edition.EDITION_2023)).isEqualTo("2023");
}
}
Loading…
Cancel
Save