Skip to content

Commit

Permalink
Normalize version numbers in Java 9+ compatible ways. This closes #130
Browse files Browse the repository at this point in the history
  • Loading branch information
uschindler committed Oct 3, 2017
1 parent 8074729 commit 14d943e
Show file tree
Hide file tree
Showing 7 changed files with 650 additions and 632 deletions.
9 changes: 3 additions & 6 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,14 @@
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"!
- To explicitely exclude specific versions (and unknown ones), add a condition to disallow "unknown" and some versions like "9"!
-->
<condition property="build.java.runtime" value="1.9">
<equals arg1="${-cleaned.specification.version}" arg2="9"/>
</condition>
<condition property="build.java.runtime" value="${-cleaned.specification.version}" else="unknown">
<or>
<equals arg1="${-cleaned.specification.version}" arg2="1.6"/>
<equals arg1="${-cleaned.specification.version}" arg2="1.7"/>
<equals arg1="${-cleaned.specification.version}" arg2="1.8"/>
<equals arg1="${-cleaned.specification.version}" arg2="1.9"/>
<equals arg1="${-cleaned.specification.version}" arg2="9"/>
</or>
</condition>

Expand Down Expand Up @@ -135,7 +132,7 @@
<equals arg1="${build.java.runtime}" arg2="1.6"/>
<equals arg1="${build.java.runtime}" arg2="1.7"/>
<equals arg1="${build.java.runtime}" arg2="1.8"/>
<equals arg1="${build.java.runtime}" arg2="1.9"/>
<equals arg1="${build.java.runtime}" arg2="9"/>
</or>
</condition>

Expand Down
22 changes: 15 additions & 7 deletions src/main/java/de/thetaphi/forbiddenapis/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,21 @@ public static String fixTargetVersion(String name) throws ParseException {
final Matcher m = JDK_SIG_PATTERN.matcher(name);
if (m.matches()) {
if (m.group(4) == null) {
// rewrite version number if it does not start with "1"
if ("1".equals(m.group(2)) && m.group(3) != null) {
return name;
} else {
if (".0".equals(m.group(3)) || m.group(3) == null) {
return m.group(1) + "1." + m.group(2);
final String prefix = m.group(1);
final int major = Integer.parseInt(m.group(2));
final int minor = m.group(3) != null ? Integer.parseInt(m.group(3).substring(1)) : 0;
if (major == 1 && minor >= 1 && minor < 9) {
// Java 1.1 till 1.8 (aka 8):
return prefix + "1." + minor;
} else if (major > 1 && major < 9) {
// fix pre-Java9 major version to use "1.x" syntax:
if (minor == 0) {
return prefix + "1." + major;
}
} else if (major >= 9 && minor > 0) {
return prefix + major + "." + minor;
} else if (major >= 9 && minor == 0) {
return prefix + major;
}
}
throw new ParseException("Invalid bundled signature reference (JDK version is invalid): " + name);
Expand All @@ -462,7 +470,7 @@ private void addBundledSignatures(String name, String jdkTargetVersion, boolean
// use Checker.class hardcoded (not getClass) so we have a fixed package name:
InputStream in = Checker.class.getResourceAsStream("signatures/" + name + ".txt");
// automatically expand the compiler version in here (for jdk-* signatures without version):
if (in == null && jdkTargetVersion != null && name.startsWith("jdk-") && !name.matches(".*?\\-\\d\\.\\d")) {
if (in == null && jdkTargetVersion != null && name.startsWith("jdk-") && !name.matches(".*?\\-\\d+(\\.\\d+)*")) {
name = name + "-" + jdkTargetVersion;
name = fixTargetVersion(name);
in = Checker.class.getResourceAsStream("signatures/" + name + ".txt");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/thetaphi/forbiddenapis/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface Constants {

final String BS_JDK_NONPORTABLE = "jdk-non-portable";

final Pattern JDK_SIG_PATTERN = Pattern.compile("(jdk\\-.*?\\-)(\\d)(\\.\\d)?(\\.\\d)*");
final Pattern JDK_SIG_PATTERN = Pattern.compile("(jdk\\-.*?\\-)(\\d+)(\\.\\d+)?(\\.\\d+)*");

final String DEPRECATED_WARN_INTERNALRUNTIME = String.format(Locale.ENGLISH,
"The setting 'internalRuntimeForbidden' was deprecated and will be removed in next version. For backwards compatibility task/mojo is using '%s' bundled signatures instead.", BS_JDK_NONPORTABLE);
Expand Down
Loading

0 comments on commit 14d943e

Please sign in to comment.