Add version replace fix for kotlinx coroutines in Maven (KT-25251)

This commit is contained in:
Nikolay Krasko
2018-07-19 15:03:48 +03:00
parent 481c428881
commit f1463b4a2e
4 changed files with 238 additions and 14 deletions
@@ -6,16 +6,21 @@
package org.jetbrains.kotlin.idea.maven.inspections
import com.intellij.codeInspection.CleanupLocalInspectionTool
import com.intellij.codeInspection.LocalQuickFix
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.MavenDomDependency
import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel
import org.jetbrains.idea.maven.model.MavenId
import org.jetbrains.idea.maven.project.MavenProjectsManager
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.idea.configuration.getWholeModuleGroup
import org.jetbrains.kotlin.idea.inspections.ReplaceStringInDocumentFix
import org.jetbrains.kotlin.idea.inspections.migration.DEPRECATED_COROUTINES_LIBRARIES_INFORMATION
import org.jetbrains.kotlin.idea.inspections.migration.DeprecatedForKotlinLibInfo
import org.jetbrains.kotlin.idea.maven.PomFile
import org.jetbrains.kotlin.idea.maven.findDependencies
import org.jetbrains.kotlin.idea.project.languageVersionSettings
@@ -28,35 +33,72 @@ class MavenCoroutinesDeprecationInspection :
val file = domFileElement.file
val module = domFileElement.module ?: return
val manager = MavenProjectsManager.getInstance(module.project) ?: return
val mavenProject = manager.findProject(module) ?: return
val pomFile = PomFile.forFileOrNull(file) ?: return
for (libInfo in DEPRECATED_COROUTINES_LIBRARIES_INFORMATION) {
if (!checkKotlinVersion(module, libInfo.sinceKotlinLanguageVersion)) {
for (deprecatedLibInfo in DEPRECATED_COROUTINES_LIBRARIES_INFORMATION) {
if (!checkKotlinVersion(module, deprecatedLibInfo.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 libMavenId = MavenId(deprecatedLibInfo.lib.groupId, deprecatedLibInfo.lib.name, null)
val moduleDependencies = pomFile.findDependencies(libMavenId)
val dependencyManagementDependencies = pomFile.domModel.dependencyManagement.dependencies.findDependencies(libMavenId)
for (dependency in pomFile.findDependencies(libMavenId)) {
val versionStr = mavenProject.findDependencies(deprecatedLibInfo.lib.groupId, deprecatedLibInfo.lib.name)
.map { it.version }
.distinct()
.singleOrNull()
if (versionStr != null) {
reportDependency(dependency, versionStr, deprecatedLibInfo, holder)
}
}
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
)
for (dependency in pomFile.domModel.dependencyManagement.dependencies.findDependencies(libMavenId)) {
val versionStr = dependency.version?.stringValue
if (versionStr != null) {
reportDependency(dependency, versionStr, deprecatedLibInfo, holder)
}
}
}
}
companion object {
private fun reportDependency(
dependency: MavenDomDependency,
versionStr: String,
deprecatedLibInfo: DeprecatedForKotlinLibInfo,
holder: DomElementAnnotationHolder
) {
val updatedVersionStr = deprecatedLibInfo.versionUpdater.updateVersion(versionStr)
if (updatedVersionStr == versionStr) {
return
}
val xmlElement = dependency.version.xmlElement ?: return
val xmlText = xmlElement.text ?: return
if (xmlText.contains(updatedVersionStr)) {
return
}
val fixes: Array<LocalQuickFix> = if (xmlText.contains(versionStr)) {
arrayOf(ReplaceStringInDocumentFix(xmlElement, versionStr, updatedVersionStr))
} else {
emptyArray()
}
holder.createProblem(
dependency.version,
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
deprecatedLibInfo.message,
null,
*fixes
)
}
private fun checkKotlinVersion(module: Module, languageVersion: LanguageVersion): Boolean {
val moduleGroup = module.getWholeModuleGroup()
return moduleGroup.sourceRootModules.any { moduleInGroup ->
@@ -54,6 +54,11 @@ public class KotlinMavenInspectionTestGenerated extends AbstractKotlinMavenInspe
runTest("idea/idea-maven/testData/maven-inspections/deprecatedKotlinxCoroutines.xml");
}
@TestMetadata("deprecatedKotlinxCoroutinesNoError.xml")
public void testDeprecatedKotlinxCoroutinesNoError() throws Exception {
runTest("idea/idea-maven/testData/maven-inspections/deprecatedKotlinxCoroutinesNoError.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.24.0-eap13</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,88 @@
<?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.30.0</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 -->