我在玩 lint 规则。
我所有的 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()
为空。
关于android - SourceCodeScanner 不调用 visitMethodCall,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61809096/