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

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1618278 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2014-08-15 21:31:27 +00:00
parent 2dd94da6ff
commit 0229a057a4
2 changed files with 61 additions and 25 deletions

View File

@ -329,20 +329,45 @@
</condition> </condition>
</fail> </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. 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+. 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"> <condition property="build.java.runtime" value="${-cleaned.specification.version}" else="unknown">
<hasmethod classname="java.util.Collections" method="emptySortedSet"/> <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> </condition>
<condition property="build.java.runtime" value="1.7">
<hasmethod classname="java.lang.Throwable" method="getSuppressed"/> <!--
</condition> <echo message="DEBUG: Cleaned java.specification.version=${-cleaned.specification.version}"/>
<fail message="Minimum supported Java version is 1.7." unless="build.java.runtime"/> <echo message="DEBUG: Detected runtime: ${build.java.runtime}"/>
-->
<condition property="documentation-lint.supported"> <condition property="documentation-lint.supported">
<and> <and>
<or> <or>
@ -362,7 +387,9 @@
<!-- workaround for https://issues.apache.org/bugzilla/show_bug.cgi?id=53347 --> <!-- workaround for https://issues.apache.org/bugzilla/show_bug.cgi?id=53347 -->
<condition property="build.compiler" value="javac1.7"> <condition property="build.compiler" value="javac1.7">
<and> <and>
<equals arg1="${build.java.runtime}" arg2="1.8"/> <not>
<equals arg1="${build.java.runtime}" arg2="1.7"/>
</not>
<or> <or>
<antversion exactly="1.8.3" /> <antversion exactly="1.8.3" />
<antversion exactly="1.8.4" /> <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!"/> <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> </target>
<!-- for now disable doclint on JDK 8: --> <!-- for now disable doclint on JDK 8+: -->
<condition property="javadoc.args" value="-Xdoclint:none" else=""> <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> </condition>
<!-- Import custom ANT tasks. --> <!-- Import custom ANT tasks. -->
@ -1824,7 +1853,10 @@ ${ant.project.name}.test.dependencies=${test.classpath.list}
<!-- ECJ Javadoc linting: --> <!-- ECJ Javadoc linting: -->
<condition property="ecj-javadoc-lint.supported"> <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>
<condition property="ecj-javadoc-lint-tests.supported"> <condition property="ecj-javadoc-lint-tests.supported">

View File

@ -18,7 +18,8 @@ package org.apache.lucene.util;
*/ */
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Collections; import java.util.StringTokenizer;
/** /**
* Some useful constants. * 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_VENDOR = System.getProperty("java.vm.vendor");
public static final String JVM_VERSION = System.getProperty("java.vm.version"); 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_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>. **/ /** The value of <tt>System.getProperty("java.version")</tt>. **/
public static final String JAVA_VERSION = System.getProperty("java.version"); 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_ARCH = System.getProperty("os.arch");
public static final String OS_VERSION = System.getProperty("os.version"); public static final String OS_VERSION = System.getProperty("os.version");
public static final String JAVA_VENDOR = System.getProperty("java.vendor"); 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 */ /** True iff running on a 64bit JVM */
public static final boolean JRE_IS_64BIT; public static final boolean JRE_IS_64BIT;
static { 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; boolean is64Bit = false;
try { try {
final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe"); final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
@ -81,16 +91,10 @@ public final class Constants {
} }
} }
JRE_IS_64BIT = is64Bit; 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. * This is the internal Lucene version, including bugfix versions, recorded into each segment.