From 0b2d96c1ef5931f2bc94709d66b7dac552b0107a Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Sat, 6 Jun 2020 00:16:43 +0300 Subject: [PATCH] Refactor Kotlin classes registration for java-library plugin --- .../kotlin/gradle/plugin/KotlinPlugin.kt | 39 +++++-------------- .../kotlin/gradle/utils/reflectionUtils.kt | 31 --------------- 2 files changed, 9 insertions(+), 61 deletions(-) delete mode 100644 libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/utils/reflectionUtils.kt diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPlugin.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPlugin.kt index 8cd118169a2..9b2b630a24e 100755 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPlugin.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPlugin.kt @@ -37,7 +37,6 @@ import org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin import org.jetbrains.kotlin.gradle.internal.KaptVariantData import org.jetbrains.kotlin.gradle.internal.checkAndroidAnnotationProcessorDependencyUsage import org.jetbrains.kotlin.gradle.logging.kotlinDebug -import org.jetbrains.kotlin.gradle.logging.kotlinWarn import org.jetbrains.kotlin.gradle.model.builder.KotlinModelBuilder import org.jetbrains.kotlin.gradle.plugin.mpp.* import org.jetbrains.kotlin.gradle.scripting.internal.ScriptingGradleSubplugin @@ -228,40 +227,20 @@ internal class Kotlin2JvmSourceSetProcessor( configureJavaTask(kotlinTask, javaTask) } - if (project.pluginManager.hasPlugin("java-library") && sourceSetName == SourceSet.MAIN_SOURCE_SET_NAME) { - registerKotlinOutputForJavaLibrary(kotlinTaskInstance.destinationDir, kotlinTaskInstance) + if (sourceSetName == SourceSet.MAIN_SOURCE_SET_NAME) { + project.pluginManager.withPlugin("java-library") { + registerKotlinOutputForJavaLibrary(kotlinTask.map { it.destinationDir }, kotlinTask) + } } } } - private fun registerKotlinOutputForJavaLibrary(outputDir: File, taskDependency: Task): Boolean { + private fun registerKotlinOutputForJavaLibrary(outputDir: Provider, taskDependency: TaskProvider<*>) { val configuration = project.configurations.getByName("apiElements") - - checkedReflection({ - val getOutgoing = configuration.javaClass.getMethod("getOutgoing") - val outgoing = getOutgoing(configuration) - - val getVariants = outgoing.javaClass.getMethod("getVariants") - val variants = getVariants(outgoing) as NamedDomainObjectCollection<*> - - val variant = variants.getByName("classes") - - val artifactMethod = variant.javaClass.getMethod("artifact", Any::class.java) - - val artifactMap = mapOf( - "file" to outputDir, - "type" to "java-classes-directory", - "builtBy" to taskDependency - ) - - artifactMethod(variant, artifactMap) - - return true - - }, { reflectException -> - logger.kotlinWarn("Could not register Kotlin output of source set $sourceSetName for java-library: $reflectException") - return false - }) + configuration.outgoing.variants.getByName("classes").artifact(outputDir) { + it.builtBy(taskDependency) + it.type = "java-classes-directory" + } } } diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/utils/reflectionUtils.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/utils/reflectionUtils.kt deleted file mode 100644 index 97be6ceb9ad..00000000000 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/utils/reflectionUtils.kt +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2010-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.gradle.utils - -import java.lang.reflect.InvocationTargetException - -internal inline fun checkedReflection(block: () -> T, onReflectionException: (Exception) -> T): T { - return try { - block() - } catch (e: InvocationTargetException) { - throw e.targetException - } catch (e: ReflectiveOperationException) { - onReflectionException(e) - } catch (e: IllegalArgumentException) { - onReflectionException(e) - } -} \ No newline at end of file