https://kotlinlang.org logo
#detekt
Title
# detekt
a

alorma

03/15/2021, 5:29 PM
Hi! One question, how can I check if a function is inside of
interface
or
class
? I want to check if a method is anotated with
@Deprecated
but only need to check interfaces, not implementations Current code on thread
Copy code
class DeprecateBlockingMethods : Rule() {

  override val issue = Issue(
    javaClass.simpleName,
    Severity.Warning,
    "This rule reports a method with Blocking on the name without @Deprecated annotation",
    Debt(mins = 1)
  )

  override fun visitNamedFunction(function: KtNamedFunction) {
    super.visitNamedFunction(function)

    if (!function.isPublic) return
    if (!function.isBlocking) return

    if (!function.isAnnotatedWithDeprecated) {
      report(CodeSmell(issue,
        Entity.from(function.identifyingElement!!),
        message = "The blocking method ${function.name} is not annotated with @Deprecated"))
    }
  }

  private val KtNamedFunction.isBlocking
    get() = name?.contains("Blocking") == true

  private val KtNamedFunction.isAnnotatedWithDeprecated: Boolean
    get() {
      return modifierList?.annotationEntries?.any { annotation ->
        annotation.shortName?.asString() == "Deprecated"
      } == true
    }
g

gammax

03/15/2021, 6:45 PM
I’d say you should try with
Copy code
getNonStrictParentOfType<KtClass>() ?: return
👍 1
c

chao

03/15/2021, 6:49 PM
The method above is also the implementation for UAST as well
g

gammax

03/15/2021, 7:21 PM
Also
KtClass
covers both
class
and
interface
FYI
a

alorma

03/16/2021, 2:51 PM
Thanks, it worked
Copy code
getNonStrictParentOfType<KtClass>() ?: return
3 Views