Skip to content

Commit

Permalink
Make JRE parsing more robust (#694)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Sep 12, 2020
2 parents d0d2170 + 7cc232e commit 511b18b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Fixed
* Improved JRE parsing to handle strings like `16-loom` (fixes [#693](https://github.com/diffplug/spotless/issues/693)).

## [2.6.0] - 2020-09-11
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
Expand Down Expand Up @@ -81,7 +83,14 @@ public static FormatterStep create(String version, String style, Provisioner pro
if (jre.startsWith("1.8")) {
JRE_VERSION = 8;
} else {
JRE_VERSION = Integer.parseInt(jre.substring(0, jre.indexOf('.')));
Matcher matcher = Pattern.compile("(\\d+)").matcher(jre);
if (!matcher.find()) {
throw new IllegalArgumentException("Expected " + jre + " to start with an integer");
}
JRE_VERSION = Integer.parseInt(matcher.group(1));
if (JRE_VERSION <= 8) {
throw new IllegalArgumentException("Expected " + jre + " to start with an integer greater than 8");
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Fixed
* Improved JRE parsing to handle strings like `16-loom` (fixes [#693](https://github.com/diffplug/spotless/issues/693)).

## [5.5.0] - 2020-09-11
### Added
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Fixed
* Improved JRE parsing to handle strings like `16-loom` (fixes [#693](https://github.com/diffplug/spotless/issues/693)).

## [2.3.0] - 2020-09-11
### Added
Expand Down
18 changes: 13 additions & 5 deletions testlib/src/main/java/com/diffplug/spotless/JreVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@
package com.diffplug.spotless;

import java.util.function.IntPredicate;

import com.diffplug.common.base.StandardSystemProperty;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JreVersion {
private JreVersion() {}

/** Returns the major version of this VM, e.g. 8, 9, 10, 11, 13, etc. */
public static int thisVm() {
String jvmVersion = StandardSystemProperty.JAVA_VERSION.value();
if (jvmVersion.startsWith("1.8.")) {
String jre = System.getProperty("java.version");
if (jre.startsWith("1.8")) {
return 8;
} else {
return Integer.parseInt(jvmVersion.substring(0, jvmVersion.indexOf('.')));
Matcher matcher = Pattern.compile("(\\d+)").matcher(jre);
if (!matcher.find()) {
throw new IllegalArgumentException("Expected " + jre + " to start with an integer");
}
int version = Integer.parseInt(matcher.group(1));
if (version <= 8) {
throw new IllegalArgumentException("Expected " + jre + " to start with an integer greater than 8");
}
return version;
}
}

Expand Down

0 comments on commit 511b18b

Please sign in to comment.