Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP support experimental java versions #2899

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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: `<majorVersion>`;"
+ " `<majorVersion>_PREVIEW`; or `<majorVersion>-<experimentalSuffix>`");
}
}
}

public static ChosenJavaVersion of(int number) {
Expand All @@ -56,8 +86,16 @@ public boolean enablePreview() {
return enablePreview;
}

public Optional<String> 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() {
Expand All @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,33 @@ 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
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
Expand Down