Spring Support: Support "Autowired members defined in invalid Spring bean" inspection on Kotlin declarations
#KT-12079 Fixed
This commit is contained in:
@@ -21,6 +21,7 @@ New features:
|
|||||||
- Apply injection for the literals in property initializer through property usages
|
- 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
|
- 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-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:
|
Issues fixed:
|
||||||
|
|
||||||
|
|||||||
@@ -92,6 +92,8 @@ class KtLightAnnotation(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun isPhysical() = true
|
||||||
|
|
||||||
override fun getName() = null
|
override fun getName() = null
|
||||||
override fun setName(newName: String) = throw IncorrectOperationException()
|
override fun setName(newName: String) = throw IncorrectOperationException()
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
Checks that autowired members are defined in valid Spring bean (@Component|@Service|...).
|
||||||
@@ -22,6 +22,14 @@
|
|||||||
enabledByDefault="true"
|
enabledByDefault="true"
|
||||||
level="WARNING"
|
level="WARNING"
|
||||||
implementationClass="org.jetbrains.kotlin.idea.spring.inspections.KotlinSpringFacetCodeInspection"/>
|
implementationClass="org.jetbrains.kotlin.idea.spring.inspections.KotlinSpringFacetCodeInspection"/>
|
||||||
|
<localInspection language="kotlin"
|
||||||
|
displayName="Autowired members defined in invalid Spring bean (Kotlin)"
|
||||||
|
groupBundle="resources.messages.SpringBundle"
|
||||||
|
groupKey="inspection.group.code"
|
||||||
|
groupPath="Spring,Spring Core"
|
||||||
|
enabledByDefault="true"
|
||||||
|
level="WARNING"
|
||||||
|
implementationClass="org.jetbrains.kotlin.idea.spring.inspections.SpringKotlinAutowiredMembersInspection"/>
|
||||||
</extensions>
|
</extensions>
|
||||||
|
|
||||||
<actions>
|
<actions>
|
||||||
|
|||||||
+54
@@ -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> ?: LocalQuickFix.EMPTY_ARRAY,
|
||||||
|
it.highlightType
|
||||||
|
)
|
||||||
|
holder.registerProblem(descriptor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
+10
@@ -0,0 +1,10 @@
|
|||||||
|
<problems>
|
||||||
|
<problem>
|
||||||
|
<file>test.kt</file>
|
||||||
|
<line>9</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Autowired members defined in invalid Spring bean (Kotlin)</problem_class>
|
||||||
|
<description>Autowired members must be defined in valid Spring bean (@Component|@Service|...)</description>
|
||||||
|
</problem>
|
||||||
|
</problems>
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.spring.inspections.SpringKotlinAutowiredMembersInspection
|
||||||
|
// FIXTURE_CLASS: org.jetbrains.kotlin.idea.spring.tests.SpringTestFixtureExtension
|
||||||
@@ -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
|
||||||
|
}
|
||||||
+6
@@ -35,6 +35,12 @@ public class UltimateInspectionTestGenerated extends AbstractInspectionTest {
|
|||||||
KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("ultimate/testData/inspections"), Pattern.compile("^(inspections\\.test)$"));
|
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")
|
@TestMetadata("spring/finalSpringAnnotatedDeclaration/inspectionData/inspections.test")
|
||||||
public void testSpring_finalSpringAnnotatedDeclaration_inspectionData_Inspections_test() throws Exception {
|
public void testSpring_finalSpringAnnotatedDeclaration_inspectionData_Inspections_test() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("ultimate/testData/inspections/spring/finalSpringAnnotatedDeclaration/inspectionData/inspections.test");
|
String fileName = KotlinTestUtils.navigationMetadata("ultimate/testData/inspections/spring/finalSpringAnnotatedDeclaration/inspectionData/inspections.test");
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class SpringTestFixtureExtension() : TestFixtureExtension {
|
|||||||
enum class SpringFramework(val version: String, vararg val artifactIds: String) {
|
enum class SpringFramework(val version: String, vararg val artifactIds: String) {
|
||||||
FRAMEWORK_4_2_0(
|
FRAMEWORK_4_2_0(
|
||||||
"4.2.0.RELEASE",
|
"4.2.0.RELEASE",
|
||||||
"core", "beans", "context", "tx"
|
"core", "beans", "context", "tx", "web"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,7 @@
|
|||||||
<get-spring-library lib="spring-beans" version="4.2.0.RELEASE"/>
|
<get-spring-library lib="spring-beans" version="4.2.0.RELEASE"/>
|
||||||
<get-spring-library lib="spring-context" version="4.2.0.RELEASE"/>
|
<get-spring-library lib="spring-context" version="4.2.0.RELEASE"/>
|
||||||
<get-spring-library lib="spring-tx" version="4.2.0.RELEASE"/>
|
<get-spring-library lib="spring-tx" version="4.2.0.RELEASE"/>
|
||||||
|
<get-spring-library lib="spring-web" version="4.2.0.RELEASE"/>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
<!-- Override fetch-third-party from the main buildfile -->
|
<!-- Override fetch-third-party from the main buildfile -->
|
||||||
|
|||||||
Reference in New Issue
Block a user