Skip to content

Apache Lucene open-source search software

LUCENE-5886: Improve Java version detection in Constants.java and common-build.xml, add Java 9 support

Uwe Schindler0229a057a4bb
diff --git a/lucene/common-build.xml b/lucene/common-build.xml
index e191d949c03..7d2483ca054 100644
--- a/lucene/common-build.xml
+++ b/lucene/common-build.xml
@@ -329,20 +329,45 @@
</condition>
</fail>
+ <fail message="Minimum supported Java version is 1.7.">
+ <condition>
+ <not><hasmethod classname="java.lang.Throwable" method="getSuppressed"/></not>
+ </condition>
+ </fail>
+
+ <!-- temporary for cleanup of java.specification.version, to be in format "x.y" -->
+ <loadresource property="-cleaned.specification.version">
+ <string value="${java.specification.version}"/>
+ <filterchain>
+ <tokenfilter>
+ <filetokenizer/>
+ <replaceregex pattern="^(\d+\.\d+)(|\..*)$" replace="\1" flags="s"/>
+ </tokenfilter>
+ </filterchain>
+ </loadresource>
+
<!--
the propery "ant.java.version" is not always correct, depending on used ANT version.
E.g. Java 8 is only detected in ANT 1.8.3+.
- Add newer Java version checks at beginning,
- because ANT will nevert override existing properties!
+
+ We want to detect here only a limited set of versions and placed in normalized form in ${build.java.runtime},
+ every other version is normalized to "unknown":
+ - To define a target to be only run on a specific version, add <equals/> condition to one of the supplied versions.
+ - To explicitely exclude specific versions (and unknown ones), add a condition to disallow "unknown" and some versions like "1.9"!
-->
- <condition property="build.java.runtime" value="1.8">
- <hasmethod classname="java.util.Collections" method="emptySortedSet"/>
- </condition>
- <condition property="build.java.runtime" value="1.7">
- <hasmethod classname="java.lang.Throwable" method="getSuppressed"/>
+ <condition property="build.java.runtime" value="${-cleaned.specification.version}" else="unknown">
+ <or>
+ <equals arg1="${-cleaned.specification.version}" arg2="1.7"/>
+ <equals arg1="${-cleaned.specification.version}" arg2="1.8"/>
+ <equals arg1="${-cleaned.specification.version}" arg2="1.9"/>
+ </or>
</condition>
- <fail message="Minimum supported Java version is 1.7." unless="build.java.runtime"/>
-
+
+ <!--
+ <echo message="DEBUG: Cleaned java.specification.version=${-cleaned.specification.version}"/>
+ <echo message="DEBUG: Detected runtime: ${build.java.runtime}"/>
+ -->
+
<condition property="documentation-lint.supported">
<and>
<or>
@@ -362,7 +387,9 @@
<!-- workaround for https://issues.apache.org/bugzilla/show_bug.cgi?id=53347 -->
<condition property="build.compiler" value="javac1.7">
<and>
- <equals arg1="${build.java.runtime}" arg2="1.8"/>
+ <not>
+ <equals arg1="${build.java.runtime}" arg2="1.7"/>
+ </not>
<or>
<antversion exactly="1.8.3" />
<antversion exactly="1.8.4" />
@@ -379,9 +406,11 @@
<echo level="warning" message="WARN: Linting documentation HTML is not supported on this Java version (${build.java.runtime}) / JVM (${java.vm.name}). NOTHING DONE!"/>
</target>
- <!-- for now disable doclint on JDK 8: -->
+ <!-- for now disable doclint on JDK 8+: -->
<condition property="javadoc.args" value="-Xdoclint:none" else="">
- <equals arg1="${build.java.runtime}" arg2="1.8"/>
+ <not>
+ <equals arg1="${build.java.runtime}" arg2="1.7"/>
+ </not>
</condition>
<!-- Import custom ANT tasks. -->
@@ -1824,7 +1853,10 @@ ${ant.project.name}.test.dependencies=${test.classpath.list}
<!-- ECJ Javadoc linting: -->
<condition property="ecj-javadoc-lint.supported">
- <not><equals arg1="${build.java.runtime}" arg2="1.9"/></not>
+ <or>
+ <equals arg1="${build.java.runtime}" arg2="1.7"/>
+ <equals arg1="${build.java.runtime}" arg2="1.8"/>
+ </or>
</condition>
<condition property="ecj-javadoc-lint-tests.supported">
diff --git a/lucene/core/src/java/org/apache/lucene/util/Constants.java b/lucene/core/src/java/org/apache/lucene/util/Constants.java
index ec703d9d6b2..4d767081570 100644
--- a/lucene/core/src/java/org/apache/lucene/util/Constants.java
+++ b/lucene/core/src/java/org/apache/lucene/util/Constants.java
@@ -18,7 +18,8 @@ package org.apache.lucene.util;
*/
import java.lang.reflect.Field;
-import java.util.Collections;
+import java.util.StringTokenizer;
+
/**
* Some useful constants.
@@ -31,6 +32,7 @@ public final class Constants {
public static final String JVM_VENDOR = System.getProperty("java.vm.vendor");
public static final String JVM_VERSION = System.getProperty("java.vm.version");
public static final String JVM_NAME = System.getProperty("java.vm.name");
+ public static final String JVM_SPEC_VERSION = System.getProperty("java.specification.version");
/** The value of <tt>System.getProperty("java.version")</tt>. **/
public static final String JAVA_VERSION = System.getProperty("java.version");
@@ -51,13 +53,21 @@ public final class Constants {
public static final String OS_ARCH = System.getProperty("os.arch");
public static final String OS_VERSION = System.getProperty("os.version");
public static final String JAVA_VENDOR = System.getProperty("java.vendor");
-
- public static final boolean JRE_IS_MINIMUM_JAVA8;
+ private static final int JVM_MAJOR_VERSION;
+ private static final int JVM_MINOR_VERSION;
+
/** True iff running on a 64bit JVM */
public static final boolean JRE_IS_64BIT;
static {
+ final StringTokenizer st = new StringTokenizer(JVM_SPEC_VERSION, ".");
+ JVM_MAJOR_VERSION = Integer.parseInt(st.nextToken());
+ if (st.hasMoreTokens()) {
+ JVM_MINOR_VERSION = Integer.parseInt(st.nextToken());
+ } else {
+ JVM_MINOR_VERSION = 0;
+ }
boolean is64Bit = false;
try {
final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
@@ -81,16 +91,10 @@ public final class Constants {
}
}
JRE_IS_64BIT = is64Bit;
-
- // this method only exists in Java 8:
- boolean v8 = true;
- try {
- Collections.class.getMethod("emptySortedSet");
- } catch (NoSuchMethodException nsme) {
- v8 = false;
- }
- JRE_IS_MINIMUM_JAVA8 = v8;
}
+
+ public static final boolean JRE_IS_MINIMUM_JAVA8 = JVM_MAJOR_VERSION > 1 || (JVM_MAJOR_VERSION == 1 && JVM_MINOR_VERSION >= 8);
+ public static final boolean JRE_IS_MINIMUM_JAVA9 = JVM_MAJOR_VERSION > 1 || (JVM_MAJOR_VERSION == 1 && JVM_MINOR_VERSION >= 9);
/**
* This is the internal Lucene version, including bugfix versions, recorded into each segment.