Skip to content

Commit

Permalink
f-x - Get java version compatibility only from JavaCompile task (#188)
Browse files Browse the repository at this point in the history
Co-authored-by: Arthur McGibbon <[email protected]>
  • Loading branch information
Arthurm1 and Arthur McGibbon authored Oct 18, 2024
1 parent e06306a commit 0654447
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.microsoft.java.bs.gradle.model.GradleModuleDependency;
import com.microsoft.java.bs.gradle.model.JavaExtension;
import com.microsoft.java.bs.gradle.model.SupportedLanguage;
import com.microsoft.java.bs.gradle.model.SupportedLanguages;
import com.microsoft.java.bs.gradle.model.impl.DefaultJavaExtension;

import org.gradle.api.Project;
import org.gradle.api.file.Directory;
Expand All @@ -32,9 +34,6 @@
import org.gradle.plugins.ide.internal.tooling.java.DefaultInstalledJdk;
import org.gradle.util.GradleVersion;

import com.microsoft.java.bs.gradle.model.SupportedLanguages;
import com.microsoft.java.bs.gradle.model.impl.DefaultJavaExtension;

/**
* The language model builder for Java language.
*/
Expand Down Expand Up @@ -67,8 +66,10 @@ public DefaultJavaExtension getExtensionFor(Project project, SourceSet sourceSet

List<String> compilerArgs = getCompilerArgs(javaCompile);
extension.setCompilerArgs(compilerArgs);
extension.setSourceCompatibility(getSourceCompatibility(compilerArgs));
extension.setTargetCompatibility(getTargetCompatibility(compilerArgs));
// Ignore options and get source/target compatibility directly from javaCompile.
extension.setSourceCompatibility(javaCompile.getSourceCompatibility());
extension.setTargetCompatibility(javaCompile.getTargetCompatibility());

return extension;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.ArrayList;

import com.microsoft.java.bs.gradle.model.GradleSourceSet;
import com.microsoft.java.bs.gradle.model.GradleSourceSets;
Expand Down Expand Up @@ -57,8 +56,8 @@ private GradleSourceSets getGradleSourceSets(ProjectConnection connect) throws I
.addArguments("-Dorg.gradle.logging.level=quiet")
.addJvmArguments("-Dbsp.gradle.supportedLanguages="
+ String.join(",", SupportedLanguages.allBspNames));
GradleSourceSets sourceSets = action.run();
return new DefaultGradleSourceSets(new ArrayList<>(sourceSets.getGradleSourceSets()));

return new DefaultGradleSourceSets(action.run());
}

private interface ConnectionConsumer {
Expand Down Expand Up @@ -161,7 +160,7 @@ static Stream<GradleVersion> versionProvider() {
// JDK source/target options changed from 1.9 -> 9 in 8.0
new GradleJreVersion("8.0", 19),
// highest supported version
new GradleJreVersion("8.8", 22)
new GradleJreVersion("8.10.2", 22)
).filter(version -> version.jreVersion >= javaVersion)
.map(GradleJreVersion::getGradleVersion);
}
Expand Down Expand Up @@ -340,9 +339,10 @@ void testJavaCompilerArgs2(GradleVersion gradleVersion) throws IOException {
assertFalse(args.contains("|-target|"), () -> "Available args: " + args);
assertTrue(args.contains("|--release|9"), () -> "Available args: " + args);
assertTrue(args.contains("|-Xlint:all"), () -> "Available args: " + args);
assertEquals("9", javaExtension.getSourceCompatibility(),
() -> "Available args: " + args);
assertEquals("9", javaExtension.getTargetCompatibility(),
String version9 = gradleVersion.compareTo(GradleVersion.version("8.0")) >= 0 ? "9" : "1.9";
assertEquals(version9, javaExtension.getSourceCompatibility(),
() -> "Available args: " + args);
assertEquals(version9, javaExtension.getTargetCompatibility(),
() -> "Available args: " + args);
}
});
Expand All @@ -363,10 +363,10 @@ void testJavaCompilerArgs3(GradleVersion gradleVersion) throws IOException {
assertFalse(args.contains("|-target|"), () -> "Available args: " + args);
assertTrue(args.contains("|--release|9"), () -> "Available args: " + args);
assertTrue(args.contains("|-Xlint:all"), () -> "Available args: " + args);
assertEquals("9", javaExtension.getSourceCompatibility(),
() -> "Available args: " + args);
assertEquals("9", javaExtension.getTargetCompatibility(),
() -> "Available args: " + args);
assertFalse(javaExtension.getSourceCompatibility().isEmpty(),
() -> "Available args: " + args);
assertFalse(javaExtension.getTargetCompatibility().isEmpty(),
() -> "Available args: " + args);
}
});
}
Expand All @@ -386,10 +386,10 @@ void testJavaCompilerArgs4(GradleVersion gradleVersion) throws IOException {
assertTrue(args.contains("|--source|1.8"), () -> "Available args: " + args);
assertTrue(args.contains("|--target|9"), () -> "Available args: " + args);
assertTrue(args.contains("|-Xlint:all"), () -> "Available args: " + args);
assertEquals("1.8", javaExtension.getSourceCompatibility(),
() -> "Available args: " + args);
assertEquals("9", javaExtension.getTargetCompatibility(),
() -> "Available args: " + args);
assertFalse(javaExtension.getSourceCompatibility().isEmpty(),
() -> "Available args: " + args);
assertFalse(javaExtension.getTargetCompatibility().isEmpty(),
() -> "Available args: " + args);
}
});
}
Expand All @@ -409,10 +409,10 @@ void testJavaCompilerArgs5(GradleVersion gradleVersion) throws IOException {
assertTrue(args.contains("|-source|1.8"), () -> "Available args: " + args);
assertTrue(args.contains("|-target|9"), () -> "Available args: " + args);
assertTrue(args.contains("|-Xlint:all"), () -> "Available args: " + args);
assertEquals("1.8", javaExtension.getSourceCompatibility(),
() -> "Available args: " + args);
assertEquals("9", javaExtension.getTargetCompatibility(),
() -> "Available args: " + args);
assertFalse(javaExtension.getSourceCompatibility().isEmpty(),
() -> "Available args: " + args);
assertFalse(javaExtension.getTargetCompatibility().isEmpty(),
() -> "Available args: " + args);
}
});
}
Expand Down Expand Up @@ -465,6 +465,31 @@ void testJavaCompilerArgsToolchain(GradleVersion gradleVersion) throws IOExcepti
});
}

