Skip to content

Commit

Permalink
Optimize Java gencode version validation
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 706053082
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 14, 2024
1 parent d9a542b commit 59a8de6
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions java/core/src/main/java/com/google/protobuf/RuntimeVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ public enum RuntimeDomain {
*/
public static void validateProtobufGencodeVersion(
RuntimeDomain domain, int major, int minor, int patch, String suffix, String location) {
if (checkDisabled()) {
return;
}
validateProtobufGencodeVersionImpl(domain, major, minor, patch, suffix, location);
}

Expand All @@ -77,10 +74,10 @@ private static void validateProtobufGencodeVersionImpl(
if (checkDisabled()) {
return;
}
String gencodeVersionString = versionString(major, minor, patch, suffix);
// Check that version numbers are valid.
if (major < 0 || minor < 0 || patch < 0) {
throw new ProtobufRuntimeVersionException("Invalid gencode version: " + gencodeVersionString);
throw new ProtobufRuntimeVersionException(
"Invalid gencode version: " + versionString(major, minor, patch, suffix));
}

// Check that runtime domain is the same as the gencode domain.
Expand All @@ -92,9 +89,12 @@ private static void validateProtobufGencodeVersionImpl(
location, domain, DOMAIN));
}

String gencodeVersionString = null;

// Check that runtime major version is the same as the gencode major version.
if (major != MAJOR) {
if (major == MAJOR - 1 && majorWarningLoggedCount < MAX_WARNING_COUNT) {
gencodeVersionString = versionString(major, minor, patch, suffix);
logger.warning(
String.format(
" Protobuf gencode version %s is exactly one major version older than the runtime"
Expand All @@ -107,12 +107,15 @@ private static void validateProtobufGencodeVersionImpl(
String.format(
"Detected mismatched Protobuf Gencode/Runtime major versions when loading %s:"
+ " gencode %s, runtime %s. Same major version is required.",
location, gencodeVersionString, VERSION_STRING));
location, versionString(major, minor, patch, suffix), VERSION_STRING));
}
}

// Check that runtime version is newer than the gencode version.
if (MINOR < minor || (minor == MINOR && PATCH < patch)) {
if (gencodeVersionString == null) {
gencodeVersionString = versionString(major, minor, patch, suffix);
}
throw new ProtobufRuntimeVersionException(
String.format(
"Detected incompatible Protobuf Gencode/Runtime versions when loading %s: gencode %s,"
Expand All @@ -122,6 +125,9 @@ private static void validateProtobufGencodeVersionImpl(

// Check that runtime version suffix is the same as the gencode version suffix.
if (!suffix.equals(SUFFIX)) {
if (gencodeVersionString == null) {
gencodeVersionString = versionString(major, minor, patch, suffix);
}
throw new ProtobufRuntimeVersionException(
String.format(
"Detected mismatched Protobuf Gencode/Runtime version suffixes when loading %s:"
Expand Down

0 comments on commit 59a8de6

Please sign in to comment.