用于“when condition”检查的自定义 lint 规则

hrs*_*krs 5 android lint jetbrains-ide kotlin android-studio

我正在使用 Gson 的序列化适配器RuntimeTypeAdapterFactory来基于disciminator序列化数据。然而,当存在未知的鉴别器(来自 API)并且未在客户端上定义时,TypeAdapter将为 null。

在这种情况下,如果我有一个 kotlin,当条件检查为:

when(fooType){
 is fooA -> //blaba
 is fooB -> //blaba
 //else or null is not handled
}
Run Code Online (Sandbox Code Playgroud)

并且fooType是,由于条件未处理,null它将崩溃。null有没有办法创建自定义 lint 规则(检测器)来检查when条件(java中的instanceof),如果它们实现elsenull检查并在Android Studio检查中注入它?

Mik*_*ski 5

我确实没有编写自定义 lint 检查的经验,但我认为类似的东西(尽管它不是最佳的,并且您不应该按原样使用它,因为它只显示了想法)可能会起作用。

class WhenNullElseMissingDetector : Detector(), Detector.UastScanner {

    override fun getApplicableUastTypes(): MutableList<Class<out UElement>> =
        mutableListOf(USwitchExpression::class.java)

    override fun createUastHandler(context: JavaContext): UElementHandler = WhenNullElseMissingHandler(context)
}

class WhenNullElseMissingHandler(private val context: JavaContext) : UElementHandler() {

    override fun visitSwitchExpression(node: USwitchExpression) {
        if (node.psi?.javaClass != KtWhenExpression::class.java) {
            // returning early here, so it doesn't go false positive on java switches
            return
        }

        var hasNullClause = false
        var hasElseClause = false

        node.body.expressions.forEach { clause ->
             // checking the child's text here just for a example, you should
             // probably check if child's class is KtWhenConditionIsPattern,
             // KtWhenConditionWithExpression or LeafPsiElement
            val clauseText = clause.psi?.firstChild?.text

            if ("null" == clauseText) {
                hasNullClause = true
            } else if ("else" == clauseText) {
                hasElseClause = true
            }
        }

        if (!hasElseClause) {
            context.report(
                WHEN_NULL_OR_ELSE_MISSING, node, context.getLocation(node),
                "When expression must include an else case"
            )
        }

        if (!hasNullClause) {
            context.report(
                WHEN_NULL_OR_ELSE_MISSING, node, context.getLocation(node),
                "When expression must include a null case"
            )
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,您必须将新检测器添加到 IssueRegistry 中。假设您在单独的 java/kotlin 模块中进行 lint 检查,它可能如下所示

package com.example.lintchecks

// omit imports

val WHEN_NULL_OR_ELSE_MISSING = Issue.create(
    "MY_ISSUE_ID",
    "A brief to show in a one line popup",
    "More detailed explanation",
    Category.CORRECTNESS,
    5,
    Severity.WARNING,
    Implementation(
        WhenNullElseMissingDetector::class.java,
        Scope.JAVA_FILE_SCOPE
    )
)

class MyIssueRegistry : IssueRegistry() {
    override val issues: List<Issue>
        get() = listOf(WHEN_NULL_OR_ELSE_MISSING)

}
Run Code Online (Sandbox Code Playgroud)

库模块的 build.gradle 文件应该具有com.android.tools.lint:lint-api:26.4.0(或相关的版本)compileOnly依赖项和这样的块

jar {
    manifest {
        attributes("Lint-Registry-v2": "com.example.lintchecks.MyIssueRegistry")
    }
}
Run Code Online (Sandbox Code Playgroud)