Kotlin Facet: Fix detection of source set name by Gradle compile task

#KT-17492 Fixed
This commit is contained in:
Alexey Sedunov
2017-06-14 14:50:57 +03:00
parent 1544467725
commit eaea160f0e
2 changed files with 134 additions and 14 deletions
@@ -24,7 +24,6 @@ import org.jetbrains.plugins.gradle.tooling.ModelBuilderService
import java.io.Serializable
import java.lang.Exception
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.util.*
import kotlin.collections.HashSet
@@ -104,18 +103,6 @@ class KotlinGradleModelBuilder : ModelBuilderService {
return result
}
private fun Class<*>.findGetterMethod(name: String): Method? {
generateSequence(this) { it.superclass }.forEach {
try {
return it.getDeclaredMethod(name)
}
catch(e: Exception) {
// Check next super class
}
}
return null
}
@Suppress("UNCHECKED_CAST")
private fun collectCompilerArguments(
compileTask: Task,
@@ -124,7 +111,7 @@ class KotlinGradleModelBuilder : ModelBuilderService {
) {
val taskClass = compileTask::class.java
val sourceSetName = try {
taskClass.findGetterMethod("getSourceSetName\$kotlin_gradle_plugin")?.invoke(compileTask) as? String
taskClass.methods.firstOrNull { it.name.startsWith("getSourceSetName") && it.parameterCount == 0 }?.invoke(compileTask) as? String
} catch (e : InvocationTargetException) {
null // can be thrown if property is not initialized yet
} ?: "main"
@@ -90,6 +90,67 @@ class GradleFacetImportTest : GradleImportingTestCase() {
}
}
@Test
fun testJvmImport_1_1_2() {
createProjectSubFile("build.gradle", """
group 'Again'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
maven {
url 'http://dl.bintray.com/kotlin/kotlin-dev'
}
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2-5")
}
}
apply plugin: 'kotlin'
repositories {
mavenCentral()
maven { url 'http://dl.bintray.com/kotlin/kotlin-dev' }
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.2-5"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.7"
kotlinOptions.freeCompilerArgs = ["-Xsingle-module", "-Xdump-declarations-to", "tmp"]
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.6"
kotlinOptions.apiVersion = "1.0"
kotlinOptions.freeCompilerArgs = ["-Xdump-declarations-to", "tmpTest"]
}
""")
importProject()
with (facetSettings) {
Assert.assertEquals("1.1", languageLevel!!.versionString)
Assert.assertEquals("1.1", apiLevel!!.versionString)
Assert.assertEquals(TargetPlatformKind.Jvm[JvmTarget.JVM_1_8], targetPlatformKind)
Assert.assertEquals("1.7", (compilerArguments as K2JVMCompilerArguments).jvmTarget)
Assert.assertEquals("-Xdump-declarations-to=tmp -Xsingle-module",
compilerSettings!!.additionalArguments)
}
with (testFacetSettings) {
Assert.assertEquals("1.1", languageLevel!!.versionString)
Assert.assertEquals("1.0", apiLevel!!.versionString)
Assert.assertEquals(TargetPlatformKind.Jvm[JvmTarget.JVM_1_6], targetPlatformKind)
Assert.assertEquals("1.6", (compilerArguments as K2JVMCompilerArguments).jvmTarget)
Assert.assertEquals("-Xdump-declarations-to=tmpTest",
compilerSettings!!.additionalArguments)
}
}
@Test
fun testJvmImportWithCustomSourceSets() {
createProjectSubFile("build.gradle", """
@@ -159,6 +220,78 @@ class GradleFacetImportTest : GradleImportingTestCase() {
}
}
@Test
fun testJvmImportWithCustomSourceSets_1_1_2() {
createProjectSubFile("build.gradle", """
group 'Again'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
maven { url 'http://dl.bintray.com/kotlin/kotlin-dev' }
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2-5")
}
}
apply plugin: 'kotlin'
repositories {
mavenCentral()
maven { url 'http://dl.bintray.com/kotlin/kotlin-dev' }
}
sourceSets {
myMain {
kotlin {
srcDir 'src'
}
}
myTest {
kotlin {
srcDir 'test'
}
}
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.2-5"
}
compileMyMainKotlin {
kotlinOptions.jvmTarget = "1.7"
kotlinOptions.freeCompilerArgs = ["-Xsingle-module", "-Xdump-declarations-to", "tmp"]
}
compileMyTestKotlin {
kotlinOptions.jvmTarget = "1.6"
kotlinOptions.apiVersion = "1.0"
kotlinOptions.freeCompilerArgs = ["-Xdump-declarations-to", "tmpTest"]
}
""")
importProject()
with (facetSettings("project_myMain")) {
Assert.assertEquals("1.1", languageLevel!!.versionString)
Assert.assertEquals("1.1", apiLevel!!.versionString)
Assert.assertEquals(TargetPlatformKind.Jvm[JvmTarget.JVM_1_8], targetPlatformKind)
Assert.assertEquals("1.7", (compilerArguments as K2JVMCompilerArguments).jvmTarget)
Assert.assertEquals("-Xdump-declarations-to=tmp -Xsingle-module",
compilerSettings!!.additionalArguments)
}
with (facetSettings("project_myTest")) {
Assert.assertEquals("1.1", languageLevel!!.versionString)
Assert.assertEquals("1.0", apiLevel!!.versionString)
Assert.assertEquals(TargetPlatformKind.Jvm[JvmTarget.JVM_1_6], targetPlatformKind)
Assert.assertEquals("1.6", (compilerArguments as K2JVMCompilerArguments).jvmTarget)
Assert.assertEquals("-Xdump-declarations-to=tmpTest",
compilerSettings!!.additionalArguments)
}
}
@Test
fun testCoroutineImportByOptions() {
createProjectSubFile("build.gradle", """