From 44f565e600248022f7bf4d825b84bf19ac6d4947 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 27 Apr 2016 12:59:05 +0300 Subject: [PATCH] Spring Support: Support "Autowired members defined in invalid Spring bean" inspection on Kotlin declarations #KT-12079 Fixed --- ChangeLog.md | 1 + .../kotlin/asJava/KtLightAnnotation.kt | 2 + ...pringKotlinAutowiredMembersInspection.html | 1 + ultimate/src/META-INF/kotlin-spring.xml | 8 +++ .../SpringKotlinAutowiredMembersInspection.kt | 54 +++++++++++++++++++ .../inspectionData/expected.xml | 10 ++++ .../inspectionData/inspections.test | 2 + .../autowiredMembersInInvalidClass/test.kt | 19 +++++++ .../UltimateInspectionTestGenerated.java | 6 +++ .../tests/SpringTestFixtureExtension.kt | 2 +- ultimate/update_dependencies.xml | 1 + 11 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 ultimate/resources/inspectionDescriptions/SpringKotlinAutowiredMembersInspection.html create mode 100644 ultimate/src/org/jetbrains/kotlin/idea/spring/inspections/SpringKotlinAutowiredMembersInspection.kt create mode 100644 ultimate/testData/inspections/spring/autowiredMembersInInvalidClass/inspectionData/expected.xml create mode 100644 ultimate/testData/inspections/spring/autowiredMembersInInvalidClass/inspectionData/inspections.test create mode 100644 ultimate/testData/inspections/spring/autowiredMembersInInvalidClass/test.kt diff --git a/ChangeLog.md b/ChangeLog.md index 130f629b02e..b1b4193a9ba 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -21,6 +21,7 @@ New features: - Apply injection for the literals in property initializer through property usages - Enable injection from Java or Kotlin function declaration by annotating parameter with @Language annotation - [KT-11807](https://youtrack.jetbrains.com/issue/KT-11807) Use function body template when generating overriding functions with default body +- [KT-12079](https://youtrack.jetbrains.com/issue/KT-12079) Support "Autowired members defined in invalid Spring bean" inspection on Kotlin declarations Issues fixed: diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/KtLightAnnotation.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/KtLightAnnotation.kt index 86193f14774..4e545b54361 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/KtLightAnnotation.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/KtLightAnnotation.kt @@ -92,6 +92,8 @@ class KtLightAnnotation( } } + override fun isPhysical() = true + override fun getName() = null override fun setName(newName: String) = throw IncorrectOperationException() diff --git a/ultimate/resources/inspectionDescriptions/SpringKotlinAutowiredMembersInspection.html b/ultimate/resources/inspectionDescriptions/SpringKotlinAutowiredMembersInspection.html new file mode 100644 index 00000000000..cf8139e2c40 --- /dev/null +++ b/ultimate/resources/inspectionDescriptions/SpringKotlinAutowiredMembersInspection.html @@ -0,0 +1 @@ +Checks that autowired members are defined in valid Spring bean (@Component|@Service|...). \ No newline at end of file diff --git a/ultimate/src/META-INF/kotlin-spring.xml b/ultimate/src/META-INF/kotlin-spring.xml index 7ef9c4a492c..cd5771e4f7d 100644 --- a/ultimate/src/META-INF/kotlin-spring.xml +++ b/ultimate/src/META-INF/kotlin-spring.xml @@ -22,6 +22,14 @@ enabledByDefault="true" level="WARNING" implementationClass="org.jetbrains.kotlin.idea.spring.inspections.KotlinSpringFacetCodeInspection"/> + diff --git a/ultimate/src/org/jetbrains/kotlin/idea/spring/inspections/SpringKotlinAutowiredMembersInspection.kt b/ultimate/src/org/jetbrains/kotlin/idea/spring/inspections/SpringKotlinAutowiredMembersInspection.kt new file mode 100644 index 00000000000..e603ec6c3e1 --- /dev/null +++ b/ultimate/src/org/jetbrains/kotlin/idea/spring/inspections/SpringKotlinAutowiredMembersInspection.kt @@ -0,0 +1,54 @@ +/* + * 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.idea.spring.inspections + +import com.intellij.codeInspection.LocalQuickFix +import com.intellij.codeInspection.ProblemsHolder +import com.intellij.psi.PsiElementVisitor +import com.intellij.spring.model.highlighting.SpringJavaAutowiredMembersInspection +import org.jetbrains.kotlin.asJava.toLightClass +import org.jetbrains.kotlin.asJava.unwrapped +import org.jetbrains.kotlin.idea.inspections.AbstractKotlinInspection +import org.jetbrains.kotlin.psi.KtClassOrObject +import org.jetbrains.kotlin.psi.KtVisitorVoid + +class SpringKotlinAutowiredMembersInspection : AbstractKotlinInspection() { + private val javaInspection by lazy { SpringJavaAutowiredMembersInspection() } + + override fun getDisplayName() = "Autowired members defined in invalid Spring bean (Kotlin)" + + override fun getGroupDisplayName() = "Code" + + override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { + return object: KtVisitorVoid() { + override fun visitClassOrObject(classOrObject: KtClassOrObject) { + val lightClass = classOrObject.toLightClass() ?: return + javaInspection.checkClass(lightClass, holder.manager, isOnTheFly)?.forEach { + @Suppress("UNCHECKED_CAST") + val descriptor = holder.manager.createProblemDescriptor( + it.psiElement.unwrapped ?: it.psiElement, + it.descriptionTemplate, + isOnTheFly, + it.fixes as? Array ?: LocalQuickFix.EMPTY_ARRAY, + it.highlightType + ) + holder.registerProblem(descriptor) + } + } + } + } +} diff --git a/ultimate/testData/inspections/spring/autowiredMembersInInvalidClass/inspectionData/expected.xml b/ultimate/testData/inspections/spring/autowiredMembersInInvalidClass/inspectionData/expected.xml new file mode 100644 index 00000000000..27b993c66d3 --- /dev/null +++ b/ultimate/testData/inspections/spring/autowiredMembersInInvalidClass/inspectionData/expected.xml @@ -0,0 +1,10 @@ + + + test.kt + 9 + light_idea_test_case + + Autowired members defined in invalid Spring bean (Kotlin) + Autowired members must be defined in valid Spring bean (@Component|@Service|...) + + diff --git a/ultimate/testData/inspections/spring/autowiredMembersInInvalidClass/inspectionData/inspections.test b/ultimate/testData/inspections/spring/autowiredMembersInInvalidClass/inspectionData/inspections.test new file mode 100644 index 00000000000..34b475331cb --- /dev/null +++ b/ultimate/testData/inspections/spring/autowiredMembersInInvalidClass/inspectionData/inspections.test @@ -0,0 +1,2 @@ +// INSPECTION_CLASS: org.jetbrains.kotlin.idea.spring.inspections.SpringKotlinAutowiredMembersInspection +// FIXTURE_CLASS: org.jetbrains.kotlin.idea.spring.tests.SpringTestFixtureExtension \ No newline at end of file diff --git a/ultimate/testData/inspections/spring/autowiredMembersInInvalidClass/test.kt b/ultimate/testData/inspections/spring/autowiredMembersInInvalidClass/test.kt new file mode 100644 index 00000000000..eabce3aad5e --- /dev/null +++ b/ultimate/testData/inspections/spring/autowiredMembersInInvalidClass/test.kt @@ -0,0 +1,19 @@ + +// WITH_RUNTIME + +import org.springframework.stereotype.Component +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.web.context.support.SpringBeanAutowiringSupport + +open class KtBean1 { + @Autowired var foo: Int = 1 +} + +@Component +open class KtBean2 { + @Autowired var foo: Int = 1 +} + +open class KtBean3 : SpringBeanAutowiringSupport() { + @Autowired var foo: Int = 1 +} \ No newline at end of file diff --git a/ultimate/tests/org/jetbrains/kotlin/idea/codeInsight/UltimateInspectionTestGenerated.java b/ultimate/tests/org/jetbrains/kotlin/idea/codeInsight/UltimateInspectionTestGenerated.java index 006e9123b14..93aaccdf0ba 100644 --- a/ultimate/tests/org/jetbrains/kotlin/idea/codeInsight/UltimateInspectionTestGenerated.java +++ b/ultimate/tests/org/jetbrains/kotlin/idea/codeInsight/UltimateInspectionTestGenerated.java @@ -35,6 +35,12 @@ public class UltimateInspectionTestGenerated extends AbstractInspectionTest { KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("ultimate/testData/inspections"), Pattern.compile("^(inspections\\.test)$")); } + @TestMetadata("spring/autowiredMembersInInvalidClass/inspectionData/inspections.test") + public void testSpring_autowiredMembersInInvalidClass_inspectionData_Inspections_test() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("ultimate/testData/inspections/spring/autowiredMembersInInvalidClass/inspectionData/inspections.test"); + doTest(fileName); + } + @TestMetadata("spring/finalSpringAnnotatedDeclaration/inspectionData/inspections.test") public void testSpring_finalSpringAnnotatedDeclaration_inspectionData_Inspections_test() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("ultimate/testData/inspections/spring/finalSpringAnnotatedDeclaration/inspectionData/inspections.test"); diff --git a/ultimate/tests/org/jetbrains/kotlin/idea/spring/tests/SpringTestFixtureExtension.kt b/ultimate/tests/org/jetbrains/kotlin/idea/spring/tests/SpringTestFixtureExtension.kt index 7d6af619bf9..7faf6e9412d 100644 --- a/ultimate/tests/org/jetbrains/kotlin/idea/spring/tests/SpringTestFixtureExtension.kt +++ b/ultimate/tests/org/jetbrains/kotlin/idea/spring/tests/SpringTestFixtureExtension.kt @@ -36,7 +36,7 @@ class SpringTestFixtureExtension() : TestFixtureExtension { enum class SpringFramework(val version: String, vararg val artifactIds: String) { FRAMEWORK_4_2_0( "4.2.0.RELEASE", - "core", "beans", "context", "tx" + "core", "beans", "context", "tx", "web" ) } diff --git a/ultimate/update_dependencies.xml b/ultimate/update_dependencies.xml index a159e4e2ed3..a96f1ddbf6d 100644 --- a/ultimate/update_dependencies.xml +++ b/ultimate/update_dependencies.xml @@ -32,6 +32,7 @@ +