Retrieve MongoDB driver version reflectively.

To avoid inlining of the final/static version value, we're using reflection to look up the version value.

Closes #4937
This commit is contained in:
Mark Paluch
2025-04-09 08:41:01 +02:00
parent 008f3b1495
commit c3aa2942dc

View File

@@ -15,9 +15,12 @@
*/
package org.springframework.data.mongodb.util;
import java.lang.reflect.Field;
import org.springframework.data.util.Version;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import com.mongodb.internal.build.MongoDriverVersion;
@@ -96,8 +99,9 @@ public class MongoClientVersion {
if (ClassUtils.isPresent("com.mongodb.internal.build.MongoDriverVersion", classLoader)) {
try {
return Version.parse(MongoDriverVersion.VERSION);
} catch (IllegalArgumentException exception) {
Field field = ReflectionUtils.findField(MongoDriverVersion.class, "VERSION");
return field != null ? Version.parse("" + field.get(null)) : null;
} catch (ReflectiveOperationException | IllegalArgumentException exception) {
// well not much we can do, right?
}
}