99
1010/**
1111 * Provides the version of this Protobuf Java runtime, and methods for Protobuf Java gencode to
12- * validate that versions are compatible.
12+ * validate that versions are compatible. Fields and methods in this class should be only accessed
13+ * by related unit tests and Protobuf Java gencode, and should not be used elsewhere.
1314 */
1415public final class RuntimeVersion {
1516
@@ -19,35 +20,71 @@ public enum RuntimeDomain {
1920 PUBLIC ,
2021 }
2122
22- // The version information for this runtime.
23+ // The version of this runtime.
2324 // Automatically updated by Protobuf release process. Do not edit manually.
24- private static final RuntimeDomain DOMAIN = RuntimeDomain .PUBLIC ;
25- private static final int MAJOR = 3 ;
26- private static final int MINOR = 26 ;
27- private static final int PATCH = 0 ;
28- private static final String SUFFIX = "-dev" ;
25+ public static final RuntimeDomain DOMAIN = RuntimeDomain .PUBLIC ;
26+ public static final int MAJOR = 3 ;
27+ public static final int MINOR = 26 ;
28+ public static final int PATCH = 0 ;
29+ public static final String SUFFIX = "-dev" ;
30+ private static final String VERSION_STRING = versionString (MAJOR , MINOR , PATCH , SUFFIX );
2931
3032 /**
31- * Validates that the gencode version is compatible with this runtime version. Currently, no
32- * validation takes place, but only checks that version numbers valid .
33+ * Validates that the gencode version is compatible with this runtime version according to
34+ * https://protobuf.dev/support/cross- version-runtime-guarantee/ .
3335 *
34- * <p>This method is only for Protobuf Java gencode; do not call it elsewhere .
36+ * <p>This method is currently only used by Protobuf Java gencode in OSS .
3537 *
36- * <p>In the future, we will validate Protobuf Java versions according to
37- * https://protobuf.dev/support/cross-version-runtime-guarantee/
38+ * <p>This method is only for Protobuf Java gencode; do not call it elsewhere.
3839 *
39- * @param domain the domain where Protobuf Java code was generated. Currently unused .
40+ * @param domain the domain where Protobuf Java code was generated. Currently ignored .
4041 * @param major the major version of Protobuf Java gencode.
4142 * @param minor the minor version of Protobuf Java gencode.
4243 * @param patch the micro/patch version of Protobuf Java gencode.
43- * @param suffix the version suffix e.g. "-rc2", "-dev", etc. Currently unused.
44+ * @param suffix the version suffix e.g. "-rc2", "-dev", etc.
45+ * @throws ProtobufRuntimeVersionException if versions are incompatible.
4446 */
4547 public static void validateProtobufGencodeVersion (
4648 RuntimeDomain domain , int major , int minor , int patch , String suffix ) {
47- // TODO: b/298200443 - Add cross-version validations.
49+
50+ // Check the environmental variable, and temporarily disable poison pills if it's set to true.
51+ String disableFlag = java .lang .System .getenv ("TEMORARILY_DISABLE_PROTOBUF_VERSION_CHECK" );
52+ if (disableFlag != null && disableFlag .equals ("true" )) {
53+ return ;
54+ }
55+
56+ // Check that version numbers are valid.
4857 if (major < 0 || minor < 0 || patch < 0 ) {
4958 throw new ProtobufRuntimeVersionException (
50- String .format ("Invalid gencode version: %d.%d.%d" , major , minor , patch ));
59+ "Invalid gencode version: " + versionString (major , minor , patch , suffix ));
60+ }
61+
62+ String gencodeVersionString = versionString (major , minor , patch , suffix );
63+ // Check that runtime major version is the same as the gencode major version.
64+ if (major != MAJOR ) {
65+ throw new ProtobufRuntimeVersionException (
66+ String .format (
67+ "Mismatched Protobuf Gencode/Runtime major versions: gencode %s, runtime %s. Same"
68+ + " major version is required." ,
69+ gencodeVersionString , VERSION_STRING ));
70+ }
71+
72+ // Check that runtime version is newer than the gencode version.
73+ if (MINOR < minor || (MINOR == minor && PATCH < patch )) {
74+ throw new ProtobufRuntimeVersionException (
75+ String .format (
76+ "Protobuf Java runtime version cannot be older than the gencode version:"
77+ + "gencode %s, runtime %s." ,
78+ gencodeVersionString , VERSION_STRING ));
79+ }
80+
81+ // Check that runtime version suffix is the same as the gencode version suffix.
82+ if (!suffix .equals (SUFFIX )) {
83+ throw new ProtobufRuntimeVersionException (
84+ String .format (
85+ "Mismatched Protobuf Gencode/Runtime version suffixes: gencode %s, runtime %s."
86+ + " Version suffixes must be the same." ,
87+ gencodeVersionString , VERSION_STRING ));
5188 }
5289 }
5390
@@ -60,4 +97,9 @@ public ProtobufRuntimeVersionException(String message) {
6097 super (message );
6198 }
6299 }
100+
101+ /** Gets the version string given the version segments. */
102+ private static String versionString (int major , int minor , int patch , String suffix ) {
103+ return String .format ("%d.%d.%d%s" , major , minor , patch , suffix );
104+ }
63105}
0 commit comments