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
alorma
03/15/2021, 5:30 PM
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