Red*_*arp 6 lint kotlin android-lint
我正在尝试编写一个 lint 检查,访问调用表达式,并报告基于泛型类型的一些违规行为。
为了更清楚,假设我有这个代码:
object Foo {
inline fun <reified T> bar() = T::class.java
}
Run Code Online (Sandbox Code Playgroud)
假设我想编写一个 lint 检查,当我bar使用Int泛型类型调用方法时会抱怨,但接受其他所有内容。
因此,使用以下代码,对 bar 的第二次调用应触发警告:
object Whatever {
fun someMethod() {
val stringClass = Foo.bar<String>() // Should not complain
val intClass = Foo.bar<Int>() // Should raise a warning
}
}
Run Code Online (Sandbox Code Playgroud)
如何实现呢?这当然不是真正的用例,我真正想做的是正确检测bar<Int>.
到目前为止,这就是我所拥有的:
class MyDetector : Detector(), SourceCodeScanner {
companion object Issues {
val ISSUE = Issue.create(
id = "IntBarTest",
briefDescription = "You used bar with an Int type!",
explanation = "Explanation",
category = Category.CORRECTNESS,
severity = FATAL,
implementation = Implementation(MyDetector::class.java, Scope.JAVA_FILE_SCOPE)
)
}
override fun getApplicableUastTypes() = listOf(UCallExpression::class.java)
override fun createUastHandler(context: JavaContext): UElementHandler {
return GenericTypeHandler(context)
}
inner class GenericTypeHandler(val context: JavaContext) : UElementHandler() {
override fun visitCallExpression(node: UCallExpression) {
if (isCallExpressionAnIntBar(node)) {
context.report(ISSUE,
context.getNameLocation(node),
"Oh no, I should not use bar<Int>")
}
}
private fun isCallExpressionAnIntBar(node: UCallExpression): Boolean {
return if ("bar".equals(node.methodName) ||
"Foo" == (node.receiverType as? PsiClassReferenceType)?.resolve()?.qualifiedName) {
// We know it's the method we are looking for but now we must identify the generic
TODO("Identify the generic")
} else {
false
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,有一个很大的 TODO :-P
答案其实很简单:
UCallExpression 公开类型参数。因此,您只需执行以下操作:
private fun isCallExpressionAnIntBar(node: UCallExpression): Boolean {
return if ("bar".equals(node.methodName) ||
"Foo" == (node.receiverType as? PsiClassReferenceType)?.resolve()?.qualifiedName) {
// We know it's the method we are looking for but now we must identify the generic
node.typeArguments.getOrNull(0)?.canonicalText == Int::class.qualifiedName
} else {
false
}
}
Run Code Online (Sandbox Code Playgroud)
它让我困惑的原因是我正在通过测试和检测器中的断点进行开发。但我的测试代码是错误的(没有正确定义依赖项等),因此检测器当然无法正常工作:我的对象的类型参数是一个空数组。
一旦我修复了我的测试设置,typeArguments就正确报告了通用类型。
| 归档时间: |
|
| 查看次数: |
83 次 |
| 最近记录: |