3

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?

GuilhE
  • 11,591
  • 16
  • 75
  • 116

2 Answers2

1

According UElementVisitor#DelegatingPsiVisitor.visitMethodCallExpression in source code, I found that some java or kotlin method can not be recognized as "Method":val function = node.resolve() is null.

YouCii
  • 221
  • 2
  • 7
  • Interesting. In my case, the methods I'm trying to `visit` are `getColor` and `parseColor` at least `getColor` I know it's recognized because I've seen some samples with it. – GuilhE May 21 '20 at 15:51
1

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...

GuilhE
  • 11,591
  • 16
  • 75
  • 116