Android “当条件”检查的自定义lint规则

Android “当条件”检查的自定义lint规则,android,android-studio,kotlin,jetbrains-ide,lint,Android,Android Studio,Kotlin,Jetbrains Ide,Lint,我正在使用Gson的序列化适配器RuntimeTypeAdapterFactory基于disciminator对数据进行序列化。但是,当存在未知的鉴别器(来自API)且未在客户端上定义时,TypeAdapter将为空 在这种情况下,如果我有一个kotlin,当条件检查为: when(fooType){ is fooA -> //blaba is fooB -> //blaba //else or null is not handled } 而fooType为null,它将崩溃

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

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

when(fooType){
 is fooA -> //blaba
 is fooB -> //blaba
 //else or null is not handled
}
fooType
null
,它将崩溃,因为未处理
null
条件。
有没有办法创建一个自定义lint规则(检测器)来检查当条件(java中的instanceof)实现了
else
null
检查并在Android Studio inspection上注入该检查时的,但是我认为这样的东西(虽然它不是最佳的,你不应该使用它,因为它只显示了想法)可能会起作用

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

}
jar {
    manifest {
        attributes("Lint-Registry-v2": "com.example.lintchecks.MyIssueRegistry")
    }
}