9
9
10
10
/**
11
11
* 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.
13
14
*/
14
15
public final class RuntimeVersion {
15
16
@@ -19,35 +20,71 @@ public enum RuntimeDomain {
19
20
PUBLIC ,
20
21
}
21
22
22
- // The version information for this runtime.
23
+ // The version of this runtime.
23
24
// 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 );
29
31
30
32
/**
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/ .
33
35
*
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 .
35
37
*
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.
38
39
*
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 .
40
41
* @param major the major version of Protobuf Java gencode.
41
42
* @param minor the minor version of Protobuf Java gencode.
42
43
* @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.
44
46
*/
45
47
public static void validateProtobufGencodeVersion (
46
48
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.
48
57
if (major < 0 || minor < 0 || patch < 0 ) {
49
58
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 ));
51
88
}
52
89
}
53
90
@@ -60,4 +97,9 @@ public ProtobufRuntimeVersionException(String message) {
60
97
super (message );
61
98
}
62
99
}
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
+ }
63
105
}
0 commit comments