SourceCodeScanner没有调用visitMethodCall方法。

3

我正在处理代码检查规则。
所有的ResourceXmlDetector都能够正常运行并通过了所有测试。但是Detector(), SourceCodeScanner失败了,因为它们返回0个警告/错误,原因是visitMethodCall没有被调用,因此context.report也不会被调用。
我的代码与Android lint-checks非常相似,例如CipherGetInstanceDetector,但我找不到我的错误。

@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)
    }
}

唯一停在断点的方法是Issue.creategetApplicableMethodNames()。还有什么遗漏吗?
2个回答

1
根据源代码中的 UElementVisitor#DelegatingPsiVisitor.visitMethodCallExpression,我发现一些 Java 或 Kotlin 方法无法被识别为“方法”:val function = node.resolve() 为空。

有趣。在我的情况下,我正在尝试“访问”的方法是getColorparseColor,至少我知道getColor被识别了,因为我看到过一些样例使用它。 - GuilhE

1

我原本认为这不重要,但我的测试规则是:

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

kotlin("Test.kt", input)替换为kotlin(input)使其正常工作...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接