@ParameterizedTest(name = "testJavaSourceTarget {0}")
@MethodSource("versionProvider")
void testJavaSourceTarget(GradleVersion gradleVersion) throws IOException {
// `java` cannot be used before 5.0
assumeTrue(gradleVersion.compareTo(GradleVersion.version("5.0")) >= 0);
withSourceSets("java-source-target", gradleVersion, gradleSourceSets -> {
assertEquals(2, gradleSourceSets.getGradleSourceSets().size());
for (GradleSourceSet gradleSourceSet : gradleSourceSets.getGradleSourceSets()) {
JavaExtension javaExtension = SupportedLanguages.JAVA.getExtension(gradleSourceSet);
assertNotNull(javaExtension);
String args = "|" + String.join("|", javaExtension.getCompilerArgs());
assertFalse(args.contains("|--source|"), () -> "Available args: " + args);
assertFalse(args.contains("|--target|"), () -> "Available args: " + args);
assertFalse(args.contains("|-source|"), () -> "Available args: " + args);
assertFalse(args.contains("|-target|"), () -> "Available args: " + args);
assertTrue(args.contains("|--release|11"), () -> "Available args: " + args);
String version9 = gradleVersion.compareTo(GradleVersion.version("8.0")) >= 0 ? "9" : "1.9";
assertEquals(version9, javaExtension.getSourceCompatibility(),
() -> "Available args: " + args);
assertEquals("1.8", javaExtension.getTargetCompatibility(),
() -> "Available args: " + args);
}
});
}

@ParameterizedTest(name = "testScala2ModelBuilder {0}")
@MethodSource("versionProvider")
void testScala2ModelBuilder(GradleVersion gradleVersion) throws IOException {
Expand Down
12 changes: 12 additions & 0 deletions testProjects/java-source-target/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
id 'java'
}

java {
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_9
}

tasks.withType(JavaCompile) {
options.compilerArgs.addAll(['--release', '11'])
}
1 change: 1 addition & 0 deletions testProjects/java-source-target/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'java-source-target'

0 comments on commit 0654447

Please sign in to comment.