You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/05/01 14:07:53 UTC
(commons-lang) 02/02: Add and use org.apache.commons.lang3.SystemProperties.getJavaSpecificationVersion(Supplier)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit a752091be667ed09ddd4d5ca28339067f713e902
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed May 1 10:07:45 2024 -0400
Add and use
org.apache.commons.lang3.SystemProperties.getJavaSpecificationVersion(Supplier<String>)
---
src/changes/changes.xml | 1 +
src/main/java/org/apache/commons/lang3/JavaVersion.java | 2 +-
.../java/org/apache/commons/lang3/SystemProperties.java | 14 ++++++++++++++
.../org/apache/commons/lang3/SystemPropertiesTest.java | 5 +++++
4 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 1d896a999..9e507e382 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -62,6 +62,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add JavaVersion.JAVA_22.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.SystemProperties.getUserName(Supplier<String>).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.SystemProperties.getLineSeparator(Supplier<String>).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.SystemProperties.getJavaSpecificationVersion(Supplier<String>).</action>
<!-- FIX -->
<action type="fix" dev="ggregory" due-to="Miklós Karakó, Gary Gregory">Improve Javadoc in ExceptionUtils #1136.</action>
<action type="fix" dev="ggregory" due-to="Saiharshith Karuneegar Ramesh, Gary Gregory">Fixed two non-deterministic tests in EnumUtilsTest.java #1131.</action>
diff --git a/src/main/java/org/apache/commons/lang3/JavaVersion.java b/src/main/java/org/apache/commons/lang3/JavaVersion.java
index 892df6c88..6b0fab9d5 100644
--- a/src/main/java/org/apache/commons/lang3/JavaVersion.java
+++ b/src/main/java/org/apache/commons/lang3/JavaVersion.java
@@ -278,7 +278,7 @@ public enum JavaVersion {
* @return the value of {@code java.specification.version} system property or 99.0 if it is not set.
*/
private static float maxVersion() {
- final float v = toFloatVersion(System.getProperty("java.specification.version", "99.0"));
+ final float v = toFloatVersion(SystemProperties.getJavaSpecificationVersion(() -> "99.0"));
return v > 0 ? v : 99f;
}
diff --git a/src/main/java/org/apache/commons/lang3/SystemProperties.java b/src/main/java/org/apache/commons/lang3/SystemProperties.java
index 11164fda5..ec22c0b72 100644
--- a/src/main/java/org/apache/commons/lang3/SystemProperties.java
+++ b/src/main/java/org/apache/commons/lang3/SystemProperties.java
@@ -536,6 +536,20 @@ public final class SystemProperties {
return getProperty(JAVA_SPECIFICATION_VERSION);
}
+ /**
+ * Gets the current value from the system properties map.
+ * <p>
+ * Returns {@code null} if the property cannot be read due to a {@link SecurityException}.
+ * </p>
+ *
+ * @param defaultValue get this Supplier when the property is empty or throws SecurityException.
+ * @return the current value from the system properties map.
+ * @since 3.15.0
+ */
+ public static String getJavaSpecificationVersion(final Supplier<String> defaultValue) {
+ return getProperty(JAVA_SPECIFICATION_VERSION, defaultValue);
+ }
+
/**
* Gets the current value from the system properties map.
* <p>
diff --git a/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java b/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java
index b1ce892ef..bae4d5e03 100644
--- a/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java
+++ b/src/test/java/org/apache/commons/lang3/SystemPropertiesTest.java
@@ -184,6 +184,11 @@ public class SystemPropertiesTest {
assertNotNull(SystemProperties.getJavaSpecificationVersion());
}
+ @Test
+ public void testGetJavaSpecificationVersionSupplier() {
+ assertNotNull(SystemProperties.getJavaSpecificationVersion(() -> "99.0"));
+ }
+
@Test
public void testGetJavaUtilPrefsPreferencesFactory() {
assertNull(SystemProperties.getJavaUtilPrefsPreferencesFactory());