This is an automated email from the ASF dual-hosted git repository.
ddekany pushed a commit to branch 2.3-gae
in repository https://gitbox.apache.org/repos/asf/freemarker.git
The following commit(s) were added to refs/heads/2.3-gae by this push:
new 429e2d0f Fixed Java version detection (Java 8 version is returned as
1.8.x, but Java 9+ it's 9.x. not 1.9.x). Also, code cleanup.
429e2d0f is described below
commit 429e2d0fbfa6781b729ef79bc151fbd9a1f111fb
Author: ddekany <[email protected]>
AuthorDate: Sat Mar 2 07:12:20 2024 +0100
Fixed Java version detection (Java 8 version is returned as 1.8.x, but Java
9+ it's 9.x. not 1.9.x). Also, code cleanup.
---
.../src/main/java/freemarker/core/_Java16.java | 2 +-
.../main/java/freemarker/core/_JavaVersions.java | 46 +++++++++++++---------
2 files changed, 28 insertions(+), 20 deletions(-)
diff --git a/freemarker-core/src/main/java/freemarker/core/_Java16.java
b/freemarker-core/src/main/java/freemarker/core/_Java16.java
index 84af6cd1..19de70a3 100644
--- a/freemarker-core/src/main/java/freemarker/core/_Java16.java
+++ b/freemarker-core/src/main/java/freemarker/core/_Java16.java
@@ -23,7 +23,7 @@ import java.util.Set;
/**
* Used internally only, might change without notice!
- * Used for accessing functionality that's only present in Java 8 or later.
+ * Used for accessing functionality that's only present in Java 16 or later.
*/
public interface _Java16 {
diff --git a/freemarker-core/src/main/java/freemarker/core/_JavaVersions.java
b/freemarker-core/src/main/java/freemarker/core/_JavaVersions.java
index 9530e593..c47d9360 100644
--- a/freemarker-core/src/main/java/freemarker/core/_JavaVersions.java
+++ b/freemarker-core/src/main/java/freemarker/core/_JavaVersions.java
@@ -31,38 +31,46 @@ public final class _JavaVersions {
// Not meant to be instantiated
}
- private static final boolean IS_AT_LEAST_16 = isAtLeast(16,
"java.net.UnixDomainSocketAddress");
-
/**
- * {@code null} if Java 8 is not available, otherwise the object through
with the Java 8 operations are available.
+ * {@code null} if Java 16 is not available, otherwise the object through
with the Java 16 operations are available.
*/
- static public final _Java16 JAVA_16;
- static {
- _Java16 java16;
- if (IS_AT_LEAST_16) {
+ static public final _Java16 JAVA_16 = isAtLeast(16,
"java.net.UnixDomainSocketAddress")
+ ? tryLoadJavaSupportSingleton(16, _Java16.class)
+ : null;
+
+ @SuppressWarnings("unchecked")
+ private static <T> T tryLoadJavaSupportSingleton(int javaVersion, Class<T>
javaSupportInterface) {
+ String implClassName = "freemarker.core._Java" + javaVersion + "Impl";
+ try {
+ return (T) Class.forName(implClassName)
+ .getField("INSTANCE")
+ .get(null);
+ } catch (Exception e) {
try {
- java16 = (_Java16)
Class.forName("freemarker.core._Java16Impl").getField("INSTANCE").get(null);
- } catch (Exception e) {
- try {
- Logger.getLogger("freemarker.runtime").error("Failed to
access Java 16 functionality", e);
- } catch (Exception e2) {
- // Suppressed
+ if (e instanceof ClassNotFoundException) {
+ // Happens when we run JUnit tests
+ Logger.getLogger("freemarker.runtime").warn(
+ "Seems that the Java " + javaVersion + " support
class (" + implClassName
+ + ") wasn't included in the build");
+ } else {
+ Logger.getLogger("freemarker.runtime").error(
+ "Failed to load Java " + javaVersion + " support
class",
+ e);
}
- java16 = null;
+ } catch (Exception e2) {
+ // Suppressed
}
- } else {
- java16 = null;
+ return null;
}
- JAVA_16 = java16;
}
- private static boolean isAtLeast(int minimumMinorVersion, String
proofClassPresence) {
+ private static boolean isAtLeast(int minimumMajorVersion, String
proofClassPresence) {
boolean result = false;
String vStr = SecurityUtilities.getSystemProperty("java.version",
null);
if (vStr != null) {
try {
Version v = new Version(vStr);
- result = v.getMajor() == 1 && v.getMinor() >=
minimumMinorVersion || v.getMajor() > 1;
+ result = v.getMajor() >= minimumMajorVersion;
} catch (Exception e) {
// Ignore
}