Skip to content

Commit 096bf2e

Browse files
leveretkaGodin
andcommitted
SONARKT-400 Migrate InterfaceCouldBeFunctionalCheck to kotlin-analysis-api
Co-authored-by: Marharyta Nedzelska <[email protected]> Co-authored-by: Evgeny Mandrikov <[email protected]>
1 parent fd49eb9 commit 096bf2e

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

sonar-kotlin-api/src/main/java/org/sonarsource/kotlin/api/checks/ApiExtensions.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ fun KtBinaryExpression.isPlus() =
598598
fun PsiElement?.getVariableType(bindingContext: BindingContext) =
599599
this?.let { bindingContext[BindingContext.VARIABLE, it]?.type }
600600

601+
/** Use [org.jetbrains.kotlin.analysis.api.components.KaTypeProvider.type] instead. */
602+
@Deprecated("use kotlin-analysis-api instead")
601603
fun KtTypeReference?.getType(bindingContext: BindingContext): KotlinType? =
602604
this?.let { bindingContext[BindingContext.TYPE, it] }
603605

sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/InterfaceCouldBeFunctionalCheck.kt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@
1616
*/
1717
package org.sonarsource.kotlin.checks
1818

19-
import org.jetbrains.kotlin.js.descriptorUtils.getKotlinTypeFqName
20-
import org.jetbrains.kotlin.psi.KtAnnotationEntry
19+
import org.jetbrains.kotlin.name.ClassId
2120
import org.jetbrains.kotlin.psi.KtClass
2221
import org.jetbrains.kotlin.psi.KtNamedFunction
2322
import org.jetbrains.kotlin.psi.KtProperty
2423
import org.sonar.check.Rule
2524
import org.sonarsource.kotlin.api.checks.AbstractCheck
26-
import org.sonarsource.kotlin.api.checks.getType
2725
import org.sonarsource.kotlin.api.frontend.KotlinFileContext
26+
import org.sonarsource.kotlin.api.visiting.withKaSession
2827

29-
@org.sonarsource.kotlin.api.frontend.K1only
3028
@Rule(key = "S6517")
3129
class InterfaceCouldBeFunctionalCheck : AbstractCheck() {
30+
private val functionalInterClassId = ClassId.fromString("java/lang/FunctionalInterface")
3231

3332
override fun visitClass(klass: KtClass, context: KotlinFileContext) {
3433
checkFunctionalInterface(klass, context)
@@ -46,9 +45,9 @@ class InterfaceCouldBeFunctionalCheck : AbstractCheck() {
4645
}
4746
}
4847

49-
private fun checkFunctionalInterfaceAnnotation(klass: KtClass, context: KotlinFileContext) {
48+
private fun checkFunctionalInterfaceAnnotation(klass: KtClass, context: KotlinFileContext) = withKaSession {
5049
klass.annotationEntries.forEach {
51-
if (isFunctionalInterfaceAnnotation(it, context)) {
50+
if (it.typeReference?.type?.isClassType(functionalInterClassId) == true) {
5251
context.reportIssue(it, """"@FunctionalInterface" annotation has no effect in Kotlin""")
5352
}
5453
}
@@ -65,8 +64,3 @@ private fun hasExactlyOneFunctionAndNoProperties(klass: KtClass): Boolean {
6564
it !is KtProperty && (it !is KtNamedFunction || functionCount++ == 0)
6665
} && functionCount > 0
6766
}
68-
69-
private fun isFunctionalInterfaceAnnotation(annotation: KtAnnotationEntry, context: KotlinFileContext): Boolean {
70-
val annotationType = annotation.typeReference.getType(context.bindingContext)
71-
return (annotationType?.getKotlinTypeFqName(false) == "java.lang.FunctionalInterface")
72-
}

0 commit comments

Comments
 (0)