Spring Support: Implement @ComponentScan inspection
#KT-12465 Fixed
This commit is contained in:
@@ -82,6 +82,7 @@
|
||||
- [`KT-12092`](https://youtrack.jetbrains.com/issue/KT-12092) Implement bean references in @Qualifier annotations
|
||||
- [`KT-12136`](https://youtrack.jetbrains.com/issue/KT-12136) Implement package references inside of string literals
|
||||
- [`KT-12278`](https://youtrack.jetbrains.com/issue/KT-12278) Implement Spring @Autowired inspection
|
||||
- [`KT-12465`](https://youtrack.jetbrains.com/issue/KT-12465) Implement Spring @ComponentScan inspection
|
||||
|
||||
###### Issues fixed
|
||||
|
||||
|
||||
@@ -45,6 +45,14 @@
|
||||
enabledByDefault="true"
|
||||
level="ERROR"
|
||||
implementationClass="org.jetbrains.kotlin.idea.spring.inspections.SpringKotlinAutowiringInspection"/>
|
||||
<localInspection language="kotlin"
|
||||
displayName="@ComponentScan setup (Kotlin)"
|
||||
groupBundle="resources.messages.SpringBundle"
|
||||
groupKey="inspection.group.code"
|
||||
groupPath="Spring,Spring Core"
|
||||
enabledByDefault="true"
|
||||
level="ERROR"
|
||||
implementationClass="org.jetbrains.kotlin.idea.spring.inspections.KotlinSpringComponentScanInspection"/>
|
||||
</extensions>
|
||||
|
||||
<actions>
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<body>
|
||||
Checks @ComponentScan (and similar annotations) for setup errors.
|
||||
<!-- tooltip end -->
|
||||
<p>
|
||||
</body>
|
||||
</html>
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package org.jetbrains.kotlin.idea.spring.inspections
|
||||
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.module.ModuleUtilCore
|
||||
import com.intellij.psi.PsiClass
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import com.intellij.spring.model.highlighting.jam.SpringComponentScanInspection
|
||||
import org.jetbrains.kotlin.asJava.toLightClass
|
||||
import org.jetbrains.kotlin.idea.inspections.AbstractKotlinInspection
|
||||
import org.jetbrains.kotlin.idea.inspections.registerWithElementsUnwrapped
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtVisitorVoid
|
||||
|
||||
class KotlinSpringComponentScanInspection : AbstractKotlinInspection() {
|
||||
private val javaInspection by lazy {
|
||||
object : SpringComponentScanInspection() {
|
||||
// make base method visible
|
||||
override public fun checkClass(aClass: PsiClass, holder: ProblemsHolder, module: Module) = super.checkClass(aClass, holder, module)
|
||||
}
|
||||
}
|
||||
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return object: KtVisitorVoid() {
|
||||
override fun visitClassOrObject(classOrObject: KtClassOrObject) {
|
||||
val module = ModuleUtilCore.findModuleForPsiElement(classOrObject) ?: return
|
||||
val lightClass = classOrObject.toLightClass() ?: return
|
||||
val tmpHolder = ProblemsHolder(holder.manager, classOrObject.containingFile, isOnTheFly)
|
||||
javaInspection.checkClass(lightClass, tmpHolder, module)
|
||||
tmpHolder.resultsArray.registerWithElementsUnwrapped(holder, isOnTheFly)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package foo
|
||||
|
||||
// Make PsiPackage not empty
|
||||
fun dummyFun() {
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<problems>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>14</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">@ComponentScan setup (Kotlin)</problem_class>
|
||||
<description>Cannot resolve package bar</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>18</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">@ComponentScan setup (Kotlin)</problem_class>
|
||||
<description>Cannot resolve package bar</description>
|
||||
</problem>
|
||||
</problems>
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.spring.inspections.KotlinSpringComponentScanInspection
|
||||
// FIXTURE_CLASS: org.jetbrains.kotlin.idea.spring.tests.SpringTestFixtureExtension
|
||||
// CONFIGURE_SPRING_FILE_SET
|
||||
@@ -0,0 +1,19 @@
|
||||
|
||||
// WITH_RUNTIME
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.context.annotation.ComponentScan
|
||||
import org.springframework.context.annotation.Configuration
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = arrayOf("foo"))
|
||||
open class App1
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = arrayOf("foo", "bar"))
|
||||
open class App2
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = arrayOf("bar"))
|
||||
open class App3
|
||||
+6
@@ -47,6 +47,12 @@ public class SpringInspectionTestGenerated extends AbstractSpringInspectionTest
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("componentScan/inspectionData/inspections.test")
|
||||
public void testComponentScan_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("ultimate/testData/inspections/spring/componentScan/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("finalSpringAnnotatedDeclaration/inspectionData/inspections.test")
|
||||
public void testFinalSpringAnnotatedDeclaration_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("ultimate/testData/inspections/spring/finalSpringAnnotatedDeclaration/inspectionData/inspections.test");
|
||||
|
||||
Reference in New Issue
Block a user