16
16
*/
17
17
package org.sonarsource.kotlin.checks
18
18
19
- import org.jetbrains.kotlin.js.descriptorUtils.getKotlinTypeFqName
20
- import org.jetbrains.kotlin.psi.KtAnnotationEntry
19
+ import org.jetbrains.kotlin.name.ClassId
21
20
import org.jetbrains.kotlin.psi.KtClass
22
21
import org.jetbrains.kotlin.psi.KtNamedFunction
23
22
import org.jetbrains.kotlin.psi.KtProperty
24
23
import org.sonar.check.Rule
25
24
import org.sonarsource.kotlin.api.checks.AbstractCheck
26
- import org.sonarsource.kotlin.api.checks.getType
27
25
import org.sonarsource.kotlin.api.frontend.KotlinFileContext
26
+ import org.sonarsource.kotlin.api.visiting.withKaSession
28
27
29
- @org.sonarsource.kotlin.api.frontend.K1only
30
28
@Rule(key = " S6517" )
31
29
class InterfaceCouldBeFunctionalCheck : AbstractCheck () {
30
+ private val functionalInterClassId = ClassId .fromString(" java/lang/FunctionalInterface" )
32
31
33
32
override fun visitClass (klass : KtClass , context : KotlinFileContext ) {
34
33
checkFunctionalInterface(klass, context)
@@ -46,9 +45,9 @@ class InterfaceCouldBeFunctionalCheck : AbstractCheck() {
46
45
}
47
46
}
48
47
49
- private fun checkFunctionalInterfaceAnnotation (klass : KtClass , context : KotlinFileContext ) {
48
+ private fun checkFunctionalInterfaceAnnotation (klass : KtClass , context : KotlinFileContext ) = withKaSession {
50
49
klass.annotationEntries.forEach {
51
- if (isFunctionalInterfaceAnnotation(it, context) ) {
50
+ if (it.typeReference?.type?.isClassType(functionalInterClassId) == true ) {
52
51
context.reportIssue(it, """ "@FunctionalInterface" annotation has no effect in Kotlin""" )
53
52
}
54
53
}
@@ -65,8 +64,3 @@ private fun hasExactlyOneFunctionAndNoProperties(klass: KtClass): Boolean {
65
64
it !is KtProperty && (it !is KtNamedFunction || functionCount++ == 0 )
66
65
} && functionCount > 0
67
66
}
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