Is there a way find all instances of a particular ...
# konsist
d
Is there a way find all instances of a particular annotation with Konsist? (the
@Suppress
annotation in my case)
s
This should work
Copy code
val classesWithSuppress = Konsist.scopeFromProduction().classes().withAnnotationNamed("Suppress")
            
val functionsWithSuppress = Konsist.scopeFromProduction().functions().withAnnotationNamed("Suppress")
If you want to use the type directly, you can also do
Copy code
withAnnotationOf(Suppress::class)
d
Won’t that miss ones that are on individual expressions? (or other targets)
With this:
Copy code
val scope = Konsist.scopeFromProject().classes() +
            Konsist.scopeFromProject().functions() +
            Konsist.scopeFromProject().properties() +
            Konsist.scopeFromProject().objects() +
            Konsist.scopeFromProject().interfaces()

        scope
            .withAnnotationNamed("Suppress")
            .also { println(it.size) }
I get 96 results, and i’m expecting 150
s
Which ones are missing? Are they within a function? Maybe you can also do
Copy code
.functions()
.withVariable {
    it.hasAnnotationWithName("Suppress")
}
You can always parse the whole text as well, though this should be used as a last resort
Copy code
.files
.withFunction {
    it.text.contains("Suppress")
}
d
doing some spot checking, it seems like the missing ones are all on expressions (e.g. on an
if(...)
)
s
If you don’t have that many, you could move them up on the function declaration. Otherwise, you probably need to parse the text
i
Konsist is checking hight level code structure (classes / interfaces / objects / functions), but not the implementation (expressions / function body). If you need to check body of the function you can query all functions and use
text
property to look for
@Suppress
string BTW Konsist
0.16.0
has recently introduced
classesAndInterfacesAndObjects
declaration accessor