Spring Support: Report object declarations in "Final Kotlin class or function with Spring annotation" inspection
#KT-12148 Fixed
This commit is contained in:
@@ -58,6 +58,7 @@ Issues fixed:
|
||||
- [KT-12366](https://youtrack.jetbrains.com/issue/KT-12366) Fixed exception on analyzing class declaration upon annotation typing
|
||||
- [KT-12122](https://youtrack.jetbrains.com/issue/KT-12122) Fixed line marker popup on functions with @Qualifier-annotated parameters
|
||||
- [KT-12363](https://youtrack.jetbrains.com/issue/KT-12363) Fixed "Autowired members defined in invalid Spring bean (Kotlin)" inspection description
|
||||
- [KT-12148](https://youtrack.jetbrains.com/issue/KT-12148) Warn about object declarations annotated with Spring `@Configuration`/`@Component`/etc.
|
||||
|
||||
#### Debugger
|
||||
|
||||
|
||||
+10
-6
@@ -54,13 +54,15 @@ class KotlinFinalClassOrFunSpringInspection : AbstractKotlinInspection() {
|
||||
|
||||
private fun getMessage(declaration: KtNamedDeclaration): String? {
|
||||
when (declaration) {
|
||||
is KtClass -> {
|
||||
is KtClassOrObject -> {
|
||||
val lightClass = declaration.toLightClass() ?: return null
|
||||
when {
|
||||
SpringConfiguration.META.getJamElement(lightClass) != null -> return "@Configuration class should be declared open"
|
||||
SpringComponent.META.getJamElement(lightClass) != null -> return "@Component class should be declared open"
|
||||
SpringTransactionalComponent.META.getJamElement(lightClass) != null -> return "@Transactional class should be declared open"
|
||||
val annotation = when {
|
||||
SpringConfiguration.META.getJamElement(lightClass) != null -> "@Configuration"
|
||||
SpringComponent.META.getJamElement(lightClass) != null -> "@Component"
|
||||
SpringTransactionalComponent.META.getJamElement(lightClass) != null -> "@Transactional"
|
||||
else -> return null
|
||||
}
|
||||
return if (declaration is KtClass) "$annotation class should be declared open" else "$annotation should not be applied to object declaration "
|
||||
}
|
||||
|
||||
is KtNamedFunction -> {
|
||||
@@ -79,17 +81,19 @@ class KotlinFinalClassOrFunSpringInspection : AbstractKotlinInspection() {
|
||||
override fun visitNamedDeclaration(declaration: KtNamedDeclaration) {
|
||||
when (declaration) {
|
||||
is KtClass -> if (declaration.isInheritable()) return
|
||||
is KtObjectDeclaration -> {}
|
||||
is KtNamedFunction -> if (declaration.isOverridable()) return
|
||||
else -> return
|
||||
}
|
||||
|
||||
val message = getMessage(declaration) ?: return
|
||||
|
||||
val fixes = if (declaration !is KtObjectDeclaration) arrayOf(QuickFix(declaration)) else LocalQuickFix.EMPTY_ARRAY
|
||||
holder.registerProblem(
|
||||
declaration.nameIdentifier ?: declaration,
|
||||
message,
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
QuickFix(declaration)
|
||||
*fixes
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+32
@@ -127,4 +127,36 @@
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Final Kotlin class or function with Spring annotation</problem_class>
|
||||
<description>@Transactional function should be declared open</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>106</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Final Kotlin class or function with Spring annotation</problem_class>
|
||||
<description>@Configuration should not be applied to object declaration</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>109</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Final Kotlin class or function with Spring annotation</problem_class>
|
||||
<description>@Configuration should not be applied to object declaration</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>112</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Final Kotlin class or function with Spring annotation</problem_class>
|
||||
<description>@Component should not be applied to object declaration</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>115</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Final Kotlin class or function with Spring annotation</problem_class>
|
||||
<description>@Transactional should not be applied to object declaration</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
+15
-1
@@ -98,4 +98,18 @@ open class Trans2 {
|
||||
|
||||
@MyTransactional
|
||||
open fun foo4() = Component4()
|
||||
}
|
||||
}
|
||||
|
||||
// Object declarations
|
||||
|
||||
@Configuration
|
||||
object Application5
|
||||
|
||||
@MyConfiguration
|
||||
object Application6
|
||||
|
||||
@Component
|
||||
object Component3
|
||||
|
||||
@Transactional
|
||||
object Trans3
|
||||
Reference in New Issue
Block a user