我正在使用皮棉规则。我的所有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.create
和getApplicableMethodNames()
。缺少什么?
根据源代码中的UElementVisitor#DelegatingPsiVisitor.visitMethodCallExpression
,我发现某些java或kotlin方法无法识别为“ Method”:val function = node.resolve()
为空。
我不认为这很重要,但是我的测试规则是:
TestLintTask.lint()
.files(TestFiles.kotlin("Test.kt", input).indented())
.issues(ISSUE)
.run()
.expectWarningCount(1)
.expect(output)
[将kotlin("Test.kt", input)
替换为kotlin(input)
使其起作用...