From e59d2a751e031ec90fd82397cb9a2a2f9414090c Mon Sep 17 00:00:00 2001 From: Andrey Cherkasov Date: Wed, 13 Dec 2023 21:42:41 +0400 Subject: [PATCH] [devkit] LightServiceMigrationInspection: provide quick-fix ...for the "Service can be converted to a light service" warning. The provided quick-fix annotates the service class with '@Service' and removes its registration from the 'plugin.xml' file. ^IDEA-254577 GitOrigin-RevId: 55cace08aa3beade8fb5e28532a29df459e2c1f0 --- .../messages/DevKitBundle.properties | 5 +--- .../LightServiceMigrationCodeInspection.kt | 23 +++++++++++------ .../inspections/LightServiceMigrationUtil.kt | 10 -------- .../LightServiceMigrationXMLInspection.kt | 6 +++-- .../AddAppServiceAnnotation.java | 3 +++ .../AddAppServiceAnnotation_after.java | 6 +++++ .../AddProjectServiceAnnotation.java | 3 +++ .../AddProjectServiceAnnotation_after.java | 6 +++++ .../CanBeLightServiceAppLevel.java | 4 --- .../CanBeLightServiceProjectLevel.java | 4 --- .../lightServiceMigration/MyService.java | 3 +++ .../addAppServiceAnnotation.xml} | 3 +-- .../addProjectServiceAnnotation.xml} | 3 +-- ...l.xml => removeAppServiceRegistration.xml} | 3 +-- .../removeAppServiceRegistration_after.xml | 8 ++++++ .../removeProjectServiceRegistration.xml | 10 ++++++++ ...removeProjectServiceRegistration_after.xml | 8 ++++++ .../LightServiceMigrationInspectionTest.kt | 25 ++++++++++++++----- .../AddAppServiceAnnotation.kt | 3 +++ .../AddAppServiceAnnotation_after.kt | 6 +++++ .../AddProjectServiceAnnotation.kt | 3 +++ .../AddProjectServiceAnnotation_after.kt | 6 +++++ .../CanBeLightServiceAppLevel.kt | 4 --- .../CanBeLightServiceProjectLevel.kt | 4 --- .../lightServiceMigration/MyService.kt | 3 +++ .../addAppServiceAnnotation.xml | 10 ++++++++ .../addProjectServiceAnnotation.xml} | 3 +-- .../removeAppServiceRegistration.xml | 10 ++++++++ .../removeAppServiceRegistration_after.xml | 8 ++++++ .../removeProjectServiceRegistration.xml | 10 ++++++++ ...removeProjectServiceRegistration_after.xml | 8 ++++++ .../KtLightServiceMigrationInspectionTest.kt | 21 +++++++++++++--- ...LightServiceMigrationInspectionTestBase.kt | 16 +++++++++++- 33 files changed, 190 insertions(+), 58 deletions(-) create mode 100644 plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation.java create mode 100644 plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation_after.java create mode 100644 plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation.java create mode 100644 plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation_after.java delete mode 100644 plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/CanBeLightServiceAppLevel.java delete mode 100644 plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/CanBeLightServiceProjectLevel.java create mode 100644 plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/MyService.java rename plugins/devkit/{devkit-kotlin-tests/testData/inspections/lightServiceMigration/canBeLightServiceAppLevel.xml => devkit-java-tests/testData/inspections/lightServiceMigration/addAppServiceAnnotation.xml} (54%) rename plugins/devkit/{devkit-kotlin-tests/testData/inspections/lightServiceMigration/canBeLightServiceProjectLevel.xml => devkit-java-tests/testData/inspections/lightServiceMigration/addProjectServiceAnnotation.xml} (54%) rename plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/{canBeLightServiceAppLevel.xml => removeAppServiceRegistration.xml} (54%) create mode 100644 plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration_after.xml create mode 100644 plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration.xml create mode 100644 plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration_after.xml create mode 100644 plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation.kt create mode 100644 plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation_after.kt create mode 100644 plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation.kt create mode 100644 plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation_after.kt delete mode 100644 plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/CanBeLightServiceAppLevel.kt delete mode 100644 plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/CanBeLightServiceProjectLevel.kt create mode 100644 plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/MyService.kt create mode 100644 plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/addAppServiceAnnotation.xml rename plugins/devkit/{devkit-java-tests/testData/inspections/lightServiceMigration/canBeLightServiceProjectLevel.xml => devkit-kotlin-tests/testData/inspections/lightServiceMigration/addProjectServiceAnnotation.xml} (54%) create mode 100644 plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration.xml create mode 100644 plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration_after.xml create mode 100644 plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration.xml create mode 100644 plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration_after.xml diff --git a/plugins/devkit/devkit-core/resources/messages/DevKitBundle.properties b/plugins/devkit/devkit-core/resources/messages/DevKitBundle.properties index 76af8ad6883a..f7129cb89524 100644 --- a/plugins/devkit/devkit-core/resources/messages/DevKitBundle.properties +++ b/plugins/devkit/devkit-core/resources/messages/DevKitBundle.properties @@ -642,10 +642,7 @@ inspection.extension.class.should.be.final.text=Extension class should be final inspection.extension.class.should.not.be.public.text=Extension class should not be public inspection.light.service.migration.display.name=A service can be converted to a light one -inspection.light.service.migration.app.level.message=Service can be converted to a light one.\n\ - Annotate the service class with '@Service' and remove its registration from the 'plugin.xml' file. -inspection.light.service.migration.project.level.message=Service can be converted to a light one.\n\ - Annotate the service class with '@Service(Service.Level.PROJECT)' and remove its registration from the 'plugin.xml' file. +inspection.light.service.migration.message=Service can be converted to a light service inspection.light.service.migration.family.name=Convert to light service inspection.light.service.migration.app.level.fix=Annotate the service class with '@Service' and remove its registration from the 'plugin.xml' file inspection.light.service.migration.project.level.fix=Annotate the service class with '@Service(Service.Level.PROJECT)' and remove its registration from the 'plugin.xml' file diff --git a/plugins/devkit/devkit-core/src/inspections/LightServiceMigrationCodeInspection.kt b/plugins/devkit/devkit-core/src/inspections/LightServiceMigrationCodeInspection.kt index cbae98c6ab43..864f3c80833e 100644 --- a/plugins/devkit/devkit-core/src/inspections/LightServiceMigrationCodeInspection.kt +++ b/plugins/devkit/devkit-core/src/inspections/LightServiceMigrationCodeInspection.kt @@ -2,13 +2,15 @@ package org.jetbrains.idea.devkit.inspections import com.intellij.codeInspection.* -import com.intellij.lang.jvm.* +import com.intellij.lang.jvm.JvmModifier import com.intellij.lang.jvm.util.JvmInheritanceUtil import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.components.PersistentStateComponent import com.intellij.openapi.components.Service import com.intellij.util.xml.DomUtil +import org.jetbrains.idea.devkit.DevKitBundle import org.jetbrains.idea.devkit.dom.Extension +import org.jetbrains.idea.devkit.inspections.quickfix.ConvertToLightServiceFix import org.jetbrains.idea.devkit.util.locateExtensionsByPsiClass import org.jetbrains.uast.UClass @@ -26,7 +28,8 @@ internal class LightServiceMigrationCodeInspection : DevKitUastInspectionBase(UC if (isVersion193OrHigher(psiClass) || ApplicationManager.getApplication().isUnitTestMode) { if (isLightService(aClass)) return ProblemDescriptor.EMPTY_ARRAY - for (candidate in locateExtensionsByPsiClass(psiClass)) { + val extensionsCandidates = locateExtensionsByPsiClass(psiClass) + for (candidate in extensionsCandidates) { val extension = DomUtil.findDomElement(candidate.pointer.element, Extension::class.java, false) ?: continue val (serviceImplementation, level) = getServiceImplementation(extension) ?: continue if (level == Service.Level.APP && @@ -34,7 +37,13 @@ internal class LightServiceMigrationCodeInspection : DevKitUastInspectionBase(UC continue } if (serviceImplementation == psiClass && !containsUnitTestOrHeadlessModeCheck(aClass)) { - return registerProblem(aClass, level, manager, isOnTheFly) + val fixes = if (extensionsCandidates.size == 1) { + arrayOf(ConvertToLightServiceFix(psiClass, extension.xmlTag, level)) + } + else { + LocalQuickFix.EMPTY_ARRAY + } + return registerProblem(aClass, manager, isOnTheFly, fixes) } } } @@ -42,12 +51,12 @@ internal class LightServiceMigrationCodeInspection : DevKitUastInspectionBase(UC } private fun registerProblem(aClass: UClass, - level: Service.Level, manager: InspectionManager, - isOnTheFly: Boolean): Array { - val message = getMessage(level) + isOnTheFly: Boolean, + fixes: Array): Array { + val message = DevKitBundle.message("inspection.light.service.migration.message") val holder = createProblemsHolder(aClass, manager, isOnTheFly) - holder.registerUProblem(aClass, message) + holder.registerUProblem(aClass, message, *fixes) return holder.resultsArray } } \ No newline at end of file diff --git a/plugins/devkit/devkit-core/src/inspections/LightServiceMigrationUtil.kt b/plugins/devkit/devkit-core/src/inspections/LightServiceMigrationUtil.kt index 6320cd9c823d..948b7747bf3e 100644 --- a/plugins/devkit/devkit-core/src/inspections/LightServiceMigrationUtil.kt +++ b/plugins/devkit/devkit-core/src/inspections/LightServiceMigrationUtil.kt @@ -12,8 +12,6 @@ import com.intellij.psi.PsiClass import com.intellij.util.xml.DomElement import com.intellij.util.xml.DomUtil import com.siyeh.ig.callMatcher.CallMatcher -import org.jetbrains.annotations.Nls -import org.jetbrains.idea.devkit.DevKitBundle import org.jetbrains.idea.devkit.dom.Extension import org.jetbrains.idea.devkit.util.DevKitDomUtil import org.jetbrains.idea.devkit.util.PluginPlatformInfo @@ -48,14 +46,6 @@ private fun hasDisallowedAttributes(extension: Extension): Boolean { return false } -@Nls(capitalization = Nls.Capitalization.Sentence) -internal fun getMessage(level: Service.Level): String { - return when (level) { - Service.Level.APP -> DevKitBundle.message("inspection.light.service.migration.app.level.message") - Service.Level.PROJECT -> DevKitBundle.message("inspection.light.service.migration.project.level.message") - } -} - internal fun isVersion193OrHigher(element: DomElement): Boolean { if (PsiUtil.isIdeaProject(element.module?.project)) return true val buildNumber = PluginPlatformInfo.forDomElement(element).sinceBuildNumber diff --git a/plugins/devkit/devkit-core/src/inspections/LightServiceMigrationXMLInspection.kt b/plugins/devkit/devkit-core/src/inspections/LightServiceMigrationXMLInspection.kt index 07a344fb6093..6ea3dc3e1c9b 100644 --- a/plugins/devkit/devkit-core/src/inspections/LightServiceMigrationXMLInspection.kt +++ b/plugins/devkit/devkit-core/src/inspections/LightServiceMigrationXMLInspection.kt @@ -16,6 +16,7 @@ import com.intellij.util.xml.highlighting.DomHighlightingHelper import com.intellij.util.xml.highlighting.RemoveDomElementQuickFix import org.jetbrains.idea.devkit.DevKitBundle import org.jetbrains.idea.devkit.dom.Extension +import org.jetbrains.idea.devkit.inspections.quickfix.ConvertToLightServiceFix import org.jetbrains.uast.UClass import org.jetbrains.uast.toUElement @@ -40,8 +41,9 @@ internal class LightServiceMigrationXMLInspection : DevKitPluginXmlInspectionBas holder.createProblem(element, ProblemHighlightType.ERROR, message, null, RemoveDomElementQuickFix(element)) } else { - val message = getMessage(level) - holder.createProblem(element, message) + val message = DevKitBundle.message("inspection.light.service.migration.message") + val fix = ConvertToLightServiceFix(aClass, element.xmlTag, level) + holder.createProblem(element, message, fix) } } } diff --git a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation.java b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation.java new file mode 100644 index 000000000000..db5fea45768d --- /dev/null +++ b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation.java @@ -0,0 +1,3 @@ +package com.example.demo; + +final class MyService {} \ No newline at end of file diff --git a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation_after.java b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation_after.java new file mode 100644 index 000000000000..071976e63c84 --- /dev/null +++ b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation_after.java @@ -0,0 +1,6 @@ +package com.example.demo; + +import com.intellij.openapi.components.Service; + +@Service +final class MyService {} \ No newline at end of file diff --git a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation.java b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation.java new file mode 100644 index 000000000000..db5fea45768d --- /dev/null +++ b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation.java @@ -0,0 +1,3 @@ +package com.example.demo; + +final class MyService {} \ No newline at end of file diff --git a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation_after.java b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation_after.java new file mode 100644 index 000000000000..7bec3b4dba9e --- /dev/null +++ b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation_after.java @@ -0,0 +1,6 @@ +package com.example.demo; + +import com.intellij.openapi.components.Service; + +@Service(Service.Level.PROJECT) +final class MyService {} \ No newline at end of file diff --git a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/CanBeLightServiceAppLevel.java b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/CanBeLightServiceAppLevel.java deleted file mode 100644 index d16c4df456b3..000000000000 --- a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/CanBeLightServiceAppLevel.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.example.demo; - -final class MyService {} \ No newline at end of file diff --git a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/CanBeLightServiceProjectLevel.java b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/CanBeLightServiceProjectLevel.java deleted file mode 100644 index 8d3883bf6b8a..000000000000 --- a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/CanBeLightServiceProjectLevel.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.example.demo; - -final class MyService {} \ No newline at end of file diff --git a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/MyService.java b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/MyService.java new file mode 100644 index 000000000000..a45174673325 --- /dev/null +++ b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/MyService.java @@ -0,0 +1,3 @@ +package com.example.demo; + +final class MyService {} \ No newline at end of file diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/canBeLightServiceAppLevel.xml b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/addAppServiceAnnotation.xml similarity index 54% rename from plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/canBeLightServiceAppLevel.xml rename to plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/addAppServiceAnnotation.xml index 00b05334aea7..8e27da3d0a04 100644 --- a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/canBeLightServiceAppLevel.xml +++ b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/addAppServiceAnnotation.xml @@ -5,7 +5,6 @@ - <applicationService serviceImplementation="com.example.demo.MyService"/> + diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/canBeLightServiceProjectLevel.xml b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/addProjectServiceAnnotation.xml similarity index 54% rename from plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/canBeLightServiceProjectLevel.xml rename to plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/addProjectServiceAnnotation.xml index 66e2ff7e8720..0c4f0cd7161f 100644 --- a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/canBeLightServiceProjectLevel.xml +++ b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/addProjectServiceAnnotation.xml @@ -5,7 +5,6 @@ - <projectService serviceImplementation="com.example.demo.MyService"/> + diff --git a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/canBeLightServiceAppLevel.xml b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration.xml similarity index 54% rename from plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/canBeLightServiceAppLevel.xml rename to plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration.xml index 00b05334aea7..f8e8b392214d 100644 --- a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/canBeLightServiceAppLevel.xml +++ b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration.xml @@ -5,7 +5,6 @@ - <applicationService serviceImplementation="com.example.demo.MyService"/> + <applicationService serviceImplementation="com.example.demo.MyService"/> diff --git a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration_after.xml b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration_after.xml new file mode 100644 index 000000000000..60d2b6a332e1 --- /dev/null +++ b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration_after.xml @@ -0,0 +1,8 @@ + + com.example.demo + + + + + + diff --git a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration.xml b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration.xml new file mode 100644 index 000000000000..66d9719e39c0 --- /dev/null +++ b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration.xml @@ -0,0 +1,10 @@ + + com.example.demo + + + + + + <projectService serviceImplementation="com.example.demo.MyService"/> + + diff --git a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration_after.xml b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration_after.xml new file mode 100644 index 000000000000..4e21ba2ef9f4 --- /dev/null +++ b/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration_after.xml @@ -0,0 +1,8 @@ + + com.example.demo + + + + + + diff --git a/plugins/devkit/devkit-java-tests/testSrc/org/jetbrains/idea/devkit/inspections/quickfix/LightServiceMigrationInspectionTest.kt b/plugins/devkit/devkit-java-tests/testSrc/org/jetbrains/idea/devkit/inspections/quickfix/LightServiceMigrationInspectionTest.kt index e78143f7c571..11511c8cf2fd 100644 --- a/plugins/devkit/devkit-java-tests/testSrc/org/jetbrains/idea/devkit/inspections/quickfix/LightServiceMigrationInspectionTest.kt +++ b/plugins/devkit/devkit-java-tests/testSrc/org/jetbrains/idea/devkit/inspections/quickfix/LightServiceMigrationInspectionTest.kt @@ -9,16 +9,29 @@ internal class LightServiceMigrationInspectionTest : LightServiceMigrationInspec private val CANNOT_BE_LIGHT_SERVICE_XML = "cannotBeLightService.xml" private val CANNOT_BE_LIGHT_SERVICE_JAVA = "CannotBeLightService.java" + private val MY_SERVICE_JAVA = "MyService.java" override fun getBasePath(): String = DevkitJavaTestsUtil.TESTDATA_PATH + "inspections/lightServiceMigration" override fun getFileExtension(): String = "java" - fun testCanBeLightServiceAppLevel() { - doTest(getCodeFilePath(), getXmlFilePath()) + fun testAddAppServiceAnnotation() { + myFixture.copyFileToProject(getXmlFilePath()) + doTest(convertToLightServiceAppLevel, fileExtension, getTestName(false)) } - fun testCanBeLightServiceProjectLevel() { - doTest(getCodeFilePath(), getXmlFilePath()) + fun testAddProjectServiceAnnotation() { + myFixture.copyFileToProject(getXmlFilePath()) + doTest(convertToLightServiceProjectLevel, fileExtension, getTestName(false)) + } + + fun testRemoveAppServiceRegistration() { + myFixture.copyFileToProject(MY_SERVICE_JAVA) + doTest(convertToLightServiceAppLevel, xmlExtension, getTestName(true)) + } + + fun testRemoveProjectServiceRegistration() { + myFixture.copyFileToProject(MY_SERVICE_JAVA) + doTest(convertToLightServiceProjectLevel, xmlExtension, getTestName(true)) } fun testNonFinalClass() { @@ -39,11 +52,11 @@ internal class LightServiceMigrationInspectionTest : LightServiceMigrationInspec fun testLightService() { myFixture.copyFileToProject(getCodeFilePath()) - DevKitInspectionFixTestBase.doTest(myFixture, "Remove element", "xml", getTestName(true)) + doTest("Remove element", xmlExtension, getTestName(true)) } fun testLibraryClass() { - myFixture.testHighlighting(getTestName(true) + ".xml") + myFixture.testHighlighting(getTestName(true) + "." + xmlExtension) } fun testUnitTestMode() { diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation.kt b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation.kt new file mode 100644 index 000000000000..ac690930646d --- /dev/null +++ b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation.kt @@ -0,0 +1,3 @@ +package com.example.demo + +class MyService \ No newline at end of file diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation_after.kt b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation_after.kt new file mode 100644 index 000000000000..a972b6467f25 --- /dev/null +++ b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddAppServiceAnnotation_after.kt @@ -0,0 +1,6 @@ +package com.example.demo + +import com.intellij.openapi.components.Service + +@Service +class MyService \ No newline at end of file diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation.kt b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation.kt new file mode 100644 index 000000000000..ac690930646d --- /dev/null +++ b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation.kt @@ -0,0 +1,3 @@ +package com.example.demo + +class MyService \ No newline at end of file diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation_after.kt b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation_after.kt new file mode 100644 index 000000000000..9f00b88e2c26 --- /dev/null +++ b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/AddProjectServiceAnnotation_after.kt @@ -0,0 +1,6 @@ +package com.example.demo + +import com.intellij.openapi.components.Service + +@Service(Service.Level.PROJECT) +class MyService \ No newline at end of file diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/CanBeLightServiceAppLevel.kt b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/CanBeLightServiceAppLevel.kt deleted file mode 100644 index e75a3ea75065..000000000000 --- a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/CanBeLightServiceAppLevel.kt +++ /dev/null @@ -1,4 +0,0 @@ -package com.example.demo - -class MyService \ No newline at end of file diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/CanBeLightServiceProjectLevel.kt b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/CanBeLightServiceProjectLevel.kt deleted file mode 100644 index a19f74c02539..000000000000 --- a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/CanBeLightServiceProjectLevel.kt +++ /dev/null @@ -1,4 +0,0 @@ -package com.example.demo - -class MyService \ No newline at end of file diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/MyService.kt b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/MyService.kt new file mode 100644 index 000000000000..49099d31c318 --- /dev/null +++ b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/MyService.kt @@ -0,0 +1,3 @@ +package com.example.demo + +class MyService \ No newline at end of file diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/addAppServiceAnnotation.xml b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/addAppServiceAnnotation.xml new file mode 100644 index 000000000000..8e27da3d0a04 --- /dev/null +++ b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/addAppServiceAnnotation.xml @@ -0,0 +1,10 @@ + + com.example.demo + + + + + + + + diff --git a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/canBeLightServiceProjectLevel.xml b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/addProjectServiceAnnotation.xml similarity index 54% rename from plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/canBeLightServiceProjectLevel.xml rename to plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/addProjectServiceAnnotation.xml index 66e2ff7e8720..0c4f0cd7161f 100644 --- a/plugins/devkit/devkit-java-tests/testData/inspections/lightServiceMigration/canBeLightServiceProjectLevel.xml +++ b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/addProjectServiceAnnotation.xml @@ -5,7 +5,6 @@ - <projectService serviceImplementation="com.example.demo.MyService"/> + diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration.xml b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration.xml new file mode 100644 index 000000000000..f8e8b392214d --- /dev/null +++ b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration.xml @@ -0,0 +1,10 @@ + + com.example.demo + + + + + + <applicationService serviceImplementation="com.example.demo.MyService"/> + + diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration_after.xml b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration_after.xml new file mode 100644 index 000000000000..60d2b6a332e1 --- /dev/null +++ b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeAppServiceRegistration_after.xml @@ -0,0 +1,8 @@ + + com.example.demo + + + + + + diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration.xml b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration.xml new file mode 100644 index 000000000000..66d9719e39c0 --- /dev/null +++ b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration.xml @@ -0,0 +1,10 @@ + + com.example.demo + + + + + + <projectService serviceImplementation="com.example.demo.MyService"/> + + diff --git a/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration_after.xml b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration_after.xml new file mode 100644 index 000000000000..4e21ba2ef9f4 --- /dev/null +++ b/plugins/devkit/devkit-kotlin-tests/testData/inspections/lightServiceMigration/removeProjectServiceRegistration_after.xml @@ -0,0 +1,8 @@ + + com.example.demo + + + + + + diff --git a/plugins/devkit/devkit-kotlin-tests/testSrc/org/jetbrains/idea/devkit/kotlin/inspections/quickfix/KtLightServiceMigrationInspectionTest.kt b/plugins/devkit/devkit-kotlin-tests/testSrc/org/jetbrains/idea/devkit/kotlin/inspections/quickfix/KtLightServiceMigrationInspectionTest.kt index c21403748830..6b413ad8ea96 100644 --- a/plugins/devkit/devkit-kotlin-tests/testSrc/org/jetbrains/idea/devkit/kotlin/inspections/quickfix/KtLightServiceMigrationInspectionTest.kt +++ b/plugins/devkit/devkit-kotlin-tests/testSrc/org/jetbrains/idea/devkit/kotlin/inspections/quickfix/KtLightServiceMigrationInspectionTest.kt @@ -10,16 +10,29 @@ internal class KtLightServiceMigrationInspectionTest : LightServiceMigrationInsp private val CANNOT_BE_LIGHT_SERVICE_XML = "cannotBeLightService.xml" private val CANNOT_BE_LIGHT_SERVICE_KT = "CannotBeLightService.kt" + private val MY_SERVICE_KT = "MyService.kt" override fun getBasePath(): String = DevkitKtTestsUtil.TESTDATA_PATH + "inspections/lightServiceMigration" override fun getFileExtension(): String = "kt" - fun testCanBeLightServiceAppLevel() { - doTest(getCodeFilePath(), getXmlFilePath()) + fun testAddAppServiceAnnotation() { + myFixture.copyFileToProject(getXmlFilePath()) + doTest(convertToLightServiceAppLevel, fileExtension, getTestName(false)) } - fun testCanBeLightServiceProjectLevel() { - doTest(getCodeFilePath(), getXmlFilePath()) + fun testAddProjectServiceAnnotation() { + myFixture.copyFileToProject(getXmlFilePath()) + doTest(convertToLightServiceProjectLevel, fileExtension, getTestName(false)) + } + + fun testRemoveAppServiceRegistration() { + myFixture.copyFileToProject(MY_SERVICE_KT) + doTest(convertToLightServiceAppLevel, xmlExtension, getTestName(true)) + } + + fun testRemoveProjectServiceRegistration() { + myFixture.copyFileToProject(MY_SERVICE_KT) + doTest(convertToLightServiceProjectLevel, xmlExtension, getTestName(true)) } fun testOpenClass() { diff --git a/plugins/devkit/devkit-tests/testSrc/org/jetbrains/idea/devkit/inspections/quickfix/LightServiceMigrationInspectionTestBase.kt b/plugins/devkit/devkit-tests/testSrc/org/jetbrains/idea/devkit/inspections/quickfix/LightServiceMigrationInspectionTestBase.kt index 631985ce0db8..fa22f7036494 100644 --- a/plugins/devkit/devkit-tests/testSrc/org/jetbrains/idea/devkit/inspections/quickfix/LightServiceMigrationInspectionTestBase.kt +++ b/plugins/devkit/devkit-tests/testSrc/org/jetbrains/idea/devkit/inspections/quickfix/LightServiceMigrationInspectionTestBase.kt @@ -1,11 +1,16 @@ // Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.idea.devkit.inspections.quickfix +import org.jetbrains.idea.devkit.DevKitBundle import org.jetbrains.idea.devkit.inspections.LightServiceMigrationCodeInspection import org.jetbrains.idea.devkit.inspections.LightServiceMigrationXMLInspection abstract class LightServiceMigrationInspectionTestBase : LightDevKitInspectionFixTestBase() { + protected val convertToLightServiceAppLevel: String = DevKitBundle.message("inspection.light.service.migration.app.level.fix") + protected val convertToLightServiceProjectLevel: String = DevKitBundle.message("inspection.light.service.migration.project.level.fix") + protected val xmlExtension = "xml" + override fun setUp() { super.setUp() addClasses() @@ -17,12 +22,21 @@ abstract class LightServiceMigrationInspectionTestBase : LightDevKitInspectionFi myFixture.testHighlightingAllFiles(true, false, false, codeFilePath, xmlFilePath) } + protected fun doTest(fixName: String, fileExtension: String, testName: String) { + val fileNameBefore = "$testName.$fileExtension" + val fileNameAfter = testName + "_after." + fileExtension + myFixture.testHighlighting(fileNameBefore) + val intention = myFixture.findSingleIntention(fixName) + myFixture.launchAction(intention) + myFixture.checkResultByFile(fileNameBefore, fileNameAfter, true) + } + protected fun getCodeFilePath(): String { return getTestName(false) + "." + fileExtension } protected fun getXmlFilePath(): String { - return getTestName(true) + ".xml" + return getTestName(true) + "." + xmlExtension } private fun addClasses() {