diff --git a/java/core/BUILD.bazel b/java/core/BUILD.bazel index 4168a54b20eec..8de5e8d722bb0 100644 --- a/java/core/BUILD.bazel +++ b/java/core/BUILD.bazel @@ -85,7 +85,7 @@ LITE_SRCS = [ "src/main/java/com/google/protobuf/RawMessageInfo.java", "src/main/java/com/google/protobuf/Reader.java", "src/main/java/com/google/protobuf/RopeByteString.java", - "src/main/java/com/google/protobuf/RuntimeVersion.java", + "src/main/java/com/google/protobuf/RuntimeVersionLite.java", "src/main/java/com/google/protobuf/Schema.java", "src/main/java/com/google/protobuf/SchemaFactory.java", "src/main/java/com/google/protobuf/SchemaUtil.java", diff --git a/java/core/src/main/java/com/google/protobuf/RuntimeVersion.java b/java/core/src/main/java/com/google/protobuf/RuntimeVersion.java index 815bb9ed18f26..6c4eec2be3481 100644 --- a/java/core/src/main/java/com/google/protobuf/RuntimeVersion.java +++ b/java/core/src/main/java/com/google/protobuf/RuntimeVersion.java @@ -7,60 +7,13 @@ package com.google.protobuf; -import java.util.logging.Logger; - -/** - * Provides the version of this Protobuf Java runtime, and methods for Protobuf Java gencode to - * validate that versions are compatible. Fields and methods in this class should be only accessed - * by related unit tests and Protobuf Java gencode, and should not be used elsewhere. - */ -public final class RuntimeVersion { - - /** Indicates the domain of the Protobuf artifact. */ - public enum RuntimeDomain { - GOOGLE_INTERNAL, - PUBLIC, - } - - // The version of this runtime. - // Automatically updated by Protobuf release process. Do not edit manually. - public static final RuntimeDomain DOMAIN = RuntimeDomain.PUBLIC; - public static final int MAJOR = 3; - public static final int MINOR = 26; - public static final int PATCH = 0; - public static final String SUFFIX = "-dev"; - private static final String VERSION_STRING = versionString(MAJOR, MINOR, PATCH, SUFFIX); - - /** - * Validates that the gencode is in the same domain as the runtime. - * - *
This method will be directly called by the google-internal gencode to verify no cross-domain - * usages. - * - * @param gencodeDomain the domain where Protobuf Java code was generated. - * @throws ProtobufRuntimeVersionException if gencodeDomain is not the same as DOMAIN. - */ - public static void validateProtobufGencodeDomain(RuntimeDomain gencodeDomain) { - // Check the environmental variable, and temporarily disable validation if it's set to true. - String disableFlag = java.lang.System.getenv("TEMORARILY_DISABLE_PROTOBUF_VERSION_CHECK"); - if ((disableFlag != null && disableFlag.equals("true"))) { - return; - } - - if (gencodeDomain != DOMAIN) { - throw new ProtobufRuntimeVersionException( - String.format( - "Mismatched Protobuf Gencode/Runtime domains: gencode %s, runtime %s. Cross-domain" - + " usage of Protobuf is not supported.", - gencodeDomain, DOMAIN)); - } - } - +/** Provides methods to check Protobuf Java runtime version matches the gencode version. */ +public class RuntimeVersion extends RuntimeVersionLite { /** * Validates that the gencode version is compatible with this runtime version according to * https://protobuf.dev/support/cross-version-runtime-guarantee/. * - *
This method is currently only used by Protobuf Java gencode in OSS. + *
This method is currently only used by Protobuf Java Full-version gencode in OSS. * *
This method is only for Protobuf Java gencode; do not call it elsewhere. * @@ -73,56 +26,6 @@ public static void validateProtobufGencodeDomain(RuntimeDomain gencodeDomain) { */ public static void validateProtobufGencodeVersion( RuntimeDomain domain, int major, int minor, int patch, String suffix) { - - // Check that version numbers are valid. - if (major < 0 || minor < 0 || patch < 0) { - throw new ProtobufRuntimeVersionException( - "Invalid gencode version: " + versionString(major, minor, patch, suffix)); - } - - validateProtobufGencodeDomain(domain); - - String gencodeVersionString = versionString(major, minor, patch, suffix); - // Check that runtime major version is the same as the gencode major version. - if (major != MAJOR) { - throw new ProtobufRuntimeVersionException( - String.format( - "Mismatched Protobuf Gencode/Runtime major versions: gencode %s, runtime %s. Same" - + " major version is required.", - gencodeVersionString, VERSION_STRING)); - } - - // Check that runtime version is newer than the gencode version. - if (MINOR < minor || (MINOR == minor && PATCH < patch)) { - throw new ProtobufRuntimeVersionException( - String.format( - "Protobuf Java runtime version cannot be older than the gencode version:" - + "gencode %s, runtime %s.", - gencodeVersionString, VERSION_STRING)); - } - - // Check that runtime version suffix is the same as the gencode version suffix. - if (!suffix.equals(SUFFIX)) { - throw new ProtobufRuntimeVersionException( - String.format( - "Mismatched Protobuf Gencode/Runtime version suffixes: gencode %s, runtime %s." - + " Version suffixes must be the same.", - gencodeVersionString, VERSION_STRING)); - } - } - - /** - * A runtime exception to be thrown by the version validator if version is not well defined or - * versions mismatch. - */ - public static final class ProtobufRuntimeVersionException extends RuntimeException { - public ProtobufRuntimeVersionException(String message) { - super(message); - } - } - - /** Gets the version string given the version segments. */ - private static String versionString(int major, int minor, int patch, String suffix) { - return String.format("%d.%d.%d%s", major, minor, patch, suffix); + validateProtobufGencodeVersionImpl(domain, major, minor, patch, suffix); } } diff --git a/java/core/src/main/java/com/google/protobuf/RuntimeVersionLite.java b/java/core/src/main/java/com/google/protobuf/RuntimeVersionLite.java new file mode 100644 index 0000000000000..4b361739f3cc1 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/RuntimeVersionLite.java @@ -0,0 +1,146 @@ +// 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; + +import java.util.logging.Logger; + +/** + * Provides the version of this Protobuf Java runtime, and methods for Protobuf Java gencode to + * validate that versions are compatible. Fields and methods in this class should be only accessed + * by related unit tests and Protobuf Java gencode, and should not be used elsewhere. + */ +public class RuntimeVersionLite { + + /** Indicates the domain of the Protobuf artifact. */ + public enum RuntimeDomain { + GOOGLE_INTERNAL, + PUBLIC, + } + + // The version of this runtime. + // Automatically updated by Protobuf release process. Do not edit manually. + public static final RuntimeDomain DOMAIN = RuntimeDomain.PUBLIC; + public static final int MAJOR = 3; + public static final int MINOR = 26; + public static final int PATCH = 0; + public static final String SUFFIX = "-dev"; + private static final String VERSION_STRING = versionString(MAJOR, MINOR, PATCH, SUFFIX); + + /** + * Validates that the gencode is in the same domain as the runtime. + * + *
This method will be directly called by the google-internal gencode to verify no cross-domain + * usages. + * + * @param gencodeDomain the domain where Protobuf Java code was generated. + * @throws ProtobufRuntimeVersionException if gencodeDomain is not the same as DOMAIN. + */ + public static void validateProtobufGencodeDomain(RuntimeDomain gencodeDomain) { + // Check the environmental variable, and temporarily disable validation if it's set to true. + String disableFlag = java.lang.System.getenv("TEMORARILY_DISABLE_PROTOBUF_VERSION_CHECK"); + if ((disableFlag != null && disableFlag.equals("true"))) { + return; + } + + if (gencodeDomain != DOMAIN) { + throw new ProtobufRuntimeVersionException( + String.format( + "Mismatched Protobuf Gencode/Runtime domains: gencode %s, runtime %s. Cross-domain" + + " usage of Protobuf is not supported.", + gencodeDomain, DOMAIN)); + } + } + + /** + * Validates that the lite gencode version is compatible with this lite runtime version according + * to https://protobuf.dev/support/cross-version-runtime-guarantee/. + * + *
This method is currently only used by Protobuf Java Lite gencode in OSS. + * + *
This method is only for Protobuf Java gencode; do not call it elsewhere.
+ *
+ * @param domain the domain where Protobuf Java code was generated.
+ * @param major the major version of Protobuf Java gencode.
+ * @param minor the minor version of Protobuf Java gencode.
+ * @param patch the micro/patch version of Protobuf Java gencode.
+ * @param suffix the version suffix e.g. "-rc2", "-dev", etc.
+ * @throws ProtobufRuntimeVersionException if versions are incompatible.
+ */
+ public static void validateProtobufLiteGencodeVersion(
+ RuntimeDomain domain, int major, int minor, int patch, String suffix) {
+ expectProtobufJavaLiteRuntime();
+ validateProtobufGencodeVersionImpl(domain, major, minor, patch, suffix);
+ }
+
+ /** Core method for version validation, regardless of the version fullness. */
+ protected static void validateProtobufGencodeVersionImpl(
+ RuntimeDomain domain, int major, int minor, int patch, String suffix) {
+
+ // Check that version numbers are valid.
+ if (major < 0 || minor < 0 || patch < 0) {
+ throw new ProtobufRuntimeVersionException(
+ "Invalid gencode version: " + versionString(major, minor, patch, suffix));
+ }
+
+ validateProtobufGencodeDomain(domain);
+
+ String gencodeVersionString = versionString(major, minor, patch, suffix);
+ // Check that runtime major version is the same as the gencode major version.
+ if (major != MAJOR) {
+ throw new ProtobufRuntimeVersionException(
+ String.format(
+ "Mismatched Protobuf Gencode/Runtime major versions: gencode %s, runtime %s. Same"
+ + " major version is required.",
+ gencodeVersionString, VERSION_STRING));
+ }
+
+ // Check that runtime version is newer than the gencode version.
+ if (MINOR < minor || (MINOR == minor && PATCH < patch)) {
+ throw new ProtobufRuntimeVersionException(
+ String.format(
+ "Protobuf Java runtime version cannot be older than the gencode version:"
+ + "gencode %s, runtime %s.",
+ gencodeVersionString, VERSION_STRING));
+ }
+
+ // Check that runtime version suffix is the same as the gencode version suffix.
+ if (!suffix.equals(SUFFIX)) {
+ throw new ProtobufRuntimeVersionException(
+ String.format(
+ "Mismatched Protobuf Gencode/Runtime version suffixes: gencode %s, runtime %s."
+ + " Version suffixes must be the same.",
+ gencodeVersionString, VERSION_STRING));
+ }
+ }
+
+ /**
+ * A runtime exception to be thrown by the version validator if version is not well defined or
+ * versions mismatch.
+ */
+ public static final class ProtobufRuntimeVersionException extends RuntimeException {
+ public ProtobufRuntimeVersionException(String message) {
+ super(message);
+ }
+ }
+
+ /** Gets the version string given the version segments. */
+ private static String versionString(int major, int minor, int patch, String suffix) {
+ return String.format("%d.%d.%d%s", major, minor, patch, suffix);
+ }
+
+ /** Checks that the full version runtime is not loaded for the lite gencode. */
+ private static void expectProtobufJavaLiteRuntime() {
+ try {
+ Class.forName("com.google.protobuf.RuntimeVersion");
+ } catch (ClassNotFoundException e) {
+ return;
+ }
+ throw new ProtobufRuntimeVersionException(
+ "Expected only Protobuf Java Lite runtime, but found Protobuf Java Full runtime.");
+ }
+}
diff --git a/java/core/src/test/java/com/google/protobuf/RuntimeVersionLiteTest.java b/java/core/src/test/java/com/google/protobuf/RuntimeVersionLiteTest.java
new file mode 100644
index 0000000000000..51e6dfe2ebdd0
--- /dev/null
+++ b/java/core/src/test/java/com/google/protobuf/RuntimeVersionLiteTest.java
@@ -0,0 +1,139 @@
+// 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;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.assertThrows;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public final class RuntimeVersionLiteTest {
+
+ @Test
+ public void versionValidation_invalidVersionNumbers() {
+ RuntimeVersionLite.ProtobufRuntimeVersionException thrown =
+ assertThrows(
+ RuntimeVersionLite.ProtobufRuntimeVersionException.class,
+ () ->
+ RuntimeVersionLite.validateProtobufLiteGencodeVersion(
+ RuntimeVersionLite.DOMAIN, 1, -2, -3, ""));
+ assertThat(thrown).hasMessageThat().contains("Invalid gencode version: 1.-2.-3");
+ }
+
+ @Test
+ public void versionValidation_crossDomainDisallowed() {
+
+ RuntimeVersionLite.RuntimeDomain gencodeDomain =
+ RuntimeVersionLite.RuntimeDomain.GOOGLE_INTERNAL;
+ RuntimeVersionLite.ProtobufRuntimeVersionException thrown =
+ assertThrows(
+ RuntimeVersionLite.ProtobufRuntimeVersionException.class,
+ () -> RuntimeVersionLite.validateProtobufGencodeDomain(gencodeDomain));
+ assertThat(thrown).hasMessageThat().contains("Mismatched Protobuf Gencode/Runtime domains");
+ }
+
+ @Test
+ public void versionValidation_sameDomainAllowed() {
+
+ RuntimeVersionLite.RuntimeDomain gencodeDomain = RuntimeVersionLite.RuntimeDomain.PUBLIC;
+ RuntimeVersionLite.validateProtobufGencodeDomain(gencodeDomain);
+ }
+
+ @Test
+ public void versionValidation_mismatchingMajorDisallowed() {
+ int gencodeMajor = 1;
+ RuntimeVersionLite.ProtobufRuntimeVersionException thrown =
+ assertThrows(
+ RuntimeVersionLite.ProtobufRuntimeVersionException.class,
+ () ->
+ RuntimeVersionLite.validateProtobufLiteGencodeVersion(
+ RuntimeVersionLite.DOMAIN,
+ gencodeMajor,
+ RuntimeVersionLite.MINOR,
+ RuntimeVersionLite.PATCH,
+ RuntimeVersionLite.SUFFIX));
+ assertThat(thrown)
+ .hasMessageThat()
+ .contains("Mismatched Protobuf Gencode/Runtime major versions");
+ }
+
+ @Test
+ public void versionValidation_versionNumbersAllTheSameAllowed() {
+ RuntimeVersionLite.validateProtobufLiteGencodeVersion(
+ RuntimeVersionLite.DOMAIN,
+ RuntimeVersionLite.MAJOR,
+ RuntimeVersionLite.MINOR,
+ RuntimeVersionLite.PATCH,
+ RuntimeVersionLite.SUFFIX);
+ }
+
+ @Test
+ public void versionValidation_NewerRuntimeVersionAllowed() {
+ int gencodeMinor = RuntimeVersionLite.MINOR - 1;
+ RuntimeVersionLite.validateProtobufLiteGencodeVersion(
+ RuntimeVersionLite.DOMAIN,
+ RuntimeVersionLite.MAJOR,
+ gencodeMinor,
+ RuntimeVersionLite.PATCH,
+ RuntimeVersionLite.SUFFIX);
+ }
+
+ @Test
+ public void versionValidation_OlderRuntimeVersionDisallowed() {
+ int gencodeMinor = RuntimeVersionLite.MINOR + 1;
+ RuntimeVersionLite.ProtobufRuntimeVersionException thrown =
+ assertThrows(
+ RuntimeVersionLite.ProtobufRuntimeVersionException.class,
+ () ->
+ RuntimeVersionLite.validateProtobufLiteGencodeVersion(
+ RuntimeVersionLite.DOMAIN,
+ RuntimeVersionLite.MAJOR,
+ gencodeMinor,
+ RuntimeVersionLite.PATCH,
+ RuntimeVersionLite.SUFFIX));
+ assertThat(thrown)
+ .hasMessageThat()
+ .contains("Protobuf Java runtime version cannot be older than the gencode version");
+
+ int gencodePatch = RuntimeVersionLite.PATCH + 1;
+ thrown =
+ assertThrows(
+ RuntimeVersionLite.ProtobufRuntimeVersionException.class,
+ () ->
+ RuntimeVersionLite.validateProtobufLiteGencodeVersion(
+ RuntimeVersionLite.DOMAIN,
+ RuntimeVersionLite.MAJOR,
+ RuntimeVersionLite.MINOR,
+ gencodePatch,
+ RuntimeVersionLite.SUFFIX));
+ assertThat(thrown)
+ .hasMessageThat()
+ .contains("Protobuf Java runtime version cannot be older than the gencode version");
+ }
+
+ @Test
+ public void versionValidation_differentVesionSuffixDisallowed() {
+ String gencodeSuffix = "-test";
+ RuntimeVersionLite.ProtobufRuntimeVersionException thrown =
+ assertThrows(
+ RuntimeVersionLite.ProtobufRuntimeVersionException.class,
+ () ->
+ RuntimeVersionLite.validateProtobufLiteGencodeVersion(
+ RuntimeVersionLite.DOMAIN,
+ RuntimeVersionLite.MAJOR,
+ RuntimeVersionLite.MINOR,
+ RuntimeVersionLite.PATCH,
+ gencodeSuffix));
+ assertThat(thrown)
+ .hasMessageThat()
+ .contains("Mismatched Protobuf Gencode/Runtime version suffixes");
+ }
+}
diff --git a/java/core/src/test/java/com/google/protobuf/RuntimeVersionTest.java b/java/core/src/test/java/com/google/protobuf/RuntimeVersionTest.java
index c30c9a6b7f0e2..58187930fc989 100644
--- a/java/core/src/test/java/com/google/protobuf/RuntimeVersionTest.java
+++ b/java/core/src/test/java/com/google/protobuf/RuntimeVersionTest.java
@@ -16,123 +16,20 @@
@RunWith(JUnit4.class)
public final class RuntimeVersionTest {
-
- @Test
- public void versionValidation_invalidVersionNumbers() {
- RuntimeVersion.ProtobufRuntimeVersionException thrown =
- assertThrows(
- RuntimeVersion.ProtobufRuntimeVersionException.class,
- () ->
- RuntimeVersion.validateProtobufGencodeVersion(
- RuntimeVersion.DOMAIN, 1, -2, -3, ""));
- assertThat(thrown).hasMessageThat().contains("Invalid gencode version: 1.-2.-3");
- }
-
- @Test
- public void versionValidation_crossDomainDisallowed() {
-
- RuntimeVersion.RuntimeDomain gencodeDomain = RuntimeVersion.RuntimeDomain.GOOGLE_INTERNAL;
- RuntimeVersion.ProtobufRuntimeVersionException thrown =
- assertThrows(
- RuntimeVersion.ProtobufRuntimeVersionException.class,
- () -> RuntimeVersion.validateProtobufGencodeDomain(gencodeDomain));
- assertThat(thrown).hasMessageThat().contains("Mismatched Protobuf Gencode/Runtime domains");
- }
-
- @Test
- public void versionValidation_sameDomainAllowed() {
-
- RuntimeVersion.RuntimeDomain gencodeDomain = RuntimeVersion.RuntimeDomain.PUBLIC;
- RuntimeVersion.validateProtobufGencodeDomain(gencodeDomain);
- }
-
@Test
- public void versionValidation_mismatchingMajorDisallowed() {
- int gencodeMajor = 1;
- RuntimeVersion.ProtobufRuntimeVersionException thrown =
+ public void versionValidation_liteGencodeWithFullVersion() {
+ RuntimeVersionLite.ProtobufRuntimeVersionException thrown =
assertThrows(
- RuntimeVersion.ProtobufRuntimeVersionException.class,
+ RuntimeVersionLite.ProtobufRuntimeVersionException.class,
() ->
- RuntimeVersion.validateProtobufGencodeVersion(
- RuntimeVersion.DOMAIN,
- gencodeMajor,
- RuntimeVersion.MINOR,
- RuntimeVersion.PATCH,
- RuntimeVersion.SUFFIX));
- assertThat(thrown)
- .hasMessageThat()
- .contains("Mismatched Protobuf Gencode/Runtime major versions");
- }
-
- @Test
- public void versionValidation_versionNumbersAllTheSameAllowed() {
- RuntimeVersion.validateProtobufGencodeVersion(
- RuntimeVersion.DOMAIN,
- RuntimeVersion.MAJOR,
- RuntimeVersion.MINOR,
- RuntimeVersion.PATCH,
- RuntimeVersion.SUFFIX);
+ RuntimeVersionLite.validateProtobufLiteGencodeVersion(
+ RuntimeVersionLite.RuntimeDomain.GOOGLE_INTERNAL, 0, 0, 0, ""));
+ assertThat(thrown).hasMessageThat().contains("Expected Protobuf Java Lite runtime");
}
@Test
- public void versionValidation_NewerRuntimeVersionAllowed() {
- int gencodeMinor = RuntimeVersion.MINOR - 1;
+ public void versionValidation_linkFullVersion() {
RuntimeVersion.validateProtobufGencodeVersion(
- RuntimeVersion.DOMAIN,
- RuntimeVersion.MAJOR,
- gencodeMinor,
- RuntimeVersion.PATCH,
- RuntimeVersion.SUFFIX);
- }
-
- @Test
- public void versionValidation_OlderRuntimeVersionDisallowed() {
- int gencodeMinor = RuntimeVersion.MINOR + 1;
- RuntimeVersion.ProtobufRuntimeVersionException thrown =
- assertThrows(
- RuntimeVersion.ProtobufRuntimeVersionException.class,
- () ->
- RuntimeVersion.validateProtobufGencodeVersion(
- RuntimeVersion.DOMAIN,
- RuntimeVersion.MAJOR,
- gencodeMinor,
- RuntimeVersion.PATCH,
- RuntimeVersion.SUFFIX));
- assertThat(thrown)
- .hasMessageThat()
- .contains("Protobuf Java runtime version cannot be older than the gencode version");
-
- int gencodePatch = RuntimeVersion.PATCH + 1;
- thrown =
- assertThrows(
- RuntimeVersion.ProtobufRuntimeVersionException.class,
- () ->
- RuntimeVersion.validateProtobufGencodeVersion(
- RuntimeVersion.DOMAIN,
- RuntimeVersion.MAJOR,
- RuntimeVersion.MINOR,
- gencodePatch,
- RuntimeVersion.SUFFIX));
- assertThat(thrown)
- .hasMessageThat()
- .contains("Protobuf Java runtime version cannot be older than the gencode version");
- }
-
- @Test
- public void versionValidation_differentVesionSuffixDisallowed() {
- String gencodeSuffix = "-test";
- RuntimeVersion.ProtobufRuntimeVersionException thrown =
- assertThrows(
- RuntimeVersion.ProtobufRuntimeVersionException.class,
- () ->
- RuntimeVersion.validateProtobufGencodeVersion(
- RuntimeVersion.DOMAIN,
- RuntimeVersion.MAJOR,
- RuntimeVersion.MINOR,
- RuntimeVersion.PATCH,
- gencodeSuffix));
- assertThat(thrown)
- .hasMessageThat()
- .contains("Mismatched Protobuf Gencode/Runtime version suffixes");
+ RuntimeVersionLite.RuntimeDomain.GOOGLE_INTERNAL, 0, 0, 0, "");
}
}
diff --git a/java/lite/pom.xml b/java/lite/pom.xml
index 38621da3ff965..07aef1f0dbb16 100644
--- a/java/lite/pom.xml
+++ b/java/lite/pom.xml
@@ -161,7 +161,7 @@