diff --git a/gradle-baseline-java/src/main/java/com/palantir/baseline/plugins/javaversions/ChosenJavaVersion.java b/gradle-baseline-java/src/main/java/com/palantir/baseline/plugins/javaversions/ChosenJavaVersion.java index 78876ce64..ac8d1b8ba 100644 --- a/gradle-baseline-java/src/main/java/com/palantir/baseline/plugins/javaversions/ChosenJavaVersion.java +++ b/gradle-baseline-java/src/main/java/com/palantir/baseline/plugins/javaversions/ChosenJavaVersion.java @@ -16,8 +16,12 @@ package com.palantir.baseline.plugins.javaversions; +import com.google.common.base.Strings; import java.io.Serializable; import java.util.Objects; +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.gradle.jvm.toolchain.JavaLanguageVersion; /** @@ -26,18 +30,44 @@ */ public final class ChosenJavaVersion implements Serializable { + private static final Pattern PREVIEW_PATTERN = Pattern.compile("^(\\d+)_PREVIEW$"); + private static final Pattern EXPERIMENTAL_PATTERN = Pattern.compile("^(\\d+)-(.*)$"); + private final JavaLanguageVersion javaLanguageVersion; private final boolean enablePreview; + private final String experimentalSuffix; public ChosenJavaVersion(JavaLanguageVersion javaLanguageVersion, boolean enablePreview) { this.javaLanguageVersion = javaLanguageVersion; this.enablePreview = enablePreview; + this.experimentalSuffix = ""; } - /** Accepts inputs like '17_PREVIEW' or '17'. */ + public ChosenJavaVersion(JavaLanguageVersion javaLanguageVersion, String experimentalSuffix) { + this.javaLanguageVersion = javaLanguageVersion; + this.enablePreview = true; + this.experimentalSuffix = experimentalSuffix; + } + + /** Accepts inputs like '17_PREVIEW' or '17', or '24-loom-experimental'. */ public static ChosenJavaVersion fromString(String string) { - return new ChosenJavaVersion( - JavaLanguageVersion.of(string.replaceAll("_PREVIEW", "")), string.endsWith("_PREVIEW")); + try { + return new ChosenJavaVersion(JavaLanguageVersion.of(string), false); + } catch (NumberFormatException e) { + // not a JDK release version, check for preview or experimental suffix + Matcher previewMatcher = PREVIEW_PATTERN.matcher(string); + Matcher experimentalMatcher = EXPERIMENTAL_PATTERN.matcher(string); + if (previewMatcher.matches()) { + return new ChosenJavaVersion(JavaLanguageVersion.of(previewMatcher.group(1)), true); + } else if (experimentalMatcher.matches()) { + String experimentalSuffix = "-" + experimentalMatcher.group(2); + return new ChosenJavaVersion(JavaLanguageVersion.of(experimentalMatcher.group(1)), experimentalSuffix); + } else { + throw new IllegalArgumentException( + "wrong format for ChosenJavaVersion, must be one of: ``;" + + " `_PREVIEW`; or `-`"); + } + } } public static ChosenJavaVersion of(int number) { @@ -56,8 +86,16 @@ public boolean enablePreview() { return enablePreview; } + public Optional experimentalSuffix() { + return Strings.isNullOrEmpty(experimentalSuffix) ? Optional.empty() : Optional.of(experimentalSuffix); + } + public String asIdeaLanguageLevel() { - return "JDK_" + javaLanguageVersion.toString() + (enablePreview ? "_PREVIEW" : ""); + if (!Strings.isNullOrEmpty(experimentalSuffix)) { + return "JDK_X"; + } else { + return "JDK_" + javaLanguageVersion.toString() + (enablePreview ? "_PREVIEW" : ""); + } } public int asBytecodeMajorVersion() { @@ -68,7 +106,11 @@ public int asBytecodeMajorVersion() { @Override public String toString() { - return javaLanguageVersion.toString() + (enablePreview ? "_PREVIEW" : ""); + if (!Strings.isNullOrEmpty(experimentalSuffix)) { + return javaLanguageVersion.toString() + experimentalSuffix; + } else { + return javaLanguageVersion.toString() + (enablePreview ? "_PREVIEW" : ""); + } } @Override @@ -80,11 +122,13 @@ public boolean equals(Object other) { return false; } ChosenJavaVersion that = (ChosenJavaVersion) other; - return enablePreview == that.enablePreview && javaLanguageVersion.equals(that.javaLanguageVersion); + return enablePreview == that.enablePreview + && Objects.equals(javaLanguageVersion, that.javaLanguageVersion) + && Objects.equals(experimentalSuffix, that.experimentalSuffix); } @Override public int hashCode() { - return Objects.hash(javaLanguageVersion, enablePreview); + return Objects.hash(javaLanguageVersion, enablePreview, experimentalSuffix); } } diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/javaversions/ChosenJavaVersionTest.java b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/javaversions/ChosenJavaVersionTest.java index 7ac0523a0..18b626323 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/javaversions/ChosenJavaVersionTest.java +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/javaversions/ChosenJavaVersionTest.java @@ -35,13 +35,24 @@ void deserialization_produces_sensible_toString() { assertThat(ChosenJavaVersion.fromString("11_PREVIEW")).hasToString("11_PREVIEW"); assertThat(ChosenJavaVersion.fromString("11_PREVIEW").enablePreview()).isTrue(); + assertThat(ChosenJavaVersion.fromString("11_PREVIEW").experimentalSuffix()) + .isEmpty(); assertThat(ChosenJavaVersion.fromString("11_PREVIEW").javaLanguageVersion()) .isEqualTo(JavaLanguageVersion.of(11)); + assertThat(ChosenJavaVersion.fromString("24-loom-experimental")).hasToString("24-loom-experimental"); + assertThat(ChosenJavaVersion.fromString("24-loom-experimental").enablePreview()) + .isTrue(); + assertThat(ChosenJavaVersion.fromString("24-loom-experimental").experimentalSuffix()) + .isPresent() + .contains("-loom-experimental"); + assertThat(ChosenJavaVersion.fromString("24-loom-experimental").javaLanguageVersion()) + .isEqualTo(JavaLanguageVersion.of(24)); + assertThat(ChosenJavaVersion.fromString("17_PREVIEW")).hasToString("17_PREVIEW"); assertThat(ChosenJavaVersion.fromString("33")).hasToString("33"); - assertThatThrownBy(() -> ChosenJavaVersion.fromString("1.5")).isInstanceOf(NumberFormatException.class); + assertThatThrownBy(() -> ChosenJavaVersion.fromString("1.5")).isInstanceOf(IllegalArgumentException.class); } @Test @@ -49,6 +60,8 @@ void idea_language_level() { assertThat(ChosenJavaVersion.of(11).asIdeaLanguageLevel()).isEqualTo("JDK_11"); assertThat(ChosenJavaVersion.fromString("17_PREVIEW").asIdeaLanguageLevel()) .isEqualTo("JDK_17_PREVIEW"); + assertThat(ChosenJavaVersion.fromString("24-loom-experimental").asIdeaLanguageLevel()) + .isEqualTo("JDK_X"); } @Test