Maven inspection for incompatible coroutines libraries (KT-25251)

#KT-25251 In Progress
This commit is contained in:
Nikolay Krasko
2018-07-17 16:25:43 +03:00
parent 3bb9288e86
commit adfa469b21
7 changed files with 221 additions and 35 deletions
@@ -13,6 +13,8 @@ import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.idea.configuration.getWholeModuleGroup
import org.jetbrains.kotlin.idea.inspections.gradle.GradleHeuristicHelper.PRODUCTION_DEPENDENCY_STATEMENTS
import org.jetbrains.kotlin.idea.inspections.migration.DEPRECATED_COROUTINES_LIBRARIES_INFORMATION
import org.jetbrains.kotlin.idea.inspections.migration.DeprecatedForKotlinLibInfo
import org.jetbrains.kotlin.idea.project.languageVersionSettings
import org.jetbrains.kotlin.idea.versions.LibInfo
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
@@ -24,41 +26,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrC
private val LibInfo.gradleMarker get() = "$groupId:$name:"
private data class DeprecatedForKotlinLibInfo(
val lib: LibInfo,
val sinceKotlinLanguageVersion: LanguageVersion,
val message: String
)
@Suppress("SpellCheckingInspection")
private fun kotlinxCoroutinesDeprecation(name: String): org.jetbrains.kotlin.idea.inspections.gradle.DeprecatedForKotlinLibInfo {
return DeprecatedForKotlinLibInfo(
lib = LibInfo("org.jetbrains.kotlinx", name),
sinceKotlinLanguageVersion = LanguageVersion.KOTLIN_1_3,
message = "Library should be updated to be compatible with Kotlin 1.3"
)
}
@Suppress("SpellCheckingInspection")
private val DEPRECATED_COROUTINES_LIBRARIES_INFORMATION = listOf(
kotlinxCoroutinesDeprecation("kotlinx-coroutines"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-android"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-core"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-core-common"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-core-js"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-guava"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-io"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-javafx"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-jdk8"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-nio"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-quasar"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-reactive"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-reactor"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-rx1"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-rx2"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-swing")
)
@Suppress("SpellCheckingInspection")
class GradleKotlinxCoroutinesDeprecationInspection : GradleBaseInspection(), CleanupLocalInspectionTool {
override fun buildVisitor(): BaseInspectionVisitor = DependencyFinder()
@@ -0,0 +1,67 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.maven.inspections
import com.intellij.codeInspection.CleanupLocalInspectionTool
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.openapi.module.Module
import com.intellij.util.xml.DomFileElement
import com.intellij.util.xml.highlighting.DomElementAnnotationHolder
import com.intellij.util.xml.highlighting.DomElementsInspection
import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel
import org.jetbrains.idea.maven.model.MavenId
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.idea.configuration.getWholeModuleGroup
import org.jetbrains.kotlin.idea.inspections.migration.DEPRECATED_COROUTINES_LIBRARIES_INFORMATION
import org.jetbrains.kotlin.idea.maven.PomFile
import org.jetbrains.kotlin.idea.maven.findDependencies
import org.jetbrains.kotlin.idea.project.languageVersionSettings
class MavenCoroutinesDeprecationInspection :
DomElementsInspection<MavenDomProjectModel>(MavenDomProjectModel::class.java), CleanupLocalInspectionTool {
override fun checkFileElement(domFileElement: DomFileElement<MavenDomProjectModel>?, holder: DomElementAnnotationHolder?) {
if (domFileElement == null || holder == null) return
val file = domFileElement.file
val module = domFileElement.module ?: return
val pomFile = PomFile.forFileOrNull(file) ?: return
for (libInfo in DEPRECATED_COROUTINES_LIBRARIES_INFORMATION) {
if (!checkKotlinVersion(module, libInfo.sinceKotlinLanguageVersion)) {
// Same result will be for all invocations in this file, so exit
return
}
val libMavenId = MavenId(libInfo.lib.groupId, libInfo.lib.name, null)
val moduleDependencies = pomFile.findDependencies(libMavenId)
val dependencyManagementDependencies = pomFile.domModel.dependencyManagement.dependencies.findDependencies(libMavenId)
for (dependency in moduleDependencies + dependencyManagementDependencies) {
val xmlElement = dependency.version.xmlElement
if (xmlElement != null) {
holder.createProblem(
dependency.version,
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
libInfo.message,
null
)
}
}
}
}
companion object {
private fun checkKotlinVersion(module: Module, languageVersion: LanguageVersion): Boolean {
val moduleGroup = module.getWholeModuleGroup()
return moduleGroup.sourceRootModules.any { moduleInGroup ->
moduleInGroup.languageVersionSettings.languageVersion >= languageVersion
}
}
}
}
@@ -49,6 +49,11 @@ public class KotlinMavenInspectionTestGenerated extends AbstractKotlinMavenInspe
runTest("idea/idea-maven/testData/maven-inspections/deprecatedJreWithDependencyManagement.xml");
}
@TestMetadata("deprecatedKotlinxCoroutines.xml")
public void testDeprecatedKotlinxCoroutines() throws Exception {
runTest("idea/idea-maven/testData/maven-inspections/deprecatedKotlinxCoroutines.xml");
}
@TestMetadata("ideAndMavenVersions.xml")
public void testIdeAndMavenVersions() throws Exception {
runTest("idea/idea-maven/testData/maven-inspections/ideAndMavenVersions.xml");
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>maventest</groupId>
<artifactId>maventest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<kotlin.version>1.2.0</kotlin.version>
<kotlin.compiler.languageVersion>1.3</kotlin.compiler.languageVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk7</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-android</artifactId>
<version>0.23.4</version>
</dependency>
</dependencies>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-kotlin-kotlin-eap</id>
<name>bintray</name>
<url>https://dl.bintray.com/kotlin/kotlin-eap</url>
</repository>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-kotlin-kotlinx</id>
<name>bintray</name>
<url>https://kotlin.bintray.com/kotlinx</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-kotlin-kotlin-eap</id>
<name>bintray-plugins</name>
<url>https://dl.bintray.com/kotlin/kotlin-eap</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
</plugins>
</build>
</project>
<!-- inspection: org.jetbrains.kotlin.idea.maven.inspections.MavenCoroutinesDeprecationInspection -->
<!-- problem: on 0.23.4, title Library should be updated to be compatible with Kotlin 1.3 -->
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports libraries dependencies in Maven that should be updated in order to be compatible with Kotlin 1.3+.
</body>
</html>
+9
View File
@@ -78,6 +78,15 @@
hasStaticDescription="true"
level="WARNING" />
<localInspection implementationClass="org.jetbrains.kotlin.idea.maven.inspections.MavenCoroutinesDeprecationInspection"
displayName="Maven dependency is incompatible with Kotlin 1.3+ and should be updated"
groupPath="Kotlin,Migration"
groupName="Maven"
enabledByDefault="true"
language="XML"
hasStaticDescription="true"
level="ERROR" />
<intentionAction>
<className>org.jetbrains.kotlin.idea.maven.actions.MavenPluginSourcesMoveToBuild</className>
<category>Kotlin</category>
@@ -0,0 +1,44 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.inspections.migration
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.idea.versions.LibInfo
data class DeprecatedForKotlinLibInfo(
val lib: LibInfo,
val sinceKotlinLanguageVersion: LanguageVersion,
val message: String
)
@Suppress("SpellCheckingInspection")
private fun kotlinxCoroutinesDeprecation(name: String): DeprecatedForKotlinLibInfo {
return DeprecatedForKotlinLibInfo(
lib = LibInfo("org.jetbrains.kotlinx", name),
sinceKotlinLanguageVersion = LanguageVersion.KOTLIN_1_3,
message = "Library should be updated to be compatible with Kotlin 1.3"
)
}
@Suppress("SpellCheckingInspection")
val DEPRECATED_COROUTINES_LIBRARIES_INFORMATION = listOf(
kotlinxCoroutinesDeprecation("kotlinx-coroutines"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-android"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-core"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-core-common"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-core-js"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-guava"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-io"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-javafx"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-jdk8"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-nio"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-quasar"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-reactive"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-reactor"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-rx1"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-rx2"),
kotlinxCoroutinesDeprecation("kotlinx-coroutines-swing")
)