Search code examples
androidandroid-testinglintandroid-lint

SourceCodeScanner not calling visitMethodCall


I'm playing with lint rules.
All my ResourceXmlDetector run without problems and pass all the tests. But Detector(), SourceCodeScanner are failing because they return 0 warnings/errors, and the reason is visitMethodCall not being called thus context.report wont either.
My code is similar to android lint-checks, for instance CipherGetInstanceDetector, but I can't find my mistake.

@Suppress("UnstableApiUsage")
class MySourceDetector : Detector(), SourceCodeScanner {

    override fun getApplicableMethodNames() = listOf("...")

    override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {
        if (context.evaluator.isMemberInClass(method, "...")) {
            ...
            reportUsage(context, node)
        }
    }

    private fun reportUsage(context: JavaContext, node: UCallExpression) {
        context.report(
            issue = ISSUE,
            scope = node,
            location = context.getCallLocation(
                call = node,
                includeReceiver = true,
                includeArguments = true
            ),
            message = ISSUE.getExplanation(TextFormat.RAW)
        )
    }

    companion object {
        @JvmField
        val ISSUE = Issue.create(...Scope.JAVA_FILE_SCOPE)
    }
}

The only methods stopping in break points are Issue.create and getApplicableMethodNames(). What's missing?


Solution

  • I didn't think this was important but my test rule was:

     TestLintTask.lint()
                .files(TestFiles.kotlin("Test.kt", input).indented())
                .issues(ISSUE)
                .run()
                .expectWarningCount(1)
                .expect(output)
    

    And replacing kotlin("Test.kt", input) with kotlin(input) made it work...