Skip to content

Commit ac53452

Browse files
authored
Use reflection to work around Gradle's scanning of plugin code (#581)
* Use reflection to work around Gradle's scanning of plugin code See gradle/gradle#8411 (comment) Additionally, run a basic test on Gradle 5
1 parent 3ea8d94 commit ac53452

File tree

2 files changed

+85
-44
lines changed

2 files changed

+85
-44
lines changed

semanticdb-gradle-plugin/src/main/scala/SemanticdbGradlePlugin.scala

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.gradle.api.DefaultTask
1111
import org.gradle.api.Plugin
1212
import org.gradle.api.Project
1313
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
14+
import org.gradle.api.provider.Property
1415
import org.gradle.api.tasks.TaskAction
1516
import org.gradle.api.tasks.compile.JavaCompile
1617
import org.gradle.api.tasks.scala.ScalaCompile
@@ -79,18 +80,32 @@ class SemanticdbGradlePlugin extends Plugin[Project] {
7980
.getTasks()
8081
.withType(classOf[JavaCompile])
8182
.configureEach { task =>
82-
if (gradle.is5 || (gradle.is6 && !gradle.is6_7_plus))
83-
println(
83+
// If we run on JDK 17, we need to add special flags to the JVM
84+
// to allow access to the compiler.
85+
86+
// JDK 17 support was only introduced in 7.3 so
87+
// we don't need to do it for earlier versions
88+
// https://docs.gradle.org/current/userguide/compatibility.html
89+
if (!gradle.is5 && !gradle.is6) {
90+
type JavaCompiler = {
91+
type Metadata = {
92+
type LangVersion = {
93+
def asInt(): Int
94+
}
95+
def getLanguageVersion(): LangVersion
96+
}
97+
def getMetadata(): Metadata
98+
}
99+
type HasCompilerProperty = {
100+
def getJavaCompiler(): Property[JavaCompiler]
101+
}
102+
103+
val toolchainCompiler = Option(
84104
task
85-
.asInstanceOf[{
86-
def getToolChain(): Any
87-
}
88-
]
89-
.getToolChain()
90-
)
91-
else {
92-
val toolchainCompiler = Option(task.getJavaCompiler().getOrNull())
93-
.map(_.getMetadata().getLanguageVersion().asInt())
105+
.asInstanceOf[HasCompilerProperty]
106+
.getJavaCompiler()
107+
.getOrNull()
108+
).map(_.getMetadata().getLanguageVersion().asInt())
94109

95110
val host = System
96111
.getProperty("java.version")
@@ -339,6 +354,7 @@ class SemanticdbGradlePlugin extends Plugin[Project] {
339354
class GradleVersion(ver: String) {
340355
override def toString(): String = s"[GradleVersion: $ver]"
341356
def is7 = ver.startsWith("7.")
357+
def is8 = ver.startsWith("8.")
342358
def is6 = ver.startsWith("6.")
343359
// 6.7 introduced toolchains support https://blog.gradle.org/java-toolchains
344360
// And javaCompiler property

tests/buildTools/src/test/scala/tests/GradleBuildToolSuite.scala

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class GradleBuildToolSuite extends BaseBuildToolSuite {
99

1010
val Gradle8 = "8.1.1"
1111
val Gradle7 = "7.6.1"
12-
val Gradle67 = "6.7"
12+
val Gradle67 = "6.7" // Introduced toolchains
13+
val Gradle5 = "5.6.4"
1314

1415
val allGradle = List(Gradle8, Gradle7, Gradle67)
1516
val allJava = List("8", "11", "17")
@@ -42,7 +43,7 @@ class GradleBuildToolSuite extends BaseBuildToolSuite {
4243
{
4344
val testName = title.withName(title.name + s"-gradle$gradleV")
4445
checkBuild(
45-
if (gradleV.startsWith("6."))
46+
if (gradleV.startsWith("6.") || gradleV.startsWith("5."))
4647
testName.tag(Java8Only)
4748
else
4849
testName,
@@ -56,37 +57,61 @@ class GradleBuildToolSuite extends BaseBuildToolSuite {
5657
}
5758
}
5859

59-
List("latest" -> "implementation", "4.0" -> "compile").foreach {
60-
case (version, config) =>
61-
checkBuild(
62-
if (version == "latest")
63-
"basic-latest"
64-
else
65-
s"basic-$version".tag(Java8Only),
66-
s"""|/build.gradle
67-
|apply plugin: 'java'
68-
|repositories {
69-
| mavenCentral()
70-
|}
71-
|dependencies {
72-
| $config 'junit:junit:4.13.1'
73-
|}
74-
|/src/main/java/Example.java
75-
|import org.junit.Assert;
76-
|public class Example {}
77-
|/src/test/java/ExampleSuite.java
78-
|public class ExampleSuite {}
79-
|""".stripMargin,
80-
expectedSemanticdbFiles = 2,
81-
expectedPackages =
82-
"""|maven:junit:junit:4.13.1
83-
|maven:org.hamcrest:hamcrest-core:1.3
84-
|""".stripMargin,
85-
initCommand = {
86-
gradleVersion(version)
87-
}
88-
)
89-
}
60+
// This is the most basic test for Java/Scala support
61+
// We run it for an extended list of Gradle versions
62+
checkGradleBuild(
63+
"basic",
64+
"""|/build.gradle
65+
|plugins {
66+
| // Apply the application plugin to add support for building a CLI application in Java.
67+
| id 'application'
68+
| id 'java'
69+
| id 'scala'
70+
|}
71+
72+
|repositories {
73+
| // Use Maven Central for resolving dependencies.
74+
| mavenCentral()
75+
|}
76+
77+
|dependencies {
78+
| // This dependency is used by the application.
79+
| implementation 'com.google.guava:guava:31.1-jre'
80+
| implementation 'org.scala-lang:scala-library:2.13.8'
81+
| testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
82+
| testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
83+
|}
84+
85+
|test {
86+
| useJUnitPlatform()
87+
|}
88+
|/src/main/java/App.java
89+
| package gradle.sample.project;
90+
| public class App {
91+
| public String getGreeting() {
92+
| return "Hello World!";
93+
| }
94+
| public static void main(String[] args) {
95+
| System.out.println(new App().getGreeting());
96+
| }
97+
| }
98+
|/src/test/java/AppTest.java
99+
| package gradle.sample.project;
100+
| import org.junit.jupiter.api.Test;
101+
| import static org.junit.jupiter.api.Assertions.assertEquals;
102+
| import static org.junit.jupiter.api.Assertions.assertNotNull;
103+
| public class AppTest {
104+
| @Test public void appHasAGreeting() {
105+
| App classUnderTest = new App();
106+
| assertNotNull("app should have a greeting", classUnderTest.getGreeting());
107+
| }
108+
| }
109+
|/src/main/scala/Howdy.scala
110+
|case class Howdy(a: Int)
111+
|""".stripMargin,
112+
expectedSemanticdbFiles = 3,
113+
gradleVersions = allGradle :+ Gradle5
114+
)
90115

91116
List("3.3", "2.2.1").foreach { version =>
92117
checkBuild(

0 commit comments

Comments
 (0